feat: currentCurrency added to AssetsController state#7991
Conversation
There was a problem hiding this comment.
Currency is already included as part of the query key for cache purposes, so no changes needed in the client.
| dataTypes: ['price'], | ||
| assetsForPriceUpdate: Object.values(this.state.assetsBalance).flatMap( | ||
| (balances) => Object.keys(balances) as Caip19AssetId[], | ||
| ), |
There was a problem hiding this comment.
This array is consumed by PriceDataSource and gets merged with detectedTokens. Duplicates are checked there, so no need to worry about them here.
|
@metamaskbot publish-previews |
|
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions. |
| // Only fetch prices for detected assets (assets without metadata) | ||
| // The subscription handles fetching prices for all existing assets | ||
| if (!response.detectedAssets) { | ||
| if (!response.detectedAssets && !request.assetsForPriceUpdate?.length) { |
There was a problem hiding this comment.
Price-only forceUpdate may not refetch
High Severity
PriceDataSource.assetsMiddleware now returns early unless response.detectedAssets exists or request.assetsForPriceUpdate is non-empty, so any getAssets(..., { forceUpdate: true, dataTypes: ['price'] }) call that doesn’t provide assetsForPriceUpdate can silently skip fetching and return stale prices.
Additional Locations (1)
| ), | ||
| }).catch((error) => { | ||
| log('Failed to fetch asset prices after current currency change', error); | ||
| }); |
There was a problem hiding this comment.
Currency switch can overwrite newer prices
Medium Severity
setSelectedCurrency fires an async getAssets price refresh without canceling or sequencing in-flight refreshes, so rapid currency changes can complete out of order and write older-currency price results after newer-currency results, leaving assetsPrice inconsistent with selectedCurrency.


Explanation
The state for
currentCurrencyis currently saved inCurrencyRateController. Since that controller is being deprecated, and we need that piece of state to be stored somewhere, it's being moved to the controller that will replace it.This PR also ensures that
PriceDataSourcegets updated with the existing currency instead of just being set up at constructor level.It also forces price updates when the selected currency changes by passing an array of assetIds that need price updates.
References
Checklist
Note
Medium Risk
Touches price fetching and persisted controller state; incorrect wiring could lead to stale or missing fiat prices after currency changes across clients.
Overview
Adds a persisted
selectedCurrency(ISO 4217, defaultusd) toAssetsControllerstate and exposessetSelectedCurrency, which triggers a one-off forced price refresh for currently-held assets when the currency changes.Refactors
PriceDataSourceto read currency dynamically via agetSelectedCurrencycallback (instead of a constructor-timecurrencyoption) and extendsDataRequestwithassetsForPriceUpdateso price middleware can fetch prices for an explicit asset list (not only newly detected assets).Written by Cursor Bugbot for commit 8afb884. This will update automatically on new commits. Configure here.