ethers.js – How to Set Gas Price on Uniswap

ethers.js

I'm trying to create a transaction but I can't figure out how to set the gas price.
Here's my current code, but according to https://uniswap.org/docs/v2/smart-contracts/router02/#swapexacttokensfortokens there is nowhere to define gas price if I'd like to have a fast transaction

  const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [WETH_ADDRESS, CONTRACT],
    RECIPIENT,
    Date.now() + 1000 * 60 * 10 //10 minutes
  );

Code below is how I create the router

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

Best Answer

You can specify the transaction fields when calling a contract function like so:

 var options = { gasPrice: 1000000000}; // in wei

 const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [WETH_ADDRESS, CONTRACT],
    RECIPIENT,
    Date.now() + 1000 * 60 * 10, //10 minutes
    options
  );