@fairmint/canton-node-sdk
Version:
Canton Node SDK
108 lines • 4.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetActiveContracts = void 0;
const zod_1 = require("zod");
const WebSocketClient_1 = require("../../../../../core/ws/WebSocketClient");
const event_format_builder_1 = require("../utils/event-format-builder");
const path = '/v2/state/active-contracts';
/**
* We intentionally do not expose the JSON/REST version of this endpoint. The REST variant is too limited, while the
* WebSocket variant returns the same snapshot and automatically closes the connection once the current state is sent.
* Wrapping the WebSocket call behind a simple awaitable method gives a better DX and keeps the door open for optional
* streaming via a callback.
*/
const ActiveContractsParamsSchema = zod_1.z.object({
/** Optional list of parties to scope the filter. */
parties: zod_1.z.array(zod_1.z.string()).optional(),
/** Optional template filters applied server-side. */
templateIds: zod_1.z.array(zod_1.z.string()).optional(),
/** Include created event blob in TemplateFilter results (default false). */
includeCreatedEventBlob: zod_1.z.boolean().optional(),
/** Allow caller to omit activeAtOffset; we'll default to ledger end */
activeAtOffset: zod_1.z.number().optional(),
});
class GetActiveContracts {
constructor(client) {
this.client = client;
}
async execute(params) {
const validated = ActiveContractsParamsSchema.parse(params);
// Determine activeAtOffset (default to ledger end if not specified)
let { activeAtOffset } = validated;
if (activeAtOffset === undefined) {
const ledgerEnd = await this.client.getLedgerEnd({});
activeAtOffset = ledgerEnd.offset;
}
// Build party list - default to client's party list if not provided
const partyList = validated.parties && validated.parties.length > 0 ? validated.parties : this.client.buildPartyList();
// Build event format
const eventFormat = (0, event_format_builder_1.buildEventFormat)({
parties: partyList,
...(validated.templateIds !== undefined && { templateIds: validated.templateIds }),
...(validated.includeCreatedEventBlob !== undefined && {
includeCreatedEventBlob: validated.includeCreatedEventBlob,
}),
});
// Build request message
const requestMessage = {
filter: undefined,
verbose: false,
activeAtOffset,
eventFormat,
};
const wsClient = new WebSocketClient_1.WebSocketClient(this.client);
return new Promise((resolve, reject) => {
const results = [];
let settled = false;
void wsClient
.connect(path, requestMessage, {
onMessage: (raw) => {
try {
// Skip Zod validation for response types - just use the raw parsed JSON
// Zod validation is only needed for input types, not outputs
const parsed = raw;
// Distinguish item vs error union members
if (typeof parsed === 'object' && 'contractEntry' in parsed) {
const item = parsed;
results.push(item);
if (typeof params.onItem === 'function') {
params.onItem(item);
}
}
else if (!settled) {
// Treat any non-item as an error message
settled = true;
reject(parsed);
}
}
catch (e) {
if (!settled) {
settled = true;
reject(e);
}
}
},
onError: (err) => {
if (!settled) {
settled = true;
reject(err);
}
},
onClose: () => {
if (!settled) {
settled = true;
resolve(results);
}
},
})
.catch((err) => {
if (!settled) {
settled = true;
reject(err);
}
});
});
}
}
exports.GetActiveContracts = GetActiveContracts;
//# sourceMappingURL=get-active-contracts.js.map