refactor(indexer): declarative event handler registry#49
Merged
Conversation
Tier 3 refactor — replaces the hardcoded ERC-20 / ERC-721 / ERC-1155
dispatch in sync.ts with a registry where each event type owns its
own decoder file. Adding a new event (DEX swap, NFT mint, custom
protocol log) is now a one-file addition under apps/indexer/src/handlers/
plus one line in handlers/index.ts — no sync.ts edit, no growing
if/else in the hot loop.
Layout:
apps/indexer/src/handlers/
├── registry.ts EventHandler interface + register/dispatch + topicToAddress
├── erc20.ts ERC-20 Transfer (3 topics)
├── erc721.ts ERC-721 Transfer (4 topics, shares topic0 with ERC-20)
├── erc1155.ts TransferSingle (decoded) + TransferBatch (deferred null-return)
└── index.ts Side-effect import barrel
The registry keys handlers by topic0; multiple handlers can share a
topic0 (the dispatcher walks them in registration order, keeps the
first non-null result). That's how the ERC-20 vs ERC-721 split works:
both register against keccak("Transfer(address,address,uint256)") but
each null-returns when the topic count doesn't match its arity.
Behaviour identical to pre-refactor — same output rows for the same
input logs. Verified by build; the refactor is type-safe end-to-end
because the EventHandler.decode return type is the Drizzle-inferred
TransferRow.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tier 3 refactor — replaces the hardcoded ERC-20 / ERC-721 / ERC-1155 dispatch in `sync.ts` with a registry where each event type owns its own decoder file. Adding a new event (DEX swap, NFT mint, custom protocol log) is now a one-file addition under `apps/indexer/src/handlers/` plus one line in `handlers/index.ts` — no `sync.ts` edit, no growing `if/else` in the hot loop.
Layout
```
apps/indexer/src/handlers/
├── registry.ts EventHandler interface + register/dispatch + topicToAddress
├── erc20.ts ERC-20 Transfer (3 topics)
├── erc721.ts ERC-721 Transfer (4 topics, shares topic0 with ERC-20)
├── erc1155.ts TransferSingle (decoded) + TransferBatch (deferred null-return)
└── index.ts Side-effect import barrel
```
How shared topic0 works
The registry keys handlers by `topic0`; multiple handlers can share a `topic0` (the dispatcher walks them in registration order, keeps the first non-null result). That's how the ERC-20 vs ERC-721 split works: both register against `keccak("Transfer(address,address,uint256)")` but each null-returns when the topic count doesn't match its arity.
Behaviour parity
Identical to pre-refactor — same output rows for the same input logs. The refactor is type-safe end-to-end because `EventHandler.decode` returns the Drizzle-inferred `TransferRow` type.
Adding a new handler
```ts
// apps/indexer/src/handlers/uniswap-v2-swap.ts
import { register, type EventHandler } from "./registry.js";
const handler: EventHandler = {
topic0: "0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822",
decode: ({ log, contract, txHash }) => { /* … */ },
};
register(handler);
```
Then `import "./uniswap-v2-swap.js";` in `handlers/index.ts`. Zero `sync.ts` change.
Test plan