@ledgerhq/coin-stacks
Version:
Ledger Stacks Coin integration
53 lines • 2.62 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sync = exports.getAccountShape = void 0;
const index_1 = require("@ledgerhq/coin-framework/account/index");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const transactions_1 = require("@stacks/transactions");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const invariant_1 = __importDefault(require("invariant"));
const api_1 = require("../network/api");
const misc_1 = require("./utils/misc");
const getAccountShape = async (info) => {
const { initialAccount, currency, rest = {}, derivationMode } = info;
// for bridge tests specifically the `rest` object is empty and therefore the publicKey is undefined
// reconciliatePublicKey tries to get pubKey from rest object and then from accountId
const pubKey = (0, misc_1.reconciliatePublicKey)(rest.publicKey, initialAccount);
(0, invariant_1.default)(pubKey, "publicKey is required");
const accountId = (0, index_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: pubKey,
derivationMode,
});
const address = (0, transactions_1.getAddressFromPublicKey)(pubKey);
const blockHeight = await (0, api_1.fetchBlockHeight)();
const balanceResp = await (0, api_1.fetchBalances)(address);
const rawTxs = await (0, api_1.fetchFullTxs)(address);
const mempoolTxs = await (0, api_1.fetchFullMempoolTxs)(address);
const balance = new bignumber_js_1.default(balanceResp.balance);
let spendableBalance = new bignumber_js_1.default(balanceResp.balance);
for (const tx of mempoolTxs) {
spendableBalance = spendableBalance
.minus(new bignumber_js_1.default(tx.fee_rate))
.minus(new bignumber_js_1.default(tx.token_transfer.amount));
}
const pendingOperations = mempoolTxs.flatMap((0, misc_1.mapPendingTxToOps)(accountId, address));
const result = {
id: accountId,
xpub: pubKey,
freshAddress: address,
balance,
spendableBalance,
operations: pendingOperations.concat(rawTxs.flatMap((0, misc_1.mapTxToOps)(accountId, address))),
blockHeight: blockHeight.chain_tip.block_height,
};
return result;
};
exports.getAccountShape = getAccountShape;
exports.sync = (0, jsHelpers_1.makeSync)({ getAccountShape: exports.getAccountShape });
//# sourceMappingURL=synchronization.js.map
;