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

fix: fallback conversion rate for token market data #4615

Merged
merged 4 commits into from
Sep 4, 2024

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Aug 16, 2024

Explanation

When the price API does not support the native currency (e.g. MATIC), prices are fetched in ETH and an additional conversion hop is performed to the native currency. But this hop was only being performed on the current price, and was missing on the extended market data like marketCap, allTimeHigh, etc.

References

Changelog

@metamask/assets-controllers

  • FIXED: On networks where the native currency is not ETH, token market data is now correctly priced in the native currency.

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

@bergeron bergeron marked this pull request as ready for review August 16, 2024 02:16
@bergeron bergeron requested a review from a team as a code owner August 16, 2024 02:16
@bergeron bergeron requested a review from salimtb August 16, 2024 02:16
@@ -745,16 +745,28 @@ export class TokenRatesController extends StaticIntervalPollingController<
return {};
}

// Converts the price in the fallback currency to the native currency
const convertFallbackToNative = (value: number | undefined) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these calculations be done with a non-destructive decimal type like BigNumber, instead of lossy floats (JS Number)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a case you're worried about that can produce a problem, like very small/big numbers? Is this different than other multiplications ?

And how would you do it in code? I think it should eventually serialize as a number, so does that look like converting to BigNumber, multiplying, then back to number?

marketCapPercentChange1d: 100,
price: 0.001,
price: (2 / 1000) * fallbackRate,
Copy link
Contributor

Choose a reason for hiding this comment

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

bergeron added a commit to MetaMask/metamask-extension that referenced this pull request Aug 16, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Patches MetaMask/core#4615

When the price API does not support the native currency (e.g. MATIC),
prices are fetched in ETH and an additional conversion hop is performed
to the native currency. But this hop was only being performed on the
current price, and was missing on the extended market data like
marketCap, allTimeHigh, etc.


[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26460?quickstart=1)

## **Related issues**


## **Manual testing steps**

1. Open the token details page for a polygon token
2. Verify market cap, all time low, all time high are correct

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<img width="357" alt="Screenshot 2024-08-15 at 6 08 43 PM"
src="https://github.com/user-attachments/assets/073e2a74-8258-4206-af1d-664eb3d4c1bb">
<img width="353" alt="Screenshot 2024-08-15 at 6 22 15 PM"
src="https://github.com/user-attachments/assets/d5603a56-9fea-401a-a7cf-a08654792c17">


### **After**
<img width="351" alt="Screenshot 2024-08-15 at 6 12 46 PM"
src="https://github.com/user-attachments/assets/13498481-8fe7-4f6d-852d-8d45508769dd">
<img width="351" alt="Screenshot 2024-08-15 at 6 17 11 PM"
src="https://github.com/user-attachments/assets/1f094ce8-0d05-4c7f-bb6d-73221eeb3fef">

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
@bergeron bergeron merged commit 756f06d into main Sep 4, 2024
116 checks passed
@bergeron bergeron deleted the brian/fix-fallback-conversion-rate branch September 4, 2024 17:01
@bergeron
Copy link
Contributor Author

bergeron commented Sep 4, 2024

merging since this was already hotfixed to prod, and i want to keep things in sync. if we hit precision issues we can do something like lego's suggestion later

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