繁体   English   中英

以太坊:NodeJS web3 - UnhandledPromiseRejectionWarning:资金不足

[英]Ethereum: NodeJS web3 - UnhandledPromiseRejectionWarning: Insufficient funds

我正在使用web3.js v1.0.0-beta.34nodeJS v9.11.2Kovan testnet上执行smart contract 同样的方法适用于我在Ropsten与另一个智能合约。 以下是我通过回调获得的两个错误:

1.)

UnhandledPromiseRejectionWarning:错误:返回错误:资金不足。 您尝试从中发送交易的帐户资金不足。 需要183675000000并获得:0。

2.)

(node:15422)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 这个错误源于在没有catch块的情况下抛出异步函数,或者拒绝未使用.catch()处理的promise。 (拒绝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和节点执行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
  });

抛出Unhandled promise rejection因为promise被拒绝但没有catch处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM