@canton-network/core-ledger-client 0.20.2 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -236,27 +236,25 @@ async function awaitCompletion(ledgerClient, ledgerEnd, partyId, userId, command
236
236
  }
237
237
  );
238
238
  const completions = responses.filter(
239
- (response) => !!response.completionResponse.Completion
239
+ (r) => "Completion" in r.completionResponse
240
240
  );
241
- const wantedCompletion = completions.find((response) => {
242
- const completion = response.completionResponse.Completion;
243
- if (!completion) return false;
244
- if (completion.value.userId !== userId) return false;
245
- if (completion.value.commandId === commandIdOrSubmissionId) return true;
246
- if (completion.value.submissionId === commandIdOrSubmissionId)
247
- return true;
241
+ const wantedCompletion = responses.find((r) => {
242
+ if ("Completion" in r.completionResponse) {
243
+ const completion = r.completionResponse.Completion.value;
244
+ return completion.userId === userId && (completion.commandId === commandIdOrSubmissionId || completion.submissionId === commandIdOrSubmissionId);
245
+ }
248
246
  return false;
249
247
  });
250
- if (wantedCompletion) {
251
- const completion = wantedCompletion.completionResponse.Completion;
252
- const status = completion.value.status;
248
+ if (wantedCompletion && "Completion" in wantedCompletion.completionResponse) {
249
+ const completion = wantedCompletion.completionResponse.Completion.value;
250
+ const status = completion.status;
253
251
  if (status && status.code !== 0) {
254
252
  throw asGrpcError(status);
255
253
  }
256
- return completion.value;
254
+ return completion;
257
255
  } else {
258
256
  const lastCompletion = completions[completions.length - 1];
259
- const newLedgerEnd = lastCompletion?.completionResponse.Completion.value.offset;
257
+ const newLedgerEnd = lastCompletion && "Completion" in lastCompletion.completionResponse ? lastCompletion.completionResponse.Completion.value.offset : void 0;
260
258
  return awaitCompletion(
261
259
  ledgerClient,
262
260
  newLedgerEnd || ledgerEnd,
@@ -316,7 +314,7 @@ async function retryable(fn, retryableOptions2, logger) {
316
314
  }
317
315
  throw new Error("retryable: Exceeded maximum retries without throwing.");
318
316
  }
319
- var isJsCantonError = (e) => typeof e === "object" && e !== null && "status" in e && "errorCategory" in e;
317
+ var isJsCantonError = (e) => typeof e === "object" && e !== null && "code" in e && "cause" in e && "errorCategory" in e;
320
318
  var asJsCantonError = (e) => {
321
319
  if (isJsCantonError(e)) {
322
320
  return e;
@@ -1026,7 +1024,7 @@ function isLeafEventNode(result) {
1026
1024
  return !!result.event;
1027
1025
  }
1028
1026
  function getNodeIdAndEvent(event) {
1029
- if (event.ExercisedEvent) {
1027
+ if ("ExercisedEvent" in event) {
1030
1028
  if (event.ExercisedEvent.choice === "Archive") {
1031
1029
  return {
1032
1030
  nodeId: event.ExercisedEvent.nodeId,
@@ -1038,12 +1036,12 @@ function getNodeIdAndEvent(event) {
1038
1036
  exercisedEvent: event.ExercisedEvent
1039
1037
  };
1040
1038
  }
1041
- } else if (event.CreatedEvent) {
1039
+ } else if ("CreatedEvent" in event) {
1042
1040
  return {
1043
1041
  nodeId: event.CreatedEvent.nodeId,
1044
1042
  createdEvent: event.CreatedEvent
1045
1043
  };
1046
- } else if (event.ArchivedEvent) {
1044
+ } else if ("ArchivedEvent" in event) {
1047
1045
  return {
1048
1046
  nodeId: event.ArchivedEvent.nodeId,
1049
1047
  archivedEvent: event.ArchivedEvent
@@ -1105,7 +1103,6 @@ function holdingChangesNonEmpty(event) {
1105
1103
  }
1106
1104
 
1107
1105
  // src/token-standard-service.ts
1108
- var MEMO_KEY = "splice.lfdecentralizedtrust.org/reason";
1109
1106
  var REQUESTED_AT_SKEW_MS = 6e4;
1110
1107
  var EMPTY_META = { values: {} };
1111
1108
  var CoreService = class _CoreService {
@@ -1210,7 +1207,7 @@ var CoreService = class _CoreService {
1210
1207
  limit: limit ?? 100
1211
1208
  };
1212
1209
  const acsResponses = await this.ledgerClient.activeContracts(options);
1213
- const isActiveContractEntry = (acsResponse) => !!acsResponse.contractEntry.JsActiveContract?.createdEvent;
1210
+ const isActiveContractEntry = (acsResponse) => "JsActiveContract" in acsResponse.contractEntry && !!acsResponse.contractEntry.JsActiveContract?.createdEvent;
1214
1211
  const activeContractEntries = acsResponses.filter(
1215
1212
  isActiveContractEntry
1216
1213
  );
@@ -1226,8 +1223,8 @@ var CoreService = class _CoreService {
1226
1223
  }
1227
1224
  }
1228
1225
  async toPrettyTransactions(updates, partyId, ledgerClient) {
1229
- const isOffsetCheckpointUpdate = (updateResponse) => !!updateResponse?.update?.OffsetCheckpoint;
1230
- const isTransactionUpdate = (updateResponse) => !!updateResponse.update?.Transaction?.value;
1226
+ const isOffsetCheckpointUpdate = (updateResponse) => "OffsetCheckpoint" in updateResponse.update;
1227
+ const isTransactionUpdate = (updateResponse) => "Transaction" in updateResponse.update && !!updateResponse.update.Transaction?.value;
1231
1228
  const offsetCheckpoints = updates.filter(isOffsetCheckpointUpdate).map((update) => update.update.OffsetCheckpoint.value.offset);
1232
1229
  const latestCheckpointOffset = Math.max(...offsetCheckpoints);
1233
1230
  const transactions = await Promise.all(
@@ -1331,9 +1328,10 @@ var AllocationService = class {
1331
1328
  }
1332
1329
  };
1333
1330
  }
1334
- async fetchAllocationFactoryChoiceContext(registryUrl, choiceArgs) {
1331
+ async fetchAllocationFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
1335
1332
  return this.core.getTokenStandardClient(registryUrl).post("/registry/allocation-instruction/v1/allocation-factory", {
1336
- choiceArguments: choiceArgs
1333
+ choiceArguments: choiceArgs,
1334
+ excludeDebugFields
1337
1335
  });
1338
1336
  }
1339
1337
  async createAllocationInstructionFromContext(factoryId, choiceArgs, choiceContext) {
@@ -1566,7 +1564,12 @@ var TransferService = class {
1566
1564
  ).toISOString(),
1567
1565
  executeBefore: (expiryDate ?? new Date(Date.now() + 24 * 60 * 60 * 1e3)).toISOString(),
1568
1566
  inputHoldingCids,
1569
- meta: { values: { [MEMO_KEY]: memo || "", ...meta?.values } }
1567
+ meta: {
1568
+ values: {
1569
+ [TokenStandardService.MEMO_KEY]: memo || "",
1570
+ ...meta?.values
1571
+ }
1572
+ }
1570
1573
  },
1571
1574
  extraArgs: {
1572
1575
  context: { values: {} },
@@ -1574,9 +1577,10 @@ var TransferService = class {
1574
1577
  }
1575
1578
  };
1576
1579
  }
1577
- async fetchTransferFactoryChoiceContext(registryUrl, choiceArgs) {
1580
+ async fetchTransferFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
1578
1581
  return await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/transfer-factory", {
1579
- choiceArguments: choiceArgs
1582
+ choiceArguments: choiceArgs,
1583
+ excludeDebugFields
1580
1584
  });
1581
1585
  }
1582
1586
  async createTransferFromContext(factoryId, choiceArgs, choiceContext) {
@@ -1874,6 +1878,28 @@ var TransferService = class {
1874
1878
  choiceContext
1875
1879
  );
1876
1880
  }
1881
+ async createTransferInstruction(transferInstructionCid, registryUrl, instructionChoice, prefetchedRegistryChoiceContext) {
1882
+ switch (instructionChoice) {
1883
+ case "Accept":
1884
+ return this.createAcceptTransferInstruction(
1885
+ transferInstructionCid,
1886
+ registryUrl,
1887
+ prefetchedRegistryChoiceContext
1888
+ );
1889
+ case "Reject":
1890
+ return this.createRejectTransferInstruction(
1891
+ transferInstructionCid,
1892
+ registryUrl,
1893
+ prefetchedRegistryChoiceContext
1894
+ );
1895
+ case "Withdraw":
1896
+ return this.createWithdrawTransferInstruction(
1897
+ transferInstructionCid,
1898
+ registryUrl,
1899
+ prefetchedRegistryChoiceContext
1900
+ );
1901
+ }
1902
+ }
1877
1903
  };
1878
1904
  var TokenStandardService = class {
1879
1905
  constructor(ledgerClient, logger, accessTokenProvider, isMasterUser) {
@@ -2067,6 +2093,7 @@ var TokenStandardService = class {
2067
2093
  ];
2068
2094
  }
2069
2095
  };
2096
+ __publicField(TokenStandardService, "MEMO_KEY", "splice.lfdecentralizedtrust.org/reason");
2070
2097
 
2071
2098
  // src/amulet-service.ts
2072
2099
  var REQUESTED_AT_SKEW_MS2 = 6e4;
@@ -2400,11 +2427,10 @@ var ACSContainer = class _ACSContainer {
2400
2427
  }
2401
2428
  });
2402
2429
  const createdContracts = acs.initialAcs.concat(addedContracts);
2403
- const result = createdContracts.filter(
2404
- (contract) => !removedContractIds.has(
2405
- contract.contractEntry.JsActiveContract?.createdEvent.contractId ?? ""
2406
- )
2407
- );
2430
+ const result = createdContracts.filter(({ contractEntry }) => {
2431
+ const id = "JsActiveContract" in contractEntry ? contractEntry.JsActiveContract.createdEvent.contractId : "";
2432
+ return !removedContractIds.has(id);
2433
+ });
2408
2434
  return Promise.resolve(result);
2409
2435
  }
2410
2436
  static extractEvents(fromOffset, updates) {
@@ -2681,7 +2707,7 @@ var ACSHelper = class _ACSHelper {
2681
2707
  cacheSize: this.contractsSet.size,
2682
2708
  hitRate: hitRate.toFixed(2) + "%",
2683
2709
  averageLookupTime: avgLookupTime.toFixed(3) + " ms",
2684
- cacheServeTime: this.totalCacheServeTime.toFixed(3)
2710
+ cacheServeTime: SharedACSCacheStats.totalCacheServeTime.toFixed(3)
2685
2711
  };
2686
2712
  }
2687
2713
  static createKey(party, templateId, interfaceId) {
@@ -2709,41 +2735,57 @@ var ACSHelper = class _ACSHelper {
2709
2735
  this.contractsSet.set(keyStr, newContainer);
2710
2736
  return newContainer;
2711
2737
  }
2712
- async activeContractsForTemplate(offset, partyFilter, templateId) {
2713
- const key = _ACSHelper.createKey(partyFilter, templateId, void 0);
2738
+ async updateSingleKey(offset, key) {
2714
2739
  const start = performance.now();
2715
2740
  const container = this.findACSContainer(key);
2716
- const result = container.update(
2741
+ const result = await container.update(
2717
2742
  offset,
2718
2743
  key,
2719
2744
  this.apiInstance,
2720
2745
  this.wsSupport
2721
2746
  );
2722
2747
  const end = performance.now();
2723
- if (this.contractsSet.has(
2724
- _ACSHelper.keyToString(key, this.apiInstance.baseUrl.href)
2725
- )) {
2748
+ const keyStr = _ACSHelper.keyToString(key, this.apiInstance.baseUrl.href);
2749
+ if (this.contractsSet.has(keyStr)) {
2726
2750
  SharedACSCacheStats.totalCacheServeTime += end - start;
2727
2751
  }
2728
2752
  return result;
2729
2753
  }
2754
+ async queryAcsByKeys(offset, keys) {
2755
+ const result = [];
2756
+ for (const key of keys) {
2757
+ const contracts = await this.updateSingleKey(offset, key);
2758
+ result.push(...contracts);
2759
+ }
2760
+ return result;
2761
+ }
2762
+ async activeContractsForTemplates(offset, parties, templateIds) {
2763
+ const keys = parties.flatMap(
2764
+ (party) => templateIds.map(
2765
+ (templateId) => _ACSHelper.createKey(party, templateId, void 0)
2766
+ )
2767
+ );
2768
+ return this.queryAcsByKeys(offset, keys);
2769
+ }
2770
+ async activeContractsForInterfaces(offset, parties, interfaceIds) {
2771
+ const keys = parties.flatMap(
2772
+ (party) => interfaceIds.map(
2773
+ (interfaceId) => _ACSHelper.createKey(party, void 0, interfaceId)
2774
+ )
2775
+ );
2776
+ return this.queryAcsByKeys(offset, keys);
2777
+ }
2778
+ async activeContractsForTemplate(offset, partyFilter, templateId) {
2779
+ return this.updateSingleKey(
2780
+ offset,
2781
+ _ACSHelper.createKey(partyFilter, templateId, void 0)
2782
+ );
2783
+ }
2730
2784
  async activeContractsForInterface(offset, partyFilter, interfaceId) {
2731
- const key = _ACSHelper.createKey(partyFilter, void 0, interfaceId);
2732
- const start = performance.now();
2733
- const container = this.findACSContainer(key);
2734
- const result = container.update(
2785
+ return this.updateSingleKey(
2735
2786
  offset,
2736
- key,
2737
- this.apiInstance,
2738
- this.wsSupport
2787
+ _ACSHelper.createKey(partyFilter, void 0, interfaceId)
2739
2788
  );
2740
- const end = performance.now();
2741
- if (this.contractsSet.has(
2742
- _ACSHelper.keyToString(key, this.apiInstance.baseUrl.href)
2743
- )) {
2744
- SharedACSCacheStats.totalCacheServeTime += end - start;
2745
- }
2746
- return result;
2747
2789
  }
2748
2790
  };
2749
2791
 
@@ -3089,41 +3131,22 @@ var LedgerClient = class {
3089
3131
  }
3090
3132
  }
3091
3133
  async activeContracts(options) {
3092
- const {
3093
- offset,
3094
- templateIds,
3095
- parties,
3096
- filterByParty,
3097
- interfaceIds,
3098
- limit
3099
- } = options;
3134
+ const { offset, templateIds, parties, interfaceIds, limit } = options;
3100
3135
  this.logger.debug(options, "options for active contracts");
3101
- if (templateIds?.length === 1 && parties?.length === 1) {
3102
- const party = parties[0];
3103
- const templateId = templateIds[0];
3104
- return this.acsHelper.activeContractsForTemplate(
3136
+ const hasParties = Array.isArray(parties) && parties.length > 0;
3137
+ if (templateIds?.length && hasParties) {
3138
+ return this.acsHelper.activeContractsForTemplates(
3105
3139
  offset,
3106
- party,
3107
- templateId
3108
- );
3109
- }
3110
- if (interfaceIds?.length === 1 && parties?.length === 1) {
3111
- const party = parties[0];
3112
- const interfaceId = interfaceIds[0];
3113
- return this.acsHelper.activeContractsForInterface(
3114
- offset,
3115
- party,
3116
- interfaceId
3140
+ parties,
3141
+ templateIds
3117
3142
  );
3118
3143
  }
3119
- if (filterByParty && !templateIds?.length && !interfaceIds?.length && parties?.length === 1) {
3120
- const party = parties[0];
3121
- const r = this.acsHelper.activeContractsForInterface(
3144
+ if (interfaceIds?.length && hasParties) {
3145
+ return this.acsHelper.activeContractsForInterfaces(
3122
3146
  offset,
3123
- party,
3124
- ""
3147
+ parties,
3148
+ interfaceIds
3125
3149
  );
3126
- return r;
3127
3150
  }
3128
3151
  const filter = this.buildActiveContractFilter(options);
3129
3152
  this.logger.debug("falling back to post request");