ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.96k stars 1.85k forks source link

Error: insufficient funds (version=4.0.47) #855

Closed idoor88 closed 4 years ago

idoor88 commented 4 years ago

Hi All, I am using this library to send usdt with the following code, but when I pass in 50 as amount to be sent to another address and my usdt balance is 97.59, then I got error: insufficient funds, but I have 97.59 usdt in my account, only sending 50, also my ETH balance in my account is 0.00125, which is for the gas fee:

wallet sendTUSDToAddress...error: Error: insufficient funds (version=4.0.47) 2020-05-28T16:38:10.716047+00:00 app[web.1]: at Object.throwError (/app/node_modules/ethers/errors.js:76:17) 2020-05-28T16:38:10.716047+00:00 app[web.1]: at /app/node_modules/ethers/providers/json-rpc-provider.js:276:36 2020-05-28T16:38:10.716048+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:97:5) { 2020-05-28T16:38:10.716049+00:00 app[web.1]: reason: 'insufficient funds', 2020-05-28T16:38:10.716050+00:00 app[web.1]: code: 'INSUFFICIENT_FUNDS', 2020-05-28T16:38:10.716050+00:00 app[web.1]: transaction: { 2020-05-28T16:38:10.716051+00:00 app[web.1]: nonce: 0, 2020-05-28T16:38:10.716052+00:00 app[web.1]: gasPrice: BigNumber { _hex: '0x0826299e00' }, 2020-05-28T16:38:10.716054+00:00 app[web.1]: gasLimit: BigNumber { _hex: '0xa0ed' }, 2020-05-28T16:38:10.716054+00:00 app[web.1]: to: '0xdAC17F958D2ee523a2206206994597C13D831ec7', 2020-05-28T16:38:10.716054+00:00 app[web.1]: value: BigNumber { _hex: '0x00' }, 2020-05-28T16:38:10.716056+00:00 app[web.1]: data: '0xa9059cbb000000000000000000000000df59a6d6c2f09210a60033e513ffd8d61c69be1f0000000000000000000000000000000000000000000000000000000002faf080',

My code is:

`try{ let usdAbi = Token.abi; let contract = new ethers.Contract(contractAddressUSD, usdAbi,PROVIDER); let wallet2 = new ethers.Wallet(privateKeyDec, PROVIDER); let contractWithSigner = contract.connect(wallet2);

    var amount2 = utils.parseUnits(amount.toString(), 6).toNumber();
    let usdtBal = await contract.balanceOf(fromAddress);  
       console.log("wallet sendTUSDToAddress...amount2: ", amount2); 
    usdtBal = usdtBal.toString();
    console.log("wallet sendTUSDToAddress...usdtBal: ", usdtBal);
    if(Number(usdtBal) >= amount2){

        var transfer = await contractWithSigner.transfer(toAddress, amount2);  
        console.log("wallet sendTUSDToAddress...transfer: ", transfer);
        return transfer;
    }else{ 
      return {
        error: "Not enough Tether USD in your wallet",
        usdtbal: usdtBal
      }
    }
}catch(error) {

  console.log("wallet sendTUSDToAddress...error: ", error);
  return {
    error: error,
  }
}`

Any clues? Thanks for your help.

ricmoo commented 4 years ago

That error looks like it the amount of ether you have in the sending account is actually insufficient to cover the gas fee. Can you include the gasPrice and gasLimit you were using (or that was populated for you)? Such the the signed transaction.

You must have enough ether to cover the gas (base + intrinsic + execution) multiplied by the gasPrice.

Let me know, and we'll double-check that 0.00125 is enough. :)

idoor88 commented 4 years ago

Thanks for your response, my code to get gasfee: ` let gasPrice = await PROVIDER.getGasPrice();

    console.log("getETHGasFee...gasPrice: ", gasPrice.toString());

    let gasLimit = 21000;

    let gas = gasPrice.mul(gasLimit);

   gas = gas.toString();

    console.log("wallet getETHGasFee...gas: ", gas);

    gas = utils.formatEther(gas);

    console.log("wallet after format getETHGasFee...gas: ", gas);

`

The execution result of the code is: getETHGasFee...: 2020-05-21T20:30:39.362282+00:00 app[web.1]: getETHGasFee...gasPrice: 28600000000 2020-05-21T20:30:39.362445+00:00 app[web.1]: wallet getETHGasFee...gas: 600600000000000 2020-05-21T20:30:39.362722+00:00 app[web.1]: wallet after format getETHGasFee...gas: 0.0006006

ricmoo commented 4 years ago

That gasLimit is too low. A bare transaction, with no data requires 21,000. Once you add data and/or specify an address with code at it (i.e. a contract) you require more. Possibly significantly more.

If you use the estimateGas method, you can get an idea of how much gas that call will likely actually require.

idoor88 commented 4 years ago

Thanks, I will raise the gaslimt.

AndyJiangPro commented 4 years ago

@ricmoo Hey, I encountered this error even if I set the gasLimit to 8M. My sample code: const overrides = { gasLimit: 8000000, gasPrice: ethers.utils.parseUnits('90', 'gwei'), }; // Call contract await myDapp.payForDinner(erc20.usdc.address, '1000000000', overrides); Besides, I dumped my account: const ethBalance = await provider.getBalance(wallet.address) and it still has 1 eth, I believe it is enough.

Could you please share some insights what could go wrong. Thanks

ricmoo commented 4 years ago

@beyondmtk Can you include the error? Is it complaining you don’t have enough ether, or is it a call_exception because you have insufficient tokens? Is there a transaction hash? Can you send me the address?

ricmoo commented 4 years ago

When you call a state changing method, this is performed by sendTransaction, the transaction encodes the name of the method payForDinner within all that hex data. So that's ok. ;)

You actually do not have sufficient funds. Your address only has 0.2 ether; but at 90 gwei, 8M gas is about 0.72 ether.

By the way, 8M is a LOT. I don't many miners would even try to include that as I think that exceeds the recommended transaction gas limit (which is only advisory) but is getting very close to the block gas limit, which is protocol-enforced.

AndyJiangPro commented 4 years ago

Thanks for that. I will change it to "2M" and give it a try, thanks for your great help. BTW, @ricmoo I am wondering if you got my address by my contract address? Again, thanks for that.

LeftoverChineseFood commented 3 years ago

({gasPrice: ethers.utils.parseUnits('100', 'gwei'), gasLimit: ethers.utils.hexlify(100000)} my gas is set to this, and It won't work with any number greater than 0 despite me having 5.3 eth in a ropsten wallet

ranal commented 2 years ago

@HolySuicidalTurkey Did you find a solution? I'm in the exact same situation, having > 5 ETH in my Ropsten wallet.

ricmoo commented 2 years ago

@Ranal can you include a code snippet?

ranal commented 2 years ago

@ricmoo I am trying to swap WETH to DAI on Uniswap v2 with the following code:

const sendTx = async() => {
    const tx = await router.swapETHForExactTokens(
        1,
        ['0xc778417e063141139fce010982780140aa0cd5ab', '0xad6d458402f60fd3bd25163575031acdce07538d'],
        '0x763CcfDa285Bb16Ac1C9619906156EfF2d72b63b',
        Date.now() + 1000 * 60 * 5,
        {
            'gasLimit': 150000,
            'gasPrice': 3
        });
}

Which results in:

"error":{"code":-32000,"message":"insufficient funds for gas * price + value"}

I have 5 ETH and 3 WETH on Ropsten.

zemse commented 2 years ago

@Ranal Can you confirm if you are using the right wallet (basically the one with funds)?

console.log(await router.signer.getAddress())

Also few more issues you might run into:

ranal commented 2 years ago

@zemse Thank you, turned out to be a problem with the wallet.

LeftoverChineseFood commented 2 years ago

also don't forget to suspend gas limits!