Skip to content

Commit b9b6e96

Browse files
authored
fix: prevent undefined or empty currencies from being queried (#5458)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> `CurrencyRateController` was getting an unexpected error when the `NetworkController.networkConfigurationsByChainId` state included any configuration with `nativeCurrency` set as `undefined`. The easiest way to prevent this error, as well as to avoid unnecessary results from the API that returns currency rates, is to filter out falsy values of `nativeCurrency` when updating currency rates. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> * Fixes [#12583](MetaMask/metamask-mobile#12583) ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/assets-controllers` - **FIXED**: Falsy values of `nativeCurrency` skipped when updating currency rates. ## Checklist - [X] I've updated the test suite for new or updated code as appropriate - [X] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [X] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [X] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 2b227d7 commit b9b6e96

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/assets-controllers/src/CurrencyRateController.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,39 @@ describe('CurrencyRateController', () => {
537537

538538
controller.destroy();
539539
});
540+
541+
it('skips updating empty or undefined native currencies', async () => {
542+
jest.spyOn(global.Date, 'now').mockImplementation(() => getStubbedDate());
543+
const cryptoCompareHost = 'https://min-api.cryptocompare.com';
544+
nock(cryptoCompareHost)
545+
.get('/data/pricemulti?fsyms=ETH&tsyms=xyz') // fsyms query only includes non-empty native currencies
546+
.reply(200, {
547+
ETH: { XYZ: 1000 },
548+
})
549+
.persist();
550+
551+
const messenger = getRestrictedMessenger();
552+
const controller = new CurrencyRateController({
553+
messenger,
554+
state: { currentCurrency: 'xyz' },
555+
});
556+
557+
const nativeCurrencies = ['ETH', undefined, ''];
558+
559+
await controller.updateExchangeRate(nativeCurrencies);
560+
561+
const conversionDate = getStubbedDate() / 1000;
562+
expect(controller.state).toStrictEqual({
563+
currentCurrency: 'xyz',
564+
currencyRates: {
565+
ETH: {
566+
conversionDate,
567+
conversionRate: 1000,
568+
usdConversionRate: null,
569+
},
570+
},
571+
});
572+
573+
controller.destroy();
574+
});
540575
});

packages/assets-controllers/src/CurrencyRateController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ export class CurrencyRateController extends StaticIntervalPollingController<Curr
157157
*
158158
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
159159
*/
160-
async updateExchangeRate(nativeCurrencies: string[]): Promise<void> {
160+
async updateExchangeRate(
161+
nativeCurrencies: (string | undefined)[],
162+
): Promise<void> {
161163
const releaseLock = await this.mutex.acquire();
162164
try {
163165
const { currentCurrency } = this.state;
@@ -167,6 +169,10 @@ export class CurrencyRateController extends StaticIntervalPollingController<Curr
167169
const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);
168170
const nativeCurrenciesToFetch = nativeCurrencies.reduce(
169171
(acc, nativeCurrency) => {
172+
if (!nativeCurrency) {
173+
return acc;
174+
}
175+
170176
acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)
171177
? FALL_BACK_VS_CURRENCY
172178
: nativeCurrency;

0 commit comments

Comments
 (0)