Execution Gas
Gas is a sum of IntrinsicGas
and ExecutionGas
. In here, we would focus on how ExecutionGas
is calculated.
Execution gas related hardfork changes can be found at the bottom of this page. Go to Hardfork Changes.
Overview
Execution gas is charged during executing a contract under three distinct circumstances. Sometimes, some policies may be omitted.
- The first and most common is the
constantGas
. It's a fee intrinsic to the computation of the operation. - Second, gas may be deducted to form the payment for a subordinate message call or contract creation; this forms part of the payment for
CREATE
,CALL
andCALLCODE
. - Finally, gas may be charged due to an increase in memory usage.
Over an account's execution, the total fee payable for memory-usage payable is proportional to the smallest multiple of 32 bytes that are required to include all memory indices (whether for read or write) in the range. This fee is paid on a just-in-time basis; consequently, referencing an area of memory at least 32 bytes greater than any previously indexed memory will result in an additional memory usage fee. Due to this fee, it is highly unlikely that addresses will ever exceed the 32-bit bounds. That said, implementations must be able to manage this eventuality.
Storage fees have a slightly nuanced behavior. To incentivize minimization of the use of storage (which corresponds directly to a larger state database on all nodes), the execution fee for an operation that clears an entry from storage is not only waived but also elicits a qualified refund; in fact, this refund is effectively paid in advance because the initial usage of a storage location costs substantially more than normal usage.
Opcode Gas Schedule
The fee schedule G
is a tuple of 37 scalar values corresponding to the relative costs, in gas, of a number of abstract operations that a transaction may incur. Also, there's gas items to calculate the gas of the precompiled contracts called by CALL_*
opcodes.
Scalar values representing constantGas
of an opcode
Name | Value | Name in code | Opcodes |
---|---|---|---|
G_base | 2 | GasQuickStep | ADDRESS , ORIGIN , CALLER , CALLVALUE , CALLDATASIZE , CODESIZE , GASPRICE , COINBASE , TIMESTAMP , NUMBER , PREVRANDAO (originally it was DIFFICULTY ), GASLIMIT , RETURNDATASIZE , POP , PC , MSIZE , GAS , CHAINID , BASEFEE , PUSH0 , BLOBBASEFEE |
G_verylow | 3 | GasFastestStep | ADD , SUB , LT , GT , SLT , SGT , EQ , ISZERO , AND , OR , XOR , NOT , BYTE , CALLDATALOAD , MLOAD , MSTORE , MSTORE8 , PUSH , DUP , SWAP , BLOBHASH , MCOPY |
G_low | 5 | GasFastStep | MUL , DIV , SDIV , MOD , SMOD , SIGNEXTEND , SELFBALANCE |
G_mid | 8 | GasMidStep | ADDMOD , MULMOD , JUMP |
G_high | 10 | GasSlowStep | JUMPI |
G_selfdestruct | 5000 | SelfdestructGas | SELFDESTRUCT |
G_warmStorageReadCost | 100 | WarmStorageReadCostEIP2929 | EXTCODECOPY , EXTCODESIZE , EXTCODEHASH , BALANCE , CALL , CALLCODE , STATICCALL , DELEGATECALL , TSTORE , TLOAD |
G_blockhash | 20 | GasExtStep | BLOCKHASH |
G_jumpdest | 1 | JumpdestGas | JUMPDEST |
G_sha3 | 30 | Sha3Gas | SHA3 |
G_create | 32000 | CreateGas | CREATE , CREATE2 |
Scalar values used to calculate the gas based on memory and log usage
Name | Value | Name in Code | Description |
---|---|---|---|
G_memory | 3 | MemoryGas | Amount of gas paid for every additional word when expanding memory |
G_copy | 3 | CopyGas | Partial payment for COPY operations, multiplied by words copied, rounded up |
G_log | 375 | LogGas | Partial payment for a LOG operation |
G_logdata | 8 | LogDataGas | Amount of gas paid for each byte in a LOG operation's data |
G_logtopic | 375 | LogTopicGas | Amount of gas paid for each topic of a LOG operation |
Scalar values used to calculate the gas of the particular opcode
Name | Value | Name in Code | Description |
---|---|---|---|
G_sset | 20000 | SstoreSetGas | Amount of gas paid when the storage value when set storage |
G_sreset | 5000 | SstoreResetGas | Amount of gas paid when the storage value remains unchanged at zero or is set to zero |
G_coldSloadCost | 2100 | ColdSloadCostEIP2929 | Amount of gas paid when the storage value is not in accessList |
R_sclear | 15000 | SstoreClearsScheduleRefundEIP3529 | G_sreset - G_coldSloadCost + TxAccessListStorageKeyGas (1900) |
G_exp | 10 | ExpGas | Partial payment |
G_expbyte | 50 | ExpByte | Partial payment when multiplied by ceil(log_256(exponent)) |
G_selfdestruct | 5000 | SelfdestructGas | Amount of gas paid for a SELFDESTRUCT operation |
G_callvalue | 9000 | CallValueTransferGas | Amount of gas paid for a nonzero value transfer |
G_callstipend | 2300 | CallStipend | Free gas given at beginning of call for a nonzero value transfer |
G_newaccount | 25000 | CallNewAccountGas | Amount of gas paid when creating an account. It is also be defined as CreateBySelfdestructGas with SELFDESTRUCT operation. |
G_codedeposit | 200 | CreateDataGas | Amount of gas paid per byte for a creating a contract that succeeds in placing code into state |
G_sha3word | 6 | Sha3WordGas | Amount of gas paid for each word (rounded up) for an SHA3 input data |
G_InitCodeWord | 2 | InitCodeWordGas | Amount of gas paid for each word of initcode for a CREATE ,CREATE2 |
Precompiled contracts gas cost table
Precompiled contracts are special kind of contracts which usually perform complex cryptographic computations and are initiated by other contracts.
Below is the gas cost table for precompiled contracts in Klaytn. Input
is a byte array input of a precompiled contract.
Address | Precompiled contracts | Gas Cost |
---|---|---|
0x01 | ecrecover | 3,000 |
0x02 | sha256hash | numOfWords(input) * 12 + 60 |
0x03 | ripemd160hash | numOfWords(input) * 120 + 600 |
0x04 | dataCopy | numOfWords(input) * 3 + 15 |
0x05 | bigModExp | see the code here |
0x06 | bn256Add | 150 |
0x07 | bn256ScalarMul | 6,000 |
0x08 | bn256Pairing | numOfPairings(input) * 34,000 + 45,000 |
0x09 | blake2f | bigEndian(getRounds(input[0:4])) |
0x0A | kzg | 50,000 |
0x3FD | vmLog | len(input) * 20 + 100 |
0x3FE | feePayer | 300 |
0x3FF | validateSender | numOfSigs(input) * 5,000 |