Proxy Contract Example
This example demonstrates how to properly index events from a proxy contract pattern, specifically using Compound's Comptroller contract as an example.
Key Components
1. Entity Generation
The example uses solidity-events-to-typeorm
to automatically generate TypeORM entities from contract ABIs. This is configured in scripts/generate.ts
:
export const config: Config = {
output: {
path: outputPath,
entities: path.resolve(outputPath, './entities/'),
abis: path.resolve(outputPath, './abi/'),
},
migrations: {
path: path.resolve(outputPath, './migrations/'),
migrationName: 'InitialSchema',
schemaName: 'SQL_SCHEMA',
schemaVariable: true,
},
docs: {
path: path.resolve(outputPath, './docs/'),
},
contracts: {
ComptrollerProxy: {
abi: ComptrollerImplementation,
},
},
};
This generates:
- Entity classes for each event in the ABI (e.g.,
DistributedSupplierCompEntity_2de01bb4
) - Database migrations for creating the tables
- API documentation for the generated entities
2. ABI Merging
The example shows how to handle proxy contracts by merging the ABIs:
abi: mergeEventAbis(proxyAbi, implementationAbi);
How It Works
-
Entity Generation Process:
- Run
pnpm generate-entities
to process the ABIs - Creates TypeORM entities in
src/output/entities/
- Generates migrations in
src/output/migrations/
- All generated entities extend
BlockchainEventEntity
for common fields
- Run
-
Generated Entities Include:
DistributedSupplierCompEntity_2de01bb4
DistributedBorrowerCompEntity_8bcf5026
- Other event entities from the Comptroller contract
-
Auto-Generated Fields: Each entity automatically includes blockchain event fields like:
uniqueEventId
blockNumber
transactionHash
blockTimestamp
- Event-specific fields from the ABI
Setup and Usage
- Generate entities from ABIs:
pnpm generate-entities
- Copy
.env.example
file to.env
file and set up environment variables:
NODE_RPC_URL=<your-ethereum-node-url>
CHAIN_ID=1
- Install dependencies:
pnpm install
- Run the indexer:
pnpm start
The generated entities require no additional code to index events - they're automatically populated when matching events are found on-chain.