Created
June 18, 2025 17:20
-
-
Save willmendesneto/30700e86a20c58b5d4733cbf4f5967c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Provider } from "@ethersproject/providers"; | |
import { ContractTransaction } from "ethers"; | |
export async function gasReport(tx:ContractTransaction, provider:Provider) { | |
const [{ gasUsed }, gasPrice] = await Promise.all([ | |
tx.wait(), | |
provider.getGasPrice(), | |
]); | |
const costInWei = gasUsed.mul(gasPrice); | |
const costInGwei = costInWei.div(1e9); | |
const costInEth = costInGwei.toNumber() / 1e9; | |
const usdPerEth = parseFloat(process.env.NAMEFI_USD_PER_ETHER || '') || 3000.0; | |
console.log(`Gas used: ${gasUsed.toString()} | |
Gas price (wei): ${gasPrice.toString()} | |
Cost (in wei): ${costInWei.toString()} | |
Cost (in gwei): ${costInGwei.toString()} | |
Cost (in ethers): ${costInEth.toString()} | |
Cost (in USD): ${costInEth * usdPerEth}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment