4

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);
4
  • 1
    Do you have enough ETH in your account to make the transaction? Commented Mar 26, 2018 at 9:27
  • i have $4 in one of my account, and im trying to send $1 to another account just for testing purpose. Did i mistakenly entered more amount than $4 in my code? I'm using this amount var amountToSend = "0.00192823123348952"; //$1
    – shawn
    Commented Mar 26, 2018 at 9:31
  • do not confuse dollars with Ether - there is no (direct) connection between them two. Only thing that matters is how much Ether you have in your acconut and whether that's enough to pay for the gas Commented Mar 26, 2018 at 9:34
  • My wallet balance is Ether: 7859274664735255 And and change my code to var amountToSend = "1859274664735255"; But still i get same error
    – shawn
    Commented Mar 26, 2018 at 9:50

2 Answers 2

2

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.

5
  • What should be my value of $1 into hex in value parameter? And how it can be converted into hex? I think this should be done with this line of code "value": web3.utils.toHex(amountToSend) And ether to send var amountToSend = "1859274664735255";
    – shawn
    Commented Mar 26, 2018 at 11:16
  • As I already said - forget about dollars. You are only dealing in Ethers. Looks like you already found a method for converting an integer to hex, so why not try it? Commented Mar 26, 2018 at 11:19
  • I have modified my new code into my original question. But still im getting same error. And variable var amountToSend = "1859274664735255"; These are the wei i want to send to another address 1859274664735255 Can you please check code again and point out what is missing
    – shawn
    Commented Mar 26, 2018 at 11:25
  • Update my answer to reflect the edit in the question Commented Mar 26, 2018 at 11:31
  • Nothing is helping me :-( I'm not sure if Hex is converted correctly or not. But i think it should be correctly converted because its done with web3 inbuilt function. My hex values becomes gasLimit : 0x5208 gasPrice : 0x2dc6c0 amountToSend : 0x69b001dee7217
    – shawn
    Commented Mar 26, 2018 at 11:59
1
  • truffle-hdwallet-provider version 0.0.3

Implement your code with " Gas limit ". Ex:

const result = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode })
    .send({ gas: '1000000', from: accounts[0] });
  • truffle-hdwallet-provider versions 0.0.4, 0.0.5 and 0.0.6

Implement your code without "Gas limit". Ex:

const result = await new web3.eth.Contract(JSON.parse(interface))
     .deploy({data: '0x' + bytecode }) // add 0x bytecode
     .send({from: accounts[0]}); // remove 'gas'

References: StephenGrider/Ethereum

you can use following link to get more ether and try again.

https://faucet.rinkeby.io/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.