Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't update currency rates on transient errors #4662

Merged
merged 2 commits into from
Sep 6, 2024

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Sep 6, 2024

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

Changelog

@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

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, what is the reason for this try - finally block inside of the finally block?

Copy link
Contributor Author

@bergeron bergeron Sep 6, 2024

Choose a reason for hiding this comment

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

to be sure the mutex always gets released, even if this.update were to throw

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I was wondering if the update function, could indeed throw an error

conversionDate,
conversionRate,
usdConversionRate,
if (shouldUpdateState) {
Copy link
Contributor

@tommasini tommasini Sep 6, 2024

Choose a reason for hiding this comment

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

Just wondering if it wouldn't be a more common pattern, if we just want to update the state when an error is not thrown, we can update the state as the last line of the try block, and have the finally just to release the mutex

That way we wouldn't need the new let variable shouldUpdateState

Copy link
Contributor Author

@bergeron bergeron Sep 6, 2024

Choose a reason for hiding this comment

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

we want the state updated on some kids of errors though. If we get market does not exist for this coin pair, then we want to update it to null because we know crypto compare doesn't support it. Trying to maintain that existing behavior, because the apps may depend on it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see! Thanks for the explanation!


it('should not update state on unexpected / transient errors', async () => {
const cryptoCompareHost = 'https://min-api.cryptocompare.com';
nock(cryptoCompareHost)
Copy link
Contributor

Choose a reason for hiding this comment

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

nock?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tommasini
Copy link
Contributor

Wondering your thoughts here: MetaMask/metamask-mobile#11087 (comment)

@bergeron bergeron merged commit 0610ed6 into main Sep 6, 2024
116 checks passed
@bergeron bergeron deleted the brian/currency-rate-transient-failures branch September 6, 2024 21:07
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