8

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

4 Answers 4

7

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});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.