Skip to content
ChainBrief

News from across Web3

How to Monitor DeFi Lending Positions Programmatically

Learn how to monitor DeFi lending positions programmatically: poll health factor, track pool events and oracle prices, and confirm alerts against on-chain state.

The ChainBrief Editors

How to Monitor DeFi Lending Positions Programmatically

Monitor a DeFi lending position by polling its pool health factor, tracking position events and oracle updates, and confirming alerts with a direct on-chain read.

On-chain state is the source of truth for liquidation, so monitoring needs to account for stale data and changing prices.

Aave V3 exposes account data through its Pool contract, and SparkLend uses the same Pool interface. SparkLend is a permissionless money market where users supply listed assets and borrow others against collateral. The methods below suit a product that needs timely alerts and a record of how a position changed.

Account data polling

Poll Pool.getUserAccountData for a direct snapshot; this suits services that need a dependable current health factor. The call returns totalCollateralBase, totalDebtBase, availableBorrowsBase, currentLiquidationThreshold, ltv and healthFactor. The base values use the oracle base currency with 8 decimals; healthFactor uses 18 decimals, and liquidation is allowed below 1.0.

Normalize healthFactor by 10^18 before comparing it with a threshold. If it is type(uint256).max, treat the account as having no debt; do not run it through ordinary arithmetic or chart it as a huge health factor. On RPC timeout or decode error, mark the snapshot stale and retry rather than reusing it as current.

Event subscription

Subscribe to Supply, Borrow, Repay, Withdraw and LiquidationCall to update a cached position; this suits products that need fast updates between polls. Store the event’s block number and transaction log index with each change, then apply events in order. Events show what changed, but they do not replace a fresh account-data read for an alert.

If the provider disconnects, backfill logs from the last processed block. If a reorg removes a log, roll back affected cached changes and replay the canonical chain. Keep the last confirmed block visible so downstream users can distinguish a caught-up cache from a lagging one.

Oracle watching

Watch the relevant oracle’s getAssetPrice values or feed updates to catch collateral and debt value changes; this suits alert systems that need to react when prices move between position events. A price update can change health factor even if the borrower makes no transaction.

Use the update to trigger a fresh Pool read, or recompute only when you have the account’s current balances, applicable liquidation thresholds and prices. If oracle data is stale or a feed update is missing, report the price input as uncertain and avoid presenting a recomputation as confirmed.

Indexer queries

Use a subgraph or other indexer for position history and account discovery; this suits dashboards and historical analysis. Query events and snapshots with their indexed block numbers, then compare the indexer’s latest block with the chain head.

Indexers can lag or reorganize. Before sending a liquidation risk alert, confirm the account with a direct RPC call to the Pool and include that read’s block number and timestamp in the alert record.

How often should a health factor be polled?

Poll often enough for the product’s alert latency, and use events and oracle updates to trigger additional reads. A fixed interval alone can miss a price-driven change between polls; record each read’s block so consumers can see its age.

How do you get liquidation alerts for a DeFi loan?

Set a product-defined warning threshold above 1.0, alert when a confirmed read crosses it, and treat a value below 1.0 as liquidation eligible. Say an account starts above the warning threshold: read its Pool data and store the block; subscribe to its events; then an oracle update arrives, so fetch account data again. If the new confirmed health factor crosses the threshold, send the alert with the block and value. If RPC is behind the oracle update, wait for a newer read instead of alerting from stale state.

  • Poll account data and handle the no-debt sentinel.
  • Subscribe to pool events and recover gaps or reorgs.
  • Trigger reads on oracle updates.
  • Use the indexer for history; confirm risk alerts by RPC.