9

i've codded a script using js to trigger a buy on uniswap. the way i achieved this is through importing uniswaps smart contracts using this line of code:

const router = new ethers.Contract(addresses.router,['function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)'

],

in any case unfortunately I was met with an error saying unable to estimate gas limit , and after setting proper parameters for 'gasPrice' and 'value' I receive this error:

Error: insufficient funds for intrinsic transaction cost (error={"code":-32000,"response":"{"jsonrpc":"2.0","id":4,"error":{"code":-32000,"message":"insufficient funds for transfer"}}"}, method="estimateGas"

this is the code i used for my Uniswap Trade:

async function Buythis(){
const token0 = addresses.WETH; //Etherium
const token1 = '0x358aa737e033f34df7c54306960a38d09aabd523' //Desired Coin Address
const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //Amount you wish to buy with. (0.15 eth in wallet currently.)
console.log('Amount in Passed')
const amounts = await router.getAmountsOut(amountIn, [token0, token1]);
console.log('Amounts out calculated')
const amountOutMin = amounts[1].sub(amounts[1].div(5)); //slippage
console.log('Slippage is set.')
var options = {gasPrice: ethers.utils.parseUnits('50', 'gwei'), value: ethers.utils.parseUnits('0.04', 'ether')};
console.log('Initiating Order.')
const tx = await router.swapExactETHForTokens(
  amountOutMin, 
  [token0, token1], 
  addresses.recipient, 
  Date.now() + 1000 * 60 * 10,
  options
  );

EDIT : yes it logged 'Initiating order' so the error is in the swapExactETHforTokens

5 Answers 5

9

The wallet that you connected to your router contract object does not have enough ETH balance to cover value + gasPrice * gasLimit.

In your case value = 0.04 ETH.

Assuming gasLimit = 200,000, gasPrice * gasLimit = 200000 * 50 * 10^-9 = 0.01.

So total you need to have at least 0.05 ETH balance (based on estimated gasLimit as above).

4

For me, the error came up because I was deploying my contract with an eth value greater than what I had in my Metamask wallet.

My total wallet balance was 0.9 ETH and I was trying to deploy with 1ETH :

var contract = await contractFactory.deploy({value: hre.ethers.utils.parseEther("1")});

changing the value to 0.07 solved it:

var contract = await contractFactory.deploy({value: hre.ethers.utils.parseEther("0.07")});

1

I also had parseEther at ("1") and increased the amount of ETH in my wallet to above 1 using the goerli faucet.

This fixed the problem for me.

my code: const sendValue = ethers.utils.parseEther("1")

You could also fix this by decreasing that amount like the above answer.

Link to faucet: https://goerlifaucet.com/

0

In my case I was testing a revert scenario and ethers couldnt estimate the gas properly because it knew the operation would fail. More here.

Just putting the asLimit manually didnt work, I had to include the maxFeePerGas, and the maxPriorityFeePerGasas well like this:

const feeData = await ethers.provider.getFeeData();
await contract.funtion(arguments, {gasLimit: 250000, maxFeePerGas: feeData.maxFeePerGas, maxPriorityFeePerGas: feeData.maxPriorityFeePerGas});
0

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'
       ]
3
  • This is a question not an answer. Also convert the value you are sending, it is "997655935916089812264981854433328". Maybe look at what is happening when you set that.
    – Maka
    Commented Sep 9, 2023 at 14:48
  • Hi, Sorry. can you please explain more.
    – kulls
    Commented Sep 11, 2023 at 4:04
  • This "value: '0x3130303030303030303030303030'," when converted from hexadecimal to integer is 997655935916089812264981854433328. Which converted from wei, would be 997655935916089.812264981854433328 tBNB
    – Maka
    Commented Sep 11, 2023 at 4:21

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.