ganache-cli 沒有足夠的資金來發送 tx - 數量與我初始化錢包的數量成正比
我正在嘗試編寫一個簡單的函式來發送交易,
web3
並將ethereumjs-tx
一個帳戶的錢包(稱為此帳戶 A)清空到另一個帳戶(帳戶 B)中。但是,無論我在使用時初始化帳戶 A 的餘額ganache-cli
,即使用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);
您正在呼叫
getBalance
,它返回帳戶持有的乙太幣數量(以 wei 為單位)。然後,您在
value
交易欄位中傳遞該金額,因此交易將嘗試發送賬戶的全部餘額。這就是為什麼當您向帳戶添加更多資金時金額會增加的原因。這樣的交易永遠不會成功,因為發送交易的賬戶必須有足夠的乙太幣來支付
value
傳輸的費用和支付 gas 所需的金額。您可以傳輸的最大值應該是result - (gasPrice * 1e9 * gasLimit)
.編輯
您還選擇了極高的 gas 價格,因為您將返回的 gas 價格乘以
getGasPrice
10 億。