Skip to content

Commit 484d344

Browse files
Reorganizing "Estimate gas costs" (#1307)
* first-round * new technology page * adding references * linter * Update docs/get-started/how-to/gas-fees.mdx Co-authored-by: Julien Marchand <[email protected]> * Update docs/get-started/how-to/gas-fees.mdx Co-authored-by: Julien Marchand <[email protected]> --------- Co-authored-by: Julien Marchand <[email protected]>
1 parent b1775a2 commit 484d344

File tree

4 files changed

+171
-148
lines changed

4 files changed

+171
-148
lines changed

docs/api/reference/linea-estimategas.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import TabItem from '@theme/TabItem';
1212
Generates and returns an estimate of how much gas is necessary to allow the transaction to complete
1313
and be published on Ethereum. The transaction will not be added to the blockchain.
1414

15-
For more information about estimating gas, and how this API formulates the transaction costs, see
16-
the [Estimate transaction costs](../../get-started/how-to/gas-fees.mdx) topic.
15+
For more information about estimating gas, see our [guide](../../get-started/how-to/gas-fees.mdx), and for more information about how this API formulates the transaction costs, see the [technology section](../../technology/gas.mdx).
1716

1817
The `priorityFeePerGas` returned by this method includes the cost of submitting the transaction to
1918
Ethereum, which can vary based on the size of the calldata.
Lines changed: 11 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,35 @@
11
---
2-
description: How to estimate Linea gas costs
2+
title: Estimate gas costs
3+
description: How to estimate gas costs on Linea
34
sidebar_position: 6
45
image: /img/socialCards/how-to-estimate-linea-gas-costs.jpg
56
---
67

78
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
89

9-
# Estimate transaction costs
10+
`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.
1011

11-
## How gas works on Linea
12-
13-
Linea supports the [Ethereum EIP-1559 gas price model](https://ethereum.org/developers/docs/gas):
14-
```
15-
total fee = units of gas used * (base fee + priority fee)
16-
```
17-
18-
Linea is EVM-equivalent, and gas therefore works extremely similarly to Ethereum. The main difference
19-
is that **the base fee effectively stabilizes at 7 wei.** Linea blocks use up to around 50% of the
20-
maximum block size of 2 billion gas, and the base fee decreases or increases by 12.5% per block
21-
according to network traffic, identically to Ethereum. However, when the base fee reaches 7 wei,
22-
12.5% is less than 1 wei, and the reduction is rounded down to zero; so the fee remains 7 wei.
23-
The base fee is also burned, like on Ethereum.
24-
25-
The gas cost to submit your transaction and include it on Ethereum involves the following fee
26-
components:
27-
28-
- **Layer 2 cost**: The execution fee; the cost of including your transaction on the Linea
29-
sequencer, and calculated using a similar formula to Ethereum (as described above).
30-
- **Layer 1 cost**: The cost of publishing your L2 transaction on Ethereum, which varies
31-
based on the blob fee market.
32-
33-
These two resource costs are abstracted by the rollup and covered by the recommended L2 gas price
34-
and gas used.
35-
36-
> Learn more about gas on Linea on our [support page](https://support.linea.build/getting-started/what-does-gas-pay-for)
37-
and release notes for [Alpha v2](../../release-notes.mdx#alpha-v2) and [Alpha v3](../../release-notes.mdx#alpha-v30).
38-
39-
`linea_estimateGas` is the recommended method for estimating gas on Linea. See our
40-
[reference page](../../api/reference/linea-estimategas.mdx) for more information.
12+
See our [API reference](../../api/reference/linea-estimategas.mdx), or our [technology section](../../technology/gas.mdx) for more information about how gas works on Linea.
4113

4214
Linea also supports:
4315
- [`eth_estimateGas`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_estimategas)
4416
- [`eth_gasPrice`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_gasprice)
4517
- [`eth_feeHistory`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_feehistory).
4618

47-
## Gas pricing
48-
49-
The gas price returned by `linea_estimateGas` is based on the variable data cost of the previous
50-
block with a multiplier applied as a buffer to ensure inclusion.
51-
52-
Each Linea block's `extraData` field is populated with the following gas price values that are used
53-
by `linea_estimateGas` to calculate the cost of a transaction:
54-
- A `FIXED_COST` of 0.03 Gwei, which reflects infrastructure costs;
55-
- `ADJUSTED_VARIABLE_COST`, which is the cost per byte of data submitted to L1, and;
56-
- `ETH_GAS_PRICE`, used to set a more accurate return value for any `eth_gasPrice` calls.
57-
58-
:::note
59-
60-
The `extraData` field is a 32-byte space used to store arbitrary data, such as metadata or
61-
additional information relevant to the block.
62-
63-
On Linea, it's used by the sequencer and Linea Besu nodes running the correct plugins to expose
64-
`linea_estimateGas`.
65-
66-
:::
67-
68-
Variable cost is calculated with the following formula:
69-
70-
```bash
71-
ADJUSTED_VARIABLE_COST = max(base_variable_cost, previous_variable_cost * ( 1 + delta / CHANGE_DENOMINATOR )
19+
:::tip
20+
Linea supports the [Ethereum EIP-1559 gas price model](https://ethereum.org/developers/docs/gas):
7221
```
73-
74-
Where:
75-
- `base_variable_cost` is calculated with the below formula:
76-
```python
77-
variable_cost (4 bytes) = min(max(
78-
(
79-
(
80-
((averageWeightedBaseFee + averageWeightedPriorityFee) *
81-
blob-submission-expected-execution-gas + averageWeightedBlobBaseFee * expected-blob-gas
82-
) / bytes-per-data-submission) * profit-margin
83-
)
84-
, min-bound), max-bound)
85-
```
86-
The `profit-margin` ensures the network is sustainable. `min-bound` and `max-bound` are variable,
87-
and guarantee the gas price stays within a reasonable range.
88-
- `previous_variable_cost` is the value of the last `ADJUSTED_VARIABLE_COST` calculation
89-
- `delta` is a decimal number that fluctuates between `-1.0` and `1.0` and is calculated as:
90-
```bash
91-
delta = (sum(block_calldata_size over BLOCK_COUNT) - calldata_target) / calldata_target
92-
```
93-
Where:
94-
- `BLOCK_COUNT` is a constant at `5`
95-
- `block_calldata_size` is the total calldata size of the target block
96-
- `calldata_target` = `109,000 * BLOCK_COUNT / 2` (given that 109,000 is the maximum calldata
97-
size allowed in each L2 block)
98-
- `CHANGE_DENOMINATOR` is a constant, currently set to `32`, designed to control the rate of change
99-
of `ADJUSTED_VARIABLE_COST`
100-
101-
### Base variable cost
102-
103-
The variable cost formula enables `linea_estimateGas` to price according to the variable costs of
104-
submitting blob data to L1, working out the per-byte cost of that data. The amount of the blob data
105-
in each block stays roughly consistent, though the amount _per transaction_ varies, so the
106-
`linea_estimateGas` API accounts for this, and ensures a gas price is returned for the transaction
107-
that reflects the amount of data it contains. In turn, it ensures that the network is sustainable,
108-
and that the cost to the protocol of L1 data availability is covered.
109-
110-
### Adjusted variable cost
111-
112-
The overarching adjusted variable cost formula enables the gas price for the end user to
113-
automatically adjust to prevent denial-of-service (DoS) attacks where a malicious actor could submit
114-
a large volume of low-compute, high-data transactions at minimal cost. In theory, such a scenario
115-
could prevent good-faith users from submitting transactions because all of the block's space for
116-
data would be taken up by the malicious actor's transactions.
117-
118-
If the calldata space of successive blocks was fully occupied, `ADJUSTED_VARIABLE_COST` would
119-
increase exponentially: `1 + 1 / 32` (i.e. `1 + delta / CHANGE_DENOMINATOR`) would cause the
120-
variable cost to double every 22 blocks (44 seconds) — 10,000x over 10 minutes. The gas cost for the
121-
attacker — who needs to fund many transactions — would quickly snowball to a prohibitively high
122-
number, while remaining acceptable for regular users who only need to submit a handful of
123-
transactions.
124-
125-
This effect also works in the reverse: when calldata space usage falls, the variable cost will
126-
exponentially decrease and eventually revert to `base_variable_cost`, depending on calldata usage.
127-
128-
### Variable cost and `linea_estimateGas`
129-
130-
To determine the priority fee per gas, `linea_estimateGas` takes the previous block's `VARIABLE_COST`
131-
into account:
132-
```python
133-
min-gas-price = previousBlock.extraData.variable_cost
134-
baseFeePerGas = vanillaProtocolBaseFee
135-
priorityFeePerGas = MINIMUM_MARGIN * (min-gas-price * L2_compressed_tx_size_in_bytes / L2_tx_gas_used + extraData.fixed_cost)
22+
total fee = units of gas used * (base fee + priority fee)
13623
```
137-
138-
Where:
139-
- `extraData.variable_cost` is where the previous block's `VARIABLE_COST` is stored
140-
block
141-
- `MINIMUM_MARGIN` varies depending on the stage of the transaction:
142-
- RPC method, i.e. calling `linea_estimateGas`: `1.2`
143-
- In the transaction pool: `0.8`
144-
- At transaction selection stage: `1.0`
145-
146-
:::note
147-
The RPC method and transaction pool values are configurable by RPC providers or those running their
148-
own nodes according to preference; the transaction selection stage value is fixed. For example,
149-
it may be preferable to set a lower margin to facilitate lower gas prices, but this risks
150-
transactions not being included.
24+
and includes support for type 0, type 1 and type 2 transactions.
15125
:::
15226

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

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

16429
## [`linea_estimateGas`](../../api/reference/linea-estimategas.mdx)
16530

166-
`linea_estimateGas` is the recommended method for estimating gas on Linea. It returns `gasLimit`,
167-
`baseFeePerGas`, and `priorityFeePerGas`, and therefore provides a more precise gas estimate than
31+
`linea_estimateGas` returns `gasLimit`,
32+
`baseFeePerGas`, and `priorityFeePerGas`, and therefore provides a more precise recommended gas price than
16833
the alternatives.
16934

17035
It can also help prevent transactions from being rejected due to exceeding [module limits](../../technology/prover/prover-limits.mdx).
@@ -194,4 +59,4 @@ curl https://linea-mainnet.infura.io/v3/YOUR-API-KEY \
19459
}
19560
```
19661

197-
See the [reference page](../../api/reference/linea-estimategas.mdx) for full usage.
62+
See the [reference page](../../api/reference/linea-estimategas.mdx) for full usage.

docs/technology/gas.mdx

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: Gas
3+
description: How gas works on Linea
4+
image: /img/socialCards/gas.jpg
5+
---
6+
7+
`linea_estimateGas` is the recommended method for estimating gas on Linea. See our [API reference](/api/reference/linea-estimategas.mdx) and [guide](/get-started/how-to/gas-fees.mdx) for more information.
8+
9+
Linea also supports:
10+
- [`eth_estimateGas`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_estimategas)
11+
- [`eth_gasPrice`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_gasprice)
12+
- [`eth_feeHistory`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_feehistory).
13+
14+
## How gas works on Linea
15+
16+
Linea supports the [Ethereum EIP-1559 gas price model](https://ethereum.org/developers/docs/gas):
17+
```
18+
total fee = units of gas used * (base fee + priority fee)
19+
```
20+
and includes support for type 0, type 1 and type 2 transactions.
21+
22+
Linea is EVM-equivalent, and gas therefore works extremely similarly to Ethereum. The main difference
23+
is that **the base fee effectively stabilizes at 7 wei.** Linea blocks use up to around 50% of the
24+
maximum block size of 2 billion gas, and the base fee decreases or increases by 12.5% per block
25+
according to network traffic, identically to Ethereum. However, when the base fee reaches 7 wei,
26+
12.5% is less than 1 wei, and the reduction is rounded down to zero; so the fee remains 7 wei.
27+
The base fee is also burned, like on Ethereum.
28+
29+
The gas cost to submit your transaction and include it on Ethereum involves the following fee
30+
components:
31+
32+
- **Layer 2 cost**: The execution fee; the cost of including your transaction on the Linea
33+
sequencer, and calculated using a similar formula to Ethereum (as described above).
34+
- **Layer 1 cost**: The cost of publishing your L2 transaction on Ethereum, which varies
35+
based on the blob fee market.
36+
37+
These two resource costs are abstracted by the rollup and covered by the recommended L2 gas price
38+
and gas used.
39+
40+
> Learn more about gas on Linea on our [support page](https://support.linea.build/getting-started/what-does-gas-pay-for)
41+
and release notes for [Alpha v2](/release-notes.mdx#alpha-v2) and [Alpha v3](/release-notes.mdx#alpha-v30).
42+
43+
## How `linea_estimateGas` calculates the gas price
44+
45+
The gas price returned by `linea_estimateGas` is based on the variable data cost of the previous
46+
block with a multiplier applied as a buffer to ensure inclusion.
47+
48+
Each Linea block's `extraData` field is populated with the following gas price values that are used
49+
by `linea_estimateGas` to calculate the cost of a transaction:
50+
- A `FIXED_COST` of 0.03 Gwei, which reflects infrastructure costs;
51+
- `ADJUSTED_VARIABLE_COST`, which is the cost per byte of data submitted to L1, and;
52+
- `ETH_GAS_PRICE`, used to set a more accurate return value for any `eth_gasPrice` calls.
53+
54+
:::note
55+
56+
The `extraData` field is a 32-byte space used to store arbitrary data, such as metadata or
57+
additional information relevant to the block.
58+
59+
On Linea, it's used by the sequencer and Linea Besu nodes running the correct plugins to expose
60+
`linea_estimateGas`.
61+
62+
:::
63+
64+
Variable cost is calculated with the following formula:
65+
66+
```bash
67+
ADJUSTED_VARIABLE_COST = max(base_variable_cost, previous_variable_cost * ( 1 + delta / CHANGE_DENOMINATOR )
68+
```
69+
70+
Where:
71+
- `base_variable_cost` is calculated with the below formula:
72+
```python
73+
variable_cost (4 bytes) = min(max(
74+
(
75+
(
76+
((averageWeightedBaseFee + averageWeightedPriorityFee) *
77+
blob-submission-expected-execution-gas + averageWeightedBlobBaseFee * expected-blob-gas
78+
) / bytes-per-data-submission) * profit-margin
79+
)
80+
, min-bound), max-bound)
81+
```
82+
The `profit-margin` ensures the network is sustainable. `min-bound` and `max-bound` are variable,
83+
and guarantee the gas price stays within a reasonable range.
84+
- `previous_variable_cost` is the value of the last `ADJUSTED_VARIABLE_COST` calculation
85+
- `delta` is a decimal number that fluctuates between `-1.0` and `1.0` and is calculated as:
86+
```bash
87+
delta = (sum(block_calldata_size over BLOCK_COUNT) - calldata_target) / calldata_target
88+
```
89+
Where:
90+
- `BLOCK_COUNT` is a constant at `5`
91+
- `block_calldata_size` is the total calldata size of the target block
92+
- `calldata_target` = `109,000 * BLOCK_COUNT / 2` (given that 109,000 is the maximum calldata
93+
size allowed in each L2 block)
94+
- `CHANGE_DENOMINATOR` is a constant, currently set to `32`, designed to control the rate of change
95+
of `ADJUSTED_VARIABLE_COST`
96+
97+
### Base variable cost
98+
99+
The variable cost formula enables `linea_estimateGas` to price according to the variable costs of
100+
submitting blob data to L1, working out the per-byte cost of that data. The amount of the blob data
101+
in each block stays roughly consistent, though the amount _per transaction_ varies, so the
102+
`linea_estimateGas` API accounts for this, and ensures a gas price is returned for the transaction
103+
that reflects the amount of data it contains. In turn, it ensures that the network is sustainable,
104+
and that the cost to the protocol of L1 data availability is covered.
105+
106+
### Adjusted variable cost
107+
108+
The overarching adjusted variable cost formula enables the gas price for the end user to
109+
automatically adjust to prevent denial-of-service (DoS) attacks where a malicious actor could submit
110+
a large volume of low-compute, high-data transactions at minimal cost. In theory, such a scenario
111+
could prevent good-faith users from submitting transactions because all of the block's space for
112+
data would be taken up by the malicious actor's transactions.
113+
114+
If the calldata space of successive blocks was fully occupied, `ADJUSTED_VARIABLE_COST` would
115+
increase exponentially: `1 + 1 / 32` (i.e. `1 + delta / CHANGE_DENOMINATOR`) would cause the
116+
variable cost to double every 22 blocks (44 seconds) — 10,000x over 10 minutes. The gas cost for the
117+
attacker — who needs to fund many transactions — would quickly snowball to a prohibitively high
118+
number, while remaining acceptable for regular users who only need to submit a handful of
119+
transactions.
120+
121+
This effect also works in the reverse: when calldata space usage falls, the variable cost will
122+
exponentially decrease and eventually revert to `base_variable_cost`, depending on calldata usage.
123+
124+
### Variable cost and `linea_estimateGas`
125+
126+
To determine the priority fee per gas, `linea_estimateGas` takes the previous block's `VARIABLE_COST`
127+
into account:
128+
```python
129+
min-gas-price = previousBlock.extraData.variable_cost
130+
baseFeePerGas = vanillaProtocolBaseFee
131+
priorityFeePerGas = MINIMUM_MARGIN * (min-gas-price * L2_compressed_tx_size_in_bytes / L2_tx_gas_used + extraData.fixed_cost)
132+
```
133+
134+
Where:
135+
- `extraData.variable_cost` is where the previous block's `VARIABLE_COST` is stored
136+
block
137+
- `MINIMUM_MARGIN` varies depending on the stage of the transaction:
138+
- RPC method, i.e. calling `linea_estimateGas`: `1.2`
139+
- In the transaction pool: `0.8`
140+
- At transaction selection stage: `1.0`
141+
142+
:::note
143+
The RPC method and transaction pool values are configurable by RPC providers or those running their
144+
own nodes according to preference; the transaction selection stage value is fixed. For example,
145+
it may be preferable to set a lower margin to facilitate lower gas prices, but this risks
146+
transactions not being included.
147+
:::
148+
149+
`linea_estimateGas` simulates the transaction ordering logic that the sequencer uses when building
150+
blocks, and then obtains a gas price that will ensure that the priority fee is high enough for
151+
inclusion. The sequencer's transaction ordering policy includes transactions in block in order of
152+
highest priority fee, a system known as a priority gas auction. It also checks that the priority
153+
fees offered by each transaction are high enough to support network sustainability, and cover L1
154+
data costs.
155+
156+
In some cases, transactions with lower priority fees are included ahead of others with higher
157+
priority fees. This is because the nonce order of transactions submitted from the same account
158+
takes precedence.

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ const sidebars = {
591591
"technology/burn",
592592
"technology/repos",
593593
"technology/transaction-lifecycle",
594+
"technology/gas",
594595
"technology/network-data",
595596
"technology/canonical-token-bridge",
596597
{

0 commit comments

Comments
 (0)