Skip to main content

Try it Live

Run Denomination examples in the interactive playground

Gas Cost Calculator

Calculate Ethereum transaction costs and convert between Wei, Gwei, Ether, and USD.

Quick Gas Cost Examples

Basic Transfer (21,000 gas)

import * as Gwei from 'tevm/Gwei'
import * as Wei from 'tevm/Wei'
import * as Uint from 'tevm/Uint'

const gasPrice = Gwei(20n)      // 20 Gwei
const gasUsed = 21_000n              // Standard transfer

// Calculate cost
const gasPriceWei = Gwei.toWei(gasPrice)
const costWei = Uint.times(gasPriceWei, Uint(gasUsed))
const costEther = Wei.toEther(Wei(costWei))

console.log(`Cost: ${costWei} Wei`)    // 420,000,000,000,000 Wei
console.log(`Cost: ${Gwei(Uint.dividedBy(costWei, Uint(1_000_000_000n)))} Gwei`)  // 420,000 Gwei
console.log(`Cost: ${costEther} ETH`)  // 0 ETH (truncates)
Cost breakdown:
  • Gas Price: 20 Gwei/gas
  • Gas Used: 21,000 gas
  • Total: 420,000 Gwei = 0.00042 ETH

Complex Contract Interactions

Token Transfer (ERC-20) - 65,000 gas

import * as Gwei from 'tevm/Gwei'
import * as Wei from 'tevm/Wei'
import * as Uint from 'tevm/Uint'

function calculateTokenTransferCost(gasPriceGwei: Gwei.Type): {
  wei: bigint
  gwei: string
  ether: string
} {
  const gasUsed = 65_000n  // ERC-20 transfer

  const gasPriceWei = Gwei.toWei(gasPriceGwei)
  const costWei = Uint.times(gasPriceWei, Uint(gasUsed))

  // Convert to different units
  const costGwei = Uint.dividedBy(costWei, Uint(1_000_000_000n))
  const costEther = Wei.toEther(Wei(costWei))

  return {
    wei: costWei,
    gwei: `${costGwei} Gwei`,
    ether: `${costEther} ETH (${Number(costWei) / 1e18} exact)`
  }
}

// Cost comparison
const slow = calculateTokenTransferCost(Gwei(20n))
const standard = calculateTokenTransferCost(Gwei(50n))
const fast = calculateTokenTransferCost(Gwei(200n))

console.log('Slow:', slow)        // 1,300,000 Gwei = 0.0013 ETH
console.log('Standard:', standard) // 3,250,000 Gwei = 0.00325 ETH
console.log('Fast:', fast)         // 13,000,000 Gwei = 0.013 ETH

Uniswap Swap - 150,000 gas

import * as Gwei from 'tevm/Gwei'
import * as Wei from 'tevm/Wei'
import * as Uint from 'tevm/Uint'

function calculateSwapCost(gasPriceGwei: Gwei.Type): {
  wei: bigint
  gwei: string
  ether: string
} {
  const gasUsed = 150_000n  // Complex DEX swap

  const gasPriceWei = Gwei.toWei(gasPriceGwei)
  const costWei = Uint.times(gasPriceWei, Uint(gasUsed))

  const costGwei = Uint.dividedBy(costWei, Uint(1_000_000_000n))
  const costEther = Wei.toEther(Wei(costWei))

  return {
    wei: costWei,
    gwei: `${costGwei} Gwei`,
    ether: `${costEther} ETH (${Number(costWei) / 1e18} exact)`
  }
}

// Cost comparison
const slow = calculateSwapCost(Gwei(20n))
const standard = calculateSwapCost(Gwei(50n))
const fast = calculateSwapCost(Gwei(200n))

console.log('Slow:', slow)        // 3,000,000 Gwei = 0.003 ETH
console.log('Standard:', standard) // 7,500,000 Gwei = 0.0075 ETH
console.log('Fast:', fast)         // 30,000,000 Gwei = 0.03 ETH

EIP-1559 Gas Calculation

With EIP-1559, gas fees include base fee + priority fee:
totalCost = (baseFee + priorityFee) × gasUsed

Example with 50 Gwei base fee, 2 Gwei priority, 21,000 gas:
totalCost = (50 + 2) Gwei × 21,000 = 52 Gwei × 21,000 = 1,092,000 Gwei

USD Conversion

Convert gas costs to USD for budget planning:
import * as Gwei from 'tevm/Gwei'
import * as Wei from 'tevm/Wei'
import * as Uint from 'tevm/Uint'

function gasCostUSD(
  gasPriceGwei: Gwei.Type,
  gasUsed: bigint,
  ethPriceUsd: number  // e.g., 2000.50
): number {
  const gasPriceWei = Gwei.toWei(gasPriceGwei)
  const costWei = Uint.times(gasPriceWei, Uint(gasUsed))

  // Convert Wei to ETH (as decimal)
  const costEth = Number(costWei) / 1e18

  return costEth * ethPriceUsd
}

// Example: 50 Gwei gas price, 21,000 gas, ETH = $2,000
const costUsd = gasCostUSD(
  Gwei(50n),
  21_000n,
  2000
)

console.log(`Cost: $${costUsd.toFixed(2)} USD`)  // $2.10 USD

Gas Estimation Tools

Safe Gas Estimates

import * as Gwei from 'tevm/Gwei'

// Get current gas recommendations
function getGasRecommendation(baseFeeGwei: number): {
  safe: Gwei.Type
  standard: Gwei.Type
  fast: Gwei.Type
  instant: Gwei.Type
} {
  return {
    safe: Gwei(BigInt(Math.ceil(baseFeeGwei * 1.2))),
    standard: Gwei(BigInt(Math.ceil(baseFeeGwei * 1.5))),
    fast: Gwei(BigInt(Math.ceil(baseFeeGwei * 2))),
    instant: Gwei(BigInt(Math.ceil(baseFeeGwei * 3)))
  }
}

// Current base fee: 40 Gwei
const recommendations = getGasRecommendation(40)

console.log(`Safe: ${recommendations.safe} Gwei`)      // 48 Gwei
console.log(`Standard: ${recommendations.standard} Gwei`) // 60 Gwei
console.log(`Fast: ${recommendations.fast} Gwei`)      // 80 Gwei
console.log(`Instant: ${recommendations.instant} Gwei`) // 120 Gwei

Cost Comparison Across Networks

See Network Comparison for gas costs across mainnet, L2s, and testnets.

Common Gas Amounts

OperationGasDescription
Transfer21,000Simple ETH transfer
ERC-20 Transfer60,000Token transfer
Uniswap V3 Swap150,000-300,000DEX swap
NFT Mint80,000-150,000NFT creation
Approve45,000Token approval
Deploy Contract500,000-2,000,000Contract deployment