@ledgerhq/coin-canton
Version:
80 lines • 3.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSubAccounts = buildSubAccounts;
const index_1 = require("@ledgerhq/ledger-wallet-framework/account/index");
const jsHelpers_1 = require("@ledgerhq/ledger-wallet-framework/bridge/jsHelpers");
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function buildSubAccounts({ accountId, tokenBalances, existingSubAccounts, allOperations, pendingTransferProposals, calTokens, }) {
if (tokenBalances.length === 0)
return [];
const tokenAccounts = [];
const existingAccountByTicker = {};
const existingAccountTickers = [];
for (const existingSubAccount of existingSubAccounts) {
if (existingSubAccount.type === "TokenAccount") {
const { ticker } = existingSubAccount.token;
existingAccountTickers.push(ticker);
existingAccountByTicker[ticker] = existingSubAccount;
}
}
for (const { totalBalance, spendableBalance, token, adminId } of tokenBalances) {
const initialTokenAccount = existingAccountByTicker[token.ticker];
const instrumentId = calTokens.get(token.id) || token.name;
// Filter operations for this specific instrument (both id and admin must match)
const tokenOperations = allOperations.filter(op => {
const extra = op.extra;
return extra?.instrumentId === instrumentId && extra?.instrumentAdmin === adminId;
});
// Filter pending transfer proposals for this specific instrument
const tokenPendingTransferProposals = pendingTransferProposals.filter(proposal => proposal.instrument_id === instrumentId && proposal.instrument_admin === adminId);
const tokenAccount = buildSubAccount({
totalBalance: new bignumber_js_1.default(totalBalance.toString()),
spendableBalance: new bignumber_js_1.default(spendableBalance.toString()),
accountId,
initialTokenAccount,
parentAccountId: accountId,
token,
operations: tokenOperations,
pendingTransferProposals: tokenPendingTransferProposals,
});
if (tokenAccount)
tokenAccounts.push(tokenAccount);
}
return tokenAccounts;
}
function buildSubAccount({ totalBalance, spendableBalance, accountId, initialTokenAccount, parentAccountId, token, operations, pendingTransferProposals, }) {
const subAccountId = (0, index_1.encodeTokenAccountId)(accountId, token);
// Merge old operations with new ones and update accountId for sub-account
const oldOperations = initialTokenAccount?.operations || [];
const newOperations = operations.map(op => ({
...op,
id: (0, operation_1.encodeOperationId)(subAccountId, op.hash, op.type),
accountId: subAccountId,
}));
const mergedOperations = (0, jsHelpers_1.mergeOps)(oldOperations, newOperations);
const creationDate = mergedOperations.length > 0
? new Date(Math.min(...mergedOperations.map(op => op.date.getTime())))
: new Date();
return {
type: "TokenAccount",
id: subAccountId,
parentId: parentAccountId,
token,
balance: totalBalance,
spendableBalance,
operationsCount: mergedOperations.length,
operations: mergedOperations,
creationDate,
pendingOperations: initialTokenAccount?.pendingOperations || [],
balanceHistoryCache: initialTokenAccount?.balanceHistoryCache || index_1.emptyHistoryCache,
swapHistory: [],
cantonResources: {
pendingTransferProposals,
},
};
}
//# sourceMappingURL=buildSubAccounts.js.map