@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
42 lines • 1.7 kB
JavaScript
import { getParentAccount } from "@ledgerhq/ledger-wallet-framework/account/helpers";
export const getTokensWithFundsMap = (accounts) => {
const tokensMap = new Map();
for (const account of accounts) {
const { balance, currency } = account || {};
if (!balance?.gt(0) || !currency)
continue;
const parentAccount = getParentAccount(account, accounts);
const networkName = parentAccount?.currency?.name;
if (!networkName)
continue;
const mainKey = `${currency.ticker}:${networkName}`;
tokensMap.set(mainKey, {
ticker: currency.ticker,
networkName,
id: currency.id,
});
account.subAccounts?.forEach(subAccount => {
const { balance, token } = subAccount || {};
if (!balance?.gt(0) || !token)
return;
const subKey = `${token.ticker}:${networkName}`;
tokensMap.set(subKey, {
ticker: token.ticker,
networkName,
id: token.id,
});
});
}
return tokensMap;
};
/** Format defaults to "USDT on Ethereum". format: "currencyId" returns the id of each token, e.g. "ethereum/erc20/usde". */
export const getTokensWithFunds = (accounts, customFormat = "default") => {
if (!accounts?.length)
return [];
const tokensMap = getTokensWithFundsMap(accounts);
if (customFormat === "currencyId") {
return Array.from(tokensMap.values(), token => token.id);
}
return Array.from(tokensMap.values(), ({ ticker, networkName }) => `${ticker} on ${networkName}`);
};
//# sourceMappingURL=getTokensWithFunds.js.map