Estimating Gas

Gas estimation for Onyx based transactions.

This guide is intended for developers and users seeking to understand gas calculations on Onyx, including how gas fees are determined and estimated before submitting transactions. This document provides a detailed breakdown of Onyx's gas estimation methods, practical ways to calculate fees, and code examples for implementation.

Practical Gas Estimation on Onyx

For most practical applications, gas estimation can be done via a standard process by calling an Onyx node’s eth_estimateGas method. This method provides a gas limit that should sufficiently cover the entire transaction fee at the given L2 gas price.

Basic Gas Calculation

To determine the transaction fee, multiply the gas limit by the current L2 gas price:

Transaction Fees = Gas Limit × L2 Gas Price

Since Onyx leverages L3 technology with an Arbitrum Orbit stack, gas estimates can fluctuate based on L1 calldata pricing. The eth_estimateGas result may change over time as the L1 calldata cost varies.

To obtain a more precise gas limit, developers can use the NodeInterface.gasEstimateComponents() method:

  • Retrieve the gasEstimate (total estimated gas required).

  • Multiply by the baseFee (third returned value) to get total cost.

For transactions that involve L1 to L3 messages (retryable tickets), the recommended approach is to use:

  • Onyx SDK: L1ToL3MessageGasEstimator.estimateAll()

  • NodeInterface API: estimateRetryableTicket()

These methods return complete gas estimations, ensuring sufficient fees are allocated for successful execution.

Detailed Breakdown of Gas Calculation Formula

Onyx transaction fees are calculated as:

TXFEES = L2 Gas Price (P) × Gas Limit (G)

The Gas Limit (G) consists of:

Gas Limit (G) = Gas used on L2 (L2G) + Extra Buffer for L1 cost (B)

Where the buffer (B) accounts for L1 calldata posting costs, computed as:

L1 Estimated Cost (L1C) = L1 price per byte (L1P) × Data size in bytes (L1S)

The buffer is then determined by:

Extra Buffer (B) = L1C / L2 Gas Price (P)

Thus, the final transaction fee formula is:

TXFEES = P × (L2G + ((L1P × L1S) / P))

Obtaining the Required Variables

To retrieve these values, developers should use the NodeInterface API:

  • L2 Gas Price (P): Call NodeInterface.GasEstimateComponents() and retrieve baseFee (third result).

  • Gas Used on L2 (L2G): Subtract gasEstimateForL1 (second result) from gasEstimate (first result).

  • L1 Price per Byte (L1P): Retrieve l1BaseFeeEstimate (fourth result) and multiply by 16.

  • L1 Data Size (L1S): Compute using gasEstimateForL1 × P / L1P.

Alternatively, calling NodeInterface.gasEstimateL1Component() provides l1BaseFeeEstimate and gasEstimateForL1 directly.

Code Example: Estimating Gas in JavaScript

Below is an implementation example using Onyx’s SDK and NodeInterface API.

const { NodeInterface__factory } = require("@onyx/sdk/dist/lib/abi/factories/NodeInterface__factory");
const { NODE_INTERFACE_ADDRESS } = require("@onyx/sdk/dist/lib/dataEntities/constants");

const nodeInterface = NodeInterface__factory.connect(NODE_INTERFACE_ADDRESS, baseL2Provider);

const gasEstimateComponents = await nodeInterface.callStatic.gasEstimateComponents(
  destinationAddress,
  false,
  txData,
  { blockTag: 'latest' }
);

const l1GasEstimated = gasEstimateComponents.gasEstimateForL1;
const l2GasUsed = gasEstimateComponents.gasEstimate.sub(gasEstimateComponents.gasEstimateForL1);
const l2EstimatedPrice = gasEstimateComponents.baseFee;
const l1EstimatedPrice = gasEstimateComponents.l1BaseFeeEstimate.mul(16);

const l1Cost = l1GasEstimated.mul(l2EstimatedPrice);
const l1Size = l1Cost.div(l1EstimatedPrice);

const P = l2EstimatedPrice;
const L2G = l2GasUsed;
const L1P = l1EstimatedPrice;
const L1S = l1Size;

const L1C = L1P.mul(L1S);
const B = L1C.div(P);
const G = L2G.add(B);
const TXFEES = P.mul(G);

Final Notes

Gas estimations using these techniques are approximate and may differ slightly in practice. Developers should set appropriate expectations with users regarding potential variations in gas costs.

For further implementation details, refer to Onyx’s official SDK documentation and gas estimation tutorials.

Last updated