@canton-network/core-token-standard-service 2.2.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,7 +2,7 @@ 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
8
  // src/types.ts
@@ -16,7 +16,7 @@ const KEY_MAPPING = {
16
16
  "allocation-instruction": "allocationInstruction",
17
17
  "allocation-request": "allocationRequest"
18
18
  };
19
- // \0@oxc-project+runtime@0.155.0/helpers/esm/typeof.js
19
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/typeof.js
20
20
  function _typeof(o) {
21
21
  "@babel/helpers - typeof";
22
22
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
@@ -25,7 +25,7 @@ function _typeof(o) {
25
25
  return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
26
26
  }, _typeof(o);
27
27
  }
28
- // \0@oxc-project+runtime@0.155.0/helpers/esm/toPrimitive.js
28
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPrimitive.js
29
29
  function toPrimitive(t, r) {
30
30
  if ("object" != _typeof(t) || !t) return t;
31
31
  var e = t[Symbol.toPrimitive];
@@ -36,12 +36,12 @@ function toPrimitive(t, r) {
36
36
  }
37
37
  return ("string" === r ? String : Number)(t);
38
38
  }
39
- // \0@oxc-project+runtime@0.155.0/helpers/esm/toPropertyKey.js
39
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/toPropertyKey.js
40
40
  function toPropertyKey(t) {
41
41
  var i = toPrimitive(t, "string");
42
42
  return "symbol" == _typeof(i) ? i : i + "";
43
43
  }
44
- // \0@oxc-project+runtime@0.155.0/helpers/esm/defineProperty.js
44
+ // \0@oxc-project+runtime@0.156.0/helpers/esm/defineProperty.js
45
45
  function _defineProperty(e, r, t) {
46
46
  return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
47
47
  value: t,
@@ -576,7 +576,7 @@ var CoreService = class CoreService {
576
576
  const party = opts.partyId ?? opts.account.owner;
577
577
  if (!party) throw new Error("No party to query as");
578
578
  const now = /* @__PURE__ */ new Date();
579
- 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));
580
580
  return opts.amount ? CoreService.getInputHoldingsCidsForAmount(opts.amount, holdings) : holdings.map((h) => h.contractId);
581
581
  }
582
582
  async getInputHoldingsCids(options) {
@@ -734,7 +734,8 @@ var AllocationService = class AllocationService {
734
734
  account: spec.authorizer,
735
735
  instrumentAdmin: admin,
736
736
  instrumentId,
737
- amount
737
+ amount,
738
+ excludeCids: used
738
739
  });
739
740
  cids.forEach((c) => used.add(c));
740
741
  inputHoldingCids.push(...cids);
@@ -780,10 +781,15 @@ var AllocationService = class AllocationService {
780
781
  return this.createAllocationChoiceFromContext(action, allocationCid, actors, ctx);
781
782
  }
782
783
  createAllocationRequestChoice(action, allocationRequestCid, actors) {
784
+ const choice = {
785
+ accept: "AllocationRequest_Accept",
786
+ reject: "AllocationRequest_Reject",
787
+ withdraw: "AllocationRequest_Withdraw"
788
+ }[action];
783
789
  return [{
784
790
  templateId: ALLOCATION_REQUEST_INTERFACE_ID_V2,
785
791
  contractId: allocationRequestCid,
786
- choice: action === "reject" ? "AllocationRequest_Reject" : "AllocationRequest_Withdraw",
792
+ choice,
787
793
  choiceArgument: {
788
794
  actors,
789
795
  extraArgs: EMPTY_EXTRA_ARGS()
@@ -825,12 +831,36 @@ var AllocationService = class AllocationService {
825
831
  choiceArgument: choiceArgs
826
832
  }, choiceContext.disclosedContracts];
827
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
+ }
828
852
  async fetchSettlementFactory(args, registryUrl) {
829
853
  return this.core.getTokenStandardClientV2(registryUrl).post("/registry/allocation/v2/settlement-factory", {
830
854
  choiceArguments: args,
831
855
  excludeDebugFields: true
832
856
  });
833
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
+ */
834
864
  async createAllocationsForRequest(opts) {
835
865
  const used = /* @__PURE__ */ new Set();
836
866
  const results = [];
@@ -859,6 +889,58 @@ var AllocationService = class AllocationService {
859
889
  const dcs = new Map(results.flatMap(([, d]) => d).filter((d) => d.contractId !== void 0).map((d) => [d.contractId, d]));
860
890
  return [results.map(([c]) => c), [...dcs.values()]];
861
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
+ }
862
944
  };
863
945
  // src/token-standard-service.ts
864
946
  function isApiVersion(v) {
@@ -884,7 +966,7 @@ function resolveCapabilities(opts) {
884
966
  for (const key in resolvedCapabilities) resolvedCapabilities[key].sort();
885
967
  return resolvedCapabilities;
886
968
  }
887
- var TokenStandardService = class {
969
+ var TokenStandardService = class TokenStandardService {
888
970
  constructor(ledgerProvider, accessTokenProvider, isMasterUser) {
889
971
  this.ledgerProvider = ledgerProvider;
890
972
  this.accessTokenProvider = accessTokenProvider;
@@ -1095,13 +1177,12 @@ var TokenStandardService = class {
1095
1177
  }
1096
1178
  }, disclosedContracts];
1097
1179
  }
1098
- static isHoldingLocked(holding, currentTime = /* @__PURE__ */ new Date()) {
1099
- const lock = holding.interfaceViewValue.lock;
1180
+ static checkLock(lock, createdAtTime, currentTime = /* @__PURE__ */ new Date()) {
1100
1181
  if (!lock) return false;
1101
1182
  let expiresAtAbsolute = null;
1102
1183
  let expiresAtRelative = null;
1103
1184
  if (lock.expiresAfter) {
1104
- const createdAt = new Date(holding.activeContract.createdEvent.createdAt);
1185
+ const createdAt = new Date(createdAtTime);
1105
1186
  const msToAdd = parseInt(lock.expiresAfter.microseconds) / 1e3;
1106
1187
  expiresAtRelative = new Date(createdAt.getTime() + msToAdd);
1107
1188
  }
@@ -1113,6 +1194,10 @@ var TokenStandardService = class {
1113
1194
  else return true;
1114
1195
  return currentTime < expiresAt;
1115
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
+ }
1116
1201
  };
1117
1202
  _defineProperty(TokenStandardService, "MEMO_KEY", "splice.lfdecentralizedtrust.org/reason");
1118
1203
  export { EMPTY_META, KEY_MAPPING, REQUESTED_AT_SKEW_MS, SUPPORTED_VERSIONS, TokenStandardService, isApiVersion, resolveCapabilities };