import * as Wei from 'tevm/Wei'import * as Gwei from 'tevm/Gwei'import * as Ether from 'tevm/Ether'// From Gweiconst gwei = Gwei(50n)const wei1 = Wei.fromGwei(gwei) // 50_000_000_000n// From Etherconst ether = Ether(1n)const wei2 = Wei.fromEther(ether) // 1_000_000_000_000_000_000n
import * as Gwei from 'tevm/Gwei'import * as Wei from 'tevm/Wei'import * as Ether from 'tevm/Ether'// From Wei (division)const wei = Wei(50_000_000_000n)const gwei1 = Gwei.fromWei(wei) // 50n// From Ether (multiplication)const ether = Ether(1n)const gwei2 = Gwei.fromEther(ether) // 1_000_000_000n
import * as Ether from 'tevm/Ether'const ether = Ether(1n)const wei = Ether.toWei(ether) // 1_000_000_000_000_000_000n// No truncation (multiplication)const ether2 = Ether(5n)const wei2 = Ether.toWei(ether2) // 5_000_000_000_000_000_000n
Formula:wei = ether * 1_000_000_000_000_000_000
Copy
Ask AI
import * as Ether from 'tevm/Ether'const ether = Ether(1n)const gwei = Ether.toGwei(ether) // 1_000_000_000n// No truncation (multiplication)const ether2 = Ether(2n)const gwei2 = Ether.toGwei(ether2) // 2_000_000_000n
Formula:gwei = ether * 1_000_000_000
Copy
Ask AI
import * as Ether from 'tevm/Ether'import * as Wei from 'tevm/Wei'import * as Gwei from 'tevm/Gwei'// From Wei (division, truncates)const wei = Wei(1_000_000_000_000_000_000n)const ether1 = Ether.fromWei(wei) // 1n// From Gwei (division, truncates)const gwei = Gwei(1_000_000_000n)const ether2 = Ether.fromGwei(gwei) // 1n
import * as Gwei from 'tevm/Gwei'import * as Wei from 'tevm/Wei'import * as Uint from 'tevm/Uint'// Gas price in Gwei (user-friendly)const gasPriceGwei = Gwei(50n)// Convert to Wei for calculationconst gasPriceWei = Gwei.toWei(gasPriceGwei)// Calculate total: gasPrice * gasUsedconst gasUsed = Uint(21_000n)const txCostWei = Uint.times(gasPriceWei, gasUsed)// Convert to Ether for displayconst txCostEther = Wei.toEther(Wei(txCostWei))
import * as Ether from 'tevm/Ether'import * as Gwei from 'tevm/Gwei'import * as Wei from 'tevm/Wei'// Start with Etherconst ether = Ether(1n)// Convert through Gwei to Weiconst gwei = Ether.toGwei(ether) // 1_000_000_000nconst wei = Gwei.toWei(gwei) // 1_000_000_000_000_000_000n// Direct conversion (more efficient)const weiDirect = Ether.toWei(ether) // Same result
import * as Wei from 'tevm/Wei'import * as Gwei from 'tevm/Gwei'import * as Uint from 'tevm/Uint'const balance = Wei(100_000_000_000n) // 100 Gwei in Weiconst threshold = Gwei(50n) // 50 Gwei// Convert to same unit for comparisonconst thresholdWei = Gwei.toWei(threshold)const balanceU256 = Wei.toU256(balance)const thresholdU256 = Wei.toU256(thresholdWei)if (balanceU256 > thresholdU256) { console.log("Balance exceeds threshold")}
import * as Wei from 'tevm/Wei'import * as Gwei from 'tevm/Gwei'import * as Uint from 'tevm/Uint'// ✗ Loses precisionconst gwei = Gwei(50n)const ether = Gwei.toEther(gwei) // 0nconst wei = Ether.toWei(ether) // 0n (lost data)// ✓ Preserves precision - work in smallest unitconst gwei = Gwei(50n)const wei = Gwei.toWei(gwei) // 50_000_000_000n// Perform calculations in Weiconst doubled = Uint.times(wei, Uint(2n)) // 100_000_000_000n// Convert to display unit only at the endconst result = Wei.toGwei(Wei(doubled)) // 100n
import * as Ether from 'tevm/Ether'import * as Wei from 'tevm/Wei'import * as Uint from 'tevm/Uint'// ✓ Good - convert to Wei firstconst value = Ether(1n)const valueWei = Ether.toWei(value)const fee = Wei(21_000_000_000_000_000n)const total = Uint.plus(valueWei, fee)// ✗ Bad - mixing unitsconst value = Ether(1n)const fee = Wei(21_000_000_000_000_000n)// Cannot add different types
import * as WeiWasm from 'tevm/Wei.wasm'import * as GweiWasm from 'tevm/Gwei.wasm'import * as EtherWasm from 'tevm/Ether.wasm'const gwei = GweiWasm(50n)const wei = GweiWasm.toWei(gwei)const ether = WeiWasm.toEther(wei)