UNPKG

@ledgerhq/coin-stellar

Version:
73 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountShape = void 0; const index_1 = require("@ledgerhq/coin-framework/account/index"); const index_2 = require("@ledgerhq/coin-framework/serialization/index"); const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers"); const network_1 = require("../network"); const tokens_1 = require("./tokens"); const types_1 = require("../types"); const logic_1 = require("./logic"); const live_env_1 = require("@ledgerhq/live-env"); const getAccountShape = async (info, syncConfig) => { const { address, currency, initialAccount, derivationMode } = info; // FIXME Workaround for burn address, see https://ledgerhq.atlassian.net/browse/LIVE-4014 if (address === logic_1.STELLAR_BURN_ADDRESS) throw new types_1.StellarBurnAddressError(); const accountId = (0, index_1.encodeAccountId)({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: address, derivationMode, }); const { blockHeight, balance, spendableBalance, assets } = await (0, network_1.fetchAccount)(address); const oldOperations = (initialAccount?.operations || []); const lastPagingToken = oldOperations[0]?.extra.pagingToken || ""; const isInitSync = lastPagingToken === ""; const newOperations = (await (0, network_1.fetchAllOperations)(accountId, address, isInitSync ? "desc" : "asc", lastPagingToken, /** * For an account with a particularly high number of historical transactions, * retrieving all the data would take a considerable amount of time and is likely to * fail due to poor network connection quality or other reasons. Therefore, we set a * limit on the number of retrieval operations here. * If users want to access historical records from earlier, I would suggest they use a professional blockchain explorer. */ isInitSync ? (0, live_env_1.getEnv)("API_STELLAR_HORIZON_INITIAL_FETCH_MAX_OPERATIONS") : undefined)) || []; const allOperations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations); const assetOperations = []; allOperations.forEach(operation => { if (operation?.extra?.assetCode && operation?.extra?.assetIssuer && !["OPT_IN", "OPT_OUT"].includes(operation.type)) { assetOperations.push(operation); } }); const subAccounts = (0, tokens_1.buildSubAccounts)({ currency, accountId, assets, syncConfig, operations: assetOperations, }) || []; const shape = { id: accountId, balance, spendableBalance, operationsCount: allOperations.length, blockHeight, subAccounts, }; return { ...shape, operations: allOperations.map(op => { const subOperations = (0, index_2.inferSubOperations)(op.hash, subAccounts); return { ...op, subOperations, }; }), }; }; exports.getAccountShape = getAccountShape; //# sourceMappingURL=synchronization.js.map