Skip to content

Conversation

@Prithpal-Sooriya
Copy link
Contributor

Explanation

This PR refactors the CurrencyRateController to improve the readability, maintainability, and testability of its exchange rate fetching logic, especially concerning fallback mechanisms.

  • Current state and why it needs to change: The #fetchExchangeRatesWithFallback method was a single, lengthy function responsible for both primary API calls and fallback logic, making it difficult to understand, maintain, and test thoroughly.
  • Solution: The monolithic #fetchExchangeRatesWithFallback method has been refactored into smaller, focused private helper methods:
    • #fetchRatesFromPriceApi: Handles the initial attempt to fetch rates from the primary Price API, identifying successful rates and currencies that failed.
    • #fetchRatesFromTokenPricesService: Manages the fallback mechanism, attempting to fetch rates for previously failed currencies using the Token Prices Service.
    • #createNullRatesForCurrencies: A utility to generate null rate entries for any currencies that failed both primary and fallback attempts.
      The main #fetchExchangeRatesWithFallback now orchestrates these smaller, well-defined steps.
  • Other changes:
    • Private members (mutex, includeUsdRate, useExternalServices) have been renamed to use the hash syntax (#mutex, etc.) to align with TypeScript's private class fields and fix linting errors.
    • Improved partial success handling ensures that if the Price API provides rates for some currencies but not others, only the failed currencies proceed to the fallback, optimizing API calls and preserving successful rates.

References

Checklist

  • I've updated the test suite for new or updated code as appropriate (Added 5 new tests for partial success/fallback scenarios and updated 2 existing tests to reflect new behavior).
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate (JSDoc added for new private helper methods).
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Open in Cursor Open in Web

Prithpal-Sooriya and others added 2 commits January 12, 2026 21:49
- Refactor #fetchExchangeRatesWithFallback into smaller helper methods:
  - #fetchRatesFromPriceApi: handles primary Price API call
  - #fetchRatesFromTokenPricesService: handles fallback fetching
  - #createNullRatesForCurrencies: creates null entries for failed currencies
- Rename private members to use hash syntax (fixing lint errors)
- Add partial success handling: when some currencies succeed and others fail,
  only failed currencies trigger fallback
- Add comprehensive tests for partial success scenarios
- Update existing tests to match new expected behavior
@cursor
Copy link

cursor bot commented Jan 12, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

Removed unused suppressions after refactoring:
- no-negated-condition: no longer needed
- no-restricted-syntax: no longer needed
- @typescript-eslint/explicit-function-return-type: reduced count from 3 to 1
@cursor cursor bot force-pushed the cursor/currency-rate-fallback-logic-c1f2 branch from cdd82a4 to b7809cd Compare January 12, 2026 22:29
…nd failedCurrencies

- Updated #fetchRatesFromTokenPricesService to return { rates, failedCurrencies }
  similar to #fetchRatesFromPriceApi for consistency
- Removed null currency creation from fallback method - failures now add to
  failedCurrencies instead of creating null rates inline
- Step 4 now takes the final list of failed currencies from step 3 to build
  null rates, simplifying the logic
- Simplified #createNullRatesForCurrencies to only take currencies array
- Renamed PriceApiResult type to FetchRatesResult for broader applicability
Prithpal-Sooriya and others added 3 commits January 12, 2026 22:44
- Wrapped the entire method body in try-catch so it never throws
- On any unexpected error, returns all currencies as failed
- Updated JSDoc to document that the method is designed to never throw
- This allows #fetchExchangeRatesWithFallback to use const instead of let
  and removes the external try-catch, simplifying the orchestration logic
@Prithpal-Sooriya Prithpal-Sooriya changed the title Currency rate fallback logic feat: improve currency rate fallback logic Jan 12, 2026
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.

3 participants