Backfill Script

Index historical transactions for the past 30 days across all protocol contracts.

Source: server/scripts/backfill.ts

Usage

# Standard usage (uses DATABASE_URL from .env)
npm run backfill

# With a custom database URL
DATABASE_URL="postgresql://..." npx tsx server/scripts/backfill.ts

Phases

Phase 1: Asset Transfers

Uses Alchemy's getAssetTransfers API to find inbound and outbound transfers for each protocol contract address.

Categories scanned:

  • EXTERNAL: ETH transfers

  • INTERNAL: Internal contract calls

  • ERC20: ERC-20 token transfers

  • ERC721: NFT transfers

For each transfer found, the script fetches the full transaction + receipt, decodes it, and stores it in the database.

Phase 1.5: Contract Token Transfers

Uses getAssetTransfers with the contractAddresses filter to find ERC-20 Transfer events emitted by protocol token contracts, even when the transaction target is a different contract.

This catches cases like ALUR tokens being transferred during an Eloqura liquidity operation: the transaction to is the Eloqura Router, but the ALUR token contract emits a Transfer event.

Phase 2: getLogs Scan

Scans eth_getLogs across all contract addresses for any events the previous phases might have missed.

circle-exclamation

Output

The script reports progress as it runs:

Duplicate Handling

The backfill is safe to run multiple times. Existing transactions are skipped via the unique txHash constraint: the script checks findUnique({ where: { txHash } }) before attempting to store.

Re-Decoding

If you update the decoder (new ABI fragments, action labels, etc.), run the re-decode script to update existing transactions:

This reads all stored transactions, re-decodes them through the updated ProtocolDecoder, and updates the protocol, actionType, functionName, and decodedData fields.

Last updated