Skip to content

About

Stablecoin APY indexer for AAVE v3, Morpho Blue, and Maple v2 on Ethereum mainnet

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Blockchain Lending Markets — Stablecoin APY Indexer

Indexes stablecoin supply and borrow APYs from AAVE v3, Morpho Blue, and Maple v2 on Ethereum mainnet. Uses Ponder to process on-chain events via Alchemy RPC and persists snapshots to Postgres.

Objective

Track how stablecoin yields evolve across DeFi lending protocols in a single, queryable dataset. Supports three use cases:

  • Historical research — analyze APY trends over time across protocols
  • Live monitoring — latest yield snapshot per market, always fresh
  • Alerting basis — threshold queries over recent data (alerting logic is downstream)

Tracked stablecoins: USDC, USDT, DAI, USDS, FRAX, LUSD, GHO, crvUSD, PYUSD.

How each protocol is indexed

AAVE v3

AAVE exposes rates directly in ReserveDataUpdated events emitted by the Pool contract on every state change (deposit, withdraw, borrow, repay). The event carries liquidityRate (supply) and variableBorrowRate (borrow) as per-year rates scaled by RAY (1e27). The handler filters to stablecoin reserves and computes APY via per-second compounding:

APY = (1 + rate / 1e27 / SECONDS_PER_YEAR) ^ SECONDS_PER_YEAR - 1

Trigger: every ReserveDataUpdated event for a tracked stablecoin.

Morpho Blue

Morpho Blue is a permissionless lending primitive — each market is defined by a (loanToken, collateralToken, oracle, IRM, LLTV) tuple. The AccrueInterest event provides the prevBorrowRate (per-second, WAD-scaled). The handler reads post-accrual state (totalSupplyAssets, totalBorrowAssets, fee) via an eth_call to derive supply APY:

borrowAPY  = (1 + prevBorrowRate / 1e18) ^ SECONDS_PER_YEAR - 1
utilization = totalBorrowAssets / totalSupplyAssets
supplyAPY  = borrowAPY * utilization * (1 - fee)

Markets are auto-discovered on first encounter via idToMarketParams — no slow historical event scan needed. Only markets where the loan token is a tracked stablecoin produce snapshots.

Trigger: every AccrueInterest event for a stablecoin-loan market with non-zero TVL.

Maple v2

Maple pools are curated (static list in config/maple-pools.ts) since Maple doesn't expose a clean factory event. APY is computed two ways:

  • Projected (canonical supply_apy): issuanceRate * SECONDS_PER_YEAR / totalAssets — forward-looking, derived from the LoanManager's continuous interest accrual rate
  • Realized (supply_apy_realized): annualized share-price growth between consecutive snapshots

Maple is lender-only from the pool perspective, so borrow_apy is always NULL.

Trigger: Deposit, Withdraw, IssuanceParamsUpdated events + a periodic block handler every ~50 blocks (~10 min).

Quick start

cp .env.example .env
# Edit .env — set PONDER_RPC_URL_1 to your Alchemy mainnet endpoint

docker compose up

This starts Postgres on localhost:5432 and the Ponder indexer. Backfill time depends on your Alchemy tier and PONDER_START_BLOCK (default: 19000000, ~Jan 2024).

For a fast test (~5 min sync), set in .env:

PONDER_START_BLOCK=<current_block - 500>

Query data

docker compose exec postgres psql -U ponder -d ponder

Latest APY per market:

SELECT DISTINCT ON (market_id) market_id, supply_apy, borrow_apy,
       to_timestamp(block_timestamp) AS as_of
FROM apy_snapshot ORDER BY market_id, block_number DESC;

Cross-protocol USDC yields over time:

SELECT date_trunc('hour', to_timestamp(a.block_timestamp)) AS hour,
       m.protocol, AVG(a.supply_apy) AS avg_supply_apy
FROM apy_snapshot a JOIN market m ON m.id = a.market_id
WHERE m.loan_asset = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
GROUP BY 1, 2 ORDER BY 1, 2;

High-yield markets (> 10%):

WITH latest AS (
  SELECT DISTINCT ON (market_id) market_id, supply_apy
  FROM apy_snapshot ORDER BY market_id, block_number DESC
)
SELECT l.market_id, m.protocol, l.supply_apy
FROM latest l JOIN market m ON m.id = l.market_id
WHERE l.supply_apy > 0.10 ORDER BY l.supply_apy DESC;

Ponder also exposes GraphQL at localhost:42069/graphql and /health at localhost:42069/health.

Development

npm test          # 44 unit + handler-logic tests
npm run smoke     # end-to-end check (requires running indexer)

Adding a stablecoin: add entry to config/stablecoins.ts, redeploy. Adding a Maple pool: add entry to config/maple-pools.ts, redeploy.

Project structure

ponder.config.ts          — contract declarations, networks, block handlers
ponder.schema.ts          — DB schema (stablecoin, market, apy_snapshot, maple_pool_state)
config/                   — stablecoin allowlist, Maple pool list, contract addresses
src/lib/                  — pure utilities (APY math, retry, market-id)
src/handlers/<proto>-logic.ts — pure protocol logic (no Ponder deps), fully tested
src/handlers/<proto>.ts       — thin Ponder binding (event registration + DB adapter)
src/api/index.ts          — /health endpoint
tests/                    — unit, handler-logic, and smoke tests

About

Stablecoin APY indexer for AAVE v3, Morpho Blue, and Maple v2 on Ethereum mainnet

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages