> ## 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 Money Market Events

## Introduction

In this guide, you'll build a real-time pipeline to track lending and borrowing activity from Polygon money markets AAVE and Compound V3. By listening to core protocol contracts, you'll capture events like deposits, borrows, repayments, and liquidations, then decode and stream them directly into your database. By the end, you'll have a working pipeline that decodes relevant contracts 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 money market contracts and their events
2. Decodes them with the event metadata
3. Streams transformed data into a unified postgres table

## Step 1: Create a Contract Filter

Then create the [filter](https://docs.indexing.co/guide/filters/add):

These are the contract addresses we'll want to listen to:

```text theme={null}
0x794a61358D6845594F94dc1DB02A252b5b4814aD
0xF25212E676D1F7F89Cd72fFEe66158f541246445
0xaeB318360f27748Acb200CE616E389A6C9409a07
```

```bash theme={null}
curl https://app.indexing.co/dw/filters/polygon_money_market_addresses \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: <your-api-key>' \
  -d '{
    "values": [
      "0x794a61358D6845594F94dc1DB02A252b5b4814aD",
      "0xF25212E676D1F7F89Cd72fFEe66158f541246445",
      "0xaeB318360f27748Acb200CE616E389A6C9409a07"
    ]
  }'
```

## Step 2: Add the Transformation Logic

We'll want to decode and return a unified set of in and out flows for known markets.

View the full code [here](https://github.com/indexing-co/docs/blob/main/examples/transformations/polygon_money_markets.js)

## Step 3: Create the Transformation

```bash theme={null}
curl https://app.indexing.co/dw/transformations/polygon_money_market_events \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: <your-api-key>' \
  -F code=@./polygon_money_market_events.js
```

## Step 4: Create the Database

```sql theme={null}
create table money_market_events (
  pool_address text,
  transaction_hash text,
  log_index int,
  method text,
  timestamp timestamptz,
  wallet_address text,
  token_address text,
  amount numeric,
  primary key (pool_address, transaction_hash, log_index)
);
create index on money_market_events (wallet_address, timestamp desc);
```

The `pool_address` represents the liquidity pool that the event is tied to and `method` can be used to identify specific events. The above provides a single, primary key index, but you may find you want other indexes added based on scale and query patterns.

### Example Queries

Identifying USD volume by liquidity pool within the last hour:

```sql theme={null}
select
  pool_address,
  sum(amount) / pow(10, 6) as volume
from
  money_market_events
where
  token_address in (
    '0x2791bca1f2de4661ed88a30c99a7a9449aa84174', -- USDC
    '0xc2132d05d31c914a87c6611c10748aeb04b58e8f' -- USDT
  )
  and timestamp > now() - interval '1 hour'
group by pool_address
order by sum(amount) desc;
```

## Step 5: 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_money_market_events",
    "transformation": "polygon_money_market_events",
    "filter": "polygon_money_market_addresses",
    "filterKeys": [
      "pool_address"
    ],
    "networks": [
      "polygon"
    ],
    "enabled": true,
    "delivery": {
      "adapter": "POSTGRES",
      "connectionUri": "postgres://...",
      "table": "money_market_events",
      "uniqueKeys": ["pool_address", "transaction_hash", "log_index"]
    }
  }'
```

## Step 6: Test the Stream

Once live, your database should receive events like:

```json theme={null}
{
  "pool_address": "0x794a61358d6845594f94dc1db02a252b5b4814ad",
  "transaction_hash": "0xcaefff01eec692ef13503959437dfac7e1aeee9ecaa5a5d7b640a5dda30cccc6",
  "log_index": 707,
  "timestamp": "2025-07-16T23:12:14.000Z",
  "wallet_address": "0xadeccaba96f86f2eb4fbe219be39a87db7534ddb",
  "token_address": null,
  "amount": "524130000"
}
```

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

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

## Wrap-Up

You've now set up a real-time stream of Polygon Money Market events using The Neighborhood. The data you're receiving is contains events from the most used Polygon Money Markets, AAVE and Compound V3. 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)
