Skip to content

refactor(indexer): declarative event handler registry#49

Merged
github-actions[bot] merged 1 commit into
mainfrom
feat/indexer-handler-registry
May 10, 2026
Merged

refactor(indexer): declarative event handler registry#49
github-actions[bot] merged 1 commit into
mainfrom
feat/indexer-handler-registry

Conversation

@satyakwok
Copy link
Copy Markdown
Member

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

  • `pnpm turbo build` passes
  • `pnpm db:migrate` no-op on staging
  • Index a known-good block with mixed ERC-20 + ERC-721 + ERC-1155 transfers; verify output rows match what the pre-refactor code produced (compare `SELECT * FROM token_transfers WHERE block_height = X`)

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.
@github-actions github-actions Bot enabled auto-merge (squash) May 10, 2026 20:54
@github-actions github-actions Bot merged commit 1ecde73 into main May 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant