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)
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 calculationsconst gasPrice = Wei(50_000_000_000n); // 50 Gwei in Wei
Use Wei for: Internal calculations, smart contract values, precision
import * as Gwei from 'tevm/Gwei';// Gwei (gigawei) = 1 billion Wei (10^9)const oneGwei = Gwei(1n); // 1_000_000_000 Weiconst gasPrice = Gwei(50n); // 50 Gwei// Convert to Wei for calculationsconst gasPriceWei = Gwei.toWei(gasPrice); // 50_000_000_000n Wei
Use Gwei for: Gas prices, transaction fees, miner tips
import * as Ether from 'tevm/Ether';// Ether = 1 quintillion Wei (10^18)const oneEther = Ether(1n); // 1_000_000_000_000_000_000 Weiconst balance = Ether(5n); // 5 ETH// Convert to Wei for transactionsconst valueWei = Ether.toWei(balance); // 5_000_000_000_000_000_000n Wei
Use Ether for: User displays, wallet balances, human-readable amounts
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 calculationconst gasPriceWei = Gwei.toWei(gasPriceGwei); // 50_000_000_000n Wei// Simple transfer uses 21,000 gasconst gasUsed = Uint(21_000n);// Calculate total cost: gasPrice × gasUsedconst txCostWei = Uint.times(gasPriceWei, gasUsed);// Result: 1_050_000_000_000_000n Wei (0.00105 ETH)// Convert to Ether for displayconst txCostEther = Wei.toEther(Wei(txCostWei));console.log(`Transaction cost: ${txCostEther} ETH`);
import * as Ether from 'tevm/Ether';import * as Wei from 'tevm/Wei';// User sees balance in Etherconst userBalance = Ether(5n); // 5 ETH// Convert to Wei for transactionconst valueWei = Ether.toWei(userBalance); // 5_000_000_000_000_000_000nconst tx = { to: "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb2", value: valueWei, // Transaction uses Wei};
import * as Wei from 'tevm/Wei';import * as Gwei from 'tevm/Gwei';// Gas price from RPC (in Wei)const gasPriceWei = Wei(50_000_000_000n);// Convert to Gwei for user displayconst gasPriceGwei = Wei.toGwei(gasPriceWei); // 50n Gweiconsole.log(`Current gas price: ${gasPriceGwei} Gwei`);
import * as Wei from 'tevm/Wei';import * as Uint from 'tevm/Uint';// Always use Wei for precise calculationsconst amount1 = Wei(1_000_000_000_000_000_000n); // 1 ETHconst amount2 = Wei(500_000_000_000_000_000n); // 0.5 ETH// Add amounts (in Wei)const total = Uint.plus(amount1, amount2);// Result: 1_500_000_000_000_000_000n Wei (1.5 ETH)// Avoid float arithmetic:// ❌ const total = 1.0 + 0.5 // Floating point errors// ✅ Use Wei with bigint // Always precise