UNPKG

@fairmint/canton-node-sdk

Version:
71 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getLockedAmuletsForParty = getLockedAmuletsForParty; function assertString(value, label) { if (typeof value !== 'string' || value.trim() === '') { throw new Error(`${label} must be a non-empty string`); } return value; } function parseEffectiveAmount(raw, label) { const parsed = Number.parseFloat(raw ?? '0'); if (!Number.isFinite(parsed) || parsed <= 0) { throw new Error(`${label} has an invalid effective amount (${raw ?? 'undefined'})`); } return parsed; } function parseHolders(raw) { if (!Array.isArray(raw)) { return []; } return raw .map((holder) => { if (typeof holder === 'string') { return holder; } if (holder && typeof holder === 'object' && typeof holder.owner === 'string') { return holder.owner; } return null; }) .filter((holder) => typeof holder === 'string' && holder.trim() !== ''); } function toLockedAmulet(entry, index) { const label = `locked amulet #${index + 1}`; const { contract } = entry.contract; const payload = (contract.payload ?? {}); const amuletPayload = (payload['amulet'] ?? {}); const lockPayload = (payload['lock'] ?? {}); const contractId = assertString(contract.contract_id, `${label} contract_id`); const templateId = assertString(contract.template_id, `${label} template_id`); const owner = assertString(amuletPayload['owner'], `${label} owner`); const holders = parseHolders(lockPayload['holders']); const { expiresAt } = lockPayload; const lockExpiresAt = typeof expiresAt === 'string' ? expiresAt : null; const effectiveAmount = parseEffectiveAmount(entry.effective_amount, `${label} effective_amount`); const domainId = assertString(entry.contract.domain_id, `${label} domain_id`); const createdEventBlob = assertString(contract.created_event_blob, `${label} created_event_blob`); return { contractId, templateId, owner, holders, lockExpiresAt, effectiveAmount, domainId, createdEventBlob, }; } /** * Fetch locked amulets for the authenticated validator and filter them for the provided party ID. This intentionally * enforces the canonical JsActiveContract shape so unexpected API responses fail fast. */ async function getLockedAmuletsForParty(validatorClient, ownerPartyId) { const response = await validatorClient.getAmulets(); const lockedEntries = response.locked_amulets; const normalizedOwner = ownerPartyId.toLowerCase(); return lockedEntries .map((entry, index) => toLockedAmulet(entry, index)) .filter((amulet) => amulet.owner.toLowerCase() === normalizedOwner); } //# sourceMappingURL=get-locked-amulets.js.map