5

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?

1 Answer 1

6

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.

2
  • Thanks for this info Sujith. You said to make sure I'm sending the values in gwei. Are you sure it's gwei? I ask because the provider.getFeeData() returns maxFeePerGas and maxPriorityFeePerGas in wei; I just formatted that to make it more legible in my post Nov 4, 2021 at 18:01
  • Sorry, it's in Wei. I meant the current gas costs are in range of Gwei. So that might confused you Nov 4, 2021 at 18:04

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.