使用 web3 v1.0 发送原始交易的无效类型错误
Posted
技术标签:
【中文标题】使用 web3 v1.0 发送原始交易的无效类型错误【英文标题】:Invalid Type error sending raw transaction using web3 v1.0 【发布时间】:2019-12-05 07:59:22 【问题描述】:我尝试通过 Infura 网关向 Ropsten 发送原始交易,我收到了错误消息“`Invalid Type”。
这是我的开发环境。
“ethereumjs-tx”:“^1.3.7” “web3”:“^1.0.0-beta.55”代码如下:
var count = web3.eth.getTransactionCount(fromAddress, 'pending').then(nonce =>
console.log('This is the nouce'+ nonce)
var data = MyContract.methods.transfer(toAddress, 10000, from: fromAddress);
var gasPrice = web3.eth.gasPrice;
var gasLimit = 90000;
var rawTransaction =
"from": fromAddress,
"nonce": web3.utils.toHex(nonce),
"gasPrice": web3.utils.toHex(gasPrice),
"gasLimit": web3.utils.toHex(gasLimit),
"to": toAddress,
"value": 0,
"data": data,
"chainId": 0x03
;
var privKey = new Buffer('0x...', 'hex');
var tx = new TX(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash)
if (!err)
console.log(hash);
else
console.log(err);
);
console.log(nonce)
)
"Error: invalid type
at Object.exports.toBuffer (blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:153167:15)
at Transaction.setter [as data] (blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:153448:21)
at blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:153505:65
at Array.forEach (<anonymous>)
at Object.exports.defineProperties (blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:153504:16)
at new Transaction (blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:152914:15)
at blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:92532:20
at tryCallOne (blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:3156:14)
at blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:3257:17
at blob:http://localhost:8089/b24018f3-44b6-48a6-a316-46ad6a2e0477:27877:21"
新的错误信息:
Error: types/values length mismatch (count="types":2,"values":3, value="types":["name":"_to","type":"address","name":"_value","type":"uint256"],"values":["0x5077174D79d9491AF15Dcf7D1496638D6062A011",10000,"from":"0xd61794624e9542495A72Cfac7Cc10B4275b8f8E5"], version=4.0.33)
Error: types/values length mismatch (count="types":2,"values":3, value="types":["name":"_to","type":"address","name":"_value","type":"uint256"],"values":["0x5077174D79d9491AF15Dcf7D1496638D6062A011",10000,"from":"0xd61794624e9542495A72Cfac7Cc10B4275b8f8E5"], version=4.0.33)
【问题讨论】:
你能打印完整的错误吗? @AnkushRasgon - 我已经更新并分享了上面的完整错误消息 【参考方案1】:web3 1.x 新版本 试试这个
to: Contract._address,
data: Contract.methods.transfer(to, 1000).encodeABI(),
【讨论】:
@AnkushRasgon- 我试过了。现在,我收到一条新的错误消息。请看上面 改变这个值:'0x0' okk wait 整体代码okk 有小错误需要时间去解决 我已经想通了。(也许),我应该将这行代码改为这个 MyContract.methods.transfer(toAddress, 10000)..encodeABI()。删除了 from:fromAddress。但是,我弹出了新错误,它说“web3.eth.sendRawTransaction 不是函数”。也许,web3 1.0 不再支持方法了? 重命名为 sendSignedTransaction() ethereum.stackexchange.com/questions/43720/…以上是关于使用 web3 v1.0 发送原始交易的无效类型错误的主要内容,如果未能解决你的问题,请参考以下文章
从 web3py 发送原始交易:TypeError: <lambda>() 缺少 4 个必需的位置参数:'hash'、'r'、's' 和 'v'
Web3 web3.eth.sendSignedTransaction 无效参数
发送原始交易 Ethereum infura nodejs npm