EvmGas

object EvmGas

Gas math for EVM data-carrying value transfers used to anchor payloads.

An anchor transaction is a plain value transfer (to an externally-owned account) carrying the payload as calldata, so its gas cost is the intrinsic transaction cost: 21,000 base gas plus per-byte calldata gas (EIP-2028: 16 gas per non-zero byte, 4 gas per zero byte) - or, since EIP-7623 (Pectra), the calldata floor 21000 + 10 * tokens with tokens = zeroBytes + 4 * nonZeroBytes (40 gas per non-zero byte, 10 per zero byte) when that is larger. A transaction whose gas limit is below max(intrinsic, floor) is invalid, so requiredGas is the minimum limit and what the limit helpers below are built on.

EVM anchor plugins use this for both fee estimation and the actual transaction gas limit instead of a blanket multi-million default, which would grossly over-reserve gas and over-report estimated fees.

Properties

Link copied to clipboard
const val DEFAULT_MARGIN_PERCENT: Int = 10

Default safety margin applied on top of the intrinsic gas, in percent.

Link copied to clipboard
const val FLOOR_TOKEN_GAS: Long = 10

EIP-7623 floor cost per calldata token.

Link copied to clipboard
const val NONZERO_BYTE_GAS: Long = 16

Calldata cost per non-zero byte (EIP-2028).

Link copied to clipboard

Tokens per non-zero calldata byte (EIP-7623); a zero byte is one token.

Link copied to clipboard
const val TX_BASE_GAS: Long

Base cost of any EVM transaction.

Link copied to clipboard
const val ZERO_BYTE_GAS: Long = 4

Calldata cost per zero byte.

Functions

Link copied to clipboard

EIP-7623 floor: 21000 + 10 * (zeroBytes + 4 * nonZeroBytes).

Link copied to clipboard

Exact intrinsic gas for a value transfer carrying data as calldata: 21000 + 16 * nonZeroBytes + 4 * zeroBytes.

Link copied to clipboard

Worst-case intrinsic gas for sizeBytes of calldata (every byte assumed non-zero).

Link copied to clipboard

The minimum valid gas limit for a value transfer carrying data: the larger of intrinsicGas and calldataFloorGas.

Link copied to clipboard

requiredGas for sizeBytes of calldata, every byte assumed non-zero (the worst case).

Link copied to clipboard
fun txGasLimit(data: ByteArray, marginPercent: Int = DEFAULT_MARGIN_PERCENT): BigInteger

requiredGas (intrinsic or EIP-7623 floor, whichever is larger) plus a safety margin (default +10%), suitable as the transaction gas limit for an anchor transaction.

Link copied to clipboard
fun txGasLimitForSize(sizeBytes: Long, marginPercent: Int = DEFAULT_MARGIN_PERCENT): BigInteger

Worst-case gas limit (requiredGasForSize) for sizeBytes of calldata plus a safety margin (default +10%).

Link copied to clipboard
fun withMargin(gas: BigInteger, marginPercent: Int = DEFAULT_MARGIN_PERCENT): BigInteger

Applies a percentage safety margin to gas, rounding up.