|
| 1 | +Users can sign transactions with gas prices higher than the suggested gas price for prioritized sequencing. That is, |
| 2 | + |
| 3 | +$$ |
| 4 | +\texttt{SignedGasPrice} > \texttt{suggestedGasPrice} |
| 5 | +$$ |
| 6 | + |
| 7 | +The priority ratio is defined as: |
| 8 | + |
| 9 | +$$ |
| 10 | +\texttt{PriorityRatio} = \frac{\texttt{SignedGasPrice}}{\texttt{suggestedGasPrice}} - 1 |
| 11 | +$$ |
| 12 | + |
| 13 | +Any transaction with $\mathtt{SignedGasPrice \leq GasPriceSuggested}$, implies that the user has chosen not to have the transaction prioritized. |
| 14 | + |
| 15 | +## Computing effective gas price |
| 16 | + |
| 17 | +The computation of the effective gas price (or EGP) involves both the break even gas price and the priority ratio: |
| 18 | + |
| 19 | +$$ |
| 20 | +\texttt{EffectiveGasPrice} = \texttt{BreakEvenGasPrice} \cdot \big(1 + \texttt{PriorityRatio}\big) |
| 21 | +$$ |
| 22 | + |
| 23 | +Among the transactions stored in the pool database, the transactions with higher $\texttt{effectiveGasPrice}$ are prioritized at the time of sequencing due to the added $\texttt{PriorityRatio}$. |
| 24 | + |
| 25 | +### Gas consumption deviations |
| 26 | + |
| 27 | +Since the actual gas consumed can deviate from the estimated gas consumption, we denote the estimated $\texttt{EGP}$ and the 'new' $\texttt{EGP}$ as $\texttt{EEGP}$ and $\texttt{NEGP}$, respectively. |
| 28 | + |
| 29 | +The extent of the deviation can be computed as a percentage: |
| 30 | + |
| 31 | +$$ |
| 32 | +\frac{|{\texttt{NEGP} - \texttt{EEGP}}|}{\texttt{EEGP}} \cdot 100 |
| 33 | +$$ |
| 34 | + |
| 35 | +The deviation percentage is compared to a parameter called $\texttt{FinalDeviationParameter}$, which is set to $10$. |
| 36 | + |
| 37 | +This presents 2 scenarios and their corresponding consequences: |
| 38 | + |
| 39 | +1. If the percentage deviation is lower than the final deviation parameter, |
| 40 | + |
| 41 | + $$ |
| 42 | + \frac{|\texttt{NEGP} − \texttt{EEGP}|}{\texttt{EEGP}} \cdot 100 < \texttt{FinalDeviationParameter} = 10, |
| 43 | + $$ |
| 44 | + |
| 45 | + it indicates that there is minimal distinction between charging the user with $\texttt{NEGP}$ compared to $\texttt{EEGP}$. |
| 46 | + |
| 47 | + Despite potential losses to the network, the user gets charged the $\texttt{EEGP}$ amount as the gas price. |
| 48 | + |
| 49 | +2. On the contrary, if the percentage deviation equals or exceeds the deviation parameter, |
| 50 | + |
| 51 | + $$ |
| 52 | + \frac{|\texttt{NEGP} − \texttt{EEGP}|}{\texttt{EEGP}} \cdot 100 ≥ \texttt{FinalDeviationParameter} = 10, |
| 53 | + $$ |
| 54 | + |
| 55 | + the difference between executions can be so big it warrants adjustment of the gas price to be $\texttt{NEGP}$ to mitigate potential losses to the network. |
| 56 | + |
| 57 | +### Effective percentage |
| 58 | + |
| 59 | +The last parameter called the $\texttt{EffectivePercentage}$ is used to measure the unused portion of the user's signed gas price. |
| 60 | + |
| 61 | +In order to calculate the $\texttt{EffectivePercentage}$, one option is to consider pricing resources based on the number of consumed counters within the Polygon zkEVM's proving system. |
| 62 | + |
| 63 | +However, understanding this metric can be challenging for users because stating the efficiency through counters is a bit complicated. |
| 64 | + |
| 65 | +In favor of better UX, a formula involving gas is applied as it is more user-friendly. |
| 66 | + |
| 67 | +The primary objective is to compute $\texttt{EffectivePercentage}$ exclusively using gas, while allowing users to prioritize their transactions through the use of gas price, without the need for complex considerations such as used ROM counters. |
| 68 | + |
| 69 | +The effective percentage is computed as follows: |
| 70 | + |
| 71 | +$$ |
| 72 | +\texttt{EffectivePercentage} = \frac{ \texttt{GasPriceFinal}}{ \texttt{SignedGasPrice}} |
| 73 | +$$ |
| 74 | + |
| 75 | +where $\texttt{GasPriceFinal}$ is the gas price charged at the end of the entire processing by the sequencer. |
| 76 | + |
| 77 | +Note that the amount of wei that the user is charged for processing their transactions can be adjusted by modifying $\texttt{GasPriceFinal}$. |
| 78 | + |
| 79 | +This $\texttt{EffectivePercentage}$ is provided by the sequencer as a single byte: |
| 80 | + |
| 81 | +$$ |
| 82 | +\texttt{EffectivePercentageByte} \in \{ 0, 1, . . . , 255 \} |
| 83 | +$$ |
| 84 | + |
| 85 | +which is computed from the $\texttt{EffectivePercentage}$ as follows: |
| 86 | + |
| 87 | +$$ |
| 88 | +\texttt{EffectivePercentageByte} = \lfloor \texttt{EffectivePercentage} · 256 \rfloor − 1 |
| 89 | +$$ |
| 90 | + |
| 91 | +Since having $\texttt{EffectivePercentage}$ implies having $\texttt{EffectivePercentageByte}$ and vice versa, the two terms are used interchangeably. |
| 92 | + |
| 93 | +So, $\texttt{EffectivePercentageByte}$ is often referred to as $\texttt{EffectivePercentage}$. |
| 94 | + |
| 95 | +**Example (Effective percentage)** |
| 96 | + |
| 97 | +Setting an $\texttt{EffectivePercentageByte}$ of $255\ (= \texttt{0xFF})$ means the $\texttt{EffectivePercentage} = 1$. |
| 98 | + |
| 99 | +In which case the user would pay the gas price they signed with, when sending the transaction, in total. That is: |
| 100 | + |
| 101 | +$$ |
| 102 | +\texttt{GasPriceFinal} = \texttt{SignedGasPrice} |
| 103 | +$$ |
| 104 | + |
| 105 | +In contrast, setting $\texttt{EffectivePercentageByte}$ to $127$ means: |
| 106 | + |
| 107 | +$$ |
| 108 | +\texttt{EffectivePercentage} = 0.5 |
| 109 | +$$ |
| 110 | + |
| 111 | +Thus, only half of the gas price the user signed with gets charged as the transaction cost: |
| 112 | + |
| 113 | +$$ |
| 114 | +\texttt{GasPriceFinal} = \frac{\texttt{SignedGasPrice}}{2} |
| 115 | +$$ |
| 116 | + |
| 117 | +The transaction execution incurs only half of the signed gas price. |
| 118 | + |
| 119 | +## Concluding remarks |
| 120 | + |
| 121 | +The effective gas price scheme, as outlined above, although steeped in details, takes all necessary factors and eventualities into consideration. |
| 122 | + |
| 123 | +Ultimately, the scheme is accurate and fair to both the users and the zkEVM network. |
| 124 | + |
| 125 | +Check out this [repo](https://github.com/0xPolygonHermez/zkevm-rom/issues/316) for a detailed example of how the effective gas price is calculated. |
| 126 | + |
| 127 | +A more elaborate and expanded documentation of the EGP scheme can be found in the specifications section [here](../../spec/user-fees/index.md). |
0 commit comments