Indexing Factory Contracts
Overview
Factory contracts dynamically create child contracts, requiring special handling for indexing. This guide explains how to effectively track and index both factory and child contracts using Blockflow.
To index factory contracts effectively, you’ll need to:
Track the child contract creation events from the factory contract.
Use Blockflow’s sdk library to initialise and index the child contract.
Define event handlers to process and store event data.
Define schemas to specify the structure of the indexed data.
Prerequisites
Blockflow CLI
Docker
Key Concepts
Factory contracts have two key characteristics when indexing:
The factory contract has a fixed address
Child contracts have dynamic addresses that are created at runtime
Step-by-Step Implementation
1. Configure Resources in YAML
Define both the factory contract and its child contract template in your studio.yaml:
Note: The child contract (pair) doesn't have an address field since its address will be set dynamically.
2. Define Database Schema
Create a schema file to structure your indexed data:
3. Implement Event Handlers
To ensure your handlers use the correct types for event data and database entries, run Blockflow’s code generation tool:
This command will generate TypeScript types for your handlers based on the schema definitions.
Factory Contract Handler
This handler processes new child contract creation:
Child Contract Handler
This handler processes events from the child contracts:
4. Query Data with GraphQL
After completing the code, run the blockflow test command to fill up data. To access the local GraphQL API, add an API resource in the Resources section and set the YAML type to api.
And again hit blockflow test command. A local apollo server will start at http://localhost:4000/graphql


This guide covers configuring a factory contract to track child creation events, initialising child contracts, and storing event data in a managed database. By following this setup, you can index factory-child relationships for UniswapV2 and view the data through a GraphQL API. Read more at: https://github.com/blockflowlabs/cli-examples/tree/v2-examples/v2/uniswap
Last updated