Estimating Transaction Fees
A tutorial showing how to accurately estimate the total transaction fees for a transaction on Mantle Network
Mantle v2 Tectonic has been released, please move to the new documentation!
Description
In this tutorial, we'll be looking at an example that shows how to use the Mantle SDK to estimate the total gas fees for a transaction on Mantle Network, which is a combination of the layer-2 (L2) execution fee, and the layer-1 (L1) rollup fee.
In SDK version @mantleio/sdk @0.2.2
, we're introducing the parameters required to make accurate estimations of the L1 rollup fee that will be incurred for sending a transaction at any given time. The calculation to obtain L1 rollup fee is as follows:
There are 4 interfaces available as part of the SDK, where:
L1GasPrice * Ratio
can be fetched by calling thegetL1GasPrice
method. It invokes thel1basefee()
function of theBVM_GasPriceOracle
contract.overhead
,decimals
, andscalar
values can be fetched from theBVM_GasPriceOracle
contract by calling the respective SDK methods with the same names.
The decimals
value in the contract is mainly used to control the precision of scalar
, which helps ensure accurate L1 rollup fee calculation.
The L2 execution fee, on the other hand, is calculated as follows:
L2 gas price is fetched by calling the
getGasPrice
methodThe gas used to execute a transaction is fetched by calling the
estimateGas
method
The total gas fee is thus calculated as:
SDK Installation
Before proceeding, please make sure you have npm
installed and configured in your local environment.
Start by installing Mantle SDK and the necessary dependencies in your environment by running the following command:
If you already have the SDK installed, please make sure you update to the latest version by running the command npm update @mantleio/sdk
since the L1 rollup fee parameters are only available for version 0.2.2, or higher.
Script
You can directly use the following JS code to estimate the total gas fees.
Result
totalEstimatedGasFee
contains the final result of the calculation, as discussed in the Description section.
Last updated