feat(pegs): L2 oracle health monitor — stables/oracles.py (#301)#306
Open
spalen0 wants to merge 3 commits into
Open
feat(pegs): L2 oracle health monitor — stables/oracles.py (#301)#306spalen0 wants to merge 3 commits into
spalen0 wants to merge 3 commits into
Conversation
Layer 2 of peg monitoring: an hourly script that watches the on-chain oracles our lending markets actually liquidate on, driven by the shared PeggedAsset registry. Stacks on the foundation branch (#299 / #305). Per Chainlink-backed asset (USDC, USDT, USDS, USDe, cbBTC, LBTC): - staleness (now - updatedAt > heartbeat + buffer), - round sanity / monotonicity (positive answer, completed round, answeredInRound >= roundId, non-decreasing roundId vs cached run), - deviation from peg (oracle price vs PegTarget), - oracle <-> market divergence (Chainlink vs DeFiLlama) — the liquidation signal. Per-check logic is pure (Alert | None) so forced-stale / forced-divergence / off-peg / round-malfunction cases are unit-tested without a chain. Rate / fundamental oracles: generic monotonicity + delta-vs-cached handler (apyusd approach); a monotonic/capped decrease is CRITICAL (per #196). LBTC Redstone is already covered by a Tenderly alert, so it is documented in TENDERLY_COVERED and not double-polled; gap-analysis notes included. Foundation: add ChainlinkFeed.quote (PegTarget) so BTC-denominated feeds (LBTC/BTC) convert to a USD basis for divergence; LBTC feed marked quote=BTC. Wired into the hourly profile in automation/jobs.yaml (renders; 24 tasks). Validation: ruff + mypy clean (new files); 52 peg tests + 13 automation tests pass; live mainnet smoke run reads all 6 feeds and computes peg/market deviations correctly (LBTC $60,405 = 1.0043 BTC x $60,146). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…301) Address review on stables/oracles.py: 1. Emergency dispatch (utils/dispatch.dispatch_emergency_withdrawal) keys off Alert.protocol and skips anything outside DISPATCHABLE_PROTOCOLS. Alerts were hardcoding protocol="pegs" and putting the owner only in channel, so an USDe oracle failure routed to Telegram but never triggered ethena's emergency withdrawal. Rename PeggedAsset.channel -> protocol (the logical owner) and add an optional channel override (empty falls back to protocol routing). Alerts now emit protocol=asset.protocol, channel=asset.channel, so dispatch fires for ethena/cap/infinifi while Telegram routing is unchanged. 2. The round cache was overwritten with the current roundId unconditionally, so a backwards roundId became the new baseline and the regression was caught only once. Add pure next_cached_round(): a health-gated high-water mark that never lowers the cached round or stores a malfunctioning one. Tests: add dispatch-routing coverage (alert.protocol in DISPATCHABLE_PROTOCOLS) and next_cached_round cases. Full suite: 637 passed, 6 skipped. ruff + mypy clean (new files). Live no-send smoke reads all six feeds, 0 alerts under current conditions. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #301. Layer 2 of peg monitoring.
What this is
Where
protocols/stables/main.pywatches market price (DeFiLlama, fast profile), this watches the on-chain oracles our lending markets actually liquidate on — the real liquidation-risk signal. Driven entirely by the sharedPeggedAssetregistry from the foundation.Checks
Chainlink-backed assets (USDC, USDT, USDS, USDe, cbBTC, LBTC):
now − updatedAt > heartbeat + buffer.answeredInRound ≥ roundId, and a non-decreasingroundIdvs the cached run.PegTarget.Each check is a pure
Alert | Nonefunction, so the forced-stale / forced-divergence / off-peg / round-malfunction cases are unit-tested without a chain (acceptance criterion).Rate / fundamental oracles: generic monotonicity + delta-vs-cached handler (reuses the
apyusdapproach). A monotonic/capped decrease isCRITICAL(per #196).Severity
roundIdbackwardsansweredInRoundTenderly coverage / gap analysis (per #196 step 6)
TENDERLY_COVEREDdocuments the LBTC Redstone fundamental oracle (0xb415…0bc81, capped @ 1) which already has a Tenderly alert — so it is not double-polled here. The registry currently exposes no uncovered fundamental oracle; the docstring records exactly what data a new one needs (address, read fn + precision, monotonic/capped flag) to be wired as arate_oracle.Foundation change
Added
ChainlinkFeed.quote: PegTarget(defaultUSD) so BTC-denominated feeds (LBTC/BTC) convert to a USD basis for the divergence comparison; LBTC's feed markedquote=BTC. Backward-compatible.Tunables (env-overridable)
PEG_ORACLE_STALENESS_BUFFER(600s),PEG_ORACLE_DIVERGENCE_THRESHOLD(1%),PEG_ORACLE_RATE_DELTA_THRESHOLD(5%).Automation
Added
stables-oraclesto the hourly profile inautomation/jobs.yaml(renders; hourly now 24 tasks;test_automation_*green).Validation
ruff format+ruff checkclean;mypyclean on new modules (remaining errors pre-exist inutils/abi.py/utils/web3_wrapper.py).52 passedpeg tests (incl. forced-stale/-divergence) +13 passedautomation tests.$60,405 = 1.0043 BTC × $60,146, all within tolerance → 0 alerts, 1 Tenderly-covered oracle skipped.Decisions for review
🤖 Generated with Claude Code