@canton-network/core-token-standard-service 2.1.0 → 2.3.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.js CHANGED
@@ -2,10 +2,10 @@ import { ALLOCATION_FACTORY_INTERFACE_ID, ALLOCATION_INSTRUCTION_INTERFACE_ID, A
2
2
  import { EventFilterBySetup } from "@canton-network/core-ledger-client-types";
3
3
  import { TokenStandardTransactionInterfaces, TransactionParser, ensureInterfaceViewIsPresent, renderTransaction } from "@canton-network/core-tx-parser";
4
4
  import Decimal$1, { Decimal } from "decimal.js";
5
- import { ALLOCATION_FACTORY_INTERFACE_ID_V2, ALLOCATION_INSTRUCTION_INTERFACE_ID_V2, ALLOCATION_INTERFACE_ID_V2, ALLOCATION_REQUEST_INTERFACE_ID_V2, TRANSFER_FACTORY_INTERFACE_ID_V2, TRANSFER_INSTRUCTION_INTERFACE_ID_V2, TokenStandardClient as TokenStandardClient$1, TransferInstruction as TransferInstruction$1 } from "@canton-network/core-token-standard-v2";
5
+ import { ALLOCATION_FACTORY_INTERFACE_ID_V2, ALLOCATION_INSTRUCTION_INTERFACE_ID_V2, ALLOCATION_INTERFACE_ID_V2, ALLOCATION_REQUEST_INTERFACE_ID_V2, AllocationInstruction, HOLDING_INTERFACE_ID_V2, TRANSFER_FACTORY_INTERFACE_ID_V2, TRANSFER_INSTRUCTION_INTERFACE_ID_V2, TokenStandardClient as TokenStandardClient$1, TransferInstruction as TransferInstruction$1 } from "@canton-network/core-token-standard-v2";
6
6
  import { getLogger } from "@logtape/logtape";
7
7
  import { ACSReader } from "@canton-network/core-acs-reader";
8
- //#region src/types.ts
8
+ // src/types.ts
9
9
  const REQUESTED_AT_SKEW_MS = 6e4;
10
10
  const EMPTY_META = { values: {} };
11
11
  const SUPPORTED_VERSIONS = ["v1", "v2"];
@@ -16,8 +16,7 @@ const KEY_MAPPING = {
16
16
  "allocation-instruction": "allocationInstruction",
17
17
  "allocation-request": "allocationRequest"
18
18
  };
19
- //#endregion
20
- //#region \0@oxc-project+runtime@0.154.0/helpers/esm/typeof.js
19
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
21
20
  function _typeof(o) {
22
21
  "@babel/helpers - typeof";
23
22
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -26,8 +25,7 @@ function _typeof(o) {
26
25
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
27
26
  }, _typeof(o);
28
27
  }
29
- //#endregion
30
- //#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPrimitive.js
28
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
31
29
  function toPrimitive(t, r) {
32
30
  if ("object" != _typeof(t) || !t) return t;
33
31
  var e = t[Symbol.toPrimitive];
@@ -38,14 +36,12 @@ function toPrimitive(t, r) {
38
36
  }
39
37
  return ("string" === r ? String : Number)(t);
40
38
  }
41
- //#endregion
42
- //#region \0@oxc-project+runtime@0.154.0/helpers/esm/toPropertyKey.js
39
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
43
40
  function toPropertyKey(t) {
44
41
  var i = toPrimitive(t, "string");
45
42
  return "symbol" == _typeof(i) ? i : i + "";
46
43
  }
47
- //#endregion
48
- //#region \0@oxc-project+runtime@0.154.0/helpers/esm/defineProperty.js
44
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
49
45
  function _defineProperty(e, r, t) {
50
46
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
51
47
  value: t,
@@ -54,8 +50,7 @@ function _defineProperty(e, r, t) {
54
50
  writable: !0
55
51
  }) : e[r] = t, e;
56
52
  }
57
- //#endregion
58
- //#region src/v2/transfer-service.ts
53
+ // src/v2/transfer-service.ts
59
54
  var TransferServiceV2 = class {
60
55
  constructor(core) {
61
56
  this.core = core;
@@ -165,8 +160,7 @@ var TransferServiceV2 = class {
165
160
  }, ctx.disclosedContracts ?? []];
166
161
  }
167
162
  };
168
- //#endregion
169
- //#region src/v1/transfer-service.ts
163
+ // src/v1/transfer-service.ts
170
164
  var TransferService = class {
171
165
  constructor(core) {
172
166
  this.core = core;
@@ -392,8 +386,7 @@ var TransferService = class {
392
386
  }
393
387
  }
394
388
  };
395
- //#endregion
396
- //#region src/v1/allocation-service.ts
389
+ // src/v1/allocation-service.ts
397
390
  var AllocationService$1 = class {
398
391
  constructor(core) {
399
392
  this.core = core;
@@ -546,8 +539,7 @@ var AllocationService$1 = class {
546
539
  }, []];
547
540
  }
548
541
  };
549
- //#endregion
550
- //#region src/core-service.ts
542
+ // src/core-service.ts
551
543
  var CoreService = class CoreService {
552
544
  constructor(ledgerProvider, accessTokenProvider, isMasterUser) {
553
545
  this.ledgerProvider = ledgerProvider;
@@ -584,7 +576,7 @@ var CoreService = class CoreService {
584
576
  const party = opts.partyId ?? opts.account.owner;
585
577
  if (!party) throw new Error("No party to query as");
586
578
  const now = /* @__PURE__ */ new Date();
587
- const holdings = (await this.listContractsByInterface("#splice-api-token-holding-v2:Splice.Api.Token.HoldingV2:Holding", party, opts.limit, opts.offset, opts.continueUntilCompletion ?? false)).filter((h) => h.interfaceViewValue.instrumentId.admin === opts.instrumentAdmin && h.interfaceViewValue.instrumentId.id == opts.instrumentId && this.isSameAccount(opts.account, h.interfaceViewValue.account) && !TokenStandardService.isHoldingLocked(h, now));
579
+ const holdings = (await this.listContractsByInterface(HOLDING_INTERFACE_ID_V2, party, opts.limit, opts.offset, opts.continueUntilCompletion ?? true)).filter((h) => h.interfaceViewValue.instrumentId.admin === opts.instrumentAdmin && h.interfaceViewValue.instrumentId.id === opts.instrumentId && this.isSameAccount(opts.account, h.interfaceViewValue.account) && !TokenStandardService.isHoldingLocked(h, now) && !opts.excludeCids?.has(h.contractId));
588
580
  return opts.amount ? CoreService.getInputHoldingsCidsForAmount(opts.amount, holdings) : holdings.map((h) => h.contractId);
589
581
  }
590
582
  async getInputHoldingsCids(options) {
@@ -705,8 +697,7 @@ var CoreService = class CoreService {
705
697
  return /^(PAR|MED)::/.test(memberId) ? memberId : `PAR::${memberId}`;
706
698
  }
707
699
  };
708
- //#endregion
709
- //#region src/v2/allocation-service.ts
700
+ // src/v2/allocation-service.ts
710
701
  const EMPTY_EXTRA_ARGS = () => ({
711
702
  context: { values: {} },
712
703
  meta: { values: {} }
@@ -743,7 +734,8 @@ var AllocationService = class AllocationService {
743
734
  account: spec.authorizer,
744
735
  instrumentAdmin: admin,
745
736
  instrumentId,
746
- amount
737
+ amount,
738
+ excludeCids: used
747
739
  });
748
740
  cids.forEach((c) => used.add(c));
749
741
  inputHoldingCids.push(...cids);
@@ -789,10 +781,15 @@ var AllocationService = class AllocationService {
789
781
  return this.createAllocationChoiceFromContext(action, allocationCid, actors, ctx);
790
782
  }
791
783
  createAllocationRequestChoice(action, allocationRequestCid, actors) {
784
+ const choice = {
785
+ accept: "AllocationRequest_Accept",
786
+ reject: "AllocationRequest_Reject",
787
+ withdraw: "AllocationRequest_Withdraw"
788
+ }[action];
792
789
  return [{
793
790
  templateId: ALLOCATION_REQUEST_INTERFACE_ID_V2,
794
791
  contractId: allocationRequestCid,
795
- choice: action === "reject" ? "AllocationRequest_Reject" : "AllocationRequest_Withdraw",
792
+ choice,
796
793
  choiceArgument: {
797
794
  actors,
798
795
  extraArgs: EMPTY_EXTRA_ARGS()
@@ -834,12 +831,36 @@ var AllocationService = class AllocationService {
834
831
  choiceArgument: choiceArgs
835
832
  }, choiceContext.disclosedContracts];
836
833
  }
834
+ async fetchAllocationInstructionChoiceContext(action, allocationInstructionCid, registryUrl, excludeDebugFields = true) {
835
+ return this.core.getTokenStandardClientV2(registryUrl).post(`/registry/allocation-instruction/v2/{allocationInstructionId}/choice-contexts/${action}`, { excludeDebugFields }, { path: { allocationInstructionId: allocationInstructionCid } });
836
+ }
837
+ async createAllocationInstructionChoice(action, allocationInstructionCid, actors, registryUrl, excludeDebugFields = true, prefetched) {
838
+ const ctx = prefetched ?? await this.fetchAllocationInstructionChoiceContext(action, allocationInstructionCid, registryUrl, excludeDebugFields);
839
+ return [{
840
+ templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID_V2,
841
+ contractId: allocationInstructionCid,
842
+ choice: action === "accept" ? AllocationInstruction.AllocationInstruction_Accept.choiceName : AllocationInstruction.AllocationInstruction_Withdraw.choiceName,
843
+ choiceArgument: {
844
+ actors,
845
+ extraArgs: {
846
+ context: ctx.choiceContextData,
847
+ meta: { values: {} }
848
+ }
849
+ }
850
+ }, ctx.disclosedContracts ?? []];
851
+ }
837
852
  async fetchSettlementFactory(args, registryUrl) {
838
853
  return this.core.getTokenStandardClientV2(registryUrl).post("/registry/allocation/v2/settlement-factory", {
839
854
  choiceArguments: args,
840
855
  excludeDebugFields: true
841
856
  });
842
857
  }
858
+ /**
859
+ *
860
+ * @deprecated Use {@link createAllocationsForRequestForLoggedInParty}
861
+ * instead, which creates only the allocations owned by a given party and
862
+ * reports allocations whose registry could not be resolved
863
+ */
843
864
  async createAllocationsForRequest(opts) {
844
865
  const used = /* @__PURE__ */ new Set();
845
866
  const results = [];
@@ -868,9 +889,60 @@ var AllocationService = class AllocationService {
868
889
  const dcs = new Map(results.flatMap(([, d]) => d).filter((d) => d.contractId !== void 0).map((d) => [d.contractId, d]));
869
890
  return [results.map(([c]) => c), [...dcs.values()]];
870
891
  }
892
+ /**
893
+ * Prepares the commands for a party to accept an allocation request.
894
+ *
895
+ * Creates one `AllocationFactory_Allocate` command for each allocation in the
896
+ * request that the party owns, followed by an `AllocationRequest_Accept`
897
+ * command. Holdings used as inputs by one allocation are excluded from later
898
+ * allocations, so no holding is spent twice within the same request.
899
+ *
900
+ * If no registry serves an allocation's instrument admin, that allocation is
901
+ * skipped and its admin is added to `skipped`. In that case `accept` is
902
+ * `null`, because the request cannot be accepted until every allocation
903
+ * exists.
904
+ *
905
+ * @param opts.requestCid - Contract ID of the allocation request.
906
+ * @param opts.request - The allocation request's interface view.
907
+ * @param opts.registry - Resolves an instrument admin to its registry URL,
908
+ * or `null` if no registry serves that admin.
909
+ * @param opts.partyId - The party accepting the request. Only allocations
910
+ * this party owns are created, and it acts on every command.
911
+ * @returns The allocate commands with their disclosed contracts, the accept
912
+ * command with its disclosed contracts (or `null` if any allocation was
913
+ * skipped), and the admins whose allocations were skipped.
914
+ * @throws If the request contains no allocations owned by `partyId`.
915
+ */
916
+ async createAllocationsForRequestForLoggedInParty(opts) {
917
+ const specs = opts.request.allocations.filter((p) => p.authorizer.owner === opts.partyId);
918
+ if (specs.length === 0) throw new Error(`Request ${opts.requestCid} has no allocation for account ${opts.partyId}`);
919
+ const used = /* @__PURE__ */ new Set();
920
+ const allocate = [];
921
+ const skipped = [];
922
+ for (const spec of specs) {
923
+ const registryUrl = await opts.registry(spec.admin);
924
+ if (!registryUrl) {
925
+ skipped.push(spec.admin);
926
+ continue;
927
+ }
928
+ const [cmd, dcs] = await this.createAllocation({
929
+ settlement: opts.request.settlement,
930
+ spec,
931
+ admin: spec.admin,
932
+ actors: [opts.partyId],
933
+ excludeCids: used
934
+ }, registryUrl);
935
+ for (const cid of cmd.choiceArgument.inputHoldingCids) used.add(cid);
936
+ allocate.push([cmd, dcs]);
937
+ }
938
+ return {
939
+ allocate,
940
+ accept: skipped.length === 0 ? this.createAllocationRequestChoice("accept", opts.requestCid, [opts.partyId]) : null,
941
+ skipped
942
+ };
943
+ }
871
944
  };
872
- //#endregion
873
- //#region src/token-standard-service.ts
945
+ // src/token-standard-service.ts
874
946
  function isApiVersion(v) {
875
947
  return SUPPORTED_VERSIONS.includes(v);
876
948
  }
@@ -894,7 +966,7 @@ function resolveCapabilities(opts) {
894
966
  for (const key in resolvedCapabilities) resolvedCapabilities[key].sort();
895
967
  return resolvedCapabilities;
896
968
  }
897
- var TokenStandardService = class {
969
+ var TokenStandardService = class TokenStandardService {
898
970
  constructor(ledgerProvider, accessTokenProvider, isMasterUser) {
899
971
  this.ledgerProvider = ledgerProvider;
900
972
  this.accessTokenProvider = accessTokenProvider;
@@ -1105,13 +1177,12 @@ var TokenStandardService = class {
1105
1177
  }
1106
1178
  }, disclosedContracts];
1107
1179
  }
1108
- static isHoldingLocked(holding, currentTime = /* @__PURE__ */ new Date()) {
1109
- const lock = holding.interfaceViewValue.lock;
1180
+ static checkLock(lock, createdAtTime, currentTime = /* @__PURE__ */ new Date()) {
1110
1181
  if (!lock) return false;
1111
1182
  let expiresAtAbsolute = null;
1112
1183
  let expiresAtRelative = null;
1113
1184
  if (lock.expiresAfter) {
1114
- const createdAt = new Date(holding.activeContract.createdEvent.createdAt);
1185
+ const createdAt = new Date(createdAtTime);
1115
1186
  const msToAdd = parseInt(lock.expiresAfter.microseconds) / 1e3;
1116
1187
  expiresAtRelative = new Date(createdAt.getTime() + msToAdd);
1117
1188
  }
@@ -1123,9 +1194,12 @@ var TokenStandardService = class {
1123
1194
  else return true;
1124
1195
  return currentTime < expiresAt;
1125
1196
  }
1197
+ static isHoldingLocked(holding, currentTime = /* @__PURE__ */ new Date()) {
1198
+ const lock = holding.interfaceViewValue.lock;
1199
+ return TokenStandardService.checkLock(lock, holding.activeContract.createdEvent.createdAt, currentTime);
1200
+ }
1126
1201
  };
1127
1202
  _defineProperty(TokenStandardService, "MEMO_KEY", "splice.lfdecentralizedtrust.org/reason");
1128
- //#endregion
1129
1203
  export { EMPTY_META, KEY_MAPPING, REQUESTED_AT_SKEW_MS, SUPPORTED_VERSIONS, TokenStandardService, isApiVersion, resolveCapabilities };
1130
1204
 
1131
1205
  //# sourceMappingURL=index.js.map