eth_getBlockByHash
Returns information about a block by block hash.
When to Use This Method
Use eth_getBlockByHash
to:
- Retrieve Specific Block - Get block data using its unique hash
- Verify Block Integrity - Confirm block contents haven't changed
- Cross-Reference Blocks - Link transactions to specific blocks
- Chain Reorganization Detection - Track block validity
- Historical Data Access - Query immutable block records
Parameters
-
Block Hash (required):
- 32-byte block hash as hexadecimal
-
Transaction Details (required):
true
- Returns full transaction objectsfalse
- Returns only transaction hashes
{
"jsonrpc": "2.0",
"method": "eth_getBlockByHash",
"params": [
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
true
],
"id": 1
}
Returns
Block object or null
if block not found.
Implementation Examples
- cURL
- JavaScript
- Python
curl -X POST https://api-xdc-mainnet.n.dwellir.com/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getBlockByHash",
"params": [
"0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b",
true
],
"id": 1
}'
// Using ethers.js
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://api-xdc-mainnet.n.dwellir.com/YOUR_API_KEY');
const blockHash = '0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b';
const block = await provider.getBlock(blockHash);
console.log('Block number:', block.number);
console.log('Timestamp:', new Date(block.timestamp * 1000));
// Using web3.js
import Web3 from 'web3';
const web3 = new Web3('https://api-xdc-mainnet.n.dwellir.com/YOUR_API_KEY');
const block = await web3.eth.getBlock(blockHash, true);
console.log('Block:', block);
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('https://api-xdc-mainnet.n.dwellir.com/YOUR_API_KEY'))
block_hash = '0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b'
block = w3.eth.get_block(block_hash)
print(f"Block #{block['number']} with {len(block['transactions'])} transactions")
Common Use Cases
Block Verification
async function verifyBlock(blockHash, expectedData) {
const block = await provider.getBlock(blockHash, true);
if (!block) {
throw new Error('Block not found');
}
return {
valid: true,
number: block.number,
transactionCount: block.transactions.length,
timestamp: new Date(block.timestamp * 1000),
gasUsed: block.gasUsed
};
}
Best Practices
- Always check for null responses
- Cache block data when possible
- Use block hashes for permanent references
- Validate block hash format before querying
Error Codes
Code | Message | Description |
---|---|---|
-32602 | Invalid params | Invalid block hash format |
-32000 | Block not found | Block doesn't exist |
Related Methods
- eth_getBlockByNumber - Get block by number
- eth_blockNumber - Get latest block number
eth_getTransactionByBlockHashAndIndex
- Get transaction from block