我试图编写一个简单的函数,使用web3和ethereumjs-tx发送一个事务,它将一个帐户的钱包(调用这个帐户A)清空到另一个帐户(帐户B)。但是,无论我在使用ganache-cli时初始化帐户A的余额(与ganache-cli --account="0x4482be4e4e36d5521d70dafa57818eed38a4d0c69562bffebf24901c3199b271, 222222222222222222222"一起),我都会得到以下错误:
Error: Returned error: sender doesn't have enough funds to send tx. The upfront cost is: 73361650815041401384341826897131069221944849241050 and the sender's account only has: 222222222222222222222
此外,如果我将钱包初始化的金额增加,“预付费用”按比例增加:
Error: Returned error: sender doesn't have enough funds to send tx. The upfront cost is: 22704331223003175573249212746801550559464702875615796870481879467237868556850 and the sender's account only has: 22222222222222222222222222222222
有人能告诉我我做错了什么吗?这是我的密码:
var Web3 = require('web3');
var web3 = new Web3(new
Web3.providers.WebsocketProvider("ws://localhost:8545/"));
const Tx = require('ethereumjs-tx');
addr = account..address;
key = account.key;
key = key.slice(2);
web3.eth.getBalance(addr).then( (result) => {
evmBalance += parseInt(result, 10);
web3.eth.getGasPrice()
.then((gasPrice) => {
var gasLimit = 25000;
web3.eth.getTransactionCount(addr).then( (nonce) => {
var nonce = nonce.toString();
var rawTransaction = {
"from": addr,
"nonce": web3.utils.toHex(nonce),
"gasPrice": web3.utils.toHex(gasPrice * 1e9),
"gasLimit": web3.utils.toHex(gasLimit),
"to": destinationAddr,
"value": result,
};
var privKey = Buffer.from(key, 'hex');
var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
var transaction = web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
transaction.on('receipt', console.log);
transaction.on('error', console.log);发布于 2018-08-31 20:10:23
您正在调用getBalance,它返回帐户所持有的以太的数量(以魏为单位)。
然后在事务的value字段中传递该金额,因此事务将尝试发送帐户的全部余额。这就是为什么当您向帐户中添加更多资金时,金额会增加的原因。
这样的交易永远不会成功,因为发送交易的帐户必须有足够的乙醚来支付正在传输的value和支付天然气所需的金额。您可以传输的最大值应该是result - (gasPrice * 1e9 * gasLimit)。
你也选择了一个非常高的汽油价格,因为你把从getGasPrice返回的汽油价格乘以10亿。
https://ethereum.stackexchange.com/questions/57859
复制相似问题