Wildcard Transfer Example

Wildcard Transfer Example

This example demonstrates different approaches to indexing ERC20 transfer events, showcasing both auto-generated and custom entity handling.

The indexer will:

  • Automatically store all Transfer events in the generated entity table
  • Log when it sees DAI or USDT transfers
  • Create a new example entity for each transfer event

Key Components

1. Auto-Generated Entity Handling (/output folder)

  • Contains entities automatically generated by the solidity-events-to-typeorm package
  • The TransferEntity_fab013d9 is auto-generated from the Transfer event ABI
  • These entities are automatically populated - no custom listener required!
  • Extends BlockchainEventEntity which provides common blockchain event fields

2. Custom Entity Handling (/custom folder)

  • Contains manually created entities for custom data storage
  • Demonstrates how to store additional/transformed data alongside auto-generated entities
  • In this example, ExampleEntity is used to demonstrate custom storage

3. Event Listeners

The example implements two different listeners:

  1. Simple Console Logger (subscriptions.ts):
onEvent('*:Transfer', {
  onIndex: async (payload) => {
    // Filters and logs USDT and DAI transfers
  },
});
  1. Custom Entity Storage (transfer-subscriber.ts):
@Injectable()
export class TransferSubscriber extends AbstractEventSubscriber {
  readonly eventPattern = '*:Transfer';
 
  async onIndex(payload: LogEvent, context: LogContext): Promise<void> {
    // Creates and stores a new ExampleEntity for each transfer
  }
}

4. Configuration

The indexer is configured in indexerConfig to:

  • Listen to Transfer events from any contract
  • Start from block 22229952
  • Track transfers from specific contracts (DAI and USDT)
  • Enable both auto-generated and custom entity storage

How It Works

  1. Auto-Generated Indexing:

    • Transfer events are automatically indexed into transfer_fab013d9 table
    • No custom code needed for basic transfer data storage
    • Handles all common event fields (block number, transaction hash, etc.)
  2. Custom Processing:

    • The subscriptions.ts listener filters and logs DAI/USDT transfers
    • The TransferSubscriber demonstrates custom entity creation
    • Each transfer triggers storage of a new example entity with random data

Example Transactions

You can view example transfers that this indexer should print "DAI" or "USDT" for here:

Setup and Usage

  1. Set up environment variables - copy .env.example to .env and replace with your variables

  2. Install dependencies:

pnpm install
  1. Run the indexer:
pnpm start

Project Structure

src/
├── output/                 # Auto-generated entities and migrations
│   └── entities/          # Contains TransferEntity_fab013d9
├── custom/                 # Custom entities and migrations
│   └── entities/          # Contains ExampleEntity
├── subscriptions.ts       # Basic transfer event listener
├── transfer-subscriber.ts # Custom entity storage listener
└── indexer.config.ts     # Indexer configuration

This example demonstrates the flexibility of the indexer, showing both automatic event indexing and custom data processing approaches.