UNPKG

@ledgerhq/coin-multiversx

Version:
117 lines 5.55 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cryptoassets_1 = require("@ledgerhq/cryptoassets"); const bignumber_js_1 = __importDefault(require("bignumber.js")); const index_1 = require("@ledgerhq/coin-framework/account/index"); const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers"); const api_1 = require("./api"); const logic_1 = require("./logic"); async function buildMultiversXESDTTokenAccount({ parentAccountId, accountAddress, token, balance, }) { const tokenAccountId = (0, index_1.encodeTokenAccountId)(parentAccountId, token); const tokenIdentifierHex = (0, logic_1.extractTokenId)(token.id); const tokenIdentifier = Buffer.from(tokenIdentifierHex, "hex").toString(); const operations = await (0, api_1.getESDTOperations)(tokenAccountId, accountAddress, tokenIdentifier, 0); const tokenAccount = { type: "TokenAccount", id: tokenAccountId, parentId: parentAccountId, token, operationsCount: operations.length, operations, pendingOperations: [], balance, spendableBalance: balance, swapHistory: [], creationDate: operations.length > 0 ? operations[operations.length - 1].date : new Date(), balanceHistoryCache: index_1.emptyHistoryCache, // calculated in the jsHelpers }; return tokenAccount; } async function syncESDTTokenAccountOperations(tokenAccount, address) { const oldOperations = tokenAccount?.operations || []; // Needed for incremental synchronisation const startAt = oldOperations.length ? Math.floor(oldOperations[0].date.valueOf() / 1000) : 0; const tokenIdentifierHex = (0, logic_1.extractTokenId)(tokenAccount.token.id); const tokenIdentifier = Buffer.from(tokenIdentifierHex, "hex").toString(); // Merge new operations with the previously synced ones const newOperations = await (0, api_1.getESDTOperations)(tokenAccount.id, address, tokenIdentifier, startAt); const operations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations); if (operations === oldOperations) return tokenAccount; const copy = { ...tokenAccount }; copy.operations = operations; copy.operationsCount = operations.length; return copy; } async function MultiversXBuildESDTTokenAccounts({ currency, accountId, accountAddress, existingAccount, syncConfig, }) { const { blacklistedTokenIds = [] } = syncConfig; if ((0, cryptoassets_1.listTokensForCryptoCurrency)(currency).length === 0) { return undefined; } const tokenAccounts = []; const existingAccountByTicker = {}; // used for fast lookup const existingAccountTickers = []; // used to keep track of ordering if (existingAccount && existingAccount.subAccounts) { for (const existingSubAccount of existingAccount.subAccounts) { if (existingSubAccount.type === "TokenAccount") { const { ticker, id } = existingSubAccount.token; if (!blacklistedTokenIds.includes(id)) { existingAccountTickers.push(ticker); existingAccountByTicker[ticker] = existingSubAccount; } } } } const accountESDTs = await (0, api_1.getAccountESDTTokens)(accountAddress); for (const esdt of accountESDTs) { const esdtIdentifierHex = Buffer.from(esdt.identifier).toString("hex"); const token = (0, cryptoassets_1.findTokenById)((0, logic_1.addPrefixToken)(esdtIdentifierHex)); if (token && !blacklistedTokenIds.includes(token.id)) { let tokenAccount = existingAccountByTicker[token.ticker]; if (!tokenAccount) { tokenAccount = await buildMultiversXESDTTokenAccount({ parentAccountId: accountId, accountAddress, token, balance: new bignumber_js_1.default(esdt.balance), }); } else { const inputTokenAccount = tokenAccount; tokenAccount = await syncESDTTokenAccountOperations(inputTokenAccount, accountAddress); const balance = new bignumber_js_1.default(esdt.balance); if (!balance.eq(tokenAccount.balance)) { // only recreate the object if balance changed if (inputTokenAccount === tokenAccount) { tokenAccount = { ...tokenAccount }; } tokenAccount.balance = balance; tokenAccount.spendableBalance = balance; } } if (tokenAccount) { tokenAccounts.push(tokenAccount); existingAccountTickers.push(token.ticker); existingAccountByTicker[token.ticker] = tokenAccount; } } } // Preserve order of tokenAccounts from the existing token accounts tokenAccounts.sort((a, b) => { const i = existingAccountTickers.indexOf(a.token.ticker); const j = existingAccountTickers.indexOf(b.token.ticker); if (i === j) return 0; if (i < 0) return 1; if (j < 0) return -1; return i - j; }); return tokenAccounts; } exports.default = MultiversXBuildESDTTokenAccounts; //# sourceMappingURL=buildSubAccounts.js.map