How to estimate the gas fees for a transaction using Ethers JS and JavaScript

In this tutorial, we are going to learn how to estimate the gas fees that a transaction will cost using Ethers JS and JavaScript.

In this tutorial, we are going to learn how to estimate the gas fees that a transaction will cost using Ethers JS and JavaScript.

If you want to estimate the gas fees for a transaction on the Ethereum blockchain or other EVM networks, you need to get the current gas price and multiply it by the amount of gas that your transaction will use.

We are going to see how to do both in this tutorial.

Here is the full example of how to calculate the gas amount for a transaction:

const ethers = require("ethers");

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE);

// 1. Get the gas amount needed for your transaction
// 2. Get the current gas price
// 3. Multiply the gas amoung by the gas price to get the gas fee


// Estimate the gas amount for a transaction by passing the transaction parameters
const gasAmount = await provider.estimateGas({
    to: "DESTINATION_ADDRESS_HERE",
    value: ethers.utils.parseEther("0.1")
});

// OR

// Estimate the gas amount needed to call a smart contract function
const contract = new ethers.Contract(contractAddress, ABI, provider);
// call the contract function you want with the params you want
const gasAmount = await contract.estimateGas.yourFunction(param1, param2);


// Get the current gas price
const gasPrice = await provider.getGasPrice();


// Calculate the gas fee
const gasFeeInWei = gasPrice.mul(gasAmount)
const gasFeeInETH = ethers.utils.formatEther(gasFeeInWei)

Now let's see in details what that code does.

How to get the gas price using Ethers

To get the current gas price on the network, Ethers provides a function called getGasPrice. It returns an estimation of the current price of the gas in Wei on the blockchain (we can't be 100% sure).

The type of the returned data is a BigNumber. That type is used to manipulate Wei because the amounts of Wei are usually extremely large numbers because they have 18 decimals.

Here is how to use that function:

const ethers = require("ethers");

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE);

const gasPrice = await provider.getGasPrice();

Then, you can convert the gas price from wei to Ether or gwei using the formatUnits function:

// converts the gas price to gwei:
const gweiPrice = ethers.utils.formatUnits(gasPrice, "gwei")

// converts the gas price to ether
const ethPrice = ethers.utils.formatUnits(gasPrice)

// also converts the gas price to ether
const alsoEthPrice = ethers.utils.formatEther(gasPrice)

If you don't intend to display it to the user and just want to calculate the gas fees, you can just leave the gas price in wei.

How to estimate the amount of gas needed for a transaction using Ethers

To get the amount of gas needed to execute a transaction, we use the estimateGas function.

There are 2 ways of using that function, you can use it to estimate the cost of a transaction based on a transaction object or use it on a smart contract to estimate the cost of calling a function.

How to estimate the gas amount needed to send a transaction

To estimate the gas amount needed to send a transaction, you can call the estimateGas function and pass in an object that contains the parameters of the transaction you want to send.

That object is the exact same object that you pass to the sendTransaction function when you want to send a transaction.

Check out this guide to learn how to send Ethereum transactions with Ethers and how to use the sendTransaction function.

Here is an example of estimating the gas amount for a transaction:

const ethers = require("ethers");

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE);

const gasAmount = await provider.estimateGas({
    // the same transaction parameters you would pass when sending a transaction
    to: "DESTINATION_ADDRESS_HERE",
    value: ethers.utils.parseEther("0.1")
});

In the code above, we estimate the gas amount needed to send 0.1 ETH to another address.

The gas amount returned will be a BigNumber.

How to estimate the gas amount needed to call a smart contract function

To do that, we create a contract object, use the estimateGas property and call the smart contract function on it.

Here is an example where we estimate the cost of calling the transfer function on an ERC-20 token smart contract:

const ethers = require("ethers");

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE);

const ABI = [ /* SMART CONTRACT ABI HERE */ ];
const contractAddress = "SMART_CONTRACT_ADDRESS_HERE";

const contract = new ethers.Contract(contractAddress, ABI, provider);

const to = "DESTINATION_ADDRESS_HERE"
const amount = 100 // the amount of tokens to transfer

const gasAmount = await contract.estimateGas.transfer(to, amount);

In the code above, the gas amount returned will be a BigNumber as well.

Multiplying the gas price by the gas amount to calculate the gas fees

Since both the gas amount and the gas price are BigNumber, you can just multiply them like normal JavaScript numbers. To multiply BigNumbers, you need to use the mul method:

const ethers = require("ethers");

const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE);

const gasAmount = await provider.estimateGas({
    to: "DESTINATION_ADDRESS_HERE",
    value: ethers.utils.parseEther("0.1")
});

const gasPrice = await provider.getGasPrice();

const gasFeeInWei = gasPrice.mul(gasAmount)
const gasFeeInETH = ethers.utils.formatEther(gasFeeInWei)

In the code above, we use the mul method to multiply the gas price by the gas amount. That gives us the gas fee our transaction will cost in wei. We can then convert that to Ether by calling ethers.utils.formatEther and passing it the amount in Wei.

This code also works if you get the gas amount needed to call a smart contract function like we've done above.

And that's it 🎉

Thank you for reading this article