UNPKG

@ledgerhq/live-common

Version:
71 lines 3.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTokenOrCryptoCurrencyById = exports.searchByNameOrTicker = exports.searchByProviderId = exports.groupCurrenciesByProvider = exports.loadCurrenciesByProvider = void 0; const currencies_1 = require("../currencies"); const api_1 = require("./api"); const loadCurrenciesByProvider = async (coinsAndTokensSupported) => { const [sortedCurrenciesSupported, assets] = await Promise.all([ (0, currencies_1.currenciesByMarketcap)(coinsAndTokensSupported), (0, api_1.getMappedAssets)(), ]); return (0, exports.groupCurrenciesByProvider)(assets, sortedCurrenciesSupported); }; exports.loadCurrenciesByProvider = loadCurrenciesByProvider; const groupCurrenciesByProvider = (assets, sortedCurrencies) => { const assetsByLedgerId = new Map(); for (const asset of assets) { /// FIXME(LIVE-10508) drop usage of toLowerCase assetsByLedgerId.set(asset.ledgerId.toLowerCase(), asset); } const assetsByProviderId = new Map(); const sortedCryptoCurrencies = []; // iterate over currencies by preserving their order for (const ledgerCurrency of sortedCurrencies) { /// FIXME(LIVE-10508) drop usage of toLowerCase const asset = assetsByLedgerId.get(ledgerCurrency.id.toLowerCase()); if (asset) { // we only yield the intersection of currencies and mapped assets const existingEntry = assetsByProviderId.get(asset.providerId); if (!existingEntry) { assetsByProviderId.set(asset.providerId, { providerId: asset.providerId, currenciesByNetwork: [ledgerCurrency], }); } else { existingEntry.currenciesByNetwork.push(ledgerCurrency); } } } // in this case, the first currency of the provider is the one we want to display (Wasn't true) // So we need to take the first crypto or token currency of each provider to fix that for (const [, { currenciesByNetwork }] of assetsByProviderId.entries()) { const firstCrypto = currenciesByNetwork.find(c => c.type === "CryptoCurrency"); const elem = firstCrypto ?? currenciesByNetwork.find(c => c.type === "TokenCurrency"); if (elem) { sortedCryptoCurrencies.push(elem); } } return { currenciesByProvider: Array.from(assetsByProviderId.values()), sortedCryptoCurrencies, }; }; exports.groupCurrenciesByProvider = groupCurrenciesByProvider; const searchByProviderId = (list, providerId) => list.filter(elem => elem.providerId.toLowerCase() === providerId.toLowerCase()); exports.searchByProviderId = searchByProviderId; const searchByNameOrTicker = (list, nameOrTicker) => list.filter(elem => elem.name.toLowerCase().includes(nameOrTicker.toLowerCase()) || elem.ticker.toLowerCase().includes(nameOrTicker.toLowerCase())); exports.searchByNameOrTicker = searchByNameOrTicker; const getTokenOrCryptoCurrencyById = (id) => { if ((0, currencies_1.hasCryptoCurrencyId)(id)) { return (0, currencies_1.getCryptoCurrencyById)(id); } const token = (0, currencies_1.findTokenById)(id); if (!token) { throw new Error(`token with id "${id}" not found`); } return token; }; exports.getTokenOrCryptoCurrencyById = getTokenOrCryptoCurrencyById; //# sourceMappingURL=helper.js.map