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