[Ethereum] Ether.js: Set Priority Fee when calling contract

ethers.jsgastransactions

Using ethers.js, when I execute a smart contract function (EIP-1559 transaction), how do I tweak the miner tip paid? Consider the following please:

/** gasEstimate (in gwei)
   {
     gasPrice: '126.962517289',
     maxFeePerGas: '254.244464762',
     maxPriorityFeePerGas: '2.5'
   }
*/
gasEstimate = await ethers.provider.getFeeData();
contract = new ethers.Contract(addr, abi, provider);
withSigner = contract.connect(signer);
await withSigner.runFunc(param1, param2)

In this example, I can see that the suggested maxFeePerGas is 264 and maxPriorityFeePerGas is 2.5. Suppose I want to rush my transaction by increasing each by 10 gwei. Where would I set this when I call my contract function?

Best Answer

You can set the params in the transaction object itself. In the given example.

contract = new ethers.Contract(addr, abi, provider);
withSigner = contract.connect(signer);
await withSigner.runFunc(param1, param2, {
    maxFeePerGas: "<YOUR GAS FEE>",
    maxPriorityFeePerGas: "<YOUR PRIORITY FEE>"
});

In the above code, you can override the gas by simply specifying your maxFeePerGas and maxPriorityFeePerGas in the transaction object. Hope this helps.

Make sure you're sending in the right values for the transaction gas fees (in gwei) or you'll get unable to estimate transaction gas error.