L1 Data Fee Calculation
This page describes how to calculate the L1 data fee for a specific transaction on a Zircuit network.
Retrieve the parameters
To perform the calculation the following parameters are needed:
L1 base fee
L1 base fee scalar
L1 blob base fee
L1 blob base fee scalar
They are available by calling the Gas Price Oracle smart contract predeployed on every Zircuit network on this address: 0x420000000000000000000000000000000000000F
To retrieve them we can use Foundry's cast. You will need the block number in which the transaction was included to retrieve the parameters from the right point in time. You will also need a valid RPC url. For example you'll find the Garfield Testnet RPC urls on this page: RPC Endpoints.
L1 base fee:
cast call 0x420000000000000000000000000000000000000F "l1BaseFee()(uint256)" --rpc-url $RPC_URL --block $BLOCK_NUMBER
L1 base fee scalar:
cast call 0x420000000000000000000000000000000000000F "baseFeeScalar()(uint256)" --rpc-url $RPC_URL --block $BLOCK_NUMBER
L1 blob base fee:
cast call 0x420000000000000000000000000000000000000F "blobBaseFee()(uint256)" --rpc-url $RPC_URL --block $BLOCK_NUMBER
L1 blob base fee scalar:
cast call 0x420000000000000000000000000000000000000F "blobBaseFeeScalar()(uint256)" --rpc-url $RPC_URL --block $BLOCK_NUMBER
Get the raw RLP-encoded signed transaction:
cast tx $TX_HASH --rpc-url $RPC_URL --raw
Zero-bytes and non zero-bytes are accounted differently, so the next step is to count all the zero bytes and all the non-zero bytes in the tx. Here is a shell script that does it. Remember to remove the "0x" at the beginning when you provide it the raw tx.
#!/usr/bin/env bash
# Usage: ./count_zero_bytes_hex.sh <hex string>
# Example: ./count_zero_bytes_hex.sh 00A1FF0033
hex="$1"
# Ensure we got a parameter
if [ -z "$hex" ]; then
echo "Usage: $0 <hex string>"
exit 1
fi
# Normalize to uppercase to keep comparison simple
hex_upper=$(echo "$hex" | tr '[:lower:]' '[:upper:]')
# The hex string length must be an even number for whole bytes (2 hex chars = 1 byte)
len=${#hex_upper}
if (( len % 2 != 0 )); then
echo "Error: Hex string must have an even number of characters."
exit 1
fi
zero_bytes=0
other_bytes=0
# Process two hex characters at a time
for ((i=0; i<len; i+=2)); do
byte="${hex_upper:$i:2}" # Extract 2-hex-character chunk
if [ "$byte" == "00" ]; then
((zero_bytes++))
else
((other_bytes++))
fi
done
echo "Hex string: $hex"
echo "Zero bytes: $zero_bytes"
echo "Other bytes: $other_bytes"
In the Gas Pricing and Transaction Fees it's specified the cost of zero bytes is 4 and the cost of non-zero bytes is 16. So your calldatagas
will be zero-bytes*4 + non-zero-bytes*16.
Now that you have all the info you need, you can just apply the following formula:
(calldataGas/16)*(l1BaseFee*16*l1BaseFeeScalar + l1BlobBaseFee*l1BlobBaseFeeScalar)/1e6
That is equivalent to the following formula better fitted for precision under integer arithmetic:
calldataGas*(l1BaseFee*16*l1BaseFeeScalar + l1BlobBaseFee*l1BlobBaseFeeScalar)/16e6
Last updated