eth_getFilterChanges
JSON-RPC method returns an array of logs or block hashes that have occurred since the last poll for the specified filter. This method is used to retrieve new results from filters created with eth_newFilter
or eth_newBlockFilter
, providing an efficient polling mechanism for monitoring blockchain events.
eth_newFilter
or eth_newBlockFilter
address
, topics
, data
, blockNumber
, transactionHash
, etc.eth_getFilterChanges
call// Get filter changes
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;
};
// Poll for log changes
const pollLogFilter = async (filterId, callback, intervalMs = 2000) => {
const poll = async () => {
try {
const changes = await getFilterChanges(filterId);
if (changes && changes.length > 0) {
for (const log of changes) {
callback(log);
}
}
} catch (error) {
console.error('Error polling filter changes:', error);
}
};
const intervalId = setInterval(poll, intervalMs);
return {
stop: () => clearInterval(intervalId),
poll: poll // Manual poll function
};
};
// Poll for block changes
const pollBlockFilter = async (filterId, callback, intervalMs = 2000) => {
const poll = async () => {
try {
const blockHashes = await getFilterChanges(filterId);
if (blockHashes && blockHashes.length > 0) {
for (const blockHash of blockHashes) {
callback(blockHash);
}
}
} catch (error) {
console.error('Error polling block changes:', error);
}
};
const intervalId = setInterval(poll, intervalMs);
return {
stop: () => clearInterval(intervalId),
poll: poll
};
};
// Advanced event processor
class EventProcessor {
constructor() {
this.filters = new Map();
}
async addLogFilter(filterId, eventHandler) {
const poller = await pollLogFilter(filterId, (log) => {
eventHandler({
type: 'log',
filterId,
data: log,
timestamp: new Date()
});
});
this.filters.set(filterId, {
type: 'log',
poller,
handler: eventHandler
});
return filterId;
}
async addBlockFilter(filterId, blockHandler) {
const poller = await pollBlockFilter(filterId, (blockHash) => {
blockHandler({
type: 'block',
filterId,
blockHash,
timestamp: new Date()
});
});
this.filters.set(filterId, {
type: 'block',
poller,
handler: blockHandler
});
return filterId;
}
stopFilter(filterId) {
const filter = this.filters.get(filterId);
if (filter) {
filter.poller.stop();
this.filters.delete(filterId);
return true;
}
return false;
}
stopAll() {
for (const [filterId, filter] of this.filters) {
filter.poller.stop();
}
this.filters.clear();
}
async pollAllOnce() {
const promises = [];
for (const [filterId, filter] of this.filters) {
promises.push(filter.poller.poll());
}
await Promise.all(promises);
}
}
// Real-time event dashboard
const createEventDashboard = async () => {
const processor = new EventProcessor();
const stats = {
totalLogs: 0,
totalBlocks: 0,
lastActivity: null
};
// Generic event handler
const handleEvent = (event) => {
if (event.type === 'log') {
stats.totalLogs++;
console.log(`Log Event from filter ${event.filterId}:`, event.data);
} else if (event.type === 'block') {
stats.totalBlocks++;
console.log(`New Block from filter ${event.filterId}: ${event.blockHash}`);
}
stats.lastActivity = event.timestamp;
};
return {
processor,
stats,
addLogFilter: (filterId) => processor.addLogFilter(filterId, handleEvent),
addBlockFilter: (filterId) => processor.addBlockFilter(filterId, handleEvent),
stop: () => processor.stopAll()
};
};
// Usage examples
const filterId = '0x1';
// Simple polling
pollLogFilter(filterId, (log) => {
console.log('New log:', log);
}).then(poller => {
console.log('Started polling for logs');
// Stop after 5 minutes
setTimeout(() => {
poller.stop();
console.log('Stopped polling');
}, 5 * 60 * 1000);
});
// Advanced event processing
createEventDashboard().then(dashboard => {
// Add filters to dashboard
dashboard.addLogFilter('0x1');
dashboard.addBlockFilter('0x2');
// Display stats every 30 seconds
setInterval(() => {
console.log('Dashboard Stats:', dashboard.stats);
}, 30000);
});
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "eth_getFilterChanges",
"params": [
"0x1"
],
"id": 1
}' \
https://hyperliquid-mainnet.core.chainstack.com/4f8d8f4040bdacd1577bff8058438274/evm
eth_getFilterChanges
method is essential for applications that need to:
Was this page helpful?