@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
50 lines (41 loc) • 1.64 kB
text/typescript
import { getParentAccount } from "@ledgerhq/ledger-wallet-framework/account/helpers";
import { Account } from "@ledgerhq/types-live";
export const getTokensWithFundsMap = (accounts: Account[]) => {
const tokensMap = new Map<string, { ticker: string; networkName: string; id: string }>();
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: Account[],
customFormat: "currencyId" | "default" = "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}`);
};