我使用web3.eth.sendSignedTransaction
函数,但它不起作用。
这是问题的代码
var nonce = web3.eth.getTransactionCount("<from address>");
const txParams ={
nonce:web3.utils.toHex(nonce),
to : "<to address>",
value : web3.utils.numberToHex(web3.utils.toWei('0.01','ether')),
gasPrice :web3.utils.numberToHex(web3.utils.toWei('1','Gwei')),
gasLimit:web3.utils.numberToHex('300000'),
chainId :3 //Ropsten
}
const tx = new Tx(txParams);
const privateKey = Buffer.from("<primary key>",'hex');
tx.sign(privateKey);
const serializedTx = '0x' + tx.serialize().toString('hex');
console.log(serializedTx);
web3.eth.sendSignedTransaction(serializedTx, function(err,txId){if(err){ console.log(' : ',txId)}});
我查了“serializedTx”日志。
0xf86d827b7d843b9aca00830493e0944f16477b610cad87e1f506e42208bc36bce6aa4887f8b0a10e470000802aa016a1bc13133cbbfd0cdb5aa0da5e049e0eba3972b313ba69887970acecf2e11ba06b2fd1b9dad15959e271a1af8020fd9e4b1f089d8eb66f99c48988032c300f78
我觉得太久了......
以太也无法转移。
我试过了
web3.utils.toWei('0.01', 'ether')
至
"web3.utils.toWei(0.01, 'ether')"
但这是错误的。
错误代码是“请将数字作为字符串或BigNumber对象传递,以避免精度错误。”
有什么不对,怎么办?
请帮我 。
谢谢。
我找到了一个方法,这个问题就消失了!我只是将web3的版本改进到1.0.0或更高版本。所以也许你可以尝试一下。像这样:
npm install -g [email protected]