@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
103 lines • 4.27 kB
JavaScript
import { initialBitcoinResourcesValue } from "@ledgerhq/coin-bitcoin/types";
import { clearAccount as commonClearAccount, isAccountEmpty as commonIsAccountEmpty, getMainAccount, } from "@ledgerhq/ledger-wallet-framework/account";
import { isAccountEmpty as isCosmosAccountEmpty } from "@ledgerhq/coin-cosmos/helpers";
import { isCosmosAccount } from "@ledgerhq/coin-cosmos/types/index";
import { isTronAccount, isAccountEmpty as isTronAccountEmpty, } from "@ledgerhq/coin-tron/index";
import { isAccountEmpty as isVechainAccountEmpty } from "@ledgerhq/coin-vechain/index";
import { isAccountEmpty as isCantonAccountEmpty } from "@ledgerhq/coin-canton";
import { isAccountDelegating } from "../families/tezos/staking";
import { getCryptoAssetsStore } from "@ledgerhq/cryptoassets/state";
// TODO: remove this export and prefer import from root file.
export { accountWithMandatoryTokens, findTokenAccountByCurrency, flattenAccounts, getMainAccount, getAccountCurrency, getAccountSpendableBalance, getFeesCurrency, getFeesUnit, getParentAccount, isAccount, isAccountBalanceUnconfirmed, isTokenAccount, listSubAccounts, shortAddressPreview, } from "@ledgerhq/ledger-wallet-framework/account/index";
export const isAccountEmpty = (a) => {
if (a.type === "Account") {
if (isCosmosAccount(a)) {
return isCosmosAccountEmpty(a);
}
if (isTronAccount(a)) {
return isTronAccountEmpty(a);
}
if (a.currency.family == "vechain") {
return isVechainAccountEmpty(a);
}
if (a.currency.family == "canton") {
return isCantonAccountEmpty(a);
}
}
return commonIsAccountEmpty(a);
};
// in future, could be a per currency thing
// clear account to a bare minimal version that can be restored via sync
// will preserve the balance to avoid user panic
export function clearAccount(account) {
// FIXME add in the coins bridge a way for a coin to define extra clean up functions to modularize this.
return commonClearAccount(account, (account) => {
if (isTronAccount(account)) {
account.tronResources = {
...account.tronResources,
cacheTransactionInfoById: {},
};
}
if (account.currency.family === "bitcoin") {
account.bitcoinResources = initialBitcoinResourcesValue;
}
});
}
export const getVotesCount = (account, parentAccount) => {
const mainAccount = getMainAccount(account, parentAccount);
// FIXME add this back in the coin bridge.
switch (mainAccount.currency.family) {
case "tezos":
return isAccountDelegating(account) ? 1 : 0;
case "tron":
return mainAccount?.tronResources?.votes.length || 0;
case "axelar":
case "quicksilver":
case "stride":
case "persistence":
case "stargaze":
case "nyx":
case "secret_network":
case "desmos":
case "dydx":
case "umee":
case "binance_beacon_chain":
case "osmosis":
case "cosmos":
case "coreum":
case "mantra":
case "crypto_org":
case "xion":
case "zenrock":
case "babylon":
return mainAccount?.cosmosResources?.delegations.length || 0;
default:
return 0;
}
};
/**
* Load blacklisted tokens and organize them into sections by parent currency
* @param tokenIds - Array of token IDs to load
* @returns Array of sections with parent currency and tokens
*/
export async function loadBlacklistedTokenSections(tokenIds) {
const tokens = await Promise.all(tokenIds.map(tokenId => getCryptoAssetsStore().findTokenById(tokenId)));
const sections = [];
for (const token of tokens) {
if (token) {
const parentCurrency = token.parentCurrency;
const index = sections.findIndex(s => s.parentCurrency === parentCurrency);
if (index < 0) {
sections.push({
parentCurrency,
tokens: [token],
});
}
else {
sections[index].tokens.push(token);
}
}
}
return sections;
}
//# sourceMappingURL=helpers.js.map