chain_getBlockHash - Moonbeam RPC Method
Get block hash by number on Moonbeam. Essential for converting block numbers to hashes, historical queries, and blockchain navigation on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.
Returns the block hash for a given block number on Moonbeam. This is the primary method for converting block numbers into block hashes, which are required by most other chain RPC methods.
Why Moonbeam? Build on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects with full EVM compatibility on Polkadot, native XCM cross-chain messaging, 10K+ TPS, 24% staking APR, and $0.015 transaction costs.
When to Use This Method
chain_getBlockHash is fundamental for cross-chain dApp developers, Polkadot builders, and teams requiring multi-chain interoperability:
- Historical Queries — Convert block numbers to hashes for state queries at specific heights on Moonbeam
- Block Navigation — Navigate the blockchain history for cross-chain DeFi, multi-chain dApps, and Ethereum-to-Polkadot bridging via XCM, Axelar, LayerZero, and Wormhole
- Data Indexing — Build block number-to-hash mappings for indexers and explorers
- Cross-Reference — Translate block numbers from events or logs into hashes for detailed lookups
Code Examples
Common Use Cases
1. Block Range Iterator
Iterate over a range of blocks on Moonbeam for indexing:
async function iterateBlocks(api, startBlock, endBlock) {
for (let num = startBlock; num <= endBlock; num++) {
const hash = await api.rpc.chain.getBlockHash(num);
const block = await api.rpc.chain.getBlock(hash);
console.log(`Block #${num}: ${block.block.extrinsics.length} extrinsics`);
}
}2. Historical State Query
Query Moonbeam state at a specific block height:
async function getBalanceAtBlock(api, address, blockNumber) {
const blockHash = await api.rpc.chain.getBlockHash(blockNumber);
const apiAt = await api.at(blockHash);
const account = await apiAt.query.system.account(address);
return {
blockNumber,
free: account.data.free.toString(),
reserved: account.data.reserved.toString()
};
}3. Genesis Hash Verification
Verify you are connected to the correct Moonbeam network:
async function verifyNetwork(api, expectedGenesisHash) {
const genesisHash = await api.rpc.chain.getBlockHash(0);
if (genesisHash.toHex() !== expectedGenesisHash) {
throw new Error(`Wrong network! Expected ${expectedGenesisHash}, got ${genesisHash.toHex()}`);
}
console.log('Connected to correct network');
}Error Handling
| Error Code | Description | Solution |
|---|---|---|
| -32602 | Invalid params | Block number must be a non-negative integer |
| -32603 | Internal error | Node may be syncing or block not yet available — retry with backoff |
| -32601 | Method not found | Verify the node supports this RPC method |
| -32005 | Rate limit exceeded | Implement client-side rate limiting |
Related Methods
chain_getBlock— Get full block data by hashchain_getHeader— Get block header by hashchain_getFinalizedHead— Get the latest finalized block hash
chain_getBlock
Retrieve block data by hash on Moonbeam. Essential for accessing block headers and extrinsics on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.
chain_getFinalizedHead
Get the finalized block hash on Moonbeam. Essential for confirmed state queries, exchange integrations, and applications requiring irreversible finality on the cross-chain connected EVM platform on Polkadot with $65M+ TVL and 100+ projects.