Introduction

This guide shows you how to stream real-time DEX activity from Polygon into your own database. You’ll tap into the most active decentralized exchanges on the network: Uniswap, Quickswap, Balancer V2, Sushi, and Curve. The template captures a range of onchain events including swaps, liquidity changes, and more. By the end, you’ll have a working pipeline that decodes relevant contract event logs and stores structured trade data for analysis or downstream use.

Prerequisites

You’ll need:

  • A Neighborhood API key. To get access, email hello@indexing.co
  • curl or Postman
  • An active postgres database

⚙️ What You’ll Build

A pipeline that:

  1. Listens to the majority of Polygon DEX contracts and their events
  2. Decodes them with the event metadata
  3. Streams transformed data into a unified postgres table

🔄 Step 1: Add the Transformation Logic

We’ll want to decode and return all events + unified swaps for known DEXs.

View the full code here

✍️ Step 2: Create the Transformation

curl https://app.indexing.co/dw/transformations/polygon_dex_events \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: <your-api-key>' \
  -F code=@./polygon_dex_events.js

💽 Step 3: Create the Database

…explainer…

create table events (
  contract_address text,
  transaction_hash text,
  log_index: int,
  method: text,
  timestamp: timestamptz,
  decoded: jsonb,
  primary key (contract_address, transaction_hash, log_index)
);
create index on events (contract_address, method, timestamp);

create table swaps (
  pool_address text,
  transaction_hash text,
  log_index int,
  timestamp timestamptz,
  token_in_address text,
  token_out_address text,
  token_in_amount numeric,
  token_out_amount numberic,
  primary key (pool_address, transaction_hash, log_index)
);
create index on swaps (token_in_address, timestamp);
create index on swaps (token_out_address, timestamp);

🔗 Step 4: Create the Pipeline

curl https://app.indexing.co/dw/pipelines \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: <your-api-key>' \
  -d '{
    "name": "polygon_dex_events",
    "transformation": "polygon_dex_events",
    "networks": [
      "polygon"
    ],
    "enabled": true,
    "delivery": {
      "adapter": "POSTGRES",
      "connectionUri": "postgres://...",
      "tableMap": {
        "events": ["contract_address", "transaction_hash", "log_index"],
        "swaps": ["pool_address", "transaction_hash", "log_index"]
      }
    }
  }'

🥺 Step 5: Test the Stream

Once live, your database should receive events like:

You can test this manually and end-to-end using:

curl --location 'https://app.indexing.co/dw/pipelines/polygon_dex_events/test/polygon/XXX' -H 'X-API-KEY: <your-api-key>'

🚀 Wrap-Up

You’ve now set up a real-time stream of Polygon DEX events using The Neighborhood. The data you’re receiving is contains events from the most used Polygon DEXs. In case you want to add new DEXs we encourage you to reach out to us or adjust the template.

Need help or want to go further?