Skip to content

feat: adds DB fallback to oracle pricing in market-data endpoint#3082

Open
stalniy wants to merge 7 commits intomainfrom
feat/stats-oracle-db-fallback-pricing
Open

feat: adds DB fallback to oracle pricing in market-data endpoint#3082
stalniy wants to merge 7 commits intomainfrom
feat/stats-oracle-db-fallback-pricing

Conversation

@stalniy
Copy link
Copy Markdown
Contributor

@stalniy stalniy commented Apr 20, 2026

Why

DenomExchangeService.getExchangeRateToUSD() calls the on-chain oracle with no fallback — if the oracle reports unhealthy or the RPC call fails, /v1/market-data errors out. Ref CON-106

What

This adds a persistent DB fallback using day.aktPrice so pricing remains available during oracle downtime.

  • DenomExchangeService: Wrapped oracle RPC calls in try/catch. On success, checks priceHealth?.isHealthy — if false, logs
    ORACLE_PRICE_UNHEALTHY and falls back to DB. On RPC exception, logs ORACLE_RPC_FAILED and falls back to DB. Fallback reads the latest
    day.aktPrice via DayRepository and returns zeroed-out 24h change metrics.
  • DayRepository: Added getLatestAktPrice() — queries most recent day row with non-null aktPrice.
  • Unit tests: 3 new tests covering oracle unhealthy, RPC failure, and RPC failure with no DB price. All 7 tests pass.
  • No frontend changes — deploy-web and stats-web consume the same /v1/market-data response shape.

Summary by CodeRabbit

  • Tests

    • Expanded coverage for oracle failure scenarios (unhealthy price, RPC failures); updated mocks to include price-health signals and verify logged warnings and fallback behavior.
  • Bug Fixes

    • Exchange-rate logic now validates oracle health and catches RPC failures, falling back to the latest cached price when available and returning safe zeroed metrics when not; warning logs are emitted for oracle issues.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 546f713d-f8e3-4f39-848b-fec51ad7f1d8

📥 Commits

Reviewing files that changed from the base of the PR and between 87600b4 and 029f493.

📒 Files selected for processing (2)
  • apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts
  • apps/api/test/functional/market-data.spec.ts
✅ Files skipped from review due to trivial changes (1)
  • apps/api/test/functional/market-data.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts

📝 Walkthrough

Walkthrough

Wraps oracle/RPC calls in try/catch and checks oracle price health; when unhealthy or RPC fails, DenomExchangeService falls back to latest AKT price from DayRepository. Constructor now accepts DayRepository and LoggerService. Tests and functional fixtures updated to cover these failure modes and logging.

Changes

Cohort / File(s) Summary
DenomExchangeService (logic + tests)
apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts, apps/api/src/chain/services/denom-exchange/denom-exchange.service.spec.ts
Constructor now injects DayRepository and LoggerService. Oracle/block calls wrapped in try/catch; checks priceHealth?.isHealthy. Added private #getFallbackExchangeRateToUSD() to use DB AKT price with zeroed volume/marketCap and 24h-change fields. Tests expanded to mock DayRepository and LoggerService, simulate unhealthy oracle and RPC rejection, assert fallback outputs and logger.warn events (ORACLE_PRICE_UNHEALTHY, ORACLE_RPC_FAILED).
DayRepository (DB helper)
apps/api/src/gpu/repositories/day.repository.ts
Added `getLatestAktPrice(): Promise<number
Functional test fixtures
apps/api/test/functional/market-data.spec.ts
Updated mocked oracle aggregated-price fixtures to include price_health: { is_healthy: true } for AKT and USDC endpoints.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/stats-oracle-db-fallback-pricing

Comment @coderabbitai help to get the list of available commands and usage tips.

stalniy and others added 6 commits April 20, 2026 14:39
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ailable

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@stalniy stalniy force-pushed the feat/stats-oracle-db-fallback-pricing branch from b6e6999 to a309942 Compare April 20, 2026 12:39
@stalniy stalniy changed the title Feat/stats oracle db fallback pricing feat: adds DB fallback to oracle pricing in market-data endpoint Apr 20, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 88.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.06%. Comparing base (abeb3db) to head (029f493).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
apps/api/src/gpu/repositories/day.repository.ts 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3082      +/-   ##
==========================================
- Coverage   60.94%   60.06%   -0.89%     
==========================================
  Files        1023      983      -40     
  Lines       24491    23503     -988     
  Branches     6055     5894     -161     
==========================================
- Hits        14927    14117     -810     
+ Misses       8352     8186     -166     
+ Partials     1212     1200      -12     
Flag Coverage Δ *Carryforward flag
api 82.63% <88.00%> (-0.01%) ⬇️
deploy-web 44.85% <ø> (ø) Carriedforward from abeb3db
log-collector ?
notifications 86.06% <ø> (ø) Carriedforward from abeb3db
provider-console 81.48% <ø> (ø) Carriedforward from abeb3db
provider-proxy 85.21% <ø> (ø) Carriedforward from abeb3db
tx-signer ?

*This pull request uses carry forward flags. Click here to find out more.

Files with missing lines Coverage Δ
.../services/denom-exchange/denom-exchange.service.ts 100.00% <100.00%> (ø)
apps/api/src/gpu/repositories/day.repository.ts 40.00% <0.00%> (-60.00%) ⬇️

... and 40 files with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts`:
- Around line 44-60: The calls to the async fallback are returned without
awaiting, so rejections bypass the outer try/catch in getExchangeRateToUSD;
update both return statements that return this.#getFallbackExchangeRateToUSD()
to use await this.#getFallbackExchangeRateToUSD() so failures are caught by the
surrounding try/catch, and additionally add internal error handling inside the
private method `#getFallbackExchangeRateToUSD`() to catch DB errors and return a
safe default object (zeros) to guarantee a valid response.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a08c7013-91d1-424d-96bf-3792b7258c7a

📥 Commits

Reviewing files that changed from the base of the PR and between abeb3db and a309942.

📒 Files selected for processing (3)
  • apps/api/src/chain/services/denom-exchange/denom-exchange.service.spec.ts
  • apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts
  • apps/api/src/gpu/repositories/day.repository.ts

Comment thread apps/api/src/chain/services/denom-exchange/denom-exchange.service.ts Outdated
… test

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@stalniy stalniy force-pushed the feat/stats-oracle-db-fallback-pricing branch from 87600b4 to 029f493 Compare April 20, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants