使用 web3 v1.0 发送原始交易时出现无效类型错误
Invalid Type error sending raw transaction using web3 v1.0
我尝试通过 Infura 网关向 Ropsten 发送原始交易,我收到错误消息“`无效类型”。
这是我的开发环境。
- "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)
web3 新版本1.x
试试这个
to: Contract._address,
data: Contract.methods.transfer(to, 1000).encodeABI(),
我尝试通过 Infura 网关向 Ropsten 发送原始交易,我收到错误消息“`无效类型”。
这是我的开发环境。
- "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)
web3 新版本1.x
试试这个
to: Contract._address,
data: Contract.methods.transfer(to, 1000).encodeABI(),