[Ethereum] web3.js keeps throwing “error: insufficient funds for gas * price + value” on ropsten

ropstenweb3js

I have been chacking around the site for possible solutions but i havent found any that would solve my (probably most basic) error. The account from which i'm sending has around 1 ether and i'm certain that the private key is correct as i had generated a new one and sent ether to it specificly to test is i had incorrect private key. I am using ropsten test network. And possible help would be much appreciated. this is the full source code.

I had written smartcontract, i can call the methods from it, but for the life of me i cannot send a transaction anywhere.

var Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/da1b------341c38904ac78bb649502"))

const rawTransaction = {
        from : '0x76a9CEA17DbA8a371944d00a2E9A17AA3669B392',
        to: '0x17eB191cDc2b3f06c25914c2da041CEeb41e9B16',
        value: web3.utils.toWei("0.0001", "ether"),
        gasLimit: 200000,
        gasPrice: web3.utils.toWei("0.000002", "ether"),
        chainid : 3,
        network_id: 3
};



web3.eth.accounts.signTransaction(rawTransaction,'EA------------5A36B3677F74AC1')
  .then(signedTx => web3.eth.sendSignedTransaction(signedTx.rawTransaction))
  .then(receipt => console.log("Transaction receipt: ", receipt))
  .catch(err => console.error(err));

The error being thrown :

Error: Returned error: insufficient funds for gas * price + value
    at Object.ErrorResponse (/home/ubuntu/ethereum_website/node_modules/web3-core-helpers/src/errors.js:29:16)
    at /home/ubuntu/ethereum_website/node_modules/web3-core-requestmanager/src/index.js:140:36
    at XMLHttpRequest.request.onreadystatechange (/home/ubuntu/ethereum_website/node_modules/web3-providers-http/src/index.js:96:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/ubuntu/ethereum_website/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/ubuntu/ethereum_website/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/ubuntu/ethereum_website/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/ubuntu/ethereum_website/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Best Answer

I believe the issue is this line:

web3.utils.toWei(web3.utils.toWei("0.00000001", "ether"))

The inner call to web3.utils.toWei returns "10000000000". The outer call defaults to ether as a unit, so it multiplies that by 10**18 again, and you get "10000000000000000000000000000", which is 10,000,000,000 ether, certainly more than you have.

Drop the outer call to web3.utils.toWei, and I believe things should work for you.