Skip to main content

Estimate gas costs

tip

See our predictable pricing feature for a high-level overview about how gas works on Linea.

linea_estimateGas is the recommended method for estimating gas price and gas limit on Linea. Unlike other L2s, Linea does not assume that all transactions will have the same gas price. Instead, a minimum gas price is applied to all transactions, and the total gas price is partly calculated based on the amount of data in a given transaction. linea_estimateGas takes this into account, and returns a more accurate gas price recommendation than the alternatives.

Linea also supports:

info

Linea supports the Ethereum EIP-1559 gas price model:

total fee = units of gas used * (base fee + priority fee)

and includes support for type 0, type 1 and type 2 transactions.

linea_estimateGas returns gasLimit, baseFeePerGas, and priorityFeePerGas, and therefore provides a more precise recommended gas price than the alternatives.

It can also help prevent transactions from being rejected due to exceeding module limits.

Example​

Request​

curl https://linea-mainnet.infura.io/v3/YOUR-API-KEY \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0","method": "linea_estimateGas","params": [{"from": "0x971e727e956690b9957be6d51Ec16E73AcAC83A7","gas":"0x21000"}],"id": 53}'

Response​

{
"jsonrpc": "2.0",
"id": 53,
"result": {
"baseFeePerGas": "0x7",
"gasLimit": "0xcf08",
"priorityFeePerGas": "0x43a82a4"
}
}

Understand the gas price calculation​

The gas price returned by linea_estimateGas is based on the variable data cost of the previous block with a multiplier applied as a buffer to ensure inclusion.

Each Linea block's extraData field is populated with the following gas price values that are used by linea_estimateGas to calculate the cost of a transaction:

  • A FIXED_COST of 0.03 Gwei, which reflects infrastructure costs
  • ADJUSTED_VARIABLE_COST, which is the cost per byte of data submitted to L1
  • ETH_GAS_PRICE, used to set a more accurate return value for any eth_gasPrice calls
note

The extraData field is a 32-byte space used to store arbitrary data, such as metadata or additional information relevant to the block.

On Linea, it's used by the sequencer and Linea Besu nodes running the correct plugins to expose linea_estimateGas.

Variable cost is calculated with the following formula:

ADJUSTED_VARIABLE_COST = max(base_variable_cost, previous_variable_cost * ( 1 + delta / CHANGE_DENOMINATOR )

Where:

  • base_variable_cost is calculated with the below formula:
    variable_cost (4 bytes) = min(max(
    (
    (
    ((averageWeightedBaseFee + averageWeightedPriorityFee) *
    blob-submission-expected-execution-gas + averageWeightedBlobBaseFee * expected-blob-gas
    ) / bytes-per-data-submission) * profit-margin
    )
    , min-bound), max-bound)
    The profit-margin ensures the network is sustainable. min-bound and max-bound are variable, and guarantee the gas price stays within a reasonable range.
  • previous_variable_cost is the value of the last ADJUSTED_VARIABLE_COST calculation
  • delta is a decimal number that fluctuates between -1.0 and 1.0 and is calculated as:
    delta = (sum(block_calldata_size over BLOCK_COUNT) - calldata_target) / calldata_target
    Where:
    • BLOCK_COUNT is a constant at 5
    • block_calldata_size is the total calldata size of the target block
    • calldata_target = 109,000 * BLOCK_COUNT / 2 (given that 109,000 is the maximum calldata size allowed in each L2 block)
  • CHANGE_DENOMINATOR is a constant, currently set to 32, designed to control the rate of change of ADJUSTED_VARIABLE_COST

Base variable cost​

The variable cost formula enables linea_estimateGas to price according to the variable costs of submitting blob data to L1, working out the per-byte cost of that data. The amount of the blob data in each block stays roughly consistent, though the amount per transaction varies, so the linea_estimateGas API accounts for this, and ensures a gas price is returned for the transaction that reflects the amount of data it contains. In turn, it ensures that the network is sustainable, and that the cost to the protocol of L1 data availability is covered.

Adjusted variable cost​

The overarching adjusted variable cost formula enables the gas price for the end user to automatically adjust to prevent denial-of-service (DoS) attacks where a malicious actor could submit a large volume of low-compute, high-data transactions at minimal cost. In theory, such a scenario could prevent good-faith users from submitting transactions because all of the block's space for data would be taken up by the malicious actor's transactions.

If the calldata space of successive blocks was fully occupied, ADJUSTED_VARIABLE_COST would increase exponentially: 1 + 1 / 32 (i.e. 1 + delta / CHANGE_DENOMINATOR) would cause the variable cost to double every 22 blocks (44 seconds)—10,000x over 10 minutes. The gas cost for the attacker—who needs to fund many transactions—would quickly snowball to a prohibitively high number, while remaining acceptable for regular users who only need to submit a handful of transactions.

This effect also works in the reverse: when calldata space usage falls, the variable cost will exponentially decrease and eventually revert to base_variable_cost, depending on calldata usage.

Variable cost and linea_estimateGas​

To determine the priority fee per gas, linea_estimateGas takes the previous block's VARIABLE_COST into account:

min-gas-price = previousBlock.extraData.variable_cost
baseFeePerGas = vanillaProtocolBaseFee
priorityFeePerGas = MINIMUM_MARGIN * (min-gas-price * L2_compressed_tx_size_in_bytes / L2_tx_gas_used + extraData.fixed_cost)

Where:

  • extraData.variable_cost is where the previous block's VARIABLE_COST is stored
  • MINIMUM_MARGIN varies depending on the stage of the transaction:
    • RPC method, i.e. calling linea_estimateGas: 1.2
    • In the transaction pool: 0.8
    • At transaction selection stage: 1.0
note

The RPC method and transaction pool values are configurable by RPC providers or those running their own nodes according to preference; the transaction selection stage value is fixed. For example, it may be preferable to set a lower margin to facilitate lower gas prices, but this risks transactions not being included.

linea_estimateGas simulates the transaction ordering logic that the sequencer uses when building blocks, and then obtains a gas price that will ensure that the priority fee is high enough for inclusion. The sequencer's transaction ordering policy includes transactions in block in order of highest priority fee, a system known as a priority gas auction. It also checks that the priority fees offered by each transaction are high enough to support network sustainability, and cover L1 data costs.

In some cases, transactions with lower priority fees are included ahead of others with higher priority fees. This is because the nonce order of transactions submitted from the same account takes precedence.

Next steps​

See the API reference page for full usage.