Skip to content

Commit

Permalink
don't update currency rates on transient errors (#4662)
Browse files Browse the repository at this point in the history
## Explanation

We think in MetaMask/metamask-mobile#11035,
the intermittent 0 balances were caused by transient errors in requests
to crypto compare. Instead of setting the conversion rate to null in
this case, we'll omit the state update and keep the conversion rate from
the previous successful fetch.

## 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?

For example:

* Fixes #12345
* Related to #67890
-->

## 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**: In `CurrencyRateController` if unexpected errors occur
during requests to crypto compare, the conversion rate in state will
remain unchanged instead of being set to null.

## 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 highlighted breaking changes using the "BREAKING" category
above as appropriate
  • Loading branch information
bergeron authored Sep 6, 2024
1 parent 00d4a6f commit 0610ed6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
31 changes: 31 additions & 0 deletions packages/assets-controllers/src/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,35 @@ describe('CurrencyRateController', () => {

controller.destroy();
});

it('should not update state on unexpected / transient errors', async () => {
const cryptoCompareHost = 'https://min-api.cryptocompare.com';
nock(cryptoCompareHost)
.get('/data/price?fsym=ETH&tsyms=XYZ')
.reply(500) // HTTP 500 transient error
.persist();

const state = {
currentCurrency: 'xyz',
currencyRates: {
ETH: {
conversionDate: 123,
conversionRate: 123,
usdConversionRate: 123,
},
},
};
const messenger = getRestrictedMessenger();
const controller = new CurrencyRateController({ messenger, state });

// Error should still be thrown
await expect(controller.updateExchangeRate('ETH')).rejects.toThrow(
`Fetch failed with status '500' for request 'https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=XYZ'`,
);

// But state should not be changed
expect(controller.state).toStrictEqual(state);

controller.destroy();
});
});
29 changes: 17 additions & 12 deletions packages/assets-controllers/src/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export class CurrencyRateController extends StaticIntervalPollingController<
? FALL_BACK_VS_CURRENCY // ETH
: nativeCurrency;

let shouldUpdateState = true;
try {
if (
currentCurrency &&
Expand All @@ -196,23 +197,27 @@ export class CurrencyRateController extends StaticIntervalPollingController<
error.message.includes('market does not exist for this coin pair')
)
) {
// Don't update state on transient / unexpected errors
shouldUpdateState = false;
throw error;
}
} finally {
try {
this.update(() => {
return {
currencyRates: {
...currencyRates,
[nativeCurrency]: {
conversionDate,
conversionRate,
usdConversionRate,
if (shouldUpdateState) {
this.update(() => {
return {
currencyRates: {
...currencyRates,
[nativeCurrency]: {
conversionDate,
conversionRate,
usdConversionRate,
},
},
},
currentCurrency,
};
});
currentCurrency,
};
});
}
} finally {
releaseLock();
}
Expand Down

0 comments on commit 0610ed6

Please sign in to comment.