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(
  [token0, token1], 
  Date.now() + 1000 * 60 * 10,

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


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).

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.