Skip to content

feat: add new action to use on bridge controller#8076

Merged
salimtb merged 6 commits intomainfrom
feat/create-exchange-rate-for-bridge
Mar 2, 2026
Merged

feat: add new action to use on bridge controller#8076
salimtb merged 6 commits intomainfrom
feat/create-exchange-rate-for-bridge

Conversation

@salimtb
Copy link
Contributor

@salimtb salimtb commented Mar 2, 2026

Explanation

What is the current state of things and why does it need to change?

The bridge controller can use either legacy controllers (MultichainAssetsRatesController, TokenRatesController, CurrencyRateController) or the unified AssetsController for exchange rates when useAssetsControllerForRates is true. When using AssetsController, the bridge needs a single action that returns the same shape: conversionRates, currencyRates, marketData, and currentCurrency. Previously there was no such action on AssetsController, and the formatting logic (which assets go in conversionRates, which currency string to use, and how rich EVM marketData entries are) did not fully match bridge expectations.

What is the solution your changes offer and how does it work?

  • New action: Add getExchangeRatesForBridge() on AssetsController and register the AssetsController:getExchangeRatesForBridge messenger action. It calls the existing formatExchangeRatesForBridge helper with state.assetsPrice and state.selectedCurrency, and returns the bridge-compatible object.
  • formatExchangeRatesForBridge behavior:
    • conversionRates: Include only non-EVM assets (e.g. Bitcoin, Solana, Tron). EVM assets are excluded so the bridge uses them from currencyRates/marketData as before.
    • Currency string: Stop hardcoding swift:0/iso4217:USD. Resolve the CAIP currency from selectedCurrency using MAP_CAIP_CURRENCIES from @metamask/assets-controllers, with fallback to USD, matching MultichainAssetsRatesController.
    • EVM marketData: Each EVM entry is built by spreading the full price/market data from state (id, marketCap, allTimeHigh, etc.) and then setting the bridge fields (priceInNative, currency, assetId, chainId, tokenAddress), so the bridge receives the same richness as from TokenRatesController.
  • Re-exports: Re-export method action types from AssetsController so all controller actions are exported from one place. Move normalizeAssetId into utils/ and re-export from the utils barrel for clearer structure.

Are there any changes whose purpose might not be obvious to those unfamiliar with the domain?

  • conversionRates is intentionally limited to non-EVM assets because the bridge uses it for non-EVM chains; EVM rates are consumed from currencyRates and marketData.
  • We export MAP_CAIP_CURRENCIES from @metamask/assets-controllers so that assets-controller can use the same currency mapping without duplicating it (and to avoid a circular dependency from assets-controllers → assets-controller).

If you had to update another package along the way, why?

  • assets-controllers: We export MAP_CAIP_CURRENCIES from the MultichainAssetsRatesController (and from the package index) so that formatExchangeRatesForBridge in assets-controller can import it and resolve the selected currency to the same CAIP string (e.g. swift:0/iso4217:EUR) that MultichainAssetsRatesController uses. This keeps a single source of truth for the currency map.

If you had to upgrade a dependency, why?

  • No dependency upgrades in this PR.

References

  • No specific issues linked.
  • Bridge controller (and any consumer that uses useAssetsControllerForRates) will call AssetsController:getExchangeRatesForBridge to get rates when this PR is merged.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • 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

Note

Medium Risk
Changes how exchange-rate data is shaped and converted for bridge consumers (including non-USD currency handling and EVM vs non-EVM separation), which could affect quotes if formatting or currency conversion is incorrect. The scope is contained and covered by new unit tests, but it touches pricing/market-data output used in bridge flows.

Overview
Adds AssetsController:getExchangeRatesForBridge (and getExchangeRatesForBridge()) to return a single bridge-compatible exchange-rate payload (conversionRates, currencyRates, marketData, currentCurrency) derived from assetsPrice + selectedCurrency.

Introduces formatExchangeRatesForBridge to produce the bridge shape, including: non-EVM-only entries in conversionRates, EVM native/token data in currencyRates/marketData (with richer market fields), and optional usdToSelectedCurrencyRate to correctly represent user-currency vs USD rates.

Exports the shared currency mapping by re-exporting MAP_CAIP_CURRENCIES from @metamask/assets-controllers, and reorganizes/exports new utils/types (formatExchangeRatesForBridge, normalizeAssetId) with added tests covering edge cases (invalid prices, ms timestamps, multi-chain EVM).

Written by Cursor Bugbot for commit 0db7602. This will update automatically on new commits. Configure here.

@salimtb salimtb marked this pull request as ready for review March 2, 2026 09:24
@salimtb salimtb requested review from a team as code owners March 2, 2026 09:24
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Comment on lines +1146 to +1152
return formatExchangeRatesForBridge({
assetsPrice: this.state.assetsPrice,
selectedCurrency: this.state.selectedCurrency,
nativeAssetIdentifiers,
networkConfigurationsByChainId,
usdToSelectedCurrencyRate: options?.usdToSelectedCurrencyRate,
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice abstraction. This is valid, fair enough.

@salimtb salimtb added this pull request to the merge queue Mar 2, 2026
Merged via the queue into main with commit 346c9e0 Mar 2, 2026
314 checks passed
@salimtb salimtb deleted the feat/create-exchange-rate-for-bridge branch March 2, 2026 13:12
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.

2 participants