Links
Comment on page

Estimating Transaction Fees

A tutorial showing how to accurately estimate the total transaction fees for a transaction on Mantle Network

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:
L1RollupFee = L1GasPrice * Overhead * Scalar * Ratio
There are 4 interfaces available as part of the SDK, where:
  • L1GasPrice * Ratio can be fetched by calling the getL1GasPrice method. It invokes the l1basefee() function of the BVM_GasPriceOracle contract.
  • overhead, decimals, and scalar values can be fetched from the BVM_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:
L2TxnFee = L2GasPrice * L2GasUsed
  • L2 gas price is fetched by calling the getGasPrice method
  • The gas used to execute a transaction is fetched by calling the estimateGas method
The total gas fee is thus calculated as:
totalEstimatedGasFee = L1RollupFee + L2TxnFee

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:
npm install @mantleio/sdk
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.
const ethers = require("ethers")
const mantleSDK = require("@mantleio/sdk");
async function estimateGasFee() {
const l2RpcProvider = new ethers.providers.JsonRpcProvider("https://rpc.mantle.xyz")
try{
// Arbitrary tx object
const tx = {
to: '0x...',
value: ethers.utils.parseEther("0.1"), // Returns value in wei
};
// By calling the BVM_GasPriceOracle contract method l1basefee()
const gasPrice = await mantleSDK.getL1GasPrice(l2RpcProvider);
const decimals = await mantleSDK.decimals(l2RpcProvider);
const scalar = await mantleSDK.scalar(l2RpcProvider);
const gasUsed = await mantleSDK.overhead(l2RpcProvider);
// L1RollupFee
const l1RollupFee = gasPrice.mul(gasUsed).mul(scalar).div(10**decimals)
// L2TxnFee
const l2Gas = await l2RpcProvider.estimateGas(tx)
const l2GasPrice = await l2RpcProvider.getGasPrice()
const l2TxnFee = l2GasPrice.mul(l2Gas);
// Total estimated Gas Fee
const totalEstimatedGasFee = l1RollupFee.add(l2TxnFee);
console.log(`Total estimated Gas Fee: ${totalEstimatedGasFee.toString()}`);
} catch (error) {
console.error('Error estimating gas:', error);
}
}
estimateGasFee();

Result

totalEstimatedGasFee contains the final result of the calculation, as discussed in the Description section.