Documentation Index Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
Use this file to discover all available pages before exploring further.
Overview
GasConstants provides comprehensive EVM gas cost constants and calculation functions according to the Ethereum Yellow Paper and various EIPs. Use this module to accurately compute gas costs for opcodes, storage operations, transactions, precompiles, and memory expansion.
import * as GasConstants from '@tevm/voltaire/GasConstants' ;
// Access constants directly
const txCost = GasConstants . Tx ; // 21000n
const sloadCost = GasConstants . Sload ; // 100n
// Use calculation functions
const intrinsicGas = GasConstants . calculateTxIntrinsicGas ( calldata , false );
const memoryCost = GasConstants . calculateMemoryExpansionCost ( 64 n , 128 n );
Basic Opcode Costs
Step-based gas costs for EVM opcodes:
Constant Gas Opcodes QuickStep2 ADDRESS, ORIGIN, CALLER, CALLVALUE, CALLDATASIZE, CODESIZE, GASPRICE, RETURNDATASIZE, PC, MSIZE, GAS, CHAINID, SELFBALANCE FastestStep3 ADD, SUB, NOT, LT, GT, SLT, SGT, EQ, ISZERO, AND, OR, XOR, CALLDATALOAD, MLOAD, MSTORE, MSTORE8, PUSH, DUP, SWAP FastStep5 MUL, DIV, SDIV, MOD, SMOD MidStep8 ADDMOD, MULMOD, SIGNEXTEND SlowStep10 JUMPI ExtStep20 BALANCE, EXTCODESIZE, BLOCKHASH
import { QuickStep , FastestStep , FastStep , MidStep , SlowStep , ExtStep } from '@tevm/voltaire/GasConstants' ;
console . log ( QuickStep ); // 2n - very cheap operations
console . log ( FastestStep ); // 3n - simple arithmetic/logic
console . log ( FastStep ); // 5n - multiplication/division
console . log ( MidStep ); // 8n - advanced arithmetic
console . log ( SlowStep ); // 10n - conditional jumps
console . log ( ExtStep ); // 20n - external account access
Hashing Operations
import { Keccak256Base , Keccak256Word , calculateKeccak256Cost } from '@tevm/voltaire/GasConstants' ;
// Constants
Keccak256Base ; // 30n - base cost
Keccak256Word ; // 6n - per 32 bytes
// Calculate cost for 100 bytes of data
const cost = calculateKeccak256Cost ( 100 n ); // 30 + ceil(100/32) * 6 = 54n
Storage Operations (EIP-2929, EIP-2200, EIP-3529)
Storage operations have different costs based on access patterns:
Constant Gas Description Sload100 Warm SLOAD ColdSload2100 Cold SLOAD (EIP-2929) ColdAccountAccess2600 Cold account access (BALANCE, EXTCODESIZE, CALL family) WarmStorageRead100 Warm storage read SstoreSentry2300 Minimum gas for SSTORE SstoreSet20000 SSTORE zero to non-zero SstoreReset5000 SSTORE modify existing non-zero SstoreClear5000 SSTORE clear to zero SstoreRefund4800 Gas refund for clearing storage (EIP-3529)
import { calculateSstoreCost } from '@tevm/voltaire/GasConstants' ;
// Cold slot, zero to non-zero
const result1 = calculateSstoreCost ( false , 0 n , 100 n );
// { cost: 22100n, refund: 0n } (ColdSload + SstoreSet)
// Warm slot, non-zero to zero
const result2 = calculateSstoreCost ( true , 100 n , 0 n );
// { cost: 5000n, refund: 4800n } (SstoreClear + refund)
// Warm slot, modify existing value
const result3 = calculateSstoreCost ( true , 50 n , 100 n );
// { cost: 5000n, refund: 0n } (SstoreReset)
Memory Expansion
Memory costs grow quadratically with size:
import { Memory , QuadCoeffDiv , calculateMemoryExpansionCost } from '@tevm/voltaire/GasConstants' ;
Memory ; // 3n - linear coefficient
QuadCoeffDiv ; // 512n - quadratic coefficient divisor
// Calculate memory expansion from 64 to 128 bytes
const expansion = calculateMemoryExpansionCost ( 64 n , 128 n );
// {
// oldCost: bigint, // Cost for 64 bytes
// newCost: bigint, // Cost for 128 bytes
// expansionCost: bigint, // Additional gas needed
// words: bigint // New memory size in words
// }
Transaction Costs
import {
Tx ,
TxContractCreation ,
TxDataZero ,
TxDataNonZero ,
calculateTxIntrinsicGas
} from '@tevm/voltaire/GasConstants' ;
Tx ; // 21000n - base transaction cost
TxContractCreation ; // 53000n - contract creation base cost
TxDataZero ; // 4n - per zero byte in calldata
TxDataNonZero ; // 16n - per non-zero byte in calldata
// Calculate intrinsic gas for transaction
const calldata = new Uint8Array ([ 0 , 1 , 2 , 0 , 0 ]);
const intrinsicGas = calculateTxIntrinsicGas ( calldata , false );
// 21000 + (3 * 4) + (2 * 16) = 21044n
// Contract creation transaction
const initcode = new Uint8Array ([ 0x60 , 0x00 , 0x60 , 0x00 ]);
const createGas = calculateTxIntrinsicGas ( initcode , true );
// 53000 + (4 * 16) = 53064n
Contract Creation and Calls
import {
Create ,
CreateData ,
InitcodeWord ,
MaxInitcodeSize ,
Call ,
CallStipend ,
CallValueTransfer ,
CallNewAccount ,
CallGasRetentionDivisor
} from '@tevm/voltaire/GasConstants' ;
Create ; // 32000n - base CREATE cost
CreateData ; // 200n - per byte of deployed code
InitcodeWord ; // 2n - per word of initcode (EIP-3860)
MaxInitcodeSize ; // 49152n - max initcode size (EIP-3860)
Call ; // 40n - base CALL cost
CallStipend ; // 2300n - gas stipend for value transfer
CallValueTransfer ; // 9000n - additional cost for value transfer
CallNewAccount ; // 25000n - additional cost for new account creation
CallGasRetentionDivisor ; // 64n - 63/64 rule divisor
Logging Operations
import { LogBase , LogData , LogTopic , calculateLogCost } from '@tevm/voltaire/GasConstants' ;
LogBase ; // 375n - base LOG cost
LogData ; // 8n - per byte of data
LogTopic ; // 375n - per topic
// LOG2 with 100 bytes of data
const logCost = calculateLogCost ( 2 n , 100 n );
// 375 + (2 * 375) + (100 * 8) = 1925n
Copy Operations
import { Copy , calculateCopyCost } from '@tevm/voltaire/GasConstants' ;
Copy ; // 3n - per word for CALLDATACOPY, CODECOPY, RETURNDATACOPY
const copyCost = calculateCopyCost ( 128 n ); // 3 * ceil(128/32) = 12n
EIP-4844: Blob Transactions
import { BlobHash , BlobBaseFee } from '@tevm/voltaire/GasConstants' ;
BlobHash ; // 3n - BLOBHASH opcode cost
BlobBaseFee ; // 2n - BLOBBASEFEE opcode cost
EIP-1153: Transient Storage
import { TLoad , TStore } from '@tevm/voltaire/GasConstants' ;
TLoad ; // 100n - TLOAD cost
TStore ; // 100n - TSTORE cost
Refund Calculation
import { MaxRefundQuotient , calculateMaxRefund } from '@tevm/voltaire/GasConstants' ;
MaxRefundQuotient ; // 5n - max refund is 1/5 of gas used (EIP-3529)
// Calculate maximum refund for transaction
const gasUsed = 100000 n ;
const maxRefund = calculateMaxRefund ( gasUsed ); // 20000n
Hardfork Utilities
Check EIP activation status:
import { hasEIP1153 , hasEIP2929 , hasEIP3529 , hasEIP3860 , hasEIP4844 } from '@tevm/voltaire/GasConstants' ;
hasEIP1153 ( 'cancun' ); // true - transient storage
hasEIP2929 ( 'berlin' ); // true - cold/warm access
hasEIP3529 ( 'london' ); // true - reduced refunds
hasEIP3860 ( 'shanghai' ); // true - initcode limits
hasEIP4844 ( 'cancun' ); // true - blob transactions
Get hardfork-specific costs:
import { getColdAccountAccessCost , getColdSloadCost , getSstoreRefund , getSelfdestructRefund } from '@tevm/voltaire/GasConstants' ;
getColdAccountAccessCost ( 'berlin' ); // 2600n (EIP-2929)
getColdAccountAccessCost ( 'istanbul' ); // 0n (pre-EIP-2929)
getColdSloadCost ( 'berlin' ); // 2100n
getSstoreRefund ( 'london' ); // 4800n (EIP-3529)
getSelfdestructRefund ( 'london' ); // 0n (removed in EIP-3529)
Precompile Gas Costs
The Precompile namespace contains all precompile gas constants and calculation functions:
ECRECOVER, SHA256, RIPEMD160, IDENTITY
import { Precompile } from '@tevm/voltaire/GasConstants' ;
Precompile . EcRecover ; // 3000n - fixed cost
Precompile . Sha256Base ; // 60n
Precompile . Sha256Word ; // 12n
Precompile . calculateSha256Cost ( 100 n ); // 60 + 4 * 12 = 108n
Precompile . Ripemd160Base ; // 600n
Precompile . Ripemd160Word ; // 120n
Precompile . calculateRipemd160Cost ( 100 n ); // 600 + 4 * 120 = 1080n
Precompile . IdentityBase ; // 15n
Precompile . IdentityWord ; // 3n
Precompile . calculateIdentityCost ( 100 n ); // 15 + 4 * 3 = 27n
MODEXP (EIP-2565)
import { Precompile } from '@tevm/voltaire/GasConstants' ;
Precompile . ModExpMin ; // 200n - minimum cost
const cost = Precompile . calculateModExpCost (
32 n , // baseLength
32 n , // expLength
32 n , // modLength
0xFF n // expHead (first 32 bytes of exponent)
);
BN254 (alt_bn128) - Hardfork-dependent
import { Precompile } from '@tevm/voltaire/GasConstants' ;
// ECADD costs
Precompile . EcAddIstanbul ; // 150n (Istanbul+)
Precompile . EcAddByzantium ; // 500n (Byzantium-Berlin)
Precompile . getEcAddCost ( 'cancun' ); // 150n
// ECMUL costs
Precompile . EcMulIstanbul ; // 6000n (Istanbul+)
Precompile . EcMulByzantium ; // 40000n (Byzantium-Berlin)
Precompile . getEcMulCost ( 'cancun' ); // 6000n
// ECPAIRING costs
Precompile . EcPairingBaseIstanbul ; // 45000n
Precompile . EcPairingPerPairIstanbul ; // 34000n
Precompile . calculateEcPairingCost ( 2 n , 'istanbul' ); // 113000n
BLAKE2F
import { Precompile } from '@tevm/voltaire/GasConstants' ;
Precompile . Blake2fPerRound ; // 1n per round
Precompile . calculateBlake2fCost ( 12 n ); // 12n
Point Evaluation (EIP-4844)
import { Precompile } from '@tevm/voltaire/GasConstants' ;
Precompile . PointEvaluation ; // 50000n - fixed cost
Precompile . calculatePointEvaluationCost (); // 50000n
BLS12-381 (EIP-2537 - Prague)
import { Precompile } from '@tevm/voltaire/GasConstants' ;
Precompile . Bls12G1Add ; // 500n
Precompile . Bls12G1Mul ; // 12000n
Precompile . Bls12G2Add ; // 800n
Precompile . Bls12G2Mul ; // 45000n
Precompile . Bls12MapFpToG1 ; // 5500n
Precompile . Bls12MapFp2ToG2 ; // 75000n
// MSM with discount table
Precompile . calculateBls12G1MsmCost ( 10 n ); // Discounted cost for 10 points
Precompile . calculateBls12G2MsmCost ( 10 n );
// Pairing
Precompile . Bls12PairingBase ; // 65000n
Precompile . Bls12PairingPerPair ; // 43000n
Precompile . calculateBls12PairingCost ( 3 n ); // 65000 + 3 * 43000 = 194000n
Complete Gas Constants Reference
Basic Operations Constant Value Description QuickStep2 Very cheap operations FastestStep3 Simple arithmetic FastStep5 Multiplication/division MidStep8 Advanced arithmetic SlowStep10 Conditional jumps ExtStep20 External account access Jumpdest1 JUMPDEST marker
Hashing Constant Value Description Keccak256Base30 Base KECCAK256 cost Keccak256Word6 Per 32-byte word
Storage Constant Value Description Sload100 Warm SLOAD ColdSload2100 Cold SLOAD (EIP-2929) ColdAccountAccess2600 Cold account access WarmStorageRead100 Warm storage read SstoreSentry2300 Minimum SSTORE gas SstoreSet20000 Zero to non-zero SstoreReset5000 Modify non-zero SstoreClear5000 Clear to zero SstoreRefund4800 Clear refund (EIP-3529)
Logging Constant Value Description LogBase375 Base LOG cost LogData8 Per byte of data LogTopic375 Per topic
Calls and Creates Constant Value Description Create32000 Base CREATE cost CreateData200 Per byte deployed InitcodeWord2 Per initcode word (EIP-3860) MaxInitcodeSize49152 Max initcode (EIP-3860) Call40 Base CALL cost CallStipend2300 Value transfer stipend CallValueTransfer9000 Value transfer cost CallNewAccount25000 New account cost CallCode700 CALLCODE (EIP-150) DelegateCall700 DELEGATECALL (EIP-150) StaticCall700 STATICCALL (EIP-214) Selfdestruct5000 SELFDESTRUCT base SelfdestructRefund24000 Removed in EIP-3529 CallGasRetentionDivisor64 63/64 rule
Memory Constant Value Description Memory3 Linear coefficient QuadCoeffDiv512 Quadratic divisor
Transactions Constant Value Description Tx21000 Base transaction TxContractCreation53000 Contract creation TxDataZero4 Per zero byte TxDataNonZero16 Per non-zero byte Copy3 Per copy word MaxRefundQuotient5 Max refund 1/5
EIP-4844 Blobs Constant Value Description BlobHash3 BLOBHASH opcode BlobBaseFee2 BLOBBASEFEE opcode
EIP-1153 Transient Storage Constant Value Description TLoad100 TLOAD cost TStore100 TSTORE cost