The transaction fee for the current Klaytn virtual machine (KLVM) is calculated as follows:
(Transaction Fee) := (Gas Used) * (Base Fee)
Gas Usedis computed by KLVM based on the gas cost of the opcode and the intrinsic gas cost.
Base Feeis the actual gas price used for the transaction. It has the same meaning as the
Effective Gas Price.
This calculated transaction fee is subtracted from the sender's or fee payer's account balance, depending on the transaction.
Every action that changes the state of the blockchain requires gas. When a node processes a user's transaction, such as sending KLAY, using KIP-7 tokens, or executing a contract, the user has to pay for the computation and storage usage. The payment amount is decided by the amount of
Gasis a measuring unit representing how much calculation is needed to process the user's transaction.
Since the Klaytn v1.9.0 hard fork, a dynamic gas fee mechanism has replaced the existing fixed fee policy. Dynamic gas fee policy provides a stable service to users by preventing network abuse and storage overuse. The gas fee changes according to the network situation. Seven parameters affect the
base fee(gas fee):
- 1.PREVIOUS_BASE_FEE: Base fee of the previous block
- 2.GAS_USED_FOR_THE_PREVIOUS_BLOCK: Gas used to process all transactions of the previous block
- 3.GAS_TARGET: The gas amount that determines the increase or decrease of the base fee (30 million at the moment)
- 4.MAX_BLOCK_GAS_USED_FOR_BASE_FEE: Implicit block gas limit to enforce the max basefee change rate (60 million at the moment)
- 5.BASE_FEE_DELTA_REDUCING_DENOMINATOR: The value to set the maximum base fee change to 5% per block (20 at the moment, can be changed later by governance)
- 6.UPPER_BOUND_BASE_FEE: The maximum value for the base fee (750 ston at the moment, can be changed later by governance)
- 7.LOWER_BOUND_BASE_FEE: The minimum value for the base fee (25 ston at the moment, can be changed later by governance)
The basic idea of this algorithm is that the
base feewould go up if the gas used exceeds the base gas and vice versa. It is closely related to the number of transactions in the network and the gas used in the process. There is an upper and lower limit for the
base feeto prevent the fee from increasing or decreasing indefinitely. There is also a cap for the gas and an adjustment value for the fluctuation to prevent abrupt changes in the
base fee. The values can be changed by governance.
(BASE_FEE_CHANGE_RATE) = (GAS_USED_FOR_THE_PREVIOUS_BLOCK - GAS_TARGET)
(ADJUSTED_BASE_FEE_CHANGE_RATE) = (BASE_FEE_CHANGE_RATE) / (GAS_TARGET) / (BASE_FEE_DELTA_REDUCING_DENOMINATOR)
(BASE_FEE_CHANGE_RANGE) = (PREVIOUS_BASE_FEE) * (ADJUSTED_BASE_FEE_CHANGE_RATE)
(BASE_FEE) = (PREVIOUS_BASE_FEE) + (BASE_FEE_CHANGE_RANGE)
base feeis calculated for every block; there could be changes every second. Transactions from a single block use the same
base feeto calculate transaction fees. Only transactions with a gas price higher than the block
base feecan be included in the block. Half of the transaction fee for each block is burned (BURN_RATIO = 0.5, cannot be changed by governance).
NOTE: An important feature that sets Klaytn apart from Ethereum's EIP-1559 is that it does not have tips. Klaytn follows the First Come, First Served(FCFS) principle for its transactions.
Klaytn currently does not provide a way to replace a transaction using the unit price but may support different methods for the transaction replacement in the future. Note that in Ethereum, a transaction with a given nonce can be replaced by a new one with a higher gas price.
Basically, Klaytn is keeping compatibility with Ethereum. So Klaytn's gas table is pretty similar with that of Ethereum. But there are some features unique to Klaytn that require several new constants.
Precompiled contracts are special kind of contracts which usually perform complex cryptographic computations and are initiated by other contracts.
Total gas of those items which has XXXBaseGas and XXXPerWordGas (e.g. Sha256BaseGas, Sha256PerWordGas) are calculated as
TotalGas = XXXBaseGas + (number of words * XXXPerWordGas)
ValidateSenderGas have to be paid per signature basis.
TotalGas = number of signatures * ValidateSenderGas
Blake2f gas cost is calculated based on the below formula.
inputis the input of the blake2f call.
Gas = uint64(binary.BigEndian.Uint32(input[0:4]))
Gas for payload data is calculated as below
GasPayload = number_of_bytes * TxDataGas
KeyValidationGas is defined as below based on the key type,
KeyCreationGas is defined as below based on the key type,