All packages

aave_v2_polygon

PaulieB14
v0.1.2/5 downloads/Repository

Package ref

aave-v2-polygon@v0.1.2

Run package

CLI

Run db_out from the command line.

substreams run aave-v2-polygon@v0.1.2 db_out -e polygon
Authenticate by running substreams auth or directly on thegraph.market (see docs).

README

AAVE V2 Polygon Substreams

Full-fidelity Substreams indexer for the AAVE V2 lending protocol on Polygon. Covers every on-chain event, aggregates reserve state and user positions into stores, and sinks to both PostgreSQL (operational) and ClickHouse (analytics) via substreams-sink-sql.

substreams.dev Network


What it indexes

Contracts tracked (verified via Etherscan API v2)

ContractAddressDeploy Block
LendingPool0x8dff5e27ea6b7ac08ebfdf9eb090f32ee9a30fcf12,687,245
LendingPoolConfigurator0x26db2B833021583566323E3b8985999981b9F1F312,687,253
IncentivesController0x357D51124f59836DeD84c8a1730D72B749d8BC2312,486,774

Events decoded

LendingPool — Deposit · Withdraw · Borrow · Repay · LiquidationCall · FlashLoan · ReserveDataUpdated · Swap (rate-mode) · ReserveUsedAsCollateralEnabled/Disabled · RebalanceStableBorrowRate · Paused / Unpaused

LendingPoolConfigurator — ReserveInitialized · CollateralConfigurationChanged · BorrowingEnabled/Disabled · ReserveActivated/Deactivated · ReserveFrozen/Unfrozen · ReserveFactorChanged

IncentivesController — RewardsAccrued · RewardsClaimed


Module pipeline

Polygon Block
     │
     ▼
map_aave_events           ← decodes all events from 3 contracts
     │
  ┌──┴──────────────────────────┐
  │        │          │         │
  ▼        ▼          ▼         ▼
store_   store_     store_   store_
reserve_ reserve_   user_    protocol_
state    lifetime   collat-  stats
(proto)  _totals    eral
         (bigint)   (string) (bigint)
  │        │          │
  └────────┴──────────┘
           │ (store deltas)
           ▼
         db_out ──► PostgreSQL  (14 normalized tables + 4 views)
                    ClickHouse  (14 tables + 6 materialized views)

Stores

StorePolicyKeys
store_reserve_stateset (proto)reserve:{address}
store_reserve_lifetime_totalsadd (bigint){reserve}:{metric}
store_user_collateralset (string){user}:{reserve}
store_protocol_statsadd (bigint)total:{action}, user:{addr}:{action}_count

ClickHouse Materialized Views

ViewPurpose
mv_hourly_ratesOHLC for liquidity rate, variable + stable borrow rates per reserve
mv_daily_volumesDeposit / withdrawal / borrow / repay volumes per reserve per day
mv_daily_flash_loansFlash loan volume, fee revenue, unique initiators per day
mv_daily_liquidationsCollateral seized, debt covered, liquidator activity per day
mv_user_borrow_modesStable vs variable preference per user per reserve
mv_rate_swap_dailyDaily stable ↔ variable swap frequency (market stress signal)

What this adds beyond the AAVE V2 subgraph

FeatureSubgraphThis Substreams
All lending events
Reserve configuration history
User collateral flags
Lifetime per-reserve totals
Incentives tracking
Rate OHLC (hourly)
Flash loan analytics
Liquidation analytics (daily)
Rate swap frequency tracking
ClickHouse analytical sink
No indexer availability issues
Sub-second historical backfill

Quick start

Prerequisites

brew install streamingfast/tap/substreams
brew install streamingfast/tap/substreams-sink-sql
substreams auth   # authenticate with The Graph Market

Stream live data

substreams gui aave-v2-polygon@v0.1.0

Run the PostgreSQL sink

# Start local databases
docker compose up -d

# Apply schema and start syncing
PG_DSN="psql://aave:aave_dev@localhost:5432/aave?sslmode=disable"
substreams-sink-sql setup "$PG_DSN" ./aave-v2-polygon-v0.1.0.spkg
substreams-sink-sql run  "$PG_DSN" ./aave-v2-polygon-v0.1.0.spkg

Run the ClickHouse sink

# Build the clickhouse spkg
substreams build substreams-clickhouse.yaml

CH_DSN="clickhouse://aave:aave_dev@localhost:9000/aave"
substreams-sink-sql setup "$CH_DSN" ./aave_v2_polygon_clickhouse-v0.1.0.spkg
substreams-sink-sql run  "$CH_DSN" ./aave_v2_polygon_clickhouse-v0.1.0.spkg

Build from source

git clone https://github.com/PaulieB14/aave-v2-polygon
cd aave-v2-polygon
substreams build

PostgreSQL tables

TableDescription
aave_depositsAll deposit events
aave_withdrawalsAll withdrawal events
aave_borrowsAll borrow events (stable + variable)
aave_repaysAll repay events
aave_liquidationsAll liquidation events
aave_flash_loansAll flash loan events
aave_rate_swapsStable ↔ variable rate mode swaps
aave_rebalancesStable borrow rate rebalances
aave_reserve_rate_historyFull rate history (every ReserveDataUpdated)
aave_incentives_accruedwMATIC reward accruals
aave_incentives_claimedwMATIC reward claims
aave_reservesCurrent reserve state (upserted)
aave_reserve_totalsLifetime cumulative totals per reserve
aave_user_collateralCurrent collateral flag per user per reserve

Plus convenience views: v_reserve_net_flows, v_reserve_rates_pct, v_flash_loan_leaderboard, v_liquidation_leaderboard.


License

Apache 2.0

Modules

Execution graph

6 modules
mapMain

db_out

Combines raw AAVE events with store deltas to produce a stream of DatabaseChanges for the substreams-sink-sql PostgreSQL sink. Tables: aave_deposits, aave_withdrawals, aave_borrows, aave_repays, aave_liquidations, aave_flash_loans, aave_rate_swaps, aave_rebalances, aave_reserve_rate_history, aave_reserves (state), aave_reserve_totals, aave_user_collateral, aave_incentives_accrued, aave_incentives_claimed.

from #12486774

Output

sf.substreams.sink.database.v1.DatabaseChanges

map

map_aave_events

Scans every Polygon block for events emitted by the AAVE V2 LendingPool, LendingPoolConfigurator, and IncentivesController contracts. Outputs a strongly-typed AaveEvents message.

from #12486774
store

store_protocol_stats

Protocol-wide rolling action counters and per-user event counts. Keys: "total:{action}", "user:{addr}:{action}_count", "liquidator:{addr}:count".

from #12486774

Store value

bigint

Update policy

add

store

store_reserve_lifetime_totals

Lifetime cumulative totals per reserve for deposits, withdrawals, borrows, repays, liquidations, and flash-loans (raw token amounts). Key: "{reserve}:{metric}" — e.g. "0xabc...:deposits".

from #12486774

Store value

bigint

Update policy

add

store

store_reserve_state

Tracks the latest configuration and rate data for each reserve. Updated by ReserveDataUpdated, ReserveInitialized, CollateralConfigurationChanged, ReserveFactor/Borrowing/Active/Frozen events. Key: "reserve:{address}".

from #12486774

Update policy

set

store

store_user_collateral

Tracks which reserves each user has enabled as collateral. Value: "{enabled}:{block}:{timestamp}". Key: "{user}:{reserve}".

from #12486774

Store value

string

Update policy

set