gpt4 book ai didi

node.js - 未处理的PromiseRejection警告: Insufficient funds

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:46 26 4
gpt4 key购买 nike

我正在使用 web3.js v1.0.0-beta.34 和 nodeJS v9.11.2 在 Kovan 测试网上执行智能合约。同样的方法适用于我在 Ropsten 上使用另一个智能合约。这是我通过回调得到的两个错误:

UnhandledPromiseRejectionWarning: Error: Returned error: Insufficientfunds. The account you tried to send transaction from does not haveenough funds. Required 183675000000 and got: 0.

(node:15422) UnhandledPromiseRejectionWarning: Unhandled promiserejection. This error originated either by throwing inside of an asyncfunction without a catch block, or by rejecting a promise which wasnot handled with .catch(). (rejection id: 1)

这是我的智能合约:

pragma solidity ^0.4.24;

contract Test2 {
address public customer;
bytes32 public productName;

struct Box {
uint size;
}
Box public box;

constructor() public {
box.size = 3;
customer = 0xDa3E3C75....;
productName = "0x576...";
}

function changeBox(uint _change) public {
box.size = _change;
}

function getBox() public returns (uint) {
return box.size;
}
}

下面是使用 web3 和 Node 进行交易并执行 function changeBox 的 JavaScript 代码:

const Tx = require('ethereumjs-tx');
var Web3 = require('web3');


var web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/api_key'));
const contractAddress = '0x36075430619b21Fff798454e2D5C81E9C18DEe81';
var contractABI = new web3.eth.Contract(
[...json abi...], contractAddress);
var boxNum;


function changeBox(boxNum, callback) {
web3.eth.defaultAccount = "0x002D189c25958c60...";
const account = '0x002D189c2595...';
const privateKey = Buffer.from('240462d5...', 'hex');
const contractFunction = contractABI.methods.changeBox(Number(boxNum));
const functionAbi = contractFunction.encodeABI();
let estimatedGas;
let nonce;

contractFunction.estimateGas(function(error, gasAmount) {
if(!error) {
console.log('Estimated Gas : ' + gasAmount);
estimatedGas = gasAmount + 10000;
console.log('New Gas: ' + estimatedGas);

web3.eth.getTransactionCount(account).then(_nonce => {
nonce = _nonce.toString(16);
console.log("Nonce: " + nonce);

const txParams = {
gasPrice: estimatedGas,
gasLimit: 5000000,
to: contractAddress,
data: functionAbi,
from: account,
nonce: '0x' + nonce
};
const tx = new Tx(txParams);
tx.sign(privateKey);
const serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', receipt => {
callback(receipt);

});
});
}
else {
callback(error);
}
});
}

//calling the contract with value 6
changeBox(6, function(err, data) {
if (!err) {
console.log(data);
}
else {
console.log(err);
}});

最佳答案

sendSignedTransaction 返回 promiEvent您可以在其上链接 thencatch:

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', receipt => {
callback(receipt);
}).then(() => {
// success
}).catch(() => {
// fail
});

抛出未处理的 promise 拒绝,因为 promise 被拒绝但没有 catch 处理程序。

关于node.js - 未处理的PromiseRejection警告: Insufficient funds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51232251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com