Skip to content

Latest commit

 

History

History

uniswap-v2-events-from-all-contracts-with-usd-price

Example: Track Uniswap v2 swaps with USD price for all contracts across all chains

This example demonstrates how to index Swap events from a Uniswap fork on all chains and all contracts with USD price of the tokens swapped at the time of the swap.

In this example you learn:

  • Tracking newly deployed pool contracts on known factories.
  • Using workers to Re-calculate Pools stats such as total volume for last 7 days, last 1 hour, etc. This job makes sure that the stats are always up-to-date and not skewed from reality when events are processed multiple times due to retries.
  • Index all "Swap" events and store USD price at the time of swap. This processor also "optimistically" increments rolling stats of aPool such as total volume for last 7 days.

Table of Contents

Getting Started

1️⃣ Clone this repo:

git clone git@github.com:flair-sdk/examples.git my-indexer
cd aggregate-protocol-fees-in-usd

2️⃣ Install packages and authenticate:
pnpm i
pnpm flair auth

3️⃣ Set the namespace and config.json:

config.dev.json and config.prod.json are sample configs for dev and prod clusters.

Set a globally unique namespace in each config file (recommended to use {ORG_NAME}-{ENV}; e.g sushiswap-dev or sushiswap-prod) and then run:

# Setting configs for dev testing
cp config.dev.json config.json

# Or setting it for production
cp config.prod.json config.json

4️⃣ Generate manifest.yml and deploy:
pnpm generate-and-deploy

5️⃣ Backfill certain contracts or block ranges:
# Index last recent 10,000 blocks of a contract like this:
pnpm flair backfill --chain 1 --address 0xbD6C7B0d2f68c2b7805d88388319cfB6EcB50eA9 -d backward --max-blocks 10000

Or you can backfill for a specific block number, if you have certain events you wanna test with:

pnpm flair backfill --chain 1 -b 17998797

Or backfill for the recent data in the last X minutes:

pnpm flair backfill --chain 1 --min-timestamp="5 mins ago" -d backward

6️⃣ Look at the logs:
pnpm flair logs --full -tag Level=warn
pnpm flair logs --full -tag TransactionHash=0xXXXXX
pnpm flair logs --full -tag ProcessorId=swap-events
pnpm flair logs --full -tag ProcessorId=swap-events --watch

7️⃣ Explore the data in playground:

Visit the playground and run the following query in Examples section.

Not seeing any data?

This is most probably because initially no Pool is being tracked. The indexer will always look at the "filterGroups" defined in your manifest, and any address/topic defined in ingestion or processing filter group will be tracked.

You have few ways to track Pools:

  • Use the correct factory address in factories.csv and run pnpm generate-and-deploy again. Then do a full (or limited) backfill for that factory to capture and track all the pools:
pnpm flair backfill --chain 1 --address 0x1F984__FactoryAddress__a31F984 --max-blocks 1000000 --provisioned
  • Or, manually add few Pool addresses to the processing filterGroup:
# manifest.yml.mustache
...
processing:
  filterGroups:
    - id: addresses 
      addresses:
        - chainId: '*'
          address: '0x0000000000'
  • Or track all Uniswap-compatible pools, remember this might be very expensive on bigger chains like Polygon or Arbitrum:
# manifest.yml.mustache
...
processing:
  filterGroups:
    - id: addresses 
      addresses:
        - chainId: '*'
          address: '*'

Examples

Get all entity types total count

query {
  sql(
    query: """
    SELECT
        COUNT(*),
        entityType
    FROM
        entities
    WHERE
        namespace = 'my-awesome-swapper'
    GROUP BY entityType
    """
  ) {
    stats {
      elapsedMs
    }
    rows
  }
}

Order all Pools by total USD volume in last 7 days

query {
  sql(
    query: """
      SELECT
        *
      FROM
        entities
      WHERE
        namespace = 'my-awesome-swapper' AND
        entityType = 'Pool' AND
        last7DVolumeUsd IS NOT NULL
      ORDER BY last7DVolumeUsd DESC
      LIMIT 100
    """
  ) {
    stats {
      elapsedMs
    }
    rows
  }
}

FAQ

Q: How do I enable/disable real-time ingestion for indexer?
A: For each indexer defined in config.json, you can enable/disable it via the enabled: true/false flag. Remember to run pnpm generate-and-deploy for the changes to apply on the cluster.

Q: Not seeing any data?
Refer to Not seeing any data? section above.