@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
92 lines • 4.38 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.genericGetAccountShape = void 0;
const index_1 = require("@ledgerhq/coin-framework/account/index");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const alpaca_1 = require("./alpaca");
const utils_1 = require("./utils");
const serialization_1 = require("@ledgerhq/coin-framework/serialization");
const buildSubAccounts_1 = require("./buildSubAccounts");
function genericGetAccountShape(network, kind) {
return async (info, syncConfig) => {
const { address, initialAccount, currency, derivationMode } = info;
const alpacaApi = (0, alpaca_1.getAlpacaApi)(currency.id, kind);
if (alpacaApi.getChainSpecificRules) {
const chainSpecificValidation = alpacaApi.getChainSpecificRules();
if (chainSpecificValidation.getAccountShape) {
chainSpecificValidation.getAccountShape(address);
}
}
const accountId = (0, index_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: address,
derivationMode,
});
const blockInfo = await alpacaApi.lastBlock();
const balanceRes = await alpacaApi.getBalance(address);
const nativeAsset = (0, utils_1.extractBalance)(balanceRes, "native");
const allTokenAssetsBalances = balanceRes.filter(b => b.asset.type !== "native");
const nativeBalance = BigInt(nativeAsset?.value ?? "0");
const spendableBalance = BigInt(nativeBalance - BigInt(nativeAsset?.locked ?? "0"));
// Normalize pre-alpaca operations to the new accountId to keep UI rendering consistent
const oldOps = (initialAccount?.operations || []).map(op => op.accountId === accountId
? op
: { ...op, accountId, id: (0, operation_1.encodeOperationId)(accountId, op.hash, op.type) });
const lastPagingToken = oldOps[0]?.extra?.pagingToken || "";
// Calculate minHeight for pagination
let minHeight = 0;
if (oldOps.length > 0 && initialAccount?.blockHeight !== 0) {
minHeight = (oldOps[0].blockHeight ?? 0) + 1;
}
const paginationParams = { minHeight, order: "asc" };
if (lastPagingToken) {
paginationParams.lastPagingToken = lastPagingToken;
}
const [newCoreOps] = await alpacaApi.listOperations(address, paginationParams);
const newOps = newCoreOps.map(op => (0, utils_1.adaptCoreOperationToLiveOperation)(accountId, op));
const mergedOps = (0, jsHelpers_1.mergeOps)(oldOps, newOps);
const assetOperations = [];
mergedOps.forEach(operation => {
if (operation?.extra?.assetReference &&
operation?.extra?.assetOwner &&
!["OPT_IN", "OPT_OUT"].includes(operation.type)) {
assetOperations.push(operation);
}
});
const subAccounts = await (0, buildSubAccounts_1.buildSubAccounts)({
currency,
accountId,
allTokenAssetsBalances,
syncConfig,
operations: assetOperations,
getTokenFromAsset: alpacaApi.getTokenFromAsset,
});
const operations = mergedOps.map(op => {
const subOperations = (0, serialization_1.inferSubOperations)(op.hash, subAccounts);
return {
...op,
subOperations,
};
});
const res = {
id: accountId,
xpub: address,
blockHeight: operations.length === 0 ? 0 : blockInfo.height || initialAccount?.blockHeight,
balance: new bignumber_js_1.default(nativeBalance.toString()),
spendableBalance: new bignumber_js_1.default(spendableBalance.toString()),
operations,
subAccounts,
operationsCount: operations.length,
};
return res;
};
}
exports.genericGetAccountShape = genericGetAccountShape;
//# sourceMappingURL=getAccountShape.js.map