Backend Services

Build indexers, analytics pipelines, and data warehouses that track Gearbox protocol state over time.

Overview

Backend services typically need to:

  1. Capture historical snapshots at specific blocks

  2. Index events for efficient state reconstruction

  3. Track rates, values, and utilization over time

  4. Store and query historical data

This guide shows patterns for each requirement.


Historical Snapshots

WHY: Track how protocol state changes over time for analytics, reporting, and historical queries.

What to Snapshot

Data
Source
Change Frequency

Pool rates

PoolState

Every block with activity

Pool liquidity

PoolState

Every deposit/borrow

Quota utilization

QuotaKeeperState

Every position change

Credit account state

CreditAccountData

Every account operation

Token prices

PriceOracle

External feed updates

How to Query at Specific Blocks

Compressors support querying at historical blocks using viem's blockTag or blockNumber:

Archive Node Requirements

Historical queries require an archive node. Standard nodes only keep recent state (~128 blocks).

RPC providers with archive access:

  • Alchemy (archive add-on)

  • Infura (archive add-on)

  • QuickNode (archive plans)

  • Self-hosted Erigon/Reth

Snapshot Pattern


Event Indexing

WHY: Events provide efficient tracking of specific state changes without polling.

Key Events

Credit Facade emits events for all account operations:

Event
When Emitted
Key Data

OpenCreditAccount

Account opened

owner, creditAccount, borrowAmount

CloseCreditAccount

Account closed

creditAccount

LiquidateCreditAccount

Account liquidated

creditAccount, liquidator, remainingFunds

StartMultiCall

Multicall begins

creditAccount

FinishMultiCall

Multicall ends

creditAccount

Pool emits events for liquidity changes:

Event
When Emitted
Key Data

Deposit

LP deposits

sender, owner, assets, shares

Withdraw

LP withdraws

sender, receiver, assets, shares

Borrow

Credit Manager borrows

creditAccount, amount

Repay

Debt repaid

creditAccount, amount, profit, loss

Watching Events with viem

Fetching Historical Events

For backfilling, fetch events in block ranges:


State Tracking

WHY: Build complete account or pool history over time by combining events and snapshots.

Credit Account Lifecycle

Track an account from open to close:

Combining Events and Snapshots

For complete state reconstruction:


Rate History

WHY: Analytics on yield, utilization trends, and rate changes over time.

Rates to Track

Rate
Source
Notes

Supply APY

pool.supplyRate

RAY scaled (10^27)

Base borrow APR

pool.baseInterestRate

RAY scaled

Quota rates

quotaKeeper.tokens[].rate

Per-token, RAY scaled

Utilization

Calculated

(totalAssets - availableLiquidity) / totalAssets

Polling Pattern

Rate Conversion

Convert RAY-scaled rates to annual percentages:


Complete Example: Simple Indexer


Next Steps

Last updated