Estimate Gas on Orb3
🔍 How to Estimate Gas in Orb3 Using Arbitrum
Understanding gas operations in Orb3, especially with its integration of Arbitrum's Nitro technology, is crucial for both users and developers. This guide aims to clarify how gas is calculated and estimated in the network.
Practical Approach to Gas Estimation:
Standard Gas Estimation: For a quick and practical gas estimation, use the Arbitrum node's
eth_estimateGas
. This call provides a gas limit that should cover the transaction fee at the specified L2 gas price. Multiply theeth_estimateGas
value by the L2 gas price to estimate the total Ether required for a successful transaction 🧮.Gas Limit Estimation: Another method is to use
NodeInterface.gasEstimateComponents()
and take the first result,gasEstimate
. Then, multiply this by the L2 gas price, available in the third result,baseFee
, to find the total cost 💰.Special Case - L1 to L2 Messages: For L1 to L2 messages, use
L1ToL2MessageGasEstimator.estimateAll()
of the Arbitrum SDK orNodeInterface.estimateRetryableTicket()
for comprehensive gas information 🔄.
Understanding the Gas Calculation Formula:
Basic Formula: The transaction fees are calculated as:
TXFEES = L2 Gas Price (P) * Gas Limit (G)
.Gas Limit Components:
Gas Limit (G) = Gas used on L2 (L2G) + Extra Buffer for L1 cost (B)
.Calculating Extra Buffer: The extra buffer considers the cost of posting the transaction on L1, calculated as:
L1 Estimated Cost (L1C) = L1 price per byte of data (L1P) * Size of data to be posted in bytes (L1S)
.Dividing by L2 Gas Price:
Extra Buffer (B) = L1 Estimated Cost (L1C) / L2 Gas Price (P)
.Final Calculation:
TXFEES = P * (L2G + ((L1P * L1S) / P))
.
Where to Get the Necessary Information:
Use Arbitrum's NodeInterface to gather all the required data for the calculation.
L2 Gas Price, L2 Gas Used, and L1 estimated prices are all accessible through NodeInterface methods.
Code Example for Gas Estimation:
See a practical example in the Orb3 tutorials repository, which walks through all the calculations mentioned above.
The example uses the Orb3 SDK to connect to the NodeInterface, illustrating how to retrieve values and apply the formula in your code.
Final Note:
Keep in mind that gas estimations are approximate, and actual gas fees may vary. It's important to communicate this to end-users, setting the right expectations for gas costs in transactions.
Last updated