web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.32k stars 4.95k forks source link

err-insufficient-funds-for-gas-price-value-address-0x6be-have-0-want-7050 #6418

Closed cullsin closed 1 year ago

cullsin commented 1 year ago

Hi,

I do not have anyone working in cryptocurrency. Can you suggest my issue? I have 0.1TBNB in the sender and receiver wallet. I set a minimum gas price is 21000 in the transaction parameters as below. I got the "insufficient balance error". Can you please suggest? stack overflow unable to help on my request.

const createTransaction =  await object.eth.accounts.signTransaction(
     {
        from: addressFrom,
        to: addressTo,
        value: object.utils.toHex(object.utils.toWei("0.00001", "ether")),
        gasPrice: await object.eth.getGasPrice()
     },
     privKey
  );

  // Deploy transaction
  const createReceipt = await object.eth.sendSignedTransaction(
     createTransaction.rawTransaction
  );

I got the below error.

innerError: { code: -32000, message: 'insufficient funds for transfer' },
  code: 101,
  data: undefined,
  request: {
    jsonrpc: '2.0',
    id: 'b4e14330-e710-4ace-934e-f27624214872',
    method: 'eth_estimateGas',
    params: [
      {
        from: '0x33De79D574B096397492c163Df18CCE737b49990',
        to: '0x15878f0A35F006864f8d480537C07630db6f48c8',
        value: '0x3130303030303030303030303030',
        gasPrice: '0x2540be400',
        nonce: '0x0',
        input: '0x',
        chain: 'mainnet',
        hardfork: 'london',
        chainId: '0x61',
        networkId: '0x61',
        type: '0x0'
      },
      'latest'
    ]

Steps before creating an issue:

Environment

windows, yarn, node 16.14.2, web3 is 4.1.1

jdevcs commented 1 year ago

In your snippet the problem is in following line: value: object.utils.toHex(object.utils.toWei("0.00001", "ether")), utils.toWei("0.00001", "ether") returns result in string and you are converting a string to hex that results into huge eth value: value: '0x3130303030303030303030303030 that is invalid so your tx is failing.

so first convert your string into number and thn hex.


const val = web3.utils.toNumber(web3.utils.toWei("0.00001", "ether"));

const createTransaction =  await object.eth.accounts.signTransaction(
     {
        from: addressFrom,
        to: addressTo,
        value: object.utils.toHex(val),
        gasPrice: await object.eth.getGasPrice()
     },
     privKey
  );
cullsin commented 1 year ago

Hi,

Thanks. The issue has moved forward and still shows different errors now.

file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/utils/get_transaction_error.js:45
            error = new TransactionRevertInstructionError(_reason, undefined, transactionReceiptFormatted);    
                    ^

TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/utils/get_transaction_error.js:45:21
    at Generator.next (<anonymous>)
    at file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/utils/get_transaction_error.js:23:71
    at new Promise (<anonymous>)
    at __awaiter (file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/utils/get_transaction_error.js:19:12)
    at getTransactionError (file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/utils/get_transaction_error.js:30:12)
    at Module.<anonymous> (file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/rpc_method_wrappers.js:418:49)
    at Generator.next (<anonymous>)
    at fulfilled (file:///C:/Users/user/lab63/awdot%20all/awdot/micro/platform/node_modules/web3-eth/lib/esm/rpc_method_wrappers.js:20:58)
    at runMicrotasks (<anonymous>) {
  innerError: undefined,
  reason: 'err: insufficient funds for gas * price + value: address 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b 
have 0 want 115000000000000 (supplied gas 21000)',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402
}
jdevcs commented 1 year ago

@cullsin seems there are no funds in your account.

cullsin commented 1 year ago

Hi,

I attached a screenshot where I have 0.1tbnb in my Metamask wallet.

image

cullsin commented 1 year ago

@cullsin seems there are no funds in your account.

Hi,

I attached a screenshot where I have 0.1tbnb in my Metamask wallet.

cullsin commented 1 year ago

Hi,

Any update on my question "insufficient fund transfer" ??

cullsin commented 1 year ago

Hi,

Any update on my question "insufficient fund transfer" ??

Hi, I badly need an answer. please suggest.

cullsin commented 1 year ago

Hi, Any update on my question "insufficient fund transfer" ??

Hi, I badly need an answer. please suggest.

Hi. Any suggestion? I got stuck.

jdevcs commented 1 year ago

@cullsin I was able to mine transaction using following snippit ( share above ) :


    let object = new Web3('https://data-seed-prebsc-1-s1.bnbchain.org:8545');

    const gaspriceVal = await object.eth.getGasPrice();
    const val = object.utils.toNumber(object.utils.toWei("0.00001", "ether"));

    const createTransaction = await object.eth.accounts.signTransaction(
        {
            from: '0xC16e42b9f4067515bF200a689Bd3Ea63198D242D',
            to: '0xC249DD580837E35aA126a469292Ea0Cf4BEB1A33',
            value: object.utils.toHex(val),
            gasPrice: gaspriceVal
        },
        ‘PKK’
    );

    // Deploy transaction
     const createReceipt = await object.eth.sendSignedTransaction(
        createTransaction.rawTransaction
     );

     console.log(createReceipt)

following is tx hash: https://testnet.bscscan.com/tx/0x530f1f1c0d2274b9f7d8132b3ce174b9d0f66cd1e578120d7e555d777d811db5

and i got following receipt:

{
  blockHash: "0xb06cd600a7927f4bc6ecb75dd6ed1dad3f002b157f7d75a6b3e7986c5d763fbb",
  blockNumber: 33452445n,
  cumulativeGasUsed: 233065n,
  effectiveGasPrice: 10000000000n,
  from: "0xc16e42b9f4067515bf200a689bd3ea63198d242d",
  gasUsed: 21000n,
  logs: [
  ],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: 1n,
  to: "0xc249dd580837e35aa126a469292ea0cf4beb1a33",
  transactionHash: "0x530f1f1c0d2274b9f7d8132b3ce174b9d0f66cd1e578120d7e555d777d811db5",
  transactionIndex: 2n,
  type: 0n,
}
jdevcs commented 1 year ago

with 4.1.1 web3. can you retry and share if you are getting error?

cullsin commented 1 year ago

with 4.1.1 web3. can you retry and share if you are getting errors?

Thanks a lot. let me try. what is your wallet balance? Is this a tesnet? I have only 0.1tBNB.

cullsin commented 1 year ago

Hi,

I am getting a same error. I guess the issue is with balance. I have pasted my snippet changed from yours.

const { Web3 } = require('web3');

async function transact() {
let object = new Web3('https://endpoints.omniatech.io/v1/bsc/testnet/public');

const gaspriceVal = await object.eth.getGasPrice();
const val = object.utils.toNumber(object.utils.toWei("0.00001", "ether"));
const privKey =
'99B3C12287537E38C90A9219D4CB074A89A16E9CDB20BF85728EBD97C343E342';
const createTransaction = await object.eth.accounts.signTransaction(
    {
        from: '0x15878f0A35F006864f8d480537C07630db6f48c8',
        to: '0x33De79D574B096397492c163Df18CCE737b49990',
        value: object.utils.toHex(val),
        gasPrice: gaspriceVal
    },
    privKey
);

// Deploy transaction
 const createReceipt = await object.eth.sendSignedTransaction(
    createTransaction.rawTransaction
 );

 console.log(createReceipt)
}

transact();

The result is below.


error = new web3_errors_1.TransactionRevertInstructionError(_reason, undefined, transactionReceiptFormatted);       
                    ^

TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:48:21     
    at Generator.next (<anonymous>)
    at C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:24:71     
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:20:12)
    at getTransactionError (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:33:12)
    at Object.<anonymous> (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\rpc_method_wrappers.js:441:100)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\rpc_method_wrappers.js:21:58) 
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  innerError: undefined,
  reason: 'err: insufficient funds for gas * price + value: address 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b have 0 want 220000000000000 (supplied gas 21000)',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402
}
jdevcs commented 1 year ago

with 4.1.1 web3. can you retry and share if you are getting errors?

Thanks a lot. let me try. what is your wallet balance? Is this a tesnet? I have only 0.1tBNB.

I had 0.1 tBNB before tx, and after 0.0998 TBNB

jdevcs commented 1 year ago

are you invoking correct js file? and correct account, because your logs has: address 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b have 0 want 220000000000000 while your code has from: 0x15878f0A35F006864f8d480537C07630db6f48c8

cullsin commented 1 year ago

are you invoking correct js file? and correct account, because your logs has: address 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b have 0 want 220000000000000 while your code has from: 0x15878f0A35F006864f8d480537C07630db6f48c8

I have copied your code and just changed the network, from and to the address. where is that anonymous address came from? I got the from and to from metamask account Id. is that correct?

cullsin commented 1 year ago

below is package.json file

{
  "dependencies": {
    "web3": "^4.1.1"
  }
}
jdevcs commented 1 year ago

its from error logs you shared above:

error = new web3_errors_1.TransactionRevertInstructionError(_reason, undefined, transactionReceiptFormatted);       
                    ^

TransactionRevertInstructionError: Transaction has been reverted by the EVM
    at C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:48:21     
    at Generator.next (<anonymous>)
    at C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:24:71     
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:20:12)
    at getTransactionError (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\utils\get_transaction_error.js:33:12)
    at Object.<anonymous> (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\rpc_method_wrappers.js:441:100)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\user\lab63\awdot all\awdot\mono\bin\node_modules\web3-eth\lib\commonjs\rpc_method_wrappers.js:21:58) 
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  innerError: undefined,
  reason: 'err: insufficient funds for gas * price + value: address 0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b have 0 want 220000000000000 (supplied gas 21000)',
  signature: undefined,
  receipt: undefined,
  data: undefined,
  code: 402

make sure you are passing right private key, matching with from addr

cullsin commented 1 year ago

Oh, Ok. What is the correct way to add a private key? Is that from the address private key from the wallet? I just passed some random value as says in my code.

cullsin commented 1 year ago

Hi,

Thanks a lot. I was confused with the private key. It works correctly. However, how to I get private key programmatically as from address will vary dynamically?

Raja K

jdevcs commented 1 year ago

https://docs.web3js.org/guides/basics/sign_and_send_tx/local_wallet#ethereum-transaction

Mae6e commented 9 months ago

I resolved that with parseInt.

const nonce = await web3.eth.getTransactionCount(fromAddr, 'latest'); const gasPrice = await web3.eth.getGasPrice(); const fee = gasPrice.toString() * 21000; const amount = parseInt(amount); const value = (amount - fee); const transaction = { 'to': toAddr, 'value': value, 'gas': 21000, 'gasPrice': gasPrice, 'nonce': nonce, }; const signedTx = await web3.eth.accounts.signTransaction(transaction, key); const response = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);