UNPKG

@ledgerhq/live-common

Version:
63 lines 3.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildSubAccounts = exports.getAssetIdFromTokenId = void 0; const bignumber_js_1 = __importDefault(require("bignumber.js")); const index_1 = require("@ledgerhq/coin-framework/account/index"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const cryptoassets_1 = require("@ledgerhq/cryptoassets"); const getAssetIdFromTokenId = (tokenId) => tokenId.split("/")[2]; exports.getAssetIdFromTokenId = getAssetIdFromTokenId; function buildTokenAccount({ parentAccountId, assetBalance, token, operations, }) { const id = (0, index_1.encodeTokenAccountId)(parentAccountId, token); const balance = new bignumber_js_1.default(assetBalance.value.toString() || "0"); // TODO: recheck this logic const spendableBalance = new bignumber_js_1.default(assetBalance.value.toString()).minus(new bignumber_js_1.default(assetBalance.locked?.toString() || "0")); const tokenOperations = operations.map(op => ({ ...op, id: (0, operation_1.encodeOperationId)(id, op.hash, op.extra?.ledgerOpType), accountId: id, type: op.extra?.ledgerOpType, value: op.extra?.assetAmount ? new bignumber_js_1.default(op.extra?.assetAmount) : op.value, })); return { type: "TokenAccount", id, parentId: parentAccountId, token, operationsCount: operations.length, operations: tokenOperations, pendingOperations: [], balance, spendableBalance: spendableBalance, swapHistory: [], creationDate: operations.length > 0 ? operations[operations.length - 1].date : new Date(), balanceHistoryCache: index_1.emptyHistoryCache, // calculated in the jsHelpers }; } async function buildSubAccounts({ currency, accountId, allTokenAssetsBalances, syncConfig, operations, getTokenFromAsset, }) { const { blacklistedTokenIds = [] } = syncConfig; const allTokens = (0, cryptoassets_1.listTokensForCryptoCurrency)(currency); const tokenAccounts = []; if (allTokens.length === 0 || allTokenAssetsBalances.length === 0) { return tokenAccounts; } for (const balance of allTokenAssetsBalances) { const token = getTokenFromAsset && (await getTokenFromAsset(balance.asset)); // NOTE: for future tokens, will need to check over currencyName/standard(erc20,trc10,trc20, etc)/id if (token && !blacklistedTokenIds.includes(token.id)) { tokenAccounts.push(buildTokenAccount({ parentAccountId: accountId, assetBalance: balance, token, operations: operations.filter(op => op.extra.assetReference === balance.asset?.["assetReference"] && op.extra.assetOwner === balance.asset?.["assetOwner"]), })); } } return tokenAccounts; } exports.buildSubAccounts = buildSubAccounts; //# sourceMappingURL=buildSubAccounts.js.map