2

Im building an app to do trades on uniswap using ethers module. ive created contracts and imported the router and the factory contracts using this code:

const factory = new ethers.Contract(
  addresses.factory,
  ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'],
  account
);
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)'
  ],
  account
);

And now i pull a trade using swapExactETHForTokens function:

        const tx = await router.swapExactETHForTokens(
            amountOutMin // Slippage is set here too, if not wanted change to => amounts[1]
            [tokenIn, tokenOut],
            addresses.recipient,
            Date.now() + 1000 * 60  //1 minute expiry time for contract.
        );

My question is how do i implement custom gwei in my trade , there dont appeart to be an argument within uniswap trade functions for gwei at all.

1 Answer 1

2

You can add an optional final parameter to any smart contract function call, which is an object, which can contain keys like value (for attaching ETH to the transaction, which is definitely what you will need to do when calling swapExactETHForTokens), and also gasPrice (for setting the gas price, in gwei).

const tx = await router.swapExactETHForTokens(
  amountOutMin,
  [await router.WETH(), tokenOut], // swapping WETH for a token
  addresses.recipient,
  Date.now() + 1000 * 60,
  { 
    value: "1000000000000000000", // 1 eth
    gasPrice: "100000000000" // 100 gwei
  }
);

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.