UNPKG

@ledgerhq/coin-canton

Version:
86 lines 3.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccountShape = void 0; 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 indexer_1 = require("../network/indexer"); const node_1 = require("../network/node"); const operation_1 = require("@ledgerhq/coin-framework/operation"); const config_1 = __importDefault(require("../config")); const operationAdapter = (accountId, address) => ({ meta: { delivered_amount }, tx: { Fee, hash, inLedger, date, Account, Destination, Sequence }, }) => { const type = Account === address ? "OUT" : "IN"; let value = delivered_amount && typeof delivered_amount === "string" ? new bignumber_js_1.default(delivered_amount) : new bignumber_js_1.default(0); const feeValue = new bignumber_js_1.default(Fee); if (type === "OUT") { if (!Number.isNaN(feeValue)) { value = value.plus(feeValue); } } const op = { id: (0, operation_1.encodeOperationId)(accountId, hash, type), hash: hash, accountId, type, value, fee: feeValue, blockHash: null, blockHeight: inLedger, senders: [Account], recipients: [Destination], date: new Date(), transactionSequenceNumber: Sequence, extra: {}, }; return op; }; const filterOperations = (transactions, accountId, address) => { return transactions .filter(({ tx, meta }) => tx.TransactionType === "Payment" && typeof meta.delivered_amount === "string") .map(operationAdapter(accountId, address)) .filter((op) => Boolean(op)); }; const getAccountShape = async (info) => { const { address, initialAccount, currency, derivationMode } = info; const accountId = (0, index_1.encodeAccountId)({ type: "js", version: "2", currencyId: currency.id, xpubOrAddress: address, derivationMode, }); // blockheight retrieval const blockHeight = await (0, node_1.getBlockHeight)(); // Account info retrieval + spendable balance calculation const accountInfo = await (0, node_1.getAccountInfo)(address); const balance = new bignumber_js_1.default(accountInfo.account_data.Balance); const reserveMin = config_1.default.getCoinConfig().minReserve; const spendableBalance = new bignumber_js_1.default(accountInfo.account_data.Balance).minus(reserveMin); // Tx history fetching const oldOperations = initialAccount?.operations || []; const startAt = oldOperations.length ? (oldOperations[0].blockHeight || 0) + 1 : 0; const newTransactions = await (0, indexer_1.getTransactions)(address, { from: startAt, size: 100, }); const newOperations = filterOperations(newTransactions, accountId, address); const operations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations); // We return the new account shape const shape = { id: accountId, xpub: address, blockHeight, balance, spendableBalance, operations, operationsCount: operations.length, }; return shape; }; exports.getAccountShape = getAccountShape; //# sourceMappingURL=sync.js.map