Skip to content

Commit

Permalink
fix: fallback conversion rate for token market data #4615 (#26460)
Browse files Browse the repository at this point in the history
<!--
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.
  • Loading branch information
bergeron authored Aug 16, 2024
1 parent b2a493b commit 2870f59
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
diff --git a/dist/chunk-FGAZXVKS.js b/dist/chunk-FGAZXVKS.js
index 4a6aa7918599269951044b9f8c7c076a7fe6a9c7..4c1054a867ff6aef1e26f8d84e2831f234c4e44e 100644
--- a/dist/chunk-FGAZXVKS.js
+++ b/dist/chunk-FGAZXVKS.js
@@ -503,6 +503,7 @@ fetchAndMapExchangeRatesForUnsupportedNativeCurrency_fn = async function({
if (fallbackCurrencyToNativeCurrencyConversionRate === null) {
return {};
}
+ const convertFallbackToNative = (value) => value !== void 0 && value !== null ? value * fallbackCurrencyToNativeCurrencyConversionRate : void 0;
const updatedContractExchangeRates = Object.entries(
contractExchangeInformations
).reduce((acc, [tokenAddress, token]) => {
@@ -510,7 +511,15 @@ fetchAndMapExchangeRatesForUnsupportedNativeCurrency_fn = async function({
...acc,
[tokenAddress]: {
...token,
- price: token.price ? token.price * fallbackCurrencyToNativeCurrencyConversionRate : void 0
+ currency: nativeCurrency,
+ price: convertFallbackToNative(token.price),
+ marketCap: convertFallbackToNative(token.marketCap),
+ allTimeHigh: convertFallbackToNative(token.allTimeHigh),
+ allTimeLow: convertFallbackToNative(token.allTimeLow),
+ totalVolume: convertFallbackToNative(token.totalVolume),
+ high1d: convertFallbackToNative(token.high1d),
+ low1d: convertFallbackToNative(token.low1d),
+ dilutedMarketCap: convertFallbackToNative(token.dilutedMarketCap)
}
};
return acc;
diff --git a/dist/chunk-P3O5CVAH.mjs b/dist/chunk-P3O5CVAH.mjs
index 32379704450baee58a37623b1d0f6b471e69de2e..069c3deec15ad85057c910ecf0db31d856fd6ae2 100644
--- a/dist/chunk-P3O5CVAH.mjs
+++ b/dist/chunk-P3O5CVAH.mjs
@@ -503,6 +503,7 @@ fetchAndMapExchangeRatesForUnsupportedNativeCurrency_fn = async function({
if (fallbackCurrencyToNativeCurrencyConversionRate === null) {
return {};
}
+ const convertFallbackToNative = (value) => value !== void 0 && value !== null ? value * fallbackCurrencyToNativeCurrencyConversionRate : void 0;
const updatedContractExchangeRates = Object.entries(
contractExchangeInformations
).reduce((acc, [tokenAddress, token]) => {
@@ -510,7 +511,15 @@ fetchAndMapExchangeRatesForUnsupportedNativeCurrency_fn = async function({
...acc,
[tokenAddress]: {
...token,
- price: token.price ? token.price * fallbackCurrencyToNativeCurrencyConversionRate : void 0
+ currency: nativeCurrency,
+ price: convertFallbackToNative(token.price),
+ marketCap: convertFallbackToNative(token.marketCap),
+ allTimeHigh: convertFallbackToNative(token.allTimeHigh),
+ allTimeLow: convertFallbackToNative(token.allTimeLow),
+ totalVolume: convertFallbackToNative(token.totalVolume),
+ high1d: convertFallbackToNative(token.high1d),
+ low1d: convertFallbackToNative(token.low1d),
+ dilutedMarketCap: convertFallbackToNative(token.dilutedMarketCap)
}
};
return acc;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"@metamask/address-book-controller": "^4.0.1",
"@metamask/announcement-controller": "^6.1.0",
"@metamask/approval-controller": "^7.0.0",
"@metamask/assets-controllers": "^33.0.0",
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A33.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-33.0.0-3e7448c4cd.patch",
"@metamask/base-controller": "^5.0.1",
"@metamask/browser-passworder": "^4.3.0",
"@metamask/contract-metadata": "^2.5.0",
Expand Down
46 changes: 44 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4824,7 +4824,7 @@ __metadata:
languageName: node
linkType: hard

"@metamask/assets-controllers@npm:^33.0.0":
"@metamask/assets-controllers@npm:33.0.0":
version: 33.0.0
resolution: "@metamask/assets-controllers@npm:33.0.0"
dependencies:
Expand Down Expand Up @@ -4866,6 +4866,48 @@ __metadata:
languageName: node
linkType: hard

"@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A33.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-33.0.0-3e7448c4cd.patch":
version: 33.0.0
resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A33.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-33.0.0-3e7448c4cd.patch::version=33.0.0&hash=4c8a6f"
dependencies:
"@ethereumjs/util": "npm:^8.1.0"
"@ethersproject/address": "npm:^5.7.0"
"@ethersproject/bignumber": "npm:^5.7.0"
"@ethersproject/contracts": "npm:^5.7.0"
"@ethersproject/providers": "npm:^5.7.0"
"@metamask/abi-utils": "npm:^2.0.2"
"@metamask/accounts-controller": "npm:^17.0.0"
"@metamask/approval-controller": "npm:^7.0.0"
"@metamask/base-controller": "npm:^6.0.0"
"@metamask/contract-metadata": "npm:^2.4.0"
"@metamask/controller-utils": "npm:^11.0.0"
"@metamask/eth-query": "npm:^4.0.0"
"@metamask/keyring-controller": "npm:^17.1.0"
"@metamask/metamask-eth-abis": "npm:^3.1.1"
"@metamask/network-controller": "npm:^19.0.0"
"@metamask/polling-controller": "npm:^8.0.0"
"@metamask/preferences-controller": "npm:^13.0.0"
"@metamask/rpc-errors": "npm:^6.2.1"
"@metamask/utils": "npm:^8.3.0"
"@types/bn.js": "npm:^5.1.5"
"@types/uuid": "npm:^8.3.0"
async-mutex: "npm:^0.5.0"
bn.js: "npm:^5.2.1"
cockatiel: "npm:^3.1.2"
lodash: "npm:^4.17.21"
multiformats: "npm:^9.5.2"
single-call-balance-checker-abi: "npm:^1.0.0"
uuid: "npm:^8.3.2"
peerDependencies:
"@metamask/accounts-controller": ^17.0.0
"@metamask/approval-controller": ^7.0.0
"@metamask/keyring-controller": ^17.0.0
"@metamask/network-controller": ^19.0.0
"@metamask/preferences-controller": ^13.0.0
checksum: 10/effeb152b776dad38e173f9a699158d6df15d4175d27de763ba6ddd8f9b50cf604722683f8d91af989a7b5d721d1b846c5a78d2553ce7f84787925440f3c1107
languageName: node
linkType: hard

"@metamask/auto-changelog@npm:^2.1.0":
version: 2.6.1
resolution: "@metamask/auto-changelog@npm:2.6.1"
Expand Down Expand Up @@ -25232,7 +25274,7 @@ __metadata:
"@metamask/announcement-controller": "npm:^6.1.0"
"@metamask/api-specs": "npm:^0.9.3"
"@metamask/approval-controller": "npm:^7.0.0"
"@metamask/assets-controllers": "npm:^33.0.0"
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A33.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-33.0.0-3e7448c4cd.patch"
"@metamask/auto-changelog": "npm:^2.1.0"
"@metamask/base-controller": "npm:^5.0.1"
"@metamask/browser-passworder": "npm:^4.3.0"
Expand Down

0 comments on commit 2870f59

Please sign in to comment.