Production engineering guide for blockchain event processing in Web3 and blockchain systems.
Blockchain event processing is the practice of capturing, interpreting, and reacting to changes in blockchain state—such as new blocks, transaction receipts, smart contract logs, or state transitions—across multiple chains. It matters when applications need to respond to on-chain activity in near real time, whether for user-facing updates, off-chain data synchronization, or triggering external workflows. This is the backbone of on-chain applications: from a user depositing ETH to a liquidity pool, to a multi-chain transaction being confirmed and a cross-chain message dispatched.
Applications consume events through blockchain nodes that expose JSON-RPC endpoints. The most common source is a node running a full client (e.g., Geth, Besu, Nethermind, Erigon) connected to a chain. The primary interface is JSON-RPC over HTTP or WebSockets.
To listen for new blocks, a client sends:
{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"],
"id": 1
}
The response is a subscription ID. Subsequent eth_getBlockByNumber calls can fetch block details, including transaction roots, state roots, and timestamps.
For transaction and log events, clients use eth_getLogs with filters. Each filter specifies:
fromBlock and toBlock (block numbers or "latest")address (contract address or list of addresses)topics (indexed event parameters, with 0 for the event signature, 1 for first indexed arg, etc.)Example filter to capture all Transfer events:
{
"jsonrpc": "2.0",
"method": "eth_getLogs",
"params": [
{
"fromBlock": "0x100",
"toBlock": "latest",
"address": "0x8f3cf7ad13aee08be4624788c846e5d4111234d0",
"topics": [
"0xddf252ad1be2c89b69c2b068fc37c61c502d86b3b35c1c711e59d41c2071b519"
]
}
],
"id": 1
}
The response is an array of Log objects:
[
{
"removed": false,
"logIndex": "0x1",
"transactionIndex": "0x2",
"transactionHash": "0x...",
"blockHash": "0x...",
"blockNumber": "0x100",
"address": "0x8f3cf7ad13aee08be4624788c846e5d4111234d0",
"data": "0x...",
"topics": [
"0xddf252ad1be2c89b69c2b068fc37c61c502d86b3b35c1c711e59d41c2071b519",
"0x000000000000000000000000a0b86952540b6564a36e9c50d9035d6f55011d3d",
"0x000000000000000000000000d841a433b6821410c8e7322883e48813943c31b2"
]
}
]
Once events are retrieved, they enter a processing pipeline. The pipeline typically consists of three stages: ingestion, transformation, and dispatch.
Polling is simple: a service calls eth_getLogs at regular intervals (e.g., every 10 seconds) and fetches new logs since the last run.
A typical polling loop in a Node.js service:
async function pollEvents() {
let lastBlock = await getLatestProcessedBlock();
const currentBlock = await getBlockNumber();
while (lastBlock < currentBlock) {
const logs = await eth_getLogs({
fromBlock: lastBlock + 1,
toBlock: currentBlock,
address: "0x8f3cf7ad13aee08be4624788c846e5d4111234d0",
topics: [ "0xddf252ad1be2c89b69c2b068fc37c61c502d86b3b35c1c711e59d41c2071b519" ]
});
await processLogs(logs);
lastBlock = currentBlock;
await saveLatestProcessedBlock(lastBlock);
await sleep(10_000);
}
}
Streaming is more efficient for high-throughput environments. Using WebSocket subscriptions, the client receives newHeads and logs events in real time.
A service using eth_subscribe to listen for new blocks and contract events:
const ws = new WebSocket("wss://mainnet.infura.io/ws/v3/abc123");
ws.on("open", () => {
ws.send(JSON.stringify({
jsonrpc: "2.0",
method: "eth_subscribe",
params: ["newHeads"],
id: 1
}));
ws.send(JSON.stringify({
jsonrpc: "2.0",
method: "eth_subscribe",
params: [
"logs",
{
address: "0x8f3cf7ad13aee08be4624788c846e5d4111234d0",
topics: [
"0xddf252ad1be2c89b69c2b068fc37c61c502d86b3b35c1c711e59d41c2071b519"
]
}
],
id: 2
}));
});
ws.on("message", (data) => {
const msg = JSON.parse(data);
if (msg.method === "eth_subscription") {
const result = msg.params.result;
if (result.subscription === "0x1") {
// newHead event
const block = result.result;
processNewBlock(block);
} else if (result.subscription === "0x2") {
// log event
const log = result.result;
processLogEvent(log);
}
}
});
Events often include raw data fields that need ABI decoding. A Transfer event may have from, to, and value as bytes32 fields. The ABI defines the structure:
[
{
"name": "Transfer",
"inputs": [
{ "name": "from", "type": "address", "indexed": true },
{ "name": "to", "type": "address", "indexed": true },
{ "name": "value", "type": "uint256", "indexed": false }
],
"anonymous": false,
"type": "event"
}
]
A common approach is to use a library like ethers.js to decode logs:
const abi = [ /* ... */ ];
const contract = new ethers.Contract(address, abi, provider);
const decodedLogs = logs.map(log => {
const event = contract.interface.parseLog(log);
return {
eventName: event.name,
args: event.args,
blockNumber: log.blockNumber,
transactionHash: log.transactionHash,
timestamp: await getBlockTimestamp(log.blockNumber)
};
});
This yields structured data:
{
"eventName": "Transfer",
"args": {
"from": "0xa0b86952540b6564a36e9c50d9035d6f55011d3d",
"to": "0xd841a433b6821410c8e7322883e48813943c31b2",
"value": "1000000000000000000"
},
"blockNumber": 1234567,
"transactionHash": "0x...",
"timestamp": 1672531200
}
Dispatching involves sending processed events to downstream systems: databases, message queues, or external APIs.
A typical dispatch step writes to a PostgreSQL database:
CREATE TABLE events (
id SERIAL PRIMARY KEY,
event_name TEXT NOT NULL,
block_number BIGINT NOT NULL,
transaction_hash BYTEA NOT NULL,
args JSONB NOT NULL,
timestamp TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE (transaction_hash)
);
A service inserts events:
await db.query(
`INSERT INTO events (event_name, block_number, transaction_hash, args, timestamp)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (transaction_hash) DO NOTHING`,
[
event.eventName,
event.blockNumber,
Buffer.from(event.transactionHash.slice(2), 'hex'),
event.args,
new Date(event.timestamp * 1000)
]
);
For higher throughput, events are published to a message queue like RabbitMQ or Kafka:
{
"event": "Transfer",
"chain": "ethereum-mainnet",
"block": 1234567,
"transaction_hash": "0x...",
"args": {
"from": "0xa0b86952540b6564a36e9c50d9035d6f55011d3d",
"to": "0xd841a433b6821410c8e7322883e48813943c31b2",
"value": "1000000000000000000"
},
"timestamp": 1672531200
}
Consumers process these events asynchronously, possibly with retries and idempotency.
A common failure: the event processor restarts but misses events between the last processed block and the first block it checks. The root cause is not polling from the correct fromBlock. The solution is to store the last block processed and query from lastBlock + 1.
A subtle bug: when a node fails during eth_getLogs, the client may miss logs in the fromBlock window. The client must handle eth_getLogs responses with nextPage tokens when the result set is large.
Multiple instances of an event processor may process the same logs. Without idempotency, a Transfer event might be processed twice, leading to duplicate balances.
Idempotency keys—like transaction_hash—must be used to deduplicate. A typical approach is to maintain a processed_events table:
CREATE TABLE processed_events (
id SERIAL PRIMARY KEY,
event_hash BYTEA NOT NULL,
processed_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE (event_hash)
);
Before processing an event, the service checks if the transaction_hash exists:
SELECT 1 FROM processed_events WHERE event_hash = $1;
If it exists, skip processing. Otherwise, insert and process.
Events may arrive out of order due to network delays or block propagation. A Transfer event may be received before its block is finalized. The processor must buffer events and emit them only when a block is finalized.
A block is finalized when its finalized field is true in newHeads. The processor can emit events when finalized === true or when a certain number of confirmations (e.g., 6) are reached.
WebSocket subscriptions can drop. The client must:
close and error eventsblockNumber using eth_getLogs after reconnectionA failure mode: after a restart, the service receives a newHead but does not receive logs for that block because the logs subscription started too late.
Smart contracts evolve. A new Transfer event may add a fee field. The event processor must handle both old and new event formats.
A common pattern: store the event ABI version with the event, or use a schema registry (e.g., Confluent Schema Registry). The processor uses the ABI to decode logs and can handle schema migrations.
Critical error: eth_getLogs returns a 500 from the node with a body:
{
"jsonrpc": "2.0",
"error": {
"code": -32000,
"message": "Invalid parameters",
"data": {
"method": "eth_getLogs",
"params": {
"fromBlock": "0x100",
"toBlock": "latest",
"address": "0x8f3cf7ad13aee08be4624788c846e5d4111234d0",
"topics": [
"0xddf252ad1be2c89b69c2b068fc37c61c502d86b3b35c1c711e59d41c2071b519"
]
},
"error": "Invalid block number"
}
},
"id": 1
}
The processor logs the full error and retries with adjusted fromBlock and toBlock.
For systems requiring exactly-once event processing, the pipeline must:
transaction_hash)A common failure: a Kafka consumer processes an event, commits the offset, but crashes before writing to the database. On restart, the consumer replays the event, resulting in duplicates.
To fix, use idempotent producers: each message includes a message_id, and the consumer checks whether that message_id has been processed before.
Event processors should expose health checks via HTTP:
GET /health
Response:
{
"status": "healthy",
"lastProcessedBlock": 1234567,
"lag": 15,
"uptime": "3h22m10s",
"errors": [
{
"timestamp": "2024-01-05T12:00:00Z",
"message": "eth_getLogs failed: Invalid block number",
"error": "RPC error: code -32000"
}
]
}
A monitoring system (e.g., Prometheus) can scrape this endpoint and alert on:
Event processing is not just fetching logs—it is a continuous, fault-tolerant, stateful system built on the simple but powerful pattern of listening to a blockchain and reacting to its changes.
This page was rewritten on 18 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy