To calculate the gas fees to pay to execute a transaction, you need to multiply the amount of gas the transaction will use by the gas price.
To get the gas price on the blockchain your Ethers provider is in, you can call the
const ethers = require("ethers") const provider = new ethers.providers.Web3Provider(YOUR_PROVIDER_HERE) // Get the current gas price const gasPrice = await provider.getGasPrice();
It's as simple as that!
If you want to learn how to estimate the amount of gas a transaction consume to be able to compute the gas fees that a transaction will cost, check out this tutorial on how to calculate the gas fees a transaction costs:
And that's it 🎉
Thank you for reading this article