Skip to main content

Try it Live

Run Denomination examples in the interactive playground
Conceptual Guide - For API reference and method documentation, see Denomination API.
Ethereum uses multiple denominations to represent value at different scales. This guide teaches denomination fundamentals using Tevm.

What are Denominations?

Denominations are units of value in Ethereum. All values are internally stored as Wei (the smallest unit), but expressed in different units for readability:
  • Wei - Base unit for precise calculations (like cents)
  • Gwei - Gas price unit (like dollars)
  • Ether - User-facing unit (like thousands of dollars)

The Three Units

import * as Wei from 'tevm/Wei';

// Wei is the smallest unit (1 Wei = 10^-18 Ether)
const oneWei = Wei(1n);
const oneEtherInWei = Wei(1_000_000_000_000_000_000n);

// Used for precise calculations
const gasPrice = Wei(50_000_000_000n);  // 50 Gwei in Wei
Use Wei for: Internal calculations, smart contract values, precision

Conversion Scale

Understanding the relationships between units:
1 Ether = 1,000,000,000 Gwei = 1,000,000,000,000,000,000 Wei

1 Ether = 10^9 Gwei = 10^18 Wei
1 Gwei  = 10^9 Wei
1 Wei   = 10^-18 Ether
Wei:   1
       |
       × 10^9
       |
Gwei:  1,000,000,000
       |
       × 10^9
       |
Ether: 1,000,000,000,000,000,000

Converting Between Units

import * as Ether from 'tevm/Ether';
import * as Gwei from 'tevm/Gwei';
import * as Wei from 'tevm/Wei';

const ethAmount = Ether(1n);  // 1 ETH

// Ether to Gwei
const gweiAmount = Ether.toGwei(ethAmount);  // 1_000_000_000n Gwei

// Ether to Wei
const weiAmount = Ether.toWei(ethAmount);    // 1_000_000_000_000_000_000n Wei

// Gwei to Wei
const weiFromGwei = Gwei.toWei(gweiAmount);  // 1_000_000_000_000_000_000n Wei

console.log(`1 ETH = ${gweiAmount} Gwei = ${weiAmount} Wei`);

Complete Example: Gas Cost Calculation

Calculate total transaction cost from gas price and gas used:
import * as Gwei from 'tevm/Gwei';
import * as Wei from 'tevm/Wei';
import * as Ether from 'tevm/Ether';
import * as Uint from 'tevm/Uint';

// Gas price from network (commonly displayed in Gwei)
const gasPriceGwei = Gwei(50n);  // 50 Gwei

// Convert to Wei for calculation
const gasPriceWei = Gwei.toWei(gasPriceGwei);  // 50_000_000_000n Wei

// Simple transfer uses 21,000 gas
const gasUsed = Uint(21_000n);

// Calculate total cost: gasPrice × gasUsed
const txCostWei = Uint.times(gasPriceWei, gasUsed);
// Result: 1_050_000_000_000_000n Wei (0.00105 ETH)

// Convert to Ether for display
const txCostEther = Wei.toEther(Wei(txCostWei));
console.log(`Transaction cost: ${txCostEther} ETH`);

Gas Price Examples

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

const gasUsed = Uint(21_000n);

// Calculate costs at different gas prices
const prices = [20n, 50n, 100n, 200n];  // Gwei

prices.forEach(price => {
  const gasPriceGwei = Gwei(price);
  const gasPriceWei = Gwei.toWei(gasPriceGwei);
  const costWei = Uint.times(gasPriceWei, gasUsed);

  console.log(`${price} Gwei: ${costWei} Wei`);
});

// Output:
// 20 Gwei: 420_000_000_000_000 Wei (0.00042 ETH)
// 50 Gwei: 1_050_000_000_000_000 Wei (0.00105 ETH)
// 100 Gwei: 2_100_000_000_000_000 Wei (0.0021 ETH)
// 200 Gwei: 4_200_000_000_000_000 Wei (0.0042 ETH)

Formatting for Display

When displaying values to users, convert to appropriate units:
import * as Wei from 'tevm/Wei';
import * as Ether from 'tevm/Ether';

const balance = Wei(1_234_567_890_123_456_789n);

// Convert to Ether for user display
const ethBalance = Wei.toEther(balance);  // 1n Ether

// For fractional display, handle remainder separately
const weiPerEther = 1_000_000_000_000_000_000n;
const wholePart = balance / weiPerEther;      // 1n
const fractionalPart = balance % weiPerEther;  // 234_567_890_123_456_789n

console.log(`Balance: ${wholePart}.${fractionalPart} ETH`);
// "Balance: 1.234567890123456789 ETH"

Common Use Cases

import * as Ether from 'tevm/Ether';
import * as Wei from 'tevm/Wei';

// User sees balance in Ether
const userBalance = Ether(5n);  // 5 ETH

// Convert to Wei for transaction
const valueWei = Ether.toWei(userBalance);  // 5_000_000_000_000_000_000n

const tx = {
  to: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb2",
  value: valueWei,  // Transaction uses Wei
};

Precision Considerations

import * as Wei from 'tevm/Wei';

// ✅ Correct: Use bigint for precise values
const correct = Wei(1_000_000_000_000_000_000n);

// ❌ Wrong: JavaScript numbers lose precision beyond 2^53
const wrong = Wei(1000000000000000000);  // Precision loss!

// ✅ Use bigint literals (n suffix)
const gasPrice = 50_000_000_000n;

// ✅ Convert from string if needed
const amount = Wei("1000000000000000000");

Type Safety

Tevm prevents accidental unit mixing at compile time:
import * as Wei from 'tevm/Wei';
import * as Gwei from 'tevm/Gwei';
import * as Uint from 'tevm/Uint';

const gasPrice = Gwei(50n);
const gasUsed = Uint(21_000n);

// ❌ Type error - cannot mix Gwei and Uint
const costWrong = Uint.times(gasPrice, gasUsed);

// ✅ Correct - explicit conversion to Wei
const gasPriceWei = Gwei.toWei(gasPrice);
const costCorrect = Uint.times(gasPriceWei, gasUsed);

Resources

Next Steps

  • Overview - Type definitions and API reference
  • Wei - Base unit operations
  • Gwei - Gas price unit
  • Ether - User-facing unit
  • Conversions - Converting between units