From 53cacd352744ae3d02284fd55e408a3db73cf466 Mon Sep 17 00:00:00 2001 From: sahar-fehri Date: Thu, 2 Jan 2025 22:22:38 +0100 Subject: [PATCH] fix: fix mantle pripce when calling fetchMultiExchangeRate --- .../assets-controllers/src/assetsUtil.test.ts | 11 +++++++++++ packages/assets-controllers/src/assetsUtil.ts | 15 +++++++++++++++ .../crypto-compare.test.ts | 16 ++++++++++++++++ .../src/crypto-compare-service/crypto-compare.ts | 13 ++++++++----- packages/assets-controllers/src/index.ts | 1 + 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/assets-controllers/src/assetsUtil.test.ts b/packages/assets-controllers/src/assetsUtil.test.ts index f32d2b3ae89..17dc36ebf93 100644 --- a/packages/assets-controllers/src/assetsUtil.test.ts +++ b/packages/assets-controllers/src/assetsUtil.test.ts @@ -743,6 +743,17 @@ describe('assetsUtil', () => { } }); }); + + describe('getKeyByValue', () => { + it('should return correct key for a specific value', () => { + const testMap = new Map([ + ['toto', 'koko'], + ['foo', 'bar'], + ]); + const result = assetsUtil.getKeyByValue(testMap, 'koko'); + expect(result).toBe('toto'); + }); + }); }); /** diff --git a/packages/assets-controllers/src/assetsUtil.ts b/packages/assets-controllers/src/assetsUtil.ts index 4cd2aef2221..48b0bcde927 100644 --- a/packages/assets-controllers/src/assetsUtil.ts +++ b/packages/assets-controllers/src/assetsUtil.ts @@ -433,3 +433,18 @@ export async function fetchTokenContractExchangeRates({ {}, ); } + +/** + * Function to search for a specific value in a given map and return the key + * @param map - map input to search value + * @param value - the value to search for + * @returns returns key that corresponds to the value + */ +export function getKeyByValue(map: Map, value: string) { + for (const [key, val] of map.entries()) { + if (val === value) { + return key; + } + } + return null; // Return null if no match is found +} diff --git a/packages/assets-controllers/src/crypto-compare-service/crypto-compare.test.ts b/packages/assets-controllers/src/crypto-compare-service/crypto-compare.test.ts index b7bdfb26645..4291b20cc0b 100644 --- a/packages/assets-controllers/src/crypto-compare-service/crypto-compare.test.ts +++ b/packages/assets-controllers/src/crypto-compare-service/crypto-compare.test.ts @@ -195,5 +195,21 @@ describe('CryptoCompare', () => { sol: { eur: 3000 }, }); }); + + it('should override native symbol for mantle native token', async () => { + nock(cryptoCompareHost) + .get('/data/pricemulti?fsyms=MANTLE,ETH&tsyms=EUR') + .reply(200, { + MANTLE: { EUR: 1000 }, + ETH: { EUR: 2000 }, + }); + + // @ts-expect-error Testing the case where the USD rate is not included + const response = await fetchMultiExchangeRate('EUR', ['MNT', 'ETH']); + expect(response).toStrictEqual({ + eth: { eur: 2000 }, + mnt: { eur: 1000 }, + }); + }); }); }); diff --git a/packages/assets-controllers/src/crypto-compare-service/crypto-compare.ts b/packages/assets-controllers/src/crypto-compare-service/crypto-compare.ts index 02b3b2c09ff..152ae22fb69 100644 --- a/packages/assets-controllers/src/crypto-compare-service/crypto-compare.ts +++ b/packages/assets-controllers/src/crypto-compare-service/crypto-compare.ts @@ -1,5 +1,7 @@ import { handleFetch } from '@metamask/controller-utils'; +import { getKeyByValue } from '../assetsUtil'; + /** * A map from native currency symbol to CryptoCompare identifier. * This is only needed when the values don't match. @@ -141,11 +143,11 @@ export async function fetchMultiExchangeRate( cryptocurrencies: string[], includeUSDRate: boolean, ): Promise>> { - const url = getMultiPricingURL( - cryptocurrencies, - [fiatCurrency], - includeUSDRate, + const fsyms = cryptocurrencies.map( + (nativeCurrency) => + nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency, ); + const url = getMultiPricingURL(fsyms, [fiatCurrency], includeUSDRate); const response = await handleFetch(url); handleErrorResponse(response); @@ -154,7 +156,8 @@ export async function fetchMultiExchangeRate( string, Record, ][]) { - rates[cryptocurrency.toLowerCase()] = { + const key = getKeyByValue(nativeSymbolOverrides, cryptocurrency); + rates[key?.toLowerCase() ?? cryptocurrency.toLowerCase()] = { [fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()], ...(includeUSDRate && { usd: values.USD }), }; diff --git a/packages/assets-controllers/src/index.ts b/packages/assets-controllers/src/index.ts index aaba11c8466..d9a8857aabb 100644 --- a/packages/assets-controllers/src/index.ts +++ b/packages/assets-controllers/src/index.ts @@ -130,6 +130,7 @@ export { formatIconUrlWithProxy, getFormattedIpfsUrl, fetchTokenContractExchangeRates, + getKeyByValue, } from './assetsUtil'; export { CodefiTokenPricesServiceV2,