[Ethereum] Error: Returned error: insufficient funds for gas * price + value

web3js

Im using this code to send payment from Address A to address B.. But im getting this error always.

(node:18492) UnhandledPromiseRejectionWarning: Error: Returned error: insufficient funds for gas * price + value

I read at many places it may be fixed if chainId is changed. I changed many chainId but it did not work for me. I always get error 🙁

// Require the web3 node module.
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');
// Show Web3 where it needs to look for a connection to Ethereum.
//web3 = new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:8545'));
web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/N6IIa2HvDYOovtgmPbhD'));
var gasPrice = '25000000000';//or get with web3.eth.gasPrice
var gasLimit = '9000';
var addr = '0x.................................';
var toAddress = '0x.................................';
var amountToSend =  "1859274664735255";
var nonce = web3.eth.getTransactionCount(addr); //211;
var rawTransaction = {
"from": addr,
"nonce": web3.utils.toHex(nonce),
"gas": web3.utils.toHex('21000'),
"gasPrice": web3.utils.toHex(gasPrice),
"gasLimit": web3.utils.toHex(gasLimit),
"to": toAddress,
"value": web3.utils.toHex(amountToSend) ,
"chainId": web3.utils.toHex('1')
};
var privateKey = '......................................................';
var privKey = new Buffer(privateKey, 'hex');
console.log("privKey  : ", privKey);
const tx = new Tx(rawTransaction);
tx.sign(privKey);
const serializedTx = `0x${tx.serialize().toString('hex')}`;
web3.eth.sendSignedTransaction(serializedTx);

Best Answer

The value parameter should be in hexadecimal. Actually all the numbers should be in hex.

Have a look at https://ethereum.stackexchange.com/a/23656/31933 for more details.

EDIT: The question is now updated to use hex values (does string value parse correctly into hex? integer would be safer).

Your gas limit is 9000. 21000 is the minimum for any transaction so the transaction is doomed to fail with only 9000 gas in use. Also there's no point in setting such high gasPrice - it doesn't help anything if there's not enough gas limit. You can use https://ethgasstation.info/ to estimate what price you should use.

Related Topic