eth_getBlockByNumber
Returns information about a block by block number on Optimism Layer 2.
When to Use This Method
eth_getBlockByNumber
is essential for:
- Block Analysis - Examine specific blocks on Optimism L2
- Transaction Tracking - Get all transactions in a block
- Historical Data - Access past block information
- Chain Monitoring - Monitor block production and health
Parameters
-
Block Number -
QUANTITY|TAG
- Block number in hex format (e.g.,
0x1b4
) "latest"
- Most recent block"earliest"
- Genesis block"pending"
- Pending block"safe"
- Latest safe block"finalized"
- Latest finalized block
- Block number in hex format (e.g.,
-
Full Transactions -
BOOLEAN
true
- Return full transaction objectsfalse
- Return only transaction hashes
{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": ["latest", true],
"id": 1
}
Returns
Block object with the following fields:
- number - Block number
- hash - Block hash
- parentHash - Hash of parent block
- timestamp - Unix timestamp
- transactions - Array of transactions or hashes
- gasUsed - Total gas used in block
- gasLimit - Gas limit for block
Implementation Examples
- cURL
- JavaScript
curl -X POST https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getBlockByNumber",
"params": ["latest", true],
"id": 1
}'
// Using ethers.js
import { JsonRpcProvider } from 'ethers';
const provider = new JsonRpcProvider('https://api-optimism-mainnet-archive.n.dwellir.com/YOUR_API_KEY');
async function getLatestOptimismBlock() {
const block = await provider.getBlock('latest', true);
console.log('Latest Optimism block:', {
number: block.number,
hash: block.hash,
timestamp: new Date(block.timestamp * 1000),
transactions: block.transactions.length,
gasUsed: block.gasUsed.toString(),
gasLimit: block.gasLimit.toString()
});
return block;
}
// Get specific block with L2 context
async function getOptimismBlock(blockNumber) {
const block = await provider.getBlock(blockNumber, true);
return {
...block,
network: 'Optimism',
chainId: 10,
layer: 'L2',
transactionCount: block.transactions.length,
avgGasPrice: block.transactions.reduce((sum, tx) =>
sum + (tx.gasPrice || 0n), 0n) / BigInt(block.transactions.length || 1)
};
}
Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"number": "0x75bcd15",
"hash": "0x1d59ff54b1eb26b013ce3cb5fc9dab3705b415a67127a003c3e61eb445bb8df2",
"parentHash": "0x2d59ff54b1eb26b013ce3cb5fc9dab3705b415a67127a003c3e61eb445bb8df1",
"timestamp": "0x55ba467c",
"transactions": [...],
"gasUsed": "0x47e7c4",
"gasLimit": "0x1c9c380"
}
}
Need help? Contact our support team or check the Optimism documentation.