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

feat: use crypto compare multi api for currency rate controller #4852

Merged
merged 7 commits into from
Oct 30, 2024

Conversation

bergeron
Copy link
Contributor

@bergeron bergeron commented Oct 25, 2024

Explanation

Updates the currency rate controller to use the pricemulti endpoint of crypto compare. This allows us to fetch exchange rates for multiple native currencies in a single http request.

References

Changelog

@metamask/assets-controllers

  • BREAKING: CurrencyRateController now accepts an array of native currencies as its polling input.
  • BREAKING: RatesController now types the conversionRate and usdConversionRate in its state as number instead of string, to match what it was actually storing.

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
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

Comment on lines -198 to -199
error instanceof Error &&
error.message.includes('market does not exist for this coin pair')
Copy link
Contributor Author

@bergeron bergeron Oct 25, 2024

Choose a reason for hiding this comment

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

Unlike the single endpoint, the multi endpoint does not appear to throw if the symbol does not exist. It just ignores that particular symbol and returns the rest. So I don't see anything to catch.

Comment on lines +22 to +24
conversionRate: number;
conversionDate: number;
usdConversionRate?: string;
usdConversionRate?: number;
Copy link
Contributor Author

@bergeron bergeron Oct 25, 2024

Choose a reason for hiding this comment

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

This is sort of extra, but these seemed wrongly typed to me. I see everything as numbers. I mainly wanted to change fetchMultiExchangeRate to be correctly typed as numbers, but it spilled over to here as well.

  1. Crypto compare returns everything as numbers:
    https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH&tsyms=USD,EUR
    {"BTC":{"USD":65834.86,"EUR":60972.28},"ETH":{"USD":2415.39,"EUR":2232.75}}

  2. The fetchMultiExchangeRate function keeps them as numbers:

Screenshot 2024-10-25 at 11 40 34 AM
  1. They're still numbers when RatesController is saving them to state:
Screenshot 2024-10-25 at 12 46 05 PM
  1. And they're still numbers when I export state from MM extension.
Screenshot 2024-10-25 at 12 46 36 PM

Copy link
Contributor

@legobeat legobeat Oct 26, 2024

Choose a reason for hiding this comment

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

The reason why these are strings is that converting to number loses precision and can change the value in the conversion to an IEEE 754 float.

JSON itself allows numbers with arbitrary precision and does not consider the specifics of JavaScript.

Downcasting to JS number (which is done internally by JSON.parse) is in general not safe and can produce incorrect results, where in particular for exchange rates loss of precision compound as values get multiplied and result in inaccuracies.

This is the reason why we should:

  • use some decimal/bignumber implementation when calculating on these values
  • not serialize them as number
    • prefer string when serializing for public APIs
  • when deserializing JSON containing numbers, either:
    • (preferred) use a different implementation than JSON.parse which does not destroy precision
    • transform the raw JSON string before desiralization, wrapping numbers to strings with " so they can be safely deserialized using JSON.parse
  • In general avoid treating them as number ever

Even if at some point we end up being forced to use number at some API endpoint for whatever reason, those should be contained.

Copy link
Contributor

@legobeat legobeat Oct 26, 2024

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That all makes sense. I know that was the intent of typing these as string. But it's not what actually happens today given we're using Response.json() which puts them into numbers. I think the types should accurately reflect that in the meantime.

Filed this to track it. #4857. The wallets consuming this state would also need to stop expecting numbers.

@bergeron bergeron marked this pull request as ready for review October 25, 2024 23:27
@bergeron bergeron requested a review from a team as a code owner October 25, 2024 23:27
@legobeat
Copy link
Contributor

legobeat commented Oct 26, 2024

Would it be viable to make the public API less tightly coupled to Crypto Compare API?

Would make it less involved to make future changes and make it more flexible and robust at the same time.

Related: https://github.com/MetaMask/core/pull/4852/files#r1817508642

@bergeron
Copy link
Contributor Author

Would it be viable to make the public API less tightly coupled to Crypto Compare API?

Can you explain what you're thinking? The crypto compare stuff is already behind a separate service with generic interfaces for fetchExchangeRate and fetchMultiExchangeRate.

@bergeron
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "18.2.2-preview-9e22b9bb",
  "@metamask-previews/address-book-controller": "6.0.1-preview-9e22b9bb",
  "@metamask-previews/announcement-controller": "7.0.1-preview-9e22b9bb",
  "@metamask-previews/approval-controller": "7.1.0-preview-9e22b9bb",
  "@metamask-previews/assets-controllers": "39.0.0-preview-9e22b9bb",
  "@metamask-previews/base-controller": "7.0.1-preview-9e22b9bb",
  "@metamask-previews/build-utils": "3.0.1-preview-9e22b9bb",
  "@metamask-previews/chain-controller": "0.1.3-preview-9e22b9bb",
  "@metamask-previews/composable-controller": "9.0.1-preview-9e22b9bb",
  "@metamask-previews/controller-utils": "11.4.0-preview-9e22b9bb",
  "@metamask-previews/ens-controller": "14.0.1-preview-9e22b9bb",
  "@metamask-previews/eth-json-rpc-provider": "4.1.5-preview-9e22b9bb",
  "@metamask-previews/gas-fee-controller": "21.0.0-preview-9e22b9bb",
  "@metamask-previews/json-rpc-engine": "10.0.0-preview-9e22b9bb",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.4-preview-9e22b9bb",
  "@metamask-previews/keyring-controller": "17.3.0-preview-9e22b9bb",
  "@metamask-previews/logging-controller": "6.0.1-preview-9e22b9bb",
  "@metamask-previews/message-manager": "11.0.0-preview-9e22b9bb",
  "@metamask-previews/multichain": "0.0.0-preview-9e22b9bb",
  "@metamask-previews/name-controller": "8.0.1-preview-9e22b9bb",
  "@metamask-previews/network-controller": "22.0.0-preview-9e22b9bb",
  "@metamask-previews/notification-controller": "7.0.0-preview-9e22b9bb",
  "@metamask-previews/notification-services-controller": "0.12.0-preview-9e22b9bb",
  "@metamask-previews/permission-controller": "11.0.2-preview-9e22b9bb",
  "@metamask-previews/permission-log-controller": "3.0.1-preview-9e22b9bb",
  "@metamask-previews/phishing-controller": "12.2.0-preview-9e22b9bb",
  "@metamask-previews/polling-controller": "11.0.0-preview-9e22b9bb",
  "@metamask-previews/preferences-controller": "13.1.0-preview-9e22b9bb",
  "@metamask-previews/profile-sync-controller": "0.9.7-preview-9e22b9bb",
  "@metamask-previews/queued-request-controller": "5.1.0-preview-9e22b9bb",
  "@metamask-previews/rate-limit-controller": "6.0.1-preview-9e22b9bb",
  "@metamask-previews/selected-network-controller": "18.0.2-preview-9e22b9bb",
  "@metamask-previews/signature-controller": "20.1.0-preview-9e22b9bb",
  "@metamask-previews/transaction-controller": "37.3.0-preview-9e22b9bb",
  "@metamask-previews/user-operation-controller": "16.0.0-preview-9e22b9bb"
}

@bergeron bergeron merged commit 7596b1b into main Oct 30, 2024
119 checks passed
@bergeron bergeron deleted the brian/multichain-currency-rates branch October 30, 2024 00:00
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Oct 31, 2024
## **Description**

Leverages MetaMask/core#4852 to fetch native
currency prices across all evm chains, instead of just the current
chain.

Metamask will now hit the `/pricemulti` endpoint of cryptocompare to
fetch an array of currencies in 1 request.

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

## **Related issues**

Fixes:

## **Manual testing steps**

No user facing changes.

1. Add some networks with native currencies other than ETH like polygon,
bnb
2. Verify native tokens have the correct fiat price
3. Export state and verify `currencyRates` has entries for each native
currency

## **Screenshots/Recordings**

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

### **Before**

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

### **After**

<!-- [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.

---------

Co-authored-by: MetaMask Bot <[email protected]>
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