chain_getBlockHash
Description
Returns the block hash for a given block number. Without parameters, it returns the hash of the latest block. This is useful when you store block numbers but need hashes to query storage or block data.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
blockNumber | string | number | No | Hex-encoded or decimal block number. If omitted, the latest block hash is returned |
Returns
Field | Type | Description |
---|---|---|
result | string | Hex-encoded block hash |
Request Example
{
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [9453714],
"id": 1
}
Response Example
{
"jsonrpc": "2.0",
"result": "0xd4b3a0e3b88b7c25215926bbf7c733f5c31e69c0c8c4b4a86d6ec3f3f086e95d",
"id": 1
}
Code Examples
JavaScript
const hash = await api.rpc.chain.getBlockHash(9053714n);
console.log('Block hash:', hash.toHex());
Python
payload = {
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": ["0x9055f2"],
"id": 1
}
resp = requests.post(
"https://api-bifrost-polkadot.n.dwellir.com/YOUR_API_KEY",
headers={"Content-Type": "application/json"},
data=json.dumps(payload)
)
print(resp.json()["result"])
Rust
let hash = api.rpc().block_hash(Some(9_053_714u32.into())).await?;
println!("Block hash: {}", hash.expect("hash"));
Tips
- Use
chain_getFinalizedHead
to obtain the latest finalized hash, then feed that hash intochain_getBlock
for deterministic data. - When interacting with storage, always pin queries to a specific block hash to avoid race conditions during runtime upgrades.