[Ethereum] web3.eth.sendSignedTransaction -> insufficient funds for gas * price + value

nodejsweb3js

I am signing this raw transaction and I can't seem to find the correct gas parameters to make the transaction valid. The from address/private key has over 3 ETH as seen here:
https://rinkeby.etherscan.io/address/0x22dd8860db83a962e8d2ec734eae62fab63e73ae

    var Web3 = require("web3")
    var web3 = new Web3("https://rinkeby.infura.io/TOKEN")
    var privateKey =
      "PRIVATE_KEY"

    var account = web3.eth.accounts.wallet.add(privateKey)

    var tx = {
      chainId: 4,
      from: "22dd8860db83a962e8d2ec734eae62fab63e73ae",
      gas: "12880",
      gasPrice: web3.utils.toWei("0.0000000001", "ether"),
      nonce: "0x0",
      to: "0x8a09e76a5255E3d07854647f4DBef7323F98323d",
      value: web3.utils.toWei(".000001", "ether")
    }

    var signed = account.signTransaction(tx)

    web3.eth.sendSignedTransaction(signed.rawTransaction).then(console.log)

I also tried converting to Hex and I receive the same error:

var tx = {
  chainId: 4,
  from: "22dd8860db83a962e8d2ec734eae62fab63e73ae",
  gas: web3.utils.toHex("21000"),
  gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
  nonce: "0x0",
  to: "0x8a09e76a5255E3d07854647f4DBef7323F98323d",
  value: web3.utils.toHex(web3.utils.toWei(".000001", "ether"))
}

Best Answer

I found the problem with the help of @num8er so I will document the issue here:

I used the BlockCypher API service to generate the key pairs. What I did not notice, is that Blockcypher removes the 0x from the addresses. When the 0x is not present, web3 generates the incorrect public address. This is why it kept giving me the error "insufficient funds".

if privateKey = "ec5fd9511901a87615d93ba088b4cce57a376efc9176ca5a9caa783607fe9e87"

account = web3.eth.accounts.wallet.add(privateKey)

> { address: '0x46c1e3E78A2Fc28943f2BD637e8b615e8906BB8F', privateKey: 'ec5fd9511901a87615d93ba088b4cce57a376efc9176ca5a9caa783607fe9e87', signTransaction: [Function: signTransaction], sign: [Function: sign], encrypt: [Function: encrypt], index: 0 }

if privateKey ="0xec5fd9511901a87615d93ba088b4cce57a376efc9176ca5a9caa783607fe9e87"

account = web3.eth.accounts.wallet.add(privateKey)

> { address: '0x22DD8860db83a962E8d2ec734eAE62faB63e73aE', privateKey: '0xec5fd9511901a87615d93ba088b4cce57a376efc9176ca5a9caa783607fe9e87', signTransaction: [Function: signTransaction], sign: [Function: sign], encrypt: [Function: encrypt], index: 0 }

Related Topic