Skip to main content

chain_getBlockHash RPC method

The chain_getBlockHash method provides a straightforward way to obtain the hash of a particular block in the blockchain. This method is commonly used to identify blocks uniquely and securely. It accepts a block number as an input parameter and returns the corresponding block hash. If no block number is specified, the method defaults to retrieving the hash of the latest block, ensuring flexibility and utility for various use cases.

Parameters

BlockNumber integer

Identifies the block by its numerical identifier (block number) for retrieving its hash. In the absence of this parameter, the hash of the latest block is returned.

Returns

result string

Yields the hash string associated with the block number provided, or the latest block's hash if no number is specified.

Code Examples

curl https://api-polkadot.dwellir.com/<API-KEY> \
-X POST \
-H "Content-Type: application/json" \
--data '{"method":"chain_getBlockHash","params":[BlockNumber],"id":1,"jsonrpc":"2.0"}'

Hints

How do I efficiently use .multi for storage queries and execute multiple RPC queries in parallel when working with a blockchain API?

For executing multiple RPC queries efficiently, use parallel execution:

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

// Function to create a request for a specific method
function createRequest(method, params, id) {
return {
method: 'POST',
headers: myHeaders,
body: JSON.stringify({
"method": method,
"params": params,
"id": id,
"jsonrpc": "2.0"
}),
redirect: 'follow'
};
}

// Create requests for different methods or parameters
var requestOptions1 = createRequest("chain_getBlockHash", [], 1);
var requestOptions2 = createRequest("chain_getBlockHash", [], 2);
var requestOptions3 = createRequest("chain_getBlockHash", [], 3);

// Fetch all requests in parallel
Promise.all([
fetch("https://api-polkadot.dwellir.com/<API-KEY>", requestOptions1),
fetch("https://api-polkadot.dwellir.com/<API-KEY>", requestOptions2),
fetch("https://api-polkadot.dwellir.com/<API-KEY>", requestOptions3)
])
.then(responses => Promise.all(responses.map(response => response.text())))
.then(results => {
console.log("result 1:", results[0]);
console.log("result 2:", results[1]);
console.log("result 3:", results[2]);
})
.catch(error => console.log('Error', error));