curl --request POST \
--url https://hyperliquid-mainnet.core.chainstack.com/4f8d8f4040bdacd1577bff8058438274/evm \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"method": "eth_newBlockFilter",
"params": [],
"id": 1
}'
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1"
}
Creates a filter object to notify when new blocks arrive. Returns a filter ID that can be used with eth_getFilterChanges to retrieve new block hashes.
curl --request POST \
--url https://hyperliquid-mainnet.core.chainstack.com/4f8d8f4040bdacd1577bff8058438274/evm \
--header 'Content-Type: application/json' \
--data '{
"jsonrpc": "2.0",
"method": "eth_newBlockFilter",
"params": [],
"id": 1
}'
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x1"
}
eth_newBlockFilter
JSON-RPC method creates a filter object to notify when new blocks arrive on the blockchain. This method returns a filter ID that can be used with eth_getFilterChanges
to retrieve new block hashes as they are mined, providing an efficient way to monitor blockchain progression.
params
field should be an empty array.
eth_getFilterChanges
to get new block hasheseth_getFilterChanges
returns only new blocks since the last call// Create a new block filter
const createBlockFilter = async () => {
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_newBlockFilter',
params: [],
id: 1
})
});
const data = await response.json();
return data.result;
};
// Get new blocks from filter
const getFilterChanges = async (filterId) => {
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_getFilterChanges',
params: [filterId],
id: 1
})
});
const data = await response.json();
return data.result;
};
// Monitor new blocks with callback
const monitorNewBlocks = async (callback) => {
const filterId = await createBlockFilter();
console.log(`Created block filter: ${filterId}`);
const pollForNewBlocks = async () => {
try {
const newBlocks = await getFilterChanges(filterId);
if (newBlocks && newBlocks.length > 0) {
for (const blockHash of newBlocks) {
callback(blockHash);
}
}
} catch (error) {
console.error('Error polling for new blocks:', error);
}
};
// Poll every 2 seconds
const intervalId = setInterval(pollForNewBlocks, 2000);
return {
filterId,
stop: () => clearInterval(intervalId)
};
};
// Block statistics tracker
const trackBlockStatistics = async () => {
const stats = {
blocksReceived: 0,
startTime: Date.now(),
blockHashes: [],
averageBlockTime: 0
};
const monitor = await monitorNewBlocks((blockHash) => {
stats.blocksReceived++;
stats.blockHashes.push({
hash: blockHash,
timestamp: Date.now()
});
// Calculate average block time
if (stats.blockHashes.length > 1) {
const timeSpan = stats.blockHashes[stats.blockHashes.length - 1].timestamp -
stats.blockHashes[0].timestamp;
stats.averageBlockTime = timeSpan / (stats.blockHashes.length - 1);
}
console.log(`New block: ${blockHash}`);
console.log(`Total blocks: ${stats.blocksReceived}, Avg time: ${stats.averageBlockTime.toFixed(0)}ms`);
});
return {
stats,
monitor
};
};
// Block notification system
const createBlockNotifier = async (webhookUrl) => {
const monitor = await monitorNewBlocks(async (blockHash) => {
try {
await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
type: 'new_block',
blockHash,
timestamp: new Date().toISOString()
})
});
} catch (error) {
console.error('Failed to send block notification:', error);
}
});
return monitor;
};
// Usage examples
monitorNewBlocks((blockHash) => {
console.log(`New block mined: ${blockHash}`);
}).then(monitor => {
console.log('Block monitoring started');
// Stop monitoring after 5 minutes
setTimeout(() => {
monitor.stop();
console.log('Block monitoring stopped');
}, 5 * 60 * 1000);
});
// Track block statistics
trackBlockStatistics().then(({ stats, monitor }) => {
console.log('Block statistics tracking started');
// Display stats every 30 seconds
setInterval(() => {
console.log('Block Stats:', stats);
}, 30000);
});
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_newBlockFilter",
"params": [],
"id": 1
}' \
https://hyperliquid-mainnet.core.chainstack.com/4f8d8f4040bdacd1577bff8058438274/evm
eth_newBlockFilter
method is essential for applications that need to:
Successful response with filter ID
The response is of type object
.