Skip to content

Commit a6e5334

Browse files
sahar-fehriPatrykLucka
authored andcommitted
fix: fix mantle price when calling fetchMultiExchangeRate (#5099)
## Explanation PR to fix the MANTLE price fetched for chainId: 5000. Cryptocompare api provides prices for both MANTLE and MNT: https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH%2CMNT%2CMANTLE&tsyms=usd And it is returning the the price for 'MNT' when the user in on MANTLE chain. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ### `@metamask/assets-controllers` - **FIXED**: Added a check if any of the provided cyrptocurrencies to `fetchMultiExchangeRate` fct exists in `nativeSymbolOverrides` map - **ADDED**: Added utility function `getKeyByValue` to assetsUtils ## 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
1 parent 9f20a96 commit a6e5334

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

packages/assets-controllers/src/assetsUtil.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,17 @@ describe('assetsUtil', () => {
743743
}
744744
});
745745
});
746+
747+
describe('getKeyByValue', () => {
748+
it('should return correct key for a specific value', () => {
749+
const testMap = new Map([
750+
['toto', 'koko'],
751+
['foo', 'bar'],
752+
]);
753+
const result = assetsUtil.getKeyByValue(testMap, 'koko');
754+
expect(result).toBe('toto');
755+
});
756+
});
746757
});
747758

748759
/**

packages/assets-controllers/src/assetsUtil.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,18 @@ export async function fetchTokenContractExchangeRates({
433433
{},
434434
);
435435
}
436+
437+
/**
438+
* Function to search for a specific value in a given map and return the key
439+
* @param map - map input to search value
440+
* @param value - the value to search for
441+
* @returns returns key that corresponds to the value
442+
*/
443+
export function getKeyByValue(map: Map<string, string>, value: string) {
444+
for (const [key, val] of map.entries()) {
445+
if (val === value) {
446+
return key;
447+
}
448+
}
449+
return null; // Return null if no match is found
450+
}

packages/assets-controllers/src/crypto-compare-service/crypto-compare.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,21 @@ describe('CryptoCompare', () => {
195195
sol: { eur: 3000 },
196196
});
197197
});
198+
199+
it('should override native symbol for mantle native token', async () => {
200+
nock(cryptoCompareHost)
201+
.get('/data/pricemulti?fsyms=MANTLE,ETH&tsyms=EUR')
202+
.reply(200, {
203+
MANTLE: { EUR: 1000 },
204+
ETH: { EUR: 2000 },
205+
});
206+
207+
// @ts-expect-error Testing the case where the USD rate is not included
208+
const response = await fetchMultiExchangeRate('EUR', ['MNT', 'ETH']);
209+
expect(response).toStrictEqual({
210+
eth: { eur: 2000 },
211+
mnt: { eur: 1000 },
212+
});
213+
});
198214
});
199215
});

packages/assets-controllers/src/crypto-compare-service/crypto-compare.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { handleFetch } from '@metamask/controller-utils';
22

3+
import { getKeyByValue } from '../assetsUtil';
4+
35
/**
46
* A map from native currency symbol to CryptoCompare identifier.
57
* This is only needed when the values don't match.
@@ -141,11 +143,11 @@ export async function fetchMultiExchangeRate(
141143
cryptocurrencies: string[],
142144
includeUSDRate: boolean,
143145
): Promise<Record<string, Record<string, number>>> {
144-
const url = getMultiPricingURL(
145-
cryptocurrencies,
146-
[fiatCurrency],
147-
includeUSDRate,
146+
const fsyms = cryptocurrencies.map(
147+
(nativeCurrency) =>
148+
nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency,
148149
);
150+
const url = getMultiPricingURL(fsyms, [fiatCurrency], includeUSDRate);
149151
const response = await handleFetch(url);
150152
handleErrorResponse(response);
151153

@@ -154,7 +156,8 @@ export async function fetchMultiExchangeRate(
154156
string,
155157
Record<string, number>,
156158
][]) {
157-
rates[cryptocurrency.toLowerCase()] = {
159+
const key = getKeyByValue(nativeSymbolOverrides, cryptocurrency);
160+
rates[key?.toLowerCase() ?? cryptocurrency.toLowerCase()] = {
158161
[fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()],
159162
...(includeUSDRate && { usd: values.USD }),
160163
};

packages/assets-controllers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export {
130130
formatIconUrlWithProxy,
131131
getFormattedIpfsUrl,
132132
fetchTokenContractExchangeRates,
133+
getKeyByValue,
133134
} from './assetsUtil';
134135
export {
135136
CodefiTokenPricesServiceV2,

0 commit comments

Comments
 (0)