Retrieve finalized L2 blocks
The finalized
tag is only available on Linea Sepolia, and will be fully activated on Mainnet
on October 9.
eth_getBlockByNumber
is the exception, and is already active on Linea Mainnet.
A finalized L2 block is a block on an L2 blockchain (Linea) that has been confirmed and validated by the L1 blockchain (Ethereum), ensuring its immutability and security.
There are two methods to obtain the current finalized block:
- Use the
finalized
block parameter tag in JSON-RPC API calls. - Query the Linea L1 rollup contract.
Use the finalized
tag​
Use the finalized
tag in API calls to specify a block that has been confirmed by the L1
network. For example, here the eth_getBlockByNumber
method returns information about the current
finalized block:
curl https://rpc.linea.build \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["finalized",false],"id":1}'
Supported methods​
eth_getBlockByNumber
is currently the only JSON-RPC API method that supports the finalized
tag
on Linea. Support for the finalized
tag will be added to other JSON-RPC API methods from October
9, 2024.
The finalized
tag works with all JSON-RPC API methods on Linea Mainnet if you are using a node
running the Besu client.
Query the rollup contract​
Using the finalized
tag in JSON-RPC API calls is recommended. As an alternative, you can query
the Linea L1 rollup contract
to retrieve the value of the current finalized L2 block number stored in the currentL2BlockNumber
variable.
Prerequisites​
Create the script​
-
In your project folder, initialize the project and install the
web3
package:npm init -y
npm install web3
-
Create a JavaScript file (for example
index.js
) and copy the following code:infoUpdate the Infura endpoint with your API key, or add a different RPC provider.
index.jsconst { Web3 } = require("web3")
// Connect to an Ethereum node (e.g., Infura)
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://mainnet.infura.io/v3/<YOUR-API-KEY>`
)
)
// Define the minimal ABI to obtain the current finalized block number
const lineaRollupAbi = [
{
"constant": true,
"inputs": [],
"name": "currentL2BlockNumber",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
// Define the contract address
const lineaRollupAddress = '0xd19d4b5d358258f05d7b411e21a1460d11b0876f';
// Create a contract instance
const lineaRollupContract = new web3.eth.Contract(lineaRollupAbi, lineaRollupAddress);
// Function to get the finalized L2 block number
async function getFinalizedL2BlockNumber() {
try {
const currentL2BlockNumber = await lineaRollupContract.methods.currentL2BlockNumber().call();
const blockNumberHex = '0x' + BigInt(currentL2BlockNumber).toString(16); // Convert BigInt to hex string
console.log('Finalized L2 Block Number:', currentL2BlockNumber.toString()); // Print BigInt as string
console.log('Finalized L2 Block Number (Hex):', blockNumberHex);
return { blockNumber: currentL2BlockNumber, blockNumberHex };
} catch (error) {
console.error('Error fetching L2 block number:', error);
}
}
// Call the function
getFinalizedL2BlockNumber(); -
In the project directory, run the script:
node index.js