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: fix mantle price when calling fetchMultiExchangeRate #5099

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/assets-controllers/src/assetsUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/assets-controllers/src/assetsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>, value: string) {
for (const [key, val] of map.entries()) {
if (val === value) {
return key;
}
}
return null; // Return null if no match is found
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
});
});
});
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -141,11 +143,11 @@ export async function fetchMultiExchangeRate(
cryptocurrencies: string[],
includeUSDRate: boolean,
): Promise<Record<string, Record<string, number>>> {
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);

Expand All @@ -154,7 +156,8 @@ export async function fetchMultiExchangeRate(
string,
Record<string, number>,
][]) {
rates[cryptocurrency.toLowerCase()] = {
const key = getKeyByValue(nativeSymbolOverrides, cryptocurrency);
rates[key?.toLowerCase() ?? cryptocurrency.toLowerCase()] = {
[fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()],
...(includeUSDRate && { usd: values.USD }),
};
Expand Down
1 change: 1 addition & 0 deletions packages/assets-controllers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export {
formatIconUrlWithProxy,
getFormattedIpfsUrl,
fetchTokenContractExchangeRates,
getKeyByValue,
} from './assetsUtil';
export {
CodefiTokenPricesServiceV2,
Expand Down
Loading