|
1 | 1 | --- |
2 | | -description: How to estimate Linea gas costs |
| 2 | +title: Estimate gas costs |
| 3 | +description: How to estimate gas costs on Linea |
3 | 4 | sidebar_position: 6 |
4 | 5 | image: /img/socialCards/how-to-estimate-linea-gas-costs.jpg |
5 | 6 | --- |
6 | 7 |
|
7 | 8 | import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; |
8 | 9 |
|
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. |
10 | 11 |
|
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. |
41 | 13 |
|
42 | 14 | Linea also supports: |
43 | 15 | - [`eth_estimateGas`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_estimategas) |
44 | 16 | - [`eth_gasPrice`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_gasprice) |
45 | 17 | - [`eth_feeHistory`](https://docs.infura.io/api/networks/linea/json-rpc-methods/eth_feehistory). |
46 | 18 |
|
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): |
72 | 21 | ``` |
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) |
136 | 23 | ``` |
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. |
151 | 25 | ::: |
152 | 26 |
|
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. |
159 | 27 |
|
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. |
163 | 28 |
|
164 | 29 | ## [`linea_estimateGas`](../../api/reference/linea-estimategas.mdx) |
165 | 30 |
|
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 |
168 | 33 | the alternatives. |
169 | 34 |
|
170 | 35 | 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 \ |
194 | 59 | } |
195 | 60 | ``` |
196 | 61 |
|
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. |
0 commit comments