Skip to content

feat(contract): Reflector oracle cross-asset sanity checks (D12)#186

Open
cybermax4200 wants to merge 1 commit into
Dgetsylver:mainfrom
cybermax4200:feat/d12-reflector-oracle
Open

feat(contract): Reflector oracle cross-asset sanity checks (D12)#186
cybermax4200 wants to merge 1 commit into
Dgetsylver:mainfrom
cybermax4200:feat/d12-reflector-oracle

Conversation

@cybermax4200

Copy link
Copy Markdown
Contributor

Summary

Adds a Reflector oracle divergence guard to the blend_leverage strategy. Before executing a deposit or rebalance, the contract fetches prices from both Blend's internal oracle and Reflector and refuses if they diverge beyond a configurable threshold.

Changes

oracle.rs (new)

  • OracleClient — single contractclient trait covering both Blend's oracle and Reflector (same interface)
  • assert_price_aligned(e, config) — fetches both prices, normalises to 1e7, computes divergence in bps, emits oracle/diverge event and returns ExternalError on failure
  • normalise_to_7(price, decimals) — converts any decimal precision to Blend's 1e7 scale
  • Staleness check: prices older than 300s are treated as downtime

storage.rs

  • Config gains reflector: Option<Address> and oracle_threshold: i128
  • None reflector disables the guard (backward compatible)

lib.rs

  • Constructor parses args[8] (reflector, optional) and args[9] (threshold bps, default 200)
  • deposit() calls oracle::assert_price_aligned before any pool interaction
  • rebalance() calls oracle::assert_price_aligned after the debt check

Tests (oracle::tests)

  • test_no_oracle_configured_passes — guard disabled when reflector is None
  • test_aligned_divergence_zero — 0 bps divergence passes
  • test_diverging_prices_exceed_threshold — 500 bps > 200 bps threshold refused
  • test_divergence_at_threshold_passes — exactly at threshold passes (not strictly greater)
  • test_oracle_downtime_returns_none — None response refused
  • test_stale_price_detected — price older than 300s refused
  • test_normalise_* — 14→7, 7→7, 4→7 decimal conversions

Closes #53

- oracle.rs: OracleClient (shared interface for Blend + Reflector),
  assert_price_aligned(), normalise_to_7(), oracle/diverge event
- storage.rs: Config gains reflector: Option<Address> and
  oracle_threshold: i128 (bps)
- lib.rs: declare oracle mod, parse args[8]/[9] in constructor
  (optional, default threshold 200 bps), call assert_price_aligned
  at start of deposit() and rebalance()
- Tests: normalise_to_7 unit tests, aligned/diverging/at-threshold/
  downtime/stale coverage in oracle::tests

Closes Dgetsylver#53
@drips-wave

drips-wave Bot commented May 29, 2026

Copy link
Copy Markdown

@cybermax4200 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Dgetsylver

Copy link
Copy Markdown
Owner

Thanks — this is valuable (oracle divergence guard). It isn't on main and conflicts with current contract code — could you rebase onto latest main? (security feature, keen to land it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D12: Reflector oracle cross-asset sanity checks

2 participants