Apibara blog

News and updates about Apibara

Introducing factory mode

This week, we released a new version of all our integrations to support updating the stream filter while indexing data. This was the most requested feature, and we're glad it's finally available to users.

Factory mode indexes data from a dynamic set of smart contracts. For example, Uniswap V2-like DEXes that deploy a new smart contract for each pair can use factory mode to index events from all pairs without knowing the pairs' addresses in advance.

const FACTORY = "0x...";
const PAIR_CREATED = selector("PairCreated");

export const config = {
  filter: {
    header: { weak: true },
    events: [
      {
        fromAddress: FACTORY,
        keys: [PAIR_CREATED],
        includeReceipt: false,
      },
    ],
  },
  // standard configuration.
  streamUrl,
  startingBlock,
};

const UPGRADED = selector("Upgraded");
const ADMIN_CHANGED = selector("AdminChanged");
const BURN = selector("Burn");
const MINT = selector("Mint");
const SWAP = selector("Swap");
const SYNC = selector("Sync");
const APPROVAL = selector("Approval");
const TRANSFER = selector("Transfer");

export function factory({ header, events }) {
  // Create a filter with events for all pools created in this block.
  const poolEvents = events.flatMap(({ event }) => {
    const pairAddress = event.data[2];
    return [
      UPGRADED,
      ADMIN_CHANGED,
      BURN,
      MINT,
      SWAP,
      SYNC,
      APPROVAL,
      TRANSFER,
    ].map((eventKey) => ({
      fromAddress: pairAddress,
      keys: [eventKey],
      includeReceipt: false,
    }));
  });

  // Insert data about the pools created in this block.
  // Values returned in `data` are handled like values returned from
  // `transform`.
  const pools = events.flatMap(({ event, transaction }) => {
    const token0 = event.data[0];
    const token1 = event.data[1];
    const pairAddress = event.data[2];
    return {
      type: "PairCreated",
      createdAt: header.timestamp,
      createdAtBlockNumber: +header.blockNumber,
      createdAtTxHash: transaction.meta.hash,
      pairId: pairAddress,
      token0Id: token0,
      token1Id: token1,
    };
  });

  return {
    filter: {
      header: { weak: true },
      events: poolEvents,
    },
    data: pools,
  };
}

export default function transform({ header, events }) {
  return events.flatMap(({ event, transaction }) => {
    // do something we the events.
  });
}

Export the factory callback from your script to enable factory mode. In this mode, the filter specified in the configuration is used to stream data passed to the factory function. The factory callback can return a filter used to stream data in the main stream. Filters from subsequent factory function invocations are merged into a single stream. On Starknet, you can index smart contracts by class hash by streaming deploy transactions. This can be used, for example, to track a specific wallet version. Head over to the documentation to learn about factory mode.

Under the hood, factory mode streams data from two parallel and synchronized DNA streams: one stream for factory data and one for the main data. The factory callback is called before the main transform function. When the the main filter is updated, the indexer restarts from the current block to include all events in that block, even events included in the new filter.

If you are running indexers in production: The new version of the indexers stores its indexing state in a different way from the previous version. The persisted state from previous versions is not compatible with the new version. To update the indexers safely:

  • stop your indexers completely.
  • note at which block the indexer stopped.
  • start the new indexers using that block number as the starting block.

If you are a dedicated DNA customer: We will roll out the new DNA server version after it has been sufficiently tested in production. If you want to start using factory mode sooner, contact us, and we will update your instance.

Status server improvements

Some users reported timeout errors when requesting data from the status server. This bug has been fixed.

Sync metrics

Indexers now publish OpenTelemetry metrics about the sync status. You can use these metrics to build dashboards for your indexers.

PostgreSQL sink fixes

We fixed an issue with the PostgreSQL sink that would cause the indexer to stall if the connection to the DB was closed.

Apibara

Apibara is the fastest platform to build production-grade indexers that connect onchain data to web2 services.

© 2024 GNC Labs Limited. All rights reserved.