// Get block header by hash
const getHeaderByHash = async (blockHash) => {
const response = await fetch('https://hyperliquid-mainnet.core.chainstack.com/YOUR_ENDPOINT/evm', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getHeaderByHash',
params: [blockHash],
id: 1
})
});
const data = await response.json();
return data.result;
};
// Validate block hash and get header info
const validateBlockHash = async (blockHash) => {
try {
const header = await getHeaderByHash(blockHash);
if (header === null) {
return { valid: false, error: 'Block not found' };
}
return {
valid: true,
header,
blockNumber: parseInt(header.number, 16),
timestamp: new Date(parseInt(header.timestamp, 16) * 1000),
gasUtilization: (parseInt(header.gasUsed, 16) / parseInt(header.gasLimit, 16) * 100).toFixed(2)
};
} catch (error) {
return { valid: false, error: error.message };
}
};
// Compare two block headers
const compareBlockHeaders = async (hash1, hash2) => {
const [header1, header2] = await Promise.all([
getHeaderByHash(hash1),
getHeaderByHash(hash2)
]);
if (!header1 || !header2) {
return { error: 'One or both blocks not found' };
}
return {
block1: {
hash: header1.hash,
number: parseInt(header1.number, 16),
timestamp: parseInt(header1.timestamp, 16),
gasUsed: parseInt(header1.gasUsed, 16)
},
block2: {
hash: header2.hash,
number: parseInt(header2.number, 16),
timestamp: parseInt(header2.timestamp, 16),
gasUsed: parseInt(header2.gasUsed, 16)
},
comparison: {
blockDifference: parseInt(header2.number, 16) - parseInt(header1.number, 16),
timeDifference: parseInt(header2.timestamp, 16) - parseInt(header1.timestamp, 16),
gasUsedDifference: parseInt(header2.gasUsed, 16) - parseInt(header1.gasUsed, 16)
}
};
};
// Track block ancestry
const getBlockAncestry = async (blockHash, generations = 5) => {
const ancestry = [];
let currentHash = blockHash;
for (let i = 0; i < generations; i++) {
const header = await getHeaderByHash(currentHash);
if (!header) break;
ancestry.push({
generation: i,
hash: header.hash,
number: parseInt(header.number, 16),
parentHash: header.parentHash,
timestamp: parseInt(header.timestamp, 16)
});
currentHash = header.parentHash;
// Stop if we reach the genesis block
if (currentHash === '0x0000000000000000000000000000000000000000000000000000000000000000') {
break;
}
}
return ancestry;
};
// Usage
const blockHash = '0x2ce91ae0ed242b4b78b432a45b982fb81a414d6b04167762ed3c7446710a4b8e';
validateBlockHash(blockHash).then(result => {
if (result.valid) {
console.log(`Block ${result.blockNumber} found:`, result.header);
} else {
console.log('Block validation failed:', result.error);
}
});
getBlockAncestry(blockHash, 3).then(ancestry => {
console.log('Block ancestry:', ancestry);
});