UnhandledPromiseRejectionWarning Insufficient funds error in Ethereum NodeJS web3

0 votes

I am using web3.js v1.0.0-beta.34 & nodeJS v9.11.2 to execute a smart contract on the Kovantestnet. The same method works for me on Ropsten with another smart contract. Here are the two errors I get via the callback:

1.) UnhandledPromiseRejectionWarning: Error: Returned error: Insufficient funds. The account you tried to send transaction from does not have enough funds. Required 183675000000 and got: 0.

This is my smart contract:

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;
    }  
}

And here is the JavaScript code to make a transaction and execute the function changeBox with web3 and node:

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);
    }});
Oct 15, 2018 in Blockchain by slayer
• 29,370 points

retagged Nov 22, 2018 by Priyaj 1,405 views

1 answer to this question.

0 votes

sendSignedTransaction returns a promiEvent onto which you can chain then and catch:

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

Unhandled promise rejection is thrown because the promise gets rejected but there is no catchhandler.

Hope this helps

answered Oct 15, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

Web3j Transfer.sendFunds() returns Error “insufficient funds for gas * price + value”

DISCLAIMER. This may not necessarily be an answer to your ...READ MORE

answered Jun 16, 2018 in Blockchain by charlie_brown
• 7,720 points
3,046 views
0 votes
1 answer

Error in sending value to an Ethereum Smart Contract.

It is stated in Solidity 4.0 documentation ...READ MORE

answered Jul 23, 2018 in Blockchain by Perry
• 17,100 points
1,414 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
0 votes
1 answer

How to read the ETH value and other token values from an account?

You can do this eth.accounts shows you all known ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 69,220 points
852 views
0 votes
1 answer
0 votes
1 answer

Ethereum nodejs: Contract address returning as undefined in console

As per the documentation, if you deploy the ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
1,871 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP