Skip to main content

Indexing Event Logs

Event logs are emitted by smart contracts during transaction execution. The addLog() method allows you to subscribe to specific event logs from EVM-compatible blockchains.

addLog(options)

Subscribe to event logs emitted by some or all contracts in the network.

Options structure

{
  // Data requests
  address?: string[]
  topic0?: string[]
  topic1?: string[]
  topic2?: string[]
  topic3?: string[]
  range?: {from: number, to?: number}

  // Related data retrieval
  transaction?: boolean
  transactionLogs?: boolean
  transactionTraces?: boolean
}

Data request parameters

  • address - Set of contract addresses emitting the logs. Omit to subscribe to events from all contracts in the network.
  • topicN - Set of values for topic0 through topic3. Topics are indexed event parameters.
  • range - Range of blocks to consider (e.g., {from: 1000000, to: 2000000}).
Request related data to avoid making separate queries later.
  • transaction = true - Retrieve all parent transactions and add them to the transactions iterable within the block data. Also exposes them via the .transaction field of each log item.
  • transactionLogs = true - Retrieve all “sibling” logs (logs emitted by transactions that emitted at least one matching log). Exposed through the regular logs block data iterable and via .transaction.logs for matching logs.
  • transactionTraces = true - Retrieve execution traces for all transactions that emitted at least one matching log. Exposed through the regular traces block data iterable and via .transaction.traces.
Logs can also be requested by addTransaction() and addTrace() as related data.

Field selection

Use the setFields() method to select exactly which data fields to retrieve for each log. See the Field selection page for details.

Examples

Example 1: Fetch Gravatar events with parent transactions

Fetch NewGravatar and UpdateGravatar event logs from a specific contract, including log topics, data, and parent transaction inputs:
const processor = new EvmBatchProcessor()
  .setGateway("https://v2.archive.subsquid.io/network/ethereum-mainnet")
  .setRpcEndpoint("<my_eth_rpc_url>")
  .setFinalityConfirmation(75)
  .addLog({
    address: ["0x2e645469f354bb4f5c8a05b3b30a929361cf77ec"],
    topic0: [
      // topic: 'NewGravatar(uint256,address,string,string)'
      "0x9ab3aefb2ba6dc12910ac1bce4692cf5c3c0d06cff16327c64a3ef78228b130b",
      // topic: 'UpdatedGravatar(uint256,address,string,string)'
      "0x76571b7a897a1509c641587568218a290018fbdc8b9a724f17b77ff0eec22c0c",
    ],
    transaction: true,
  })
  .setFields({
    log: {
      topics: true,
      data: true,
    },
    transaction: {
      input: true,
    },
  });
TypeScript ABI modules generated by squid-evm-typegen provide event signatures/topic0 values as constants:
import * as gravatarAbi from './abi/gravatar'

// ...
  topic0: [
    gravatarAbi.events.NewGravatar.topic,
    gravatarAbi.events.UpdatedGravatar.topic,
  ],
// ...

Example 2: Filter by indexed address parameter

Fetch every Transfer(address,address,uint256) event where topic2 (destination address) matches vitalik.eth (0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045):
const processor = new EvmBatchProcessor()
  .setGateway("https://v2.archive.subsquid.io/network/ethereum-mainnet")
  .setRpcEndpoint("<my_eth_rpc_url>")
  .setFinalityConfirmation(75)
  .addLog({
    topic0: [
      // topic0: 'Transfer(address,address,uint256)'
      "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    ],
    topic2: [
      // vitalik.eth (padded to 66 chars)
      "0x000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045",
    ],
  })
  .setFields({
    log: {
      transactionHash: true,
    },
  });
Address padding for topics: The address in topic2 must be 66 characters long (including the 0x prefix) because Squid SDK expects Bytes32[] values. Pad shorter addresses with leading zeros:
const address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
const topic = "0x" + address.replace("x", "0").padStart(64, "0").toLowerCase();

Next Steps