Scroll Alpha has a pre-deployed contract
0x5300000000000000000000000000000000000002) used to estimate the L1 gas fee given raw transaction data. This is a push oracle, updated by a relayer run by Scroll.
It stores the L1 base fee gas price and provides a public API, which can be used to estimate the total transaction fee for some L2 transactions.
The L1 fee calculation works as follows.
- 1.Read three fields
L1GasPriceOraclecontract. The slots for these fields in the contract are
- 2.Count the number of zero bytes and non-zero bytes from the transaction callData.
- 3.Calculate the sumL1 data fee (
PRECISION = 1e9)
Encoding of a transaction in the commit tx [length] [RLP-encoded transaction with signature] consists of two parts:
- 1.The sum of zero bytes and non-zero bytes in the RLP-encoded transaction without signature
- 2.Additional 74 bytes
- 4 bytes: the length prefix of transaction data
- 1 byte: RLP prefix for V
- 3 bytes: V
- 1 byte: RLP prefix for R
- 32 bytes: R
- 1 byte: RLP prefix for S
- 32 bytes: S
function overhead() external view returns (uint256);
Returns the current L1 fee overhead
function scalar() external view returns (uint256);
Returns the current l1 fee scalar
function l1BaseFee() external view returns (uint256);
Returns the latest known l1 base fee
function getL1Fee(bytes memory data) external view returns (uint256);
Computes the L1 portion of the fee based on the size of the RLP encoded input transaction, the current L1 base fee, and the various dynamic parameters.
Returns: L1 fee that should be paid for the transaction
function getL1GasUsed(bytes memory data) external view returns (uint256);
Computes the amount of L1 gas used for a transaction. Adds the overhead which represents the per-transaction gas overhead of posting the transaction and state roots to L1. Adds 74 bytes of padding to account for the fact that the input does not have a signature.
Returns: Amount of L1 gas used to publish the transaction.