Documentation
API reference
Indexing functions

Indexing function API

Indexing functions are user-defined functions that receive blockchain data (a log, block, transaction, trace, or transfer) and insert data into the database. You can register indexing functions within any .ts file inside the src/ directory.

Registration

To register an indexing function, use the .on() method of the ponder object exported from "ponder:registry".

Values returned by indexing functions are ignored.

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("ContractName:EventName", async ({ event, context }) => {
  const { db, network, client, contracts } = context;
 
  // ...
});

Log event

Log events are specified with "ContractName:EventName".

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("ContractName:EventName", async ({ event, context }) => {
  // ...
});

The event argument contains the decoded log arguments and the raw log, block, and transaction.

type LogEvent = {
  name: string;
  args: Args;
  log: Log;
  block: Block;
  transaction: Transaction;
  // Enabled using `includeTransactionReceipts` in contract config
  transactionReceipt?: TransactionReceipt;
};

Log event arguments

The event.args object contains decoded log.topics and log.data decoded using Viem's decodeEventLog function.

/** Sample `args` type for an ERC20 Transfer event. */
type Args = {
  from: `0x${string}`;
  to: `0x${string}`;
  value: bigint;
};

Call trace event

Call trace events are specified using "ContractName.functionName()".

đź’ˇ

The includeCallTraces contract option must be enabled to use call trace events.

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("ContractName.functionName()", async ({ event, context }) => {
  // ...
});

The event argument contains the decoded call trace args and result and the raw trace, block, and transaction.

type TraceEvent = {
  name: string;
  args: Args;
  result: Result;
  trace: Trace;
  block: Block;
  transaction: Transaction;
  // Enabled using `includeTransactionReceipts` in contract config
  transactionReceipt?: TransactionReceipt;
};

Call trace event arguments

The event.args and event.result objects contain trace.input and trace.output decoded using Viem's decodeFunctionData and decodeFunctionResult functions, respectively.

Transaction event

Transaction events are specified using "AccountName:transaction:from" or "AccountName:transaction:to".

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("AccountName:transaction:from", async ({ event, context }) => {
  // ...
});

The event argument contains the raw block, transaction, and transaction receipt.

type TransactionEvent = {
  block: Block;
  transaction: Transaction;
  transactionReceipt: TransactionReceipt;
};

Transfer event

Native transfer events are specified using "AccountName:transfer:from" or "AccountName:transfer:to".

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("AccountName:transfer:from", async ({ event, context }) => {
  // ...
});

The event argument contains the transfer and raw block, transaction, and trace.

type TransferEvent = {
  transfer: {
    from: `0x${string}`;
    to: `0x${string}`;
    value: bigint;
  };
  block: Block;
  transaction: Transaction;
  trace: Trace;
  // Enabled using `includeTransactionReceipts` in account config
  transactionReceipt?: TransactionReceipt;
};

Block event

Block events are specified using "SourceName:block".

src/index.ts
import { ponder } from "ponder:registry";
 
ponder.on("SourceName:block", async ({ event, context }) => {
  // ...
});

The event argument contains the raw block.

type BlockEvent = {
  block: Block;
};

Event types

Event types
/** The block containing the transaction that emitted the log being processed. */
type Block = {
  /** Base fee per gas */
  baseFeePerGas: bigint | null;
  /** "Extra data" field of this block */
  extraData: `0x${string}`;
  /** Maximum gas allowed in this block */
  gasLimit: bigint;
  /** Total used gas by all transactions in this block */
  gasUsed: bigint;
  /** Block hash */
  hash: `0x${string}`;
  /** Logs bloom filter */
  logsBloom: `0x${string}`;
  /** Address that received this block’s mining rewards */
  miner: `0x${string}`;
  /** Block number */
  number: bigint;
  /** Parent block hash */
  parentHash: `0x${string}`;
  /** Root of the this block’s receipts trie */
  receiptsRoot: `0x${string}`;
  /** Size of this block in bytes */
  size: bigint;
  /** Root of this block’s final state trie */
  stateRoot: `0x${string}`;
  /** Unix timestamp of when this block was collated */
  timestamp: bigint;
  /** Total difficulty of the chain until this block */
  totalDifficulty: bigint | null;
  /** Root of this block’s transaction trie */
  transactionsRoot: `0x${string}`;
};
 
/** The transaction that emitted the log being processed. */
type Transaction = {
  /** Transaction sender */
  from: `0x${string}`;
  /** Gas provided for transaction execution */
  gas: bigint;
  /** Base fee per gas. */
  gasPrice?: bigint | undefined;
  /** Hash of this transaction */
  hash: `0x${string}`;
  /** Contract code or a hashed method call */
  input: `0x${string}`;
  /** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). */
  maxFeePerGas?: bigint | undefined;
  /** Max priority fee per gas (in wei). */
  maxPriorityFeePerGas?: bigint | undefined;
  /** Unique number identifying this transaction */
  nonce: number;
  /** Transaction recipient or `null` if deploying a contract */
  to: `0x${string}` | null;
  /** Index of this transaction in the block */
  transactionIndex: number;
  /** Value in wei sent with this transaction */
  value: bigint;
};
 
/** A confirmed Ethereum transaction receipt. */
type TransactionReceipt = {
  /** Address of new contract or `null` if no contract was created */
  contractAddress: Address | null;
  /** Gas used by this and all preceding transactions in this block */
  cumulativeGasUsed: bigint;
  /** Pre-London, it is equal to the transaction's gasPrice. Post-London, it is equal to the actual gas price paid for inclusion. */
  effectiveGasPrice: bigint;
  /** Transaction sender */
  from: Address;
  /** Gas used by this transaction */
  gasUsed: bigint;
  /** List of log objects generated by this transaction */
  logs: Log[];
  /** Logs bloom filter */
  logsBloom: Hex;
  /** `success` if this transaction was successful or `reverted` if it failed */
  status: "success" | "reverted";
  /** Transaction recipient or `null` if deploying a contract */
  to: Address | null;
  /** Transaction type */
  type: TransactionType;
};
 
/** The log being processed. */
type Log = {
  /** Globally unique identifier for this log (`${blockHash}-${logIndex}`). */
  id: string;
  /** The address from which this log originated */
  address: `0x${string}`;
  /** Contains the non-indexed arguments of the log */
  data: `0x${string}`;
  /** Index of this log within its block */
  logIndex: number;
  /** `true` if this log has been removed in a chain reorganization */
  removed: boolean;
  /** List of order-dependent topics */
  topics: [`0x${string}`, ...`0x${string}`[]] | [];
};
 
type Trace = {
  /** Globally unique identifier for this trace (`${transactionHash}-${tracePosition}`) */
  id: string;
  /** The type of the call. */
  type:
    | "CALL"
    | "CALLCODE"
    | "DELEGATECALL"
    | "STATICCALL"
    | "CREATE"
    | "CREATE2"
    | "SELFDESTRUCT";
  /** The address of that initiated the call. */
  from: Address;
  /** The address of the contract that was called. */
  to: Address | null;
  /** How much gas was left before the call. */
  gas: bigint;
  /** How much gas was used by the call. */
  gasUsed: bigint;
  /** Calldata input. */
  input: Hex;
  /** Output of the call, if any. */
  output?: Hex;
  /** Error message, if any. */
  error?: string;
  /** Why this call reverted, if it reverted. */
  revertReason?: string;
  /** Value transferred. */
  value: bigint | null;
  /** Index of this trace in the transaction. */
  traceIndex: number;
  /** Number of subcalls. */
  subcalls: number;
};

Context

The context argument passed to each indexing function contains database model objects and helper objects based on your config.

At runtime, the indexing engine uses a different context object depending on the network the current event was emitted on. The TypeScript types for the context object reflect this by creating a union of possible types for context.network and context.contracts.

type Context = {
  db: Database;
  network: { name: string; chainId: number };
  client: ReadOnlyClient;
  contracts: Record<
    string,
    { abi: Abi; address?: `0x${string}`; startBlock?: number; endBlock?: number; }
  >;
};

Database

The context.db object is a live database connection. Read more about writing to the database.

src/index.ts
import { ponder } from "ponder:registry";
import { persons, dogs } from "ponder:schema";
 
ponder.on("Neighborhood:NewNeighbor", async ({ event, context }) => {
  await context.db.insert(persons).values({ name: "bob", age: 30 });
  await context.db.insert(dogs).values({ name: "jake", ownerName: "bob" });
  const jake = await context.db.find(dogs, { name: "jake" });
});

Network

The context.network object includes information about the network that the current event is from. The object is strictly typed according to the networks you defined in your config.

src/index.ts
ponder.on("UniswapV3Factory:Ownership", async ({ event, context }) => {
  context.network;
  //      ^? { name: "mainnet", chainId 1 } | { name: "base", chainId 8453 }
 
  if (context.network.name === "mainnet") {
    // Do mainnet-specific stuff!
  }
});

Client

See the Read contract data guide for more details.

Contracts

See the Read contract data guide for more details.

"setup" event

You can also define a setup function for each contract that runs before indexing begins.

  • The indexing function does not receive an event argument, only context.
  • If you read from contracts in a "setup" indexing function, the blockNumber for the request is set to the contract's startBlock.

For example, you might have a singleton World record that occasionally gets updated in indexing functions.

src/index.ts
import { ponder } from "ponder:registry";
import { world } from "ponder:schema";
 
ponder.on("FunGame:NewPlayer", async ({ context }) => {
  await context.db
    .insert(world)
    .values({ id: 1, playerCount: 0 })
    .onConflictDoUpdate((row) => ({
      playerCount: row.playerCount + 1,
    }));
});

Without the "setup" event, you need to upsert the record in each indexing function that attempts to use it, which is clunky and bad for performance. Instead, use the "setup" event to create the singleton record once at the beginning of indexing.

src/index.ts
import { ponder } from "ponder:registry";
import { world } from "ponder:schema";
 
ponder.on("FunGame:setup", async ({ context }) => {
  await context.db.insert(world).values({
    id: 1,
    playerCount: 0,
  });
});
 
ponder.on("FunGame:NewPlayer", async ({ context }) => {
  await context.db
    .update(world, { id: 1 })
    .set((row) => ({
      playerCount: row.playerCount + 1,
  }));
});