Estimate Fees
Estimate Fees in Mantle v2 Tectonic
Concerning the selection of the gas fee token on layer 2 (L2), Mantle v2 Tectonic departs from the conventional ETH solution adopted by most rollups, and employs MNT instead. This introduces a variable called tokenRatio
for dynamic adjustment of gas fees. The overall gas fee calculation is as follows:
Let's break down the variables involved. Both scalar
and overhead
are retrieved from the BVM_GasPriceOracle
contract, while L1gasPrice
can be obtained from layer 1 (L1). tokenRatio
is a variable updated through an external Oracle contract, which you can fetch from the BVM_GasPriceOracle
contract. Therefore, obtaining L2gasPrice
and rollupDataGas
allows the calculation of a suitable gasLimit
.
Why is a suitable gasLimit necessary?
Determining an appropriate gasLimit
is crucial. If the gasLimit
is set too low, the transaction may fail to execute. If set too high, the computed upfront transaction fee might exceed the user's balance, leading to insufficient funds. Additionally, setting a high gasLimit
may discourage validators from including the transaction in a block, as validators tend to prefer including more transactions with lower gasLimit
within the fixed block gasLimit
. Therefore, estimating the fee is a pivotal step.
How to Obtain Parameters for BVM_GasPriceOracle
overhead
cast call --rpc-url https://rpc.mantle.xyz 0x420000000000000000000000000000000000000F 'overhead()(uint256)'
scalar
cast call --rpc-url https://rpc.mantle.xyz 0x420000000000000000000000000000000000000F 'scalar()(uint256)'
You need to divide the value you get by 1,000,000
to get the true scalar value. For example, if the value you get is 10,000
, you need to divide it by 1,000,000
to get 0.01
.
tokenRatio
cast call --rpc-url https://rpc.mantle.xyz 0x420000000000000000000000000000000000000F 'tokenRatio()(uint256)'
L1gasPrice
Also known as L1BaseFee
.
cast call --rpc-url https://rpc.mantle.xyz 0x420000000000000000000000000000000000000F 'l1BaseFee()(uint256)'
How to Obtain L2 gasPrice
Different transaction types, such as Legacy and EIP-1559 transactions, require distinct handling. For detailed information, please refer to this.
How to Obtain rollupDataGas
It's essential to note that rollupDataGas
is not constructed during the estimateGas
process in the OP Stack codebase. Thus, an interface is needed to estimate the cost. Mantle addresses this by constructing rollupDataGas
and introducing a constant to cover V
, R
, S
signatures, and other data, ensuring that the estimateGas
's gasLimit
covers L1 costs.
We provide interfaces that return L1GasUsed
data directly, i.e., the sum of (rollupDataGas + overhead)
# _data is rlp-encoded data of the signed tx
cast call --rpc-url https://rpc.mantle.xyz 0x420000000000000000000000000000000000000F 'getL1GasUsed(_data)(uint256)'
If you want to learn more, please refer to this.
Ensuring transaction success
estimateGas
provides only an estimate. To prevent transaction failure due to insufficient gas fees, Mantle Network internally magnifies the estimate by a factor, ensuring the correct execution of transactions.
Why We Need to Use estimateGas
The cost of fees on L1 is difficult to predict accurately due to the influence of L1 gasPrice, therefore, in Mantle v1, the estimateGas
function only returns an estimate of the L2 gas fee. However, in Mantle v2 Tectonic, we have addressed this issue by providing an optimized solution. The estimateGas
interface now returns an estimate of the total gas fee, including both L1 and L2 costs. Consequently, when constructing transactions, adjustments need to be made to accommodate this optimization. We encourage all transactions to first call estimateGas
to obtain an estimate of the gas fee before construction.
Example
Here is an example illustrating the difference in gasLimit
settings for transaction construction between Mantle v1 and Mantle v2 Tectonic.
In Mantle v1, the gasUsed
on a typical native token transfer on L2 is a fixed value (21,000), so developers might opt to skip calling estimateGas
and set it to a fixed value.
However, in Mantle v2 Tectonic, the gasLimit
that developers need to set when constructing transactions will include both L1 and L2 components, so without calling estimateGas
and setting it to a fixed value may result in transaction failures.
To avoid transaction failures, we recommend calling estimateGas
when constructing transactions to obtain an estimate of the gas fee.
::: info
If you want to do estimateGas
with the Mantle SDK, here's a demo.
:::