Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created June 18, 2025 17:20
Show Gist options
  • Save willmendesneto/30700e86a20c58b5d4733cbf4f5967c1 to your computer and use it in GitHub Desktop.
Save willmendesneto/30700e86a20c58b5d4733cbf4f5967c1 to your computer and use it in GitHub Desktop.
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