Skip to content

feat(oracle): add exchange rate oracle for real-time fiat value conversion#278

Merged
0xDeon merged 1 commit intoSuncrest-Labs:mainfrom
Salmatcre8:feature/issue-211-exchange-rate-oracle
Apr 23, 2026
Merged

feat(oracle): add exchange rate oracle for real-time fiat value conversion#278
0xDeon merged 1 commit intoSuncrest-Labs:mainfrom
Salmatcre8:feature/issue-211-exchange-rate-oracle

Conversation

@Salmatcre8
Copy link
Copy Markdown
Contributor

Closes #211

Summary

  • New apps/api/internal/oracle/ package with five files: rate.go (types/errors), cache.go (TTL cache), stellar.go (Horizon order book), defillama.go (DeFiLlama fallback), fiat.go (open.er-api.com forex), service.go (aggregation + fallback)
  • New endpoint GET /api/v1/rates?base=USDC&quote=NGN returning { base, quote, rate, source, fetched_at, expires_at, stale? }
  • Supported pairs: USDC→NGN, USDC→GHS, USDC→KES, USDC→USD (fixed 1.0), XLM→USD

Caching / resilience

Rate type TTL On all-sources-fail
Crypto (XLM/USD) 30 s Serve stale + stale: true
Fiat (USDC/NGN etc.) 5 min Serve stale + stale: true

Provider priority for XLM/USD: Stellar Horizon → DeFiLlama. No API key is required for any source.

Test plan

  • 6 cache unit tests (cache_test.go): miss, store/retrieve, freshness before/after expiry, overwrite, independent keys
  • 8 service unit tests (service_test.go): fixed USDC/USD, unsupported pair error, XLM fallback chain, stale serve on all-fail, no-cache error path, fiat provider, fiat stale fallback, cache-hit skips providers
  • GOOS=linux go build ./... — clean build

Response shape

{
  "success": true,
  "data": {
    "base": "USDC",
    "quote": "NGN",
    "rate": 1647.50,
    "source": "forex",
    "fetched_at": "2026-04-23T12:00:00Z",
    "expires_at": "2026-04-23T12:05:00Z"
  }
}

…rsion (Suncrest-Labs#211)

New oracle package (apps/api/internal/oracle/) with:
- In-memory TTL cache (30s crypto / 5min fiat) with stale-while-serve fallback
- Stellar Horizon order book provider for XLM/USD mid-market rate
- DeFiLlama fallback for XLM/USD when Horizon is unavailable
- open.er-api.com (key-free) forex provider for USD→NGN/GHS/KES/USD
- RateService that chains providers with priority fallback and serves
  stale cached data (Stale: true) when all live sources fail

New endpoint: GET /api/v1/rates?base=USDC&quote=NGN

Supported pairs: USDC→NGN, USDC→GHS, USDC→KES, USDC→USD, XLM→USD

14 unit tests covering cache freshness, stale fallback, provider fallback
chain, cache hit bypass, and all supported pair types.
@Salmatcre8 Salmatcre8 requested a review from 0xDeon as a code owner April 23, 2026 13:11
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Apr 23, 2026

@Salmatcre8 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

Copy link
Copy Markdown
Contributor

@0xDeon 0xDeon left a comment

Choose a reason for hiding this comment

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

Lgtm

@0xDeon 0xDeon merged commit 8fcc80b into Suncrest-Labs:main Apr 23, 2026
6 checks passed
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.

feat(backend): integrate exchange rate oracle for real-time fiat value conversion

2 participants