Custom log filters

Custom log filters

The simplest way to tell Ponder which events to fetch is the contracts field in ponder.config.ts. Each item in this list corresponds to a single smart contract.

Custom log filters are a more flexible and powerful way to specify which event logs to include. To add a custom log filter, add an item to the filters field in ponder.config.ts.

Examples

All ERC20 Transfer events to a specific address

This filter includes all ERC20 Transfer events where the value of the to argument matches the specified address. Note that no contract address is specified, so this filter includes events emitted by any contract on the network.

ponder.config.ts
import type { Config } from "@ponder/core";
import { parseAbiItem } from "viem";
 
export const config: Config = {
  filters: [
    {
      name: "ERC20TransfersToBob",
      abi: "./abis/ERC20.json",
      filter: {
        event: parseAbiItem(
          "event Transfer(address indexed from, address indexed to, uint256, value)"
        ),
        args: {
          to: "0xa0ee7a142d267c1f36714e4a8f75612f20a79720",
        },
      },
    },
  ],
};
src/index.ts
import { ponder } from "@/generated";
 
ponder.on("ERC20TransfersToBob:Transfer", async ({ event }) => {
  // 0xa0ee7a142d267c1f36714e4a8f75612f20a79720
  console.log(event.params.to);
});

Events from multiple contracts with the same ABI

This filter includes all events emitted by any of the specified addresses. Note that for this to work as expected, all specified contracts must have the same ABI.

ponder.config.ts
import type { Config } from "@ponder/core";
 
export const config: Config = {
  filters: [
    {
      name: "ERC721Contracts",
      abi: "./abis/ERC721.json",
      filter: {
        address: [
          "0xa0ee7a142d267c1f36714e4a8f75612f20a79720",
          "0x06012c8cf97bead5deae237070f9587f8e7a266d",
          "0x7c40c393dc0f283f318791d746d894ddd3693572",
        ],
      },
    },
  ],
};
src/index.ts
import { ponder } from "@/generated";
 
ponder.on("ERC721Contracts:Transfer", async ({ event }) => {
  // One of the addresses specified in the filter
  console.log(event.log.address);
});

All Swap events between two users on two Uniswap pools

This filter includes all Swap events emitted by the two specified pools where the sender and recipient values match those specified.

ponder.config.ts
import type { Config } from "@ponder/core";
 
export const config: Config = {
  filters: [
    {
      name: "UniswapV3Pool",
      abi: "./abis/UniswapV3Pool.json",
      filter: {
        address: [
          // ETH-USDC 30bps
          "0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8",
          // ETH-USDC 5bps
          "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640",
        ],
        event: parseAbiItem(
          "event Swap(address sender, address recipient, int256 amount0, int256 amount1, uint160 sqrtPriceX96, uint128 liquidity, int24 tick)"
        ),
        args: {
          sender: "0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F",
          recipient: "0xf977814e90da44bfa03b6295a0616a897441acec",
        },
      },
    },
  ],
};
src/index.ts
import { ponder } from "@/generated";
 
ponder.on("UniswapV3Pool:Swap", async ({ event }) => {
  // One of the specified contract addresses
  console.log(event.log.address);
 
  // 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F
  console.log(event.params.sender);
 
  // 0xf977814e90da44bfa03b6295a0616a897441acec
  console.log(event.params.recipient);
});