23

I'm looking to estimate transaction fees in an application. In order to do so I need to estimate the current gas price before I execute the transaction.

I see web3.js has a getGasPrice method: https://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#getgasprice

Is there a similar method in ethers.js? Or what is the recommended approach here?

4 Answers 4

21

There is a method called estimateGas in the Contract class. Example for how to use it:

const erc20Abi = [ /* ... */ ];
const address = "TOKEN_ADDRESS_HERE";
const provider = ethers.getDefaultProvider();
const erc20 = new ethers.Contract(address, abi, provider);

const recipient = "SOME_ADDRESS_HERE";
const estimation = await erc20.estimateGas.transfer(recipient, 100);

Caveats:

  1. You have to be connected to a provider, but luckily this is easy to do with the getDefaultProvider function.
  2. Don't expect the gas estimation to be super precise, especially when your contract call is making additional calls to other contracts.
5
  • 4
    Thanks Paul! I did see the estimateGas method but that returns the "units of gas that would be required". In order to arrive at the transaction fee, don't I need both units of gas AND the gas price? ethereum.stackexchange.com/questions/19665/… Jun 2, 2021 at 21:58
  • 1
    Whoops, I misunderstood your question. Sorry. Check out my answer in the How to get the actual gas price thread. Jun 2, 2021 at 22:03
  • Got it, thanks! So no ability to query this within ethers.js directly? Jun 2, 2021 at 22:11
  • 1
    It looks like you found the answer - the getGasPrice function. Jun 3, 2021 at 9:43
  • are you sure this works? who is the from address in this case? it will fail with ERC20: transfer from the zero address. When you create the erc20 variable you will need a signer, also the signer must have at least the amount you want to send as balance. If you want to use the default provider, you have to override the from attribute.
    – gabkov
    Feb 17 at 14:13
18

Found it! https://docs.ethers.io/v5/api/providers/provider/#Provider-getGasPrice

Hint: the search functionality on docs.ethers.io isn't great - this method didn't show up by searching for "gasprice": https://docs.ethers.io/v5/search/?search=gasprice

11

As of 2022 you should use getFeeData() since getGasPrice() has been deprecated. This accounts for network fee changes introduced in EIP-1559. The creator of Ethers.js talks about this in the above link and also provided an example of how to use it.

getFeeData() documentation: https://docs.ethers.io/v5/api/providers/provider/#Provider-getFeeData

0

Firstly, I'd like to say that the third top answer and second top answer are correct but vague. The top answer also implicitly relies on the use of a Contract and a DefaultProvider which is not ideal because a custom provider is much more practical, and you don't always have a Contract to rely on for estimating gas. I had some difficulty with the migration so I am going to share a working example here for everyone.

If you're using TypeScript, the import should look like this:

import { ethers } from "ethers";

To create a provider, you can use the following line:

const provider = new ethers.JsonRpcProvider("some_provider_url");

As the other answers suggested, you can use provider#getFeeData():

const feeData = await provider.getFeeData();

I also had issues processing the output, but instead of using ethers.utils.formatUnits, we now use ethers.formatUnits:

// These are not in a very useful format
let gasPrice = feeData.gasPrice;
let maxFeePerGas = feeData.maxFeePerGas;
let maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;

// Convert the gas price from Wei to Gwei for better readability
const gasPriceInEth = ethers.formatUnits(gasPrice, 'ether');
const gasPriceInGwei = ethers.formatUnits(gasPrice, 'gwei');
const gasPriceInWei = ethers.formatUnits(gasPrice, 'wei');

Should be more than enough to help anyone else with migration issues as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.