> ## Documentation Index
> Fetch the complete documentation index at: https://docs.indexing.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Polygon DEX Events

## 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, sign up at [**accounts.indexing.co**](https://accounts.indexing.co) or email [**hello@indexing.co**](mailto: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](https://github.com/indexing-co/docs/blob/main/examples/transformations/polygon_dex_events.js)

## Step 2: Create the Transformation

```bash theme={null}
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

```sql theme={null}
create table dex_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 dex_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 numeric,
  from_address text,
  to_address text,
  primary key (pool_address, transaction_hash, log_index)
);

create index on swaps (token_in_address, timestamp);
create index on swaps (token_out_address, timestamp);
```

The `contract_address` and `pool_address` represents the swap pool for `dex_events` and `swaps`, respectively. The above provides a single, primary key index for each table, but you may find you want other indexes added based on scale and query patterns.

## Step 4: Create the Pipeline

```bash theme={null}
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": {
        "dex_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:

```json theme={null}
{
  "pool_address": "0x4e3288c9ca110bcc82bf38f09a7b425c095d92bf",
  "transaction_hash": "0xf087c159221c15b751979539d0db9c45b0e509c3f80cc988c9cebc51b7b69e40",
  "log_index": 504,
  "timestamp": "2025-07-16T23:21:42.000Z",
  "token_in_address": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
  "token_in_amount": "2323",
  "token_out_address": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
  "token_out_amount": "2323"
}
```

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

```bash theme={null}
curl 'https://app.indexing.co/dw/pipelines/polygon_dex_events/test/polygon/74046728' -H 'X-API-KEY: <your-api-key>' -X POST
```

## 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?

* Reach out at [indexing.co/contact](https://www.indexing.co/contact)
* Or email us at [hello@indexing.co](mailto:hello@indexing.co)
