Event handler context

Event handler functions receive two arguments: event and context.

The context object exposes the entities defined in schema.graphql and the contracts defined in ponder.config.ts.

type Context = {
  // Keyed by entity type names from schema.graphql
  entities: Record<string, EntityModel>;
  // Keyed by contract names from ponder.config.ts
  contracts: Record<string, ReadOnlyContract>;
};

Entities

context.entities an object-relational mapper for each entity in your schema.graphql. You can use these objects to insert and update entities that will be served automatically by the GraphQL server.

type EntityModel = {
  get: (id: string) => Promise<Entity | null>;
  insert: (id: string, obj: Omit<Entity, "id">) => Promise<Entity>;
  update: (id: string, obj: Partial<Omit<Entity, "id">>) => Promise<Entity>;
  delete: (id: string) => Promise<boolean>;
  upsert: (id: string, obj: Omit<Entity, "id">) => Promise<Entity>;
};

Contracts

context.contracts contains a ReadOnlyContract object for each contract in your ponder.config.ts. You can use these to read data directly from the smart contract using RPC calls.

⚠️

Reading directly from contracts is slow and expensive. If you're designing smart contracts that will be indexed by Ponder, try to emit events that include all the data you need.

Last updated on March 8, 2023