@canton-network/core-token-standard-service 1.8.0 → 1.10.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 +748 -1143
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +747 -1141
- package/dist/index.js.map +1 -1
- package/dist/token-standard-service.d.ts +4 -0
- package/dist/token-standard-service.d.ts.map +1 -1
- package/package.json +17 -17
package/dist/index.js
CHANGED
|
@@ -1,1150 +1,756 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EventFilterBySetup } from
|
|
3
|
-
import { ACSReader } from
|
|
4
|
-
import {
|
|
5
|
-
import { Decimal } from
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const parser = new TransactionParser(
|
|
172
|
-
this.ledgerProvider,
|
|
173
|
-
tx,
|
|
174
|
-
partyId,
|
|
175
|
-
this.isMasterUser
|
|
176
|
-
);
|
|
177
|
-
return await parser.parseTransaction();
|
|
178
|
-
})
|
|
179
|
-
);
|
|
180
|
-
const transactionOffsets = transactions.map((tx) => tx.offset).filter((offset) => offset !== void 0);
|
|
181
|
-
return {
|
|
182
|
-
// OffsetCheckpoint can be anywhere... or not at all, maybe
|
|
183
|
-
nextOffset: Math.max(
|
|
184
|
-
latestCheckpointOffset,
|
|
185
|
-
...transactionOffsets.length > 0 ? transactionOffsets : [0]
|
|
186
|
-
),
|
|
187
|
-
transactions: transactions.filter((tx) => tx.events.length > 0).map(renderTransaction)
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
async toPrettyTransaction(getTransactionResponse, partyId) {
|
|
191
|
-
const tx = getTransactionResponse.transaction;
|
|
192
|
-
const parser = new TransactionParser(
|
|
193
|
-
this.ledgerProvider,
|
|
194
|
-
tx,
|
|
195
|
-
partyId,
|
|
196
|
-
this.isMasterUser
|
|
197
|
-
);
|
|
198
|
-
const parsedTx = await parser.parseTransaction();
|
|
199
|
-
return renderTransaction(parsedTx);
|
|
200
|
-
}
|
|
201
|
-
async toPrettyTransferObjects(getTransactionResponse, partyId) {
|
|
202
|
-
const tx = getTransactionResponse.transaction;
|
|
203
|
-
const parser = new TransactionParser(
|
|
204
|
-
this.ledgerProvider,
|
|
205
|
-
tx,
|
|
206
|
-
partyId,
|
|
207
|
-
this.isMasterUser
|
|
208
|
-
);
|
|
209
|
-
return await parser.parseTransferObjects();
|
|
210
|
-
}
|
|
211
|
-
async toPrettyTransactionsPerParty(updates, parties) {
|
|
212
|
-
const all = await Promise.all(
|
|
213
|
-
parties.map(
|
|
214
|
-
async (partyId) => [
|
|
215
|
-
partyId,
|
|
216
|
-
await this.toPrettyTransactions(updates, partyId)
|
|
217
|
-
]
|
|
218
|
-
)
|
|
219
|
-
);
|
|
220
|
-
return new Map(all);
|
|
221
|
-
}
|
|
222
|
-
// returns object with JsActiveContract content
|
|
223
|
-
// and contractId and interface view value extracted from it as separate fields for convenience
|
|
224
|
-
toPrettyContract(interfaceId, response, offset) {
|
|
225
|
-
const activeContract = response.contractEntry.JsActiveContract;
|
|
226
|
-
const { createdEvent } = activeContract;
|
|
227
|
-
return {
|
|
228
|
-
contractId: createdEvent.contractId,
|
|
229
|
-
activeContract,
|
|
230
|
-
interfaceViewValue: ensureInterfaceViewIsPresent(
|
|
231
|
-
createdEvent,
|
|
232
|
-
interfaceId
|
|
233
|
-
).viewValue,
|
|
234
|
-
fetchedAtOffset: offset
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
toQualifiedMemberId(memberId) {
|
|
238
|
-
if (!memberId) throw new Error("memberId is required");
|
|
239
|
-
return /^(PAR|MED)::/.test(memberId) ? memberId : `PAR::${memberId}`;
|
|
240
|
-
}
|
|
1
|
+
import { ALLOCATION_FACTORY_INTERFACE_ID, ALLOCATION_INSTRUCTION_INTERFACE_ID, ALLOCATION_INTERFACE_ID, ALLOCATION_REQUEST_INTERFACE_ID, FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID, HOLDING_INTERFACE_ID, TRANSFER_FACTORY_INTERFACE_ID, TRANSFER_INSTRUCTION_INTERFACE_ID, TokenStandardClient } from "@canton-network/core-token-standard";
|
|
2
|
+
import { EventFilterBySetup } from "@canton-network/core-ledger-client-types";
|
|
3
|
+
import { ACSReader } from "@canton-network/core-acs-reader";
|
|
4
|
+
import { TokenStandardTransactionInterfaces, TransactionParser, ensureInterfaceViewIsPresent, renderTransaction } from "@canton-network/core-tx-parser";
|
|
5
|
+
import { Decimal } from "decimal.js";
|
|
6
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/typeof.js
|
|
7
|
+
function _typeof(o) {
|
|
8
|
+
"@babel/helpers - typeof";
|
|
9
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
|
|
10
|
+
return typeof o;
|
|
11
|
+
} : function(o) {
|
|
12
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
13
|
+
}, _typeof(o);
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPrimitive.js
|
|
17
|
+
function toPrimitive(t, r) {
|
|
18
|
+
if ("object" != _typeof(t) || !t) return t;
|
|
19
|
+
var e = t[Symbol.toPrimitive];
|
|
20
|
+
if (void 0 !== e) {
|
|
21
|
+
var i = e.call(t, r || "default");
|
|
22
|
+
if ("object" != _typeof(i)) return i;
|
|
23
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
24
|
+
}
|
|
25
|
+
return ("string" === r ? String : Number)(t);
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/toPropertyKey.js
|
|
29
|
+
function toPropertyKey(t) {
|
|
30
|
+
var i = toPrimitive(t, "string");
|
|
31
|
+
return "symbol" == _typeof(i) ? i : i + "";
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region \0@oxc-project+runtime@0.142.0/helpers/esm/defineProperty.js
|
|
35
|
+
function _defineProperty(e, r, t) {
|
|
36
|
+
return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
37
|
+
value: t,
|
|
38
|
+
enumerable: !0,
|
|
39
|
+
configurable: !0,
|
|
40
|
+
writable: !0
|
|
41
|
+
}) : e[r] = t, e;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/token-standard-service.ts
|
|
45
|
+
const REQUESTED_AT_SKEW_MS = 6e4;
|
|
46
|
+
const EMPTY_META = { values: {} };
|
|
47
|
+
var CoreService = class CoreService {
|
|
48
|
+
constructor(ledgerProvider, logger, accessTokenProvider, isMasterUser) {
|
|
49
|
+
this.ledgerProvider = ledgerProvider;
|
|
50
|
+
this.logger = logger;
|
|
51
|
+
this.accessTokenProvider = accessTokenProvider;
|
|
52
|
+
this.isMasterUser = isMasterUser;
|
|
53
|
+
}
|
|
54
|
+
getTokenStandardClient(registryUrl) {
|
|
55
|
+
return new TokenStandardClient(registryUrl, this.logger, this.accessTokenProvider);
|
|
56
|
+
}
|
|
57
|
+
async getInputHoldingsCids(options) {
|
|
58
|
+
const { sender, instrumentAdmin, instrumentId } = options;
|
|
59
|
+
const now = /* @__PURE__ */ new Date();
|
|
60
|
+
if (options.inputUtxos && options.inputUtxos.length > 0) return options.inputUtxos;
|
|
61
|
+
const senderHoldings = await this.listContractsByInterface(HOLDING_INTERFACE_ID, sender, void 0, void 0, options.continueUntilCompletion);
|
|
62
|
+
if (senderHoldings.length === 0) throw new Error("Sender has no holdings, so transfer can't be executed.");
|
|
63
|
+
const unlockedSenderHoldings = senderHoldings.filter((utxo) => {
|
|
64
|
+
const lock = utxo.interfaceViewValue.lock;
|
|
65
|
+
if (!lock) return true;
|
|
66
|
+
const expiresAt = lock.expiresAt;
|
|
67
|
+
if (!expiresAt) return false;
|
|
68
|
+
return new Date(expiresAt) <= now;
|
|
69
|
+
});
|
|
70
|
+
if (unlockedSenderHoldings.length > 100) this.logger.warn(`Sender has more than 100 unlocked utxos.`);
|
|
71
|
+
const unlockedHoldingsForInstrument = instrumentAdmin && instrumentId ? await CoreService.filterHoldingsByInstrument({
|
|
72
|
+
holdings: unlockedSenderHoldings,
|
|
73
|
+
instrumentAdmin,
|
|
74
|
+
instrumentId
|
|
75
|
+
}) : unlockedSenderHoldings;
|
|
76
|
+
if (options.amount) return CoreService.getInputHoldingsCidsForAmount(options.amount, unlockedHoldingsForInstrument);
|
|
77
|
+
else return unlockedHoldingsForInstrument.map((h) => h.contractId);
|
|
78
|
+
}
|
|
79
|
+
static async filterHoldingsByInstrument(options) {
|
|
80
|
+
const { holdings, instrumentAdmin, instrumentId } = options;
|
|
81
|
+
return holdings.filter((utxo) => {
|
|
82
|
+
return utxo.interfaceViewValue.instrumentId.id === instrumentId && utxo.interfaceViewValue.instrumentId.admin === instrumentAdmin;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
static async getInputHoldingsCidsForAmount(amount, unlockedSenderHoldings) {
|
|
86
|
+
const exactAmount = unlockedSenderHoldings.find((holding) => new Decimal(holding.interfaceViewValue.amount).equals(amount));
|
|
87
|
+
if (exactAmount) return [exactAmount.contractId];
|
|
88
|
+
const sortedUnlockedSenderHoldings = unlockedSenderHoldings.toSorted((a, b) => new Decimal(a.interfaceViewValue.amount).comparedTo(new Decimal(b.interfaceViewValue.amount)));
|
|
89
|
+
const largestHoldingAmount = sortedUnlockedSenderHoldings.pop();
|
|
90
|
+
if (!largestHoldingAmount) throw new Error(`Sender doesn't have any unlocked holdings`);
|
|
91
|
+
let currentSum = new Decimal(largestHoldingAmount.interfaceViewValue.amount);
|
|
92
|
+
const cIds = [largestHoldingAmount.contractId];
|
|
93
|
+
for (const h of sortedUnlockedSenderHoldings) {
|
|
94
|
+
if (currentSum.greaterThanOrEqualTo(amount)) break;
|
|
95
|
+
const currentHoldingAmount = new Decimal(h.interfaceViewValue.amount);
|
|
96
|
+
currentSum = currentSum.plus(currentHoldingAmount);
|
|
97
|
+
cIds.push(h.contractId);
|
|
98
|
+
}
|
|
99
|
+
if (currentSum.lessThan(amount)) throw new Error(`Sender doesn't have sufficient funds for this transfer. Missing amount: ${amount.minus(currentSum)}`);
|
|
100
|
+
if (cIds.length > 100) throw new Error(`Exceeded the maximum of 100 utxos in 1 transaction`);
|
|
101
|
+
return cIds;
|
|
102
|
+
}
|
|
103
|
+
async listContractsByInterface(interfaceId, partyId, limit, offset, continueUntilCompletion) {
|
|
104
|
+
try {
|
|
105
|
+
const ledgerEnd = offset ?? (await this.ledgerProvider.request({
|
|
106
|
+
method: "ledgerApi",
|
|
107
|
+
params: {
|
|
108
|
+
resource: "/v2/state/ledger-end",
|
|
109
|
+
requestMethod: "get"
|
|
110
|
+
}
|
|
111
|
+
})).offset;
|
|
112
|
+
const options = {
|
|
113
|
+
offset: ledgerEnd,
|
|
114
|
+
interfaceIds: [interfaceId],
|
|
115
|
+
parties: [partyId],
|
|
116
|
+
filterByParty: true,
|
|
117
|
+
continueUntilCompletion: Boolean(continueUntilCompletion)
|
|
118
|
+
};
|
|
119
|
+
if (limit !== void 0) options.limit = limit;
|
|
120
|
+
this.logger.info(`continue to completion: ${Boolean(continueUntilCompletion)}`);
|
|
121
|
+
const acsResponses = await new ACSReader(this.ledgerProvider).raw.read(options);
|
|
122
|
+
const isActiveContractEntry = (acsResponse) => acsResponse.contractEntry != null && "JsActiveContract" in acsResponse.contractEntry && acsResponse.contractEntry.JsActiveContract != null && acsResponse.contractEntry.JsActiveContract.createdEvent != null;
|
|
123
|
+
return acsResponses.filter(isActiveContractEntry).map((response) => this.toPrettyContract(interfaceId, response, ledgerEnd));
|
|
124
|
+
} catch (err) {
|
|
125
|
+
this.logger.error(`Failed to list contracts of interface ${interfaceId}`, err);
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async toPrettyTransactions(updates, partyId) {
|
|
130
|
+
const isOffsetCheckpointUpdate = (updateResponse) => updateResponse.update != null && "OffsetCheckpoint" in updateResponse.update;
|
|
131
|
+
const isTransactionUpdate = (updateResponse) => updateResponse.update != null && "Transaction" in updateResponse.update && !!updateResponse.update.Transaction?.value;
|
|
132
|
+
const offsetCheckpoints = updates.filter(isOffsetCheckpointUpdate).map((update) => update.update.OffsetCheckpoint.value.offset);
|
|
133
|
+
const latestCheckpointOffset = offsetCheckpoints.length > 0 ? Math.max(...offsetCheckpoints) : 0;
|
|
134
|
+
const transactions = await Promise.all(updates.filter(isTransactionUpdate).map(async (update) => {
|
|
135
|
+
const tx = update.update.Transaction.value;
|
|
136
|
+
return await new TransactionParser(this.ledgerProvider, tx, partyId, this.isMasterUser).parseTransaction();
|
|
137
|
+
}));
|
|
138
|
+
const transactionOffsets = transactions.map((tx) => tx.offset).filter((offset) => offset !== void 0);
|
|
139
|
+
return {
|
|
140
|
+
nextOffset: Math.max(latestCheckpointOffset, ...transactionOffsets.length > 0 ? transactionOffsets : [0]),
|
|
141
|
+
transactions: transactions.filter((tx) => tx.events.length > 0).map(renderTransaction)
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
async toPrettyTransaction(getTransactionResponse, partyId) {
|
|
145
|
+
const tx = getTransactionResponse.transaction;
|
|
146
|
+
const parsedTx = await new TransactionParser(this.ledgerProvider, tx, partyId, this.isMasterUser).parseTransaction();
|
|
147
|
+
return renderTransaction(parsedTx);
|
|
148
|
+
}
|
|
149
|
+
async toPrettyTransferObjects(getTransactionResponse, partyId) {
|
|
150
|
+
const tx = getTransactionResponse.transaction;
|
|
151
|
+
return await new TransactionParser(this.ledgerProvider, tx, partyId, this.isMasterUser).parseTransferObjects();
|
|
152
|
+
}
|
|
153
|
+
async toPrettyTransactionsPerParty(updates, parties) {
|
|
154
|
+
const all = await Promise.all(parties.map(async (partyId) => [partyId, await this.toPrettyTransactions(updates, partyId)]));
|
|
155
|
+
return new Map(all);
|
|
156
|
+
}
|
|
157
|
+
toPrettyContract(interfaceId, response, offset) {
|
|
158
|
+
const activeContract = response.contractEntry.JsActiveContract;
|
|
159
|
+
const { createdEvent } = activeContract;
|
|
160
|
+
return {
|
|
161
|
+
contractId: createdEvent.contractId,
|
|
162
|
+
activeContract,
|
|
163
|
+
interfaceViewValue: ensureInterfaceViewIsPresent(createdEvent, interfaceId).viewValue,
|
|
164
|
+
fetchedAtOffset: offset
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
toQualifiedMemberId(memberId) {
|
|
168
|
+
if (!memberId) throw new Error("memberId is required");
|
|
169
|
+
return /^(PAR|MED)::/.test(memberId) ? memberId : `PAR::${memberId}`;
|
|
170
|
+
}
|
|
241
171
|
};
|
|
242
172
|
var AllocationService = class {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
const choiceContext = await this.fetchWithdrawAllocationChoiceContext(
|
|
397
|
-
allocationCid,
|
|
398
|
-
registryUrl
|
|
399
|
-
);
|
|
400
|
-
return this.createWithdrawAllocationFromContext(
|
|
401
|
-
allocationCid,
|
|
402
|
-
choiceContext
|
|
403
|
-
);
|
|
404
|
-
}
|
|
405
|
-
async fetchCancelAllocationChoiceContext(allocationCid, registryUrl) {
|
|
406
|
-
return this.core.getTokenStandardClient(registryUrl).post(
|
|
407
|
-
"/registry/allocations/v1/{allocationId}/choice-contexts/cancel",
|
|
408
|
-
{
|
|
409
|
-
excludeDebugFields: true
|
|
410
|
-
},
|
|
411
|
-
{ path: { allocationId: allocationCid } }
|
|
412
|
-
);
|
|
413
|
-
}
|
|
414
|
-
createCancelAllocationFromContext(allocationCid, choiceContext) {
|
|
415
|
-
return this.buildAllocationExerciseWithContext(
|
|
416
|
-
ALLOCATION_INTERFACE_ID,
|
|
417
|
-
allocationCid,
|
|
418
|
-
"Allocation_Cancel",
|
|
419
|
-
choiceContext
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
async createCancelAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
423
|
-
if (prefetchedRegistryChoiceContext) {
|
|
424
|
-
return this.createCancelAllocationFromContext(
|
|
425
|
-
allocationCid,
|
|
426
|
-
prefetchedRegistryChoiceContext
|
|
427
|
-
);
|
|
428
|
-
}
|
|
429
|
-
const choiceContext = await this.fetchCancelAllocationChoiceContext(
|
|
430
|
-
allocationCid,
|
|
431
|
-
registryUrl
|
|
432
|
-
);
|
|
433
|
-
return this.createCancelAllocationFromContext(
|
|
434
|
-
allocationCid,
|
|
435
|
-
choiceContext
|
|
436
|
-
);
|
|
437
|
-
}
|
|
438
|
-
async createWithdrawAllocationInstruction(allocationInstructionCid) {
|
|
439
|
-
const exercise = {
|
|
440
|
-
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
441
|
-
contractId: allocationInstructionCid,
|
|
442
|
-
choice: "AllocationInstruction_Withdraw",
|
|
443
|
-
choiceArgument: {
|
|
444
|
-
extraArgs: {
|
|
445
|
-
context: { values: {} },
|
|
446
|
-
meta: { values: {} }
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
};
|
|
450
|
-
return [exercise, []];
|
|
451
|
-
}
|
|
452
|
-
async createUpdateAllocationInstruction(allocationInstructionCid, extraActors = [], extraArgsContext = {}, extraArgsMeta = {}) {
|
|
453
|
-
const exercise = {
|
|
454
|
-
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
455
|
-
contractId: allocationInstructionCid,
|
|
456
|
-
choice: "AllocationInstruction_Update",
|
|
457
|
-
choiceArgument: {
|
|
458
|
-
extraActors,
|
|
459
|
-
extraArgs: {
|
|
460
|
-
context: { values: extraArgsContext },
|
|
461
|
-
meta: { values: extraArgsMeta }
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
return [exercise, []];
|
|
466
|
-
}
|
|
467
|
-
async createRejectAllocationRequest(allocationRequestCid, actor) {
|
|
468
|
-
const exercise = {
|
|
469
|
-
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
470
|
-
contractId: allocationRequestCid,
|
|
471
|
-
choice: "AllocationRequest_Reject",
|
|
472
|
-
choiceArgument: {
|
|
473
|
-
actor,
|
|
474
|
-
extraArgs: {
|
|
475
|
-
context: { values: {} },
|
|
476
|
-
meta: { values: {} }
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
return [exercise, []];
|
|
481
|
-
}
|
|
482
|
-
async createWithdrawAllocationRequest(allocationRequestCid) {
|
|
483
|
-
const exercise = {
|
|
484
|
-
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
485
|
-
contractId: allocationRequestCid,
|
|
486
|
-
choice: "AllocationRequest_Withdraw",
|
|
487
|
-
choiceArgument: {
|
|
488
|
-
extraArgs: {
|
|
489
|
-
context: { values: {} },
|
|
490
|
-
meta: { values: {} }
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
};
|
|
494
|
-
return [exercise, []];
|
|
495
|
-
}
|
|
173
|
+
constructor(core, logger) {
|
|
174
|
+
this.core = core;
|
|
175
|
+
this.logger = logger;
|
|
176
|
+
}
|
|
177
|
+
async buildAllocationFactoryChoiceArgs(allocationSpecification, expectedAdmin, inputUtxos, requestedAt) {
|
|
178
|
+
const allocationSpecificationNormalized = {
|
|
179
|
+
...allocationSpecification,
|
|
180
|
+
settlement: {
|
|
181
|
+
...allocationSpecification.settlement,
|
|
182
|
+
meta: allocationSpecification.settlement.meta ?? { values: {} }
|
|
183
|
+
},
|
|
184
|
+
transferLeg: {
|
|
185
|
+
...allocationSpecification.transferLeg,
|
|
186
|
+
meta: allocationSpecification.transferLeg.meta ?? { values: {} }
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const inputHoldingCids = await this.core.getInputHoldingsCids({
|
|
190
|
+
sender: allocationSpecificationNormalized.transferLeg.sender,
|
|
191
|
+
inputUtxos: inputUtxos ?? [],
|
|
192
|
+
instrumentAdmin: allocationSpecificationNormalized.transferLeg.instrumentId.admin,
|
|
193
|
+
instrumentId: allocationSpecificationNormalized.transferLeg.instrumentId.id
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
expectedAdmin,
|
|
197
|
+
allocation: allocationSpecificationNormalized,
|
|
198
|
+
requestedAt: requestedAt ?? (/* @__PURE__ */ new Date(Date.now() - REQUESTED_AT_SKEW_MS)).toISOString(),
|
|
199
|
+
inputHoldingCids,
|
|
200
|
+
extraArgs: {
|
|
201
|
+
context: { values: {} },
|
|
202
|
+
meta: { values: {} }
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
async fetchAllocationFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
|
|
207
|
+
return this.core.getTokenStandardClient(registryUrl).post("/registry/allocation-instruction/v1/allocation-factory", {
|
|
208
|
+
choiceArguments: choiceArgs,
|
|
209
|
+
excludeDebugFields
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async createAllocationInstructionFromContext(factoryId, choiceArgs, choiceContext) {
|
|
213
|
+
choiceArgs.extraArgs.context = {
|
|
214
|
+
...choiceContext.choiceContextData,
|
|
215
|
+
values: choiceContext.choiceContextData?.values ?? {}
|
|
216
|
+
};
|
|
217
|
+
return [{
|
|
218
|
+
templateId: ALLOCATION_FACTORY_INTERFACE_ID,
|
|
219
|
+
contractId: factoryId,
|
|
220
|
+
choice: "AllocationFactory_Allocate",
|
|
221
|
+
choiceArgument: choiceArgs
|
|
222
|
+
}, choiceContext.disclosedContracts];
|
|
223
|
+
}
|
|
224
|
+
async createAllocationInstruction(allocationSpecification, expectedAdmin, registryUrl, inputUtxos, requestedAt, prefetchedRegistryChoiceContext) {
|
|
225
|
+
const choiceArgs = await this.buildAllocationFactoryChoiceArgs(allocationSpecification, expectedAdmin, inputUtxos, requestedAt);
|
|
226
|
+
if (prefetchedRegistryChoiceContext) return this.createAllocationInstructionFromContext(prefetchedRegistryChoiceContext.factoryId, choiceArgs, prefetchedRegistryChoiceContext.choiceContext);
|
|
227
|
+
const { factoryId, choiceContext } = await this.fetchAllocationFactoryChoiceContext(registryUrl, choiceArgs);
|
|
228
|
+
return this.createAllocationInstructionFromContext(factoryId, choiceArgs, choiceContext);
|
|
229
|
+
}
|
|
230
|
+
buildAllocationExerciseWithContext(templateId, contractId, choice, choiceContext) {
|
|
231
|
+
return [{
|
|
232
|
+
templateId,
|
|
233
|
+
contractId,
|
|
234
|
+
choice,
|
|
235
|
+
choiceArgument: { extraArgs: {
|
|
236
|
+
context: choiceContext.choiceContextData,
|
|
237
|
+
meta: EMPTY_META
|
|
238
|
+
} }
|
|
239
|
+
}, choiceContext.disclosedContracts ?? []];
|
|
240
|
+
}
|
|
241
|
+
async fetchExecuteTransferChoiceContext(allocationId, registryUrl) {
|
|
242
|
+
return this.core.getTokenStandardClient(registryUrl).post("/registry/allocations/v1/{allocationId}/choice-contexts/execute-transfer", { excludeDebugFields: true }, { path: { allocationId } });
|
|
243
|
+
}
|
|
244
|
+
createExecuteTransferAllocationFromContext(allocationCid, choiceContext) {
|
|
245
|
+
return this.buildAllocationExerciseWithContext(ALLOCATION_INTERFACE_ID, allocationCid, "Allocation_ExecuteTransfer", choiceContext);
|
|
246
|
+
}
|
|
247
|
+
async createExecuteTransferAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
248
|
+
if (prefetchedRegistryChoiceContext) return this.createExecuteTransferAllocationFromContext(allocationCid, prefetchedRegistryChoiceContext);
|
|
249
|
+
const choiceContext = await this.fetchExecuteTransferChoiceContext(allocationCid, registryUrl);
|
|
250
|
+
return this.createExecuteTransferAllocationFromContext(allocationCid, choiceContext);
|
|
251
|
+
}
|
|
252
|
+
async fetchWithdrawAllocationChoiceContext(allocationCid, registryUrl) {
|
|
253
|
+
return this.core.getTokenStandardClient(registryUrl).post("/registry/allocations/v1/{allocationId}/choice-contexts/withdraw", { excludeDebugFields: true }, { path: { allocationId: allocationCid } });
|
|
254
|
+
}
|
|
255
|
+
createWithdrawAllocationFromContext(allocationCid, choiceContext) {
|
|
256
|
+
return this.buildAllocationExerciseWithContext(ALLOCATION_INTERFACE_ID, allocationCid, "Allocation_Withdraw", choiceContext);
|
|
257
|
+
}
|
|
258
|
+
async createWithdrawAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
259
|
+
if (prefetchedRegistryChoiceContext) return this.createWithdrawAllocationFromContext(allocationCid, prefetchedRegistryChoiceContext);
|
|
260
|
+
const choiceContext = await this.fetchWithdrawAllocationChoiceContext(allocationCid, registryUrl);
|
|
261
|
+
return this.createWithdrawAllocationFromContext(allocationCid, choiceContext);
|
|
262
|
+
}
|
|
263
|
+
async fetchCancelAllocationChoiceContext(allocationCid, registryUrl) {
|
|
264
|
+
return this.core.getTokenStandardClient(registryUrl).post("/registry/allocations/v1/{allocationId}/choice-contexts/cancel", { excludeDebugFields: true }, { path: { allocationId: allocationCid } });
|
|
265
|
+
}
|
|
266
|
+
createCancelAllocationFromContext(allocationCid, choiceContext) {
|
|
267
|
+
return this.buildAllocationExerciseWithContext(ALLOCATION_INTERFACE_ID, allocationCid, "Allocation_Cancel", choiceContext);
|
|
268
|
+
}
|
|
269
|
+
async createCancelAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
270
|
+
if (prefetchedRegistryChoiceContext) return this.createCancelAllocationFromContext(allocationCid, prefetchedRegistryChoiceContext);
|
|
271
|
+
const choiceContext = await this.fetchCancelAllocationChoiceContext(allocationCid, registryUrl);
|
|
272
|
+
return this.createCancelAllocationFromContext(allocationCid, choiceContext);
|
|
273
|
+
}
|
|
274
|
+
async createWithdrawAllocationInstruction(allocationInstructionCid) {
|
|
275
|
+
return [{
|
|
276
|
+
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
277
|
+
contractId: allocationInstructionCid,
|
|
278
|
+
choice: "AllocationInstruction_Withdraw",
|
|
279
|
+
choiceArgument: { extraArgs: {
|
|
280
|
+
context: { values: {} },
|
|
281
|
+
meta: { values: {} }
|
|
282
|
+
} }
|
|
283
|
+
}, []];
|
|
284
|
+
}
|
|
285
|
+
async createUpdateAllocationInstruction(allocationInstructionCid, extraActors = [], extraArgsContext = {}, extraArgsMeta = {}) {
|
|
286
|
+
return [{
|
|
287
|
+
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
288
|
+
contractId: allocationInstructionCid,
|
|
289
|
+
choice: "AllocationInstruction_Update",
|
|
290
|
+
choiceArgument: {
|
|
291
|
+
extraActors,
|
|
292
|
+
extraArgs: {
|
|
293
|
+
context: { values: extraArgsContext },
|
|
294
|
+
meta: { values: extraArgsMeta }
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}, []];
|
|
298
|
+
}
|
|
299
|
+
async createRejectAllocationRequest(allocationRequestCid, actor) {
|
|
300
|
+
return [{
|
|
301
|
+
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
302
|
+
contractId: allocationRequestCid,
|
|
303
|
+
choice: "AllocationRequest_Reject",
|
|
304
|
+
choiceArgument: {
|
|
305
|
+
actor,
|
|
306
|
+
extraArgs: {
|
|
307
|
+
context: { values: {} },
|
|
308
|
+
meta: { values: {} }
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}, []];
|
|
312
|
+
}
|
|
313
|
+
async createWithdrawAllocationRequest(allocationRequestCid) {
|
|
314
|
+
return [{
|
|
315
|
+
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
316
|
+
contractId: allocationRequestCid,
|
|
317
|
+
choice: "AllocationRequest_Withdraw",
|
|
318
|
+
choiceArgument: { extraArgs: {
|
|
319
|
+
context: { values: {} },
|
|
320
|
+
meta: { values: {} }
|
|
321
|
+
} }
|
|
322
|
+
}, []];
|
|
323
|
+
}
|
|
496
324
|
};
|
|
497
325
|
var TransferService = class {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
}
|
|
718
|
-
);
|
|
719
|
-
}
|
|
720
|
-
const choiceContext = await this.fetchAcceptTransferInstructionChoiceContext(
|
|
721
|
-
transferInstructionCid,
|
|
722
|
-
registryUrl
|
|
723
|
-
);
|
|
724
|
-
return this.createAcceptTransferInstructionFromContext(
|
|
725
|
-
transferInstructionCid,
|
|
726
|
-
choiceContext
|
|
727
|
-
);
|
|
728
|
-
}
|
|
729
|
-
async fetchRejectTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
730
|
-
const client = this.core.getTokenStandardClient(registryUrl);
|
|
731
|
-
const choiceContext = await client.post(
|
|
732
|
-
"/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/reject",
|
|
733
|
-
{
|
|
734
|
-
excludeDebugFields: true
|
|
735
|
-
},
|
|
736
|
-
{
|
|
737
|
-
path: {
|
|
738
|
-
transferInstructionId: transferInstructionCid
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
);
|
|
742
|
-
return {
|
|
743
|
-
choiceContextData: choiceContext.choiceContextData,
|
|
744
|
-
disclosedContracts: choiceContext.disclosedContracts
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
async createRejectTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
748
|
-
try {
|
|
749
|
-
const exercise = {
|
|
750
|
-
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
751
|
-
contractId: transferInstructionCid,
|
|
752
|
-
choice: "TransferInstruction_Reject",
|
|
753
|
-
choiceArgument: {
|
|
754
|
-
extraArgs: {
|
|
755
|
-
context: choiceContext.choiceContextData,
|
|
756
|
-
meta: { values: {} }
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
};
|
|
760
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
761
|
-
} catch (e) {
|
|
762
|
-
this.logger.error(
|
|
763
|
-
"Failed to create reject transfer instruction:",
|
|
764
|
-
e
|
|
765
|
-
);
|
|
766
|
-
throw e;
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
async createRejectTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
770
|
-
if (prefetchedRegistryChoiceContext) {
|
|
771
|
-
return this.createRejectTransferInstructionFromContext(
|
|
772
|
-
transferInstructionCid,
|
|
773
|
-
{
|
|
774
|
-
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
775
|
-
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
776
|
-
}
|
|
777
|
-
);
|
|
778
|
-
}
|
|
779
|
-
const choiceContext = await this.fetchRejectTransferInstructionChoiceContext(
|
|
780
|
-
transferInstructionCid,
|
|
781
|
-
registryUrl
|
|
782
|
-
);
|
|
783
|
-
return this.createRejectTransferInstructionFromContext(
|
|
784
|
-
transferInstructionCid,
|
|
785
|
-
choiceContext
|
|
786
|
-
);
|
|
787
|
-
}
|
|
788
|
-
async fetchWithdrawTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
789
|
-
const client = this.core.getTokenStandardClient(registryUrl);
|
|
790
|
-
const choiceContext = await client.post(
|
|
791
|
-
"/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/withdraw",
|
|
792
|
-
{
|
|
793
|
-
excludeDebugFields: true
|
|
794
|
-
},
|
|
795
|
-
{
|
|
796
|
-
path: {
|
|
797
|
-
transferInstructionId: transferInstructionCid
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
);
|
|
801
|
-
return {
|
|
802
|
-
choiceContextData: choiceContext.choiceContextData,
|
|
803
|
-
disclosedContracts: choiceContext.disclosedContracts
|
|
804
|
-
};
|
|
805
|
-
}
|
|
806
|
-
async createWithdrawTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
807
|
-
try {
|
|
808
|
-
const exercise = {
|
|
809
|
-
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
810
|
-
contractId: transferInstructionCid,
|
|
811
|
-
choice: "TransferInstruction_Withdraw",
|
|
812
|
-
choiceArgument: {
|
|
813
|
-
extraArgs: {
|
|
814
|
-
context: choiceContext.choiceContextData,
|
|
815
|
-
meta: { values: {} }
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
};
|
|
819
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
820
|
-
} catch (e) {
|
|
821
|
-
this.logger.error(
|
|
822
|
-
"Failed to create withdraw transfer instruction:",
|
|
823
|
-
e
|
|
824
|
-
);
|
|
825
|
-
throw e;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
async createWithdrawTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
829
|
-
if (prefetchedRegistryChoiceContext) {
|
|
830
|
-
return this.createWithdrawTransferInstructionFromContext(
|
|
831
|
-
transferInstructionCid,
|
|
832
|
-
{
|
|
833
|
-
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
834
|
-
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
835
|
-
}
|
|
836
|
-
);
|
|
837
|
-
}
|
|
838
|
-
const choiceContext = await this.fetchWithdrawTransferInstructionChoiceContext(
|
|
839
|
-
transferInstructionCid,
|
|
840
|
-
registryUrl
|
|
841
|
-
);
|
|
842
|
-
return this.createWithdrawTransferInstructionFromContext(
|
|
843
|
-
transferInstructionCid,
|
|
844
|
-
choiceContext
|
|
845
|
-
);
|
|
846
|
-
}
|
|
847
|
-
async createTransferInstruction(transferInstructionCid, registryUrl, instructionChoice, prefetchedRegistryChoiceContext) {
|
|
848
|
-
switch (instructionChoice) {
|
|
849
|
-
case "Accept":
|
|
850
|
-
return this.createAcceptTransferInstruction(
|
|
851
|
-
transferInstructionCid,
|
|
852
|
-
registryUrl,
|
|
853
|
-
prefetchedRegistryChoiceContext
|
|
854
|
-
);
|
|
855
|
-
case "Reject":
|
|
856
|
-
return this.createRejectTransferInstruction(
|
|
857
|
-
transferInstructionCid,
|
|
858
|
-
registryUrl,
|
|
859
|
-
prefetchedRegistryChoiceContext
|
|
860
|
-
);
|
|
861
|
-
case "Withdraw":
|
|
862
|
-
return this.createWithdrawTransferInstruction(
|
|
863
|
-
transferInstructionCid,
|
|
864
|
-
registryUrl,
|
|
865
|
-
prefetchedRegistryChoiceContext
|
|
866
|
-
);
|
|
867
|
-
}
|
|
868
|
-
}
|
|
326
|
+
constructor(core, logger) {
|
|
327
|
+
this.core = core;
|
|
328
|
+
this.logger = logger;
|
|
329
|
+
}
|
|
330
|
+
async buildTransferChoiceArgs(sender, receiver, amount, instrumentAdmin, instrumentId, inputUtxos, memo, expiryDate, meta, continueUntilCompletion) {
|
|
331
|
+
const inputHoldingCids = await this.core.getInputHoldingsCids({
|
|
332
|
+
sender,
|
|
333
|
+
instrumentAdmin,
|
|
334
|
+
instrumentId,
|
|
335
|
+
inputUtxos: inputUtxos ?? [],
|
|
336
|
+
amount: new Decimal(amount),
|
|
337
|
+
continueUntilCompletion: continueUntilCompletion ?? false
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
expectedAdmin: instrumentAdmin,
|
|
341
|
+
transfer: {
|
|
342
|
+
sender,
|
|
343
|
+
receiver,
|
|
344
|
+
amount,
|
|
345
|
+
instrumentId: {
|
|
346
|
+
admin: instrumentAdmin,
|
|
347
|
+
id: instrumentId
|
|
348
|
+
},
|
|
349
|
+
requestedAt: (/* @__PURE__ */ new Date(Date.now() - REQUESTED_AT_SKEW_MS)).toISOString(),
|
|
350
|
+
executeBefore: (expiryDate ?? new Date(Date.now() + 864e5)).toISOString(),
|
|
351
|
+
inputHoldingCids,
|
|
352
|
+
meta: { values: {
|
|
353
|
+
[TokenStandardService.MEMO_KEY]: memo || "",
|
|
354
|
+
...meta?.values
|
|
355
|
+
} }
|
|
356
|
+
},
|
|
357
|
+
extraArgs: {
|
|
358
|
+
context: { values: {} },
|
|
359
|
+
meta: { values: {} }
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
async fetchTransferFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
|
|
364
|
+
return await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/transfer-factory", {
|
|
365
|
+
choiceArguments: choiceArgs,
|
|
366
|
+
excludeDebugFields
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
async createTransferFromContext(factoryId, choiceArgs, choiceContext) {
|
|
370
|
+
this.logger.debug("Creating transfer from pre-fetched context...");
|
|
371
|
+
choiceArgs.extraArgs.context = {
|
|
372
|
+
...choiceContext.choiceContextData,
|
|
373
|
+
values: choiceContext.choiceContextData?.values ?? {}
|
|
374
|
+
};
|
|
375
|
+
return [{
|
|
376
|
+
templateId: TRANSFER_FACTORY_INTERFACE_ID,
|
|
377
|
+
contractId: factoryId,
|
|
378
|
+
choice: "TransferFactory_Transfer",
|
|
379
|
+
choiceArgument: choiceArgs
|
|
380
|
+
}, choiceContext.disclosedContracts];
|
|
381
|
+
}
|
|
382
|
+
async createTransfer(sender, receiver, amount, instrumentAdmin, instrumentId, registryUrl, inputUtxos, memo, expiryDate, meta, prefetchedRegistryChoiceContext, continueUntilCompletion) {
|
|
383
|
+
try {
|
|
384
|
+
const choiceArgs = await this.buildTransferChoiceArgs(sender, receiver, amount, instrumentAdmin, instrumentId, inputUtxos, memo, expiryDate, meta, continueUntilCompletion);
|
|
385
|
+
if (prefetchedRegistryChoiceContext) return this.createTransferFromContext(prefetchedRegistryChoiceContext.factoryId, choiceArgs, prefetchedRegistryChoiceContext.choiceContext);
|
|
386
|
+
const { factoryId, choiceContext } = await this.fetchTransferFactoryChoiceContext(registryUrl, choiceArgs);
|
|
387
|
+
return this.createTransferFromContext(factoryId, choiceArgs, choiceContext);
|
|
388
|
+
} catch (e) {
|
|
389
|
+
this.logger.error("Failed to execute transfer:", e);
|
|
390
|
+
throw e;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async fetchAcceptTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
394
|
+
const choiceContext = await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/accept", { excludeDebugFields: true }, { path: { transferInstructionId: transferInstructionCid } });
|
|
395
|
+
return {
|
|
396
|
+
choiceContextData: choiceContext.choiceContextData,
|
|
397
|
+
disclosedContracts: choiceContext.disclosedContracts
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
async createAcceptTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
401
|
+
try {
|
|
402
|
+
return [{
|
|
403
|
+
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
404
|
+
contractId: transferInstructionCid,
|
|
405
|
+
choice: "TransferInstruction_Accept",
|
|
406
|
+
choiceArgument: { extraArgs: {
|
|
407
|
+
context: choiceContext.choiceContextData,
|
|
408
|
+
meta: { values: {} }
|
|
409
|
+
} }
|
|
410
|
+
}, choiceContext.disclosedContracts];
|
|
411
|
+
} catch (e) {
|
|
412
|
+
this.logger.error("Failed to create accept transfer instruction:", e);
|
|
413
|
+
throw e;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
async exerciseDelegateProxyTransferInstructionAccept(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
417
|
+
const [acceptTransferInstructionContext, disclosedContracts] = await this.createAcceptTransferInstruction(transferInstructionCid, registryUrl.href);
|
|
418
|
+
const choiceArgs = {
|
|
419
|
+
cid: acceptTransferInstructionContext.contractId,
|
|
420
|
+
proxyArg: {
|
|
421
|
+
featuredAppRightCid,
|
|
422
|
+
beneficiaries,
|
|
423
|
+
choiceArg: acceptTransferInstructionContext.choiceArgument
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
return [{
|
|
427
|
+
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
428
|
+
contractId: proxyCid,
|
|
429
|
+
choice: "DelegateProxy_TransferInstruction_Accept",
|
|
430
|
+
choiceArgument: choiceArgs
|
|
431
|
+
}, disclosedContracts];
|
|
432
|
+
}
|
|
433
|
+
async exerciseDelegateProxyTransferInstructionReject(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
434
|
+
const [rejectTransferInstructionContext, disclosedContracts] = await this.createRejectTransferInstruction(transferInstructionCid, registryUrl.href);
|
|
435
|
+
const choiceArgs = {
|
|
436
|
+
cid: rejectTransferInstructionContext.contractId,
|
|
437
|
+
proxyArg: {
|
|
438
|
+
featuredAppRightCid,
|
|
439
|
+
beneficiaries,
|
|
440
|
+
choiceArg: rejectTransferInstructionContext.choiceArgument
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
return [{
|
|
444
|
+
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
445
|
+
contractId: proxyCid,
|
|
446
|
+
choice: "DelegateProxy_TransferInstruction_Reject",
|
|
447
|
+
choiceArgument: choiceArgs
|
|
448
|
+
}, disclosedContracts];
|
|
449
|
+
}
|
|
450
|
+
async exerciseDelegateProxyTransferInstructioWithdraw(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
451
|
+
const [withdrawTransferInstructionContext, disclosedContracts] = await this.createWithdrawTransferInstruction(transferInstructionCid, registryUrl.href);
|
|
452
|
+
if (beneficiaries.reduce((totalWeight, beneficiary) => totalWeight + beneficiary.weight, 0) > 1) throw new Error("Sum of beneficiary weights is larger than 1.");
|
|
453
|
+
const choiceArgs = {
|
|
454
|
+
cid: withdrawTransferInstructionContext.contractId,
|
|
455
|
+
proxyArg: {
|
|
456
|
+
featuredAppRightCid,
|
|
457
|
+
beneficiaries,
|
|
458
|
+
choiceArg: withdrawTransferInstructionContext.choiceArgument
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
return [{
|
|
462
|
+
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
463
|
+
contractId: proxyCid,
|
|
464
|
+
choice: "DelegateProxy_TransferInstruction_Withdraw",
|
|
465
|
+
choiceArgument: choiceArgs
|
|
466
|
+
}, disclosedContracts];
|
|
467
|
+
}
|
|
468
|
+
async createAcceptTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
469
|
+
if (prefetchedRegistryChoiceContext) return this.createAcceptTransferInstructionFromContext(transferInstructionCid, {
|
|
470
|
+
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
471
|
+
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
472
|
+
});
|
|
473
|
+
const choiceContext = await this.fetchAcceptTransferInstructionChoiceContext(transferInstructionCid, registryUrl);
|
|
474
|
+
return this.createAcceptTransferInstructionFromContext(transferInstructionCid, choiceContext);
|
|
475
|
+
}
|
|
476
|
+
async fetchRejectTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
477
|
+
const choiceContext = await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/reject", { excludeDebugFields: true }, { path: { transferInstructionId: transferInstructionCid } });
|
|
478
|
+
return {
|
|
479
|
+
choiceContextData: choiceContext.choiceContextData,
|
|
480
|
+
disclosedContracts: choiceContext.disclosedContracts
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
async createRejectTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
484
|
+
try {
|
|
485
|
+
return [{
|
|
486
|
+
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
487
|
+
contractId: transferInstructionCid,
|
|
488
|
+
choice: "TransferInstruction_Reject",
|
|
489
|
+
choiceArgument: { extraArgs: {
|
|
490
|
+
context: choiceContext.choiceContextData,
|
|
491
|
+
meta: { values: {} }
|
|
492
|
+
} }
|
|
493
|
+
}, choiceContext.disclosedContracts];
|
|
494
|
+
} catch (e) {
|
|
495
|
+
this.logger.error("Failed to create reject transfer instruction:", e);
|
|
496
|
+
throw e;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
async createRejectTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
500
|
+
if (prefetchedRegistryChoiceContext) return this.createRejectTransferInstructionFromContext(transferInstructionCid, {
|
|
501
|
+
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
502
|
+
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
503
|
+
});
|
|
504
|
+
const choiceContext = await this.fetchRejectTransferInstructionChoiceContext(transferInstructionCid, registryUrl);
|
|
505
|
+
return this.createRejectTransferInstructionFromContext(transferInstructionCid, choiceContext);
|
|
506
|
+
}
|
|
507
|
+
async fetchWithdrawTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
508
|
+
const choiceContext = await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/withdraw", { excludeDebugFields: true }, { path: { transferInstructionId: transferInstructionCid } });
|
|
509
|
+
return {
|
|
510
|
+
choiceContextData: choiceContext.choiceContextData,
|
|
511
|
+
disclosedContracts: choiceContext.disclosedContracts
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
async createWithdrawTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
515
|
+
try {
|
|
516
|
+
return [{
|
|
517
|
+
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
518
|
+
contractId: transferInstructionCid,
|
|
519
|
+
choice: "TransferInstruction_Withdraw",
|
|
520
|
+
choiceArgument: { extraArgs: {
|
|
521
|
+
context: choiceContext.choiceContextData,
|
|
522
|
+
meta: { values: {} }
|
|
523
|
+
} }
|
|
524
|
+
}, choiceContext.disclosedContracts];
|
|
525
|
+
} catch (e) {
|
|
526
|
+
this.logger.error("Failed to create withdraw transfer instruction:", e);
|
|
527
|
+
throw e;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
async createWithdrawTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
531
|
+
if (prefetchedRegistryChoiceContext) return this.createWithdrawTransferInstructionFromContext(transferInstructionCid, {
|
|
532
|
+
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
533
|
+
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
534
|
+
});
|
|
535
|
+
const choiceContext = await this.fetchWithdrawTransferInstructionChoiceContext(transferInstructionCid, registryUrl);
|
|
536
|
+
return this.createWithdrawTransferInstructionFromContext(transferInstructionCid, choiceContext);
|
|
537
|
+
}
|
|
538
|
+
async createTransferInstruction(transferInstructionCid, registryUrl, instructionChoice, prefetchedRegistryChoiceContext) {
|
|
539
|
+
switch (instructionChoice) {
|
|
540
|
+
case "Accept": return this.createAcceptTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext);
|
|
541
|
+
case "Reject": return this.createRejectTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext);
|
|
542
|
+
case "Withdraw": return this.createWithdrawTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
869
545
|
};
|
|
870
546
|
var TokenStandardService = class {
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
sender,
|
|
1076
|
-
receiver,
|
|
1077
|
-
amount,
|
|
1078
|
-
instrumentAdmin,
|
|
1079
|
-
instrumentId,
|
|
1080
|
-
registryUrl,
|
|
1081
|
-
inputUtxos,
|
|
1082
|
-
memo,
|
|
1083
|
-
expiryDate,
|
|
1084
|
-
meta
|
|
1085
|
-
);
|
|
1086
|
-
const sumOfWeights = beneficiaries.reduce(
|
|
1087
|
-
(totalWeight, beneficiary) => totalWeight + beneficiary.weight,
|
|
1088
|
-
0
|
|
1089
|
-
);
|
|
1090
|
-
if (sumOfWeights > 1) {
|
|
1091
|
-
throw new Error("Sum of beneficiary weights is larger than 1.");
|
|
1092
|
-
}
|
|
1093
|
-
const choiceArgs = {
|
|
1094
|
-
cid: transferCommand.contractId,
|
|
1095
|
-
proxyArg: {
|
|
1096
|
-
featuredAppRightCid,
|
|
1097
|
-
beneficiaries,
|
|
1098
|
-
choiceArg: transferCommand.choiceArgument
|
|
1099
|
-
}
|
|
1100
|
-
};
|
|
1101
|
-
const exercise = {
|
|
1102
|
-
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
1103
|
-
contractId: proxyCid,
|
|
1104
|
-
choice: "DelegateProxy_TransferFactory_Transfer",
|
|
1105
|
-
choiceArgument: choiceArgs
|
|
1106
|
-
};
|
|
1107
|
-
return [exercise, disclosedContracts];
|
|
1108
|
-
}
|
|
1109
|
-
async exerciseDelegateProxyTransferInstructionAccept(exchangeParty, proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid) {
|
|
1110
|
-
const [acceptTransferInstructionContext, disclosedContracts] = await this.transfer.createAcceptTransferInstruction(
|
|
1111
|
-
transferInstructionCid,
|
|
1112
|
-
registryUrl
|
|
1113
|
-
);
|
|
1114
|
-
const choiceArgs = {
|
|
1115
|
-
cid: acceptTransferInstructionContext.contractId,
|
|
1116
|
-
proxyArg: {
|
|
1117
|
-
featuredAppRightCid,
|
|
1118
|
-
beneficiaries: [
|
|
1119
|
-
{
|
|
1120
|
-
beneficiary: exchangeParty,
|
|
1121
|
-
weight: 1
|
|
1122
|
-
}
|
|
1123
|
-
],
|
|
1124
|
-
choiceArg: acceptTransferInstructionContext.choiceArgument
|
|
1125
|
-
}
|
|
1126
|
-
};
|
|
1127
|
-
return [
|
|
1128
|
-
{
|
|
1129
|
-
templateId: "#splice-util-featured-app-proxies:Splice.Util.FeaturedApp.DelegateProxy:DelegateProxy",
|
|
1130
|
-
contractId: proxyCid,
|
|
1131
|
-
choice: "DelegateProxy_TransferInstruction_Accept",
|
|
1132
|
-
choiceArgument: choiceArgs
|
|
1133
|
-
},
|
|
1134
|
-
disclosedContracts
|
|
1135
|
-
];
|
|
1136
|
-
}
|
|
1137
|
-
static isHoldingLocked(holding, currentTime = /* @__PURE__ */ new Date()) {
|
|
1138
|
-
const lock = holding.lock;
|
|
1139
|
-
if (!lock) return false;
|
|
1140
|
-
const expiresAt = lock.expiresAt;
|
|
1141
|
-
if (!expiresAt) return true;
|
|
1142
|
-
const expiresAtDate = new Date(expiresAt);
|
|
1143
|
-
return currentTime < expiresAtDate;
|
|
1144
|
-
}
|
|
547
|
+
constructor(ledgerProvider, logger, accessTokenProvider, isMasterUser) {
|
|
548
|
+
this.ledgerProvider = ledgerProvider;
|
|
549
|
+
this.logger = logger;
|
|
550
|
+
this.accessTokenProvider = accessTokenProvider;
|
|
551
|
+
this.isMasterUser = isMasterUser;
|
|
552
|
+
_defineProperty(this, "core", void 0);
|
|
553
|
+
_defineProperty(this, "allocation", void 0);
|
|
554
|
+
_defineProperty(this, "transfer", void 0);
|
|
555
|
+
this.core = new CoreService(ledgerProvider, logger, accessTokenProvider, isMasterUser);
|
|
556
|
+
this.allocation = new AllocationService(this.core, this.logger);
|
|
557
|
+
this.transfer = new TransferService(this.core, this.logger);
|
|
558
|
+
}
|
|
559
|
+
async getInstrumentById(registryUrl, instrumentId) {
|
|
560
|
+
try {
|
|
561
|
+
const params = { path: { instrumentId } };
|
|
562
|
+
return this.core.getTokenStandardClient(registryUrl).get("/registry/metadata/v1/instruments/{instrumentId}", params);
|
|
563
|
+
} catch (e) {
|
|
564
|
+
this.logger.error(e);
|
|
565
|
+
throw new Error(`Instrument id ${instrumentId} does not exist for this instrument admin.`, { cause: e });
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async getInstrumentAdmin(registryUrl) {
|
|
569
|
+
return (await this.core.getTokenStandardClient(registryUrl).get("/registry/metadata/v1/info")).adminId;
|
|
570
|
+
}
|
|
571
|
+
async listInstruments(registryUrl, pageSize, pageToken) {
|
|
572
|
+
return this.core.getTokenStandardClient(registryUrl).get("/registry/metadata/v1/instruments", { query: {
|
|
573
|
+
...pageSize && { pageSize },
|
|
574
|
+
...pageToken && { pageToken }
|
|
575
|
+
} });
|
|
576
|
+
}
|
|
577
|
+
async instrumentsToAsset(registryUrl) {
|
|
578
|
+
let instrumentsResponse = await this.listInstruments(registryUrl);
|
|
579
|
+
const instruments = [...instrumentsResponse.instruments];
|
|
580
|
+
while (instrumentsResponse.nextPageToken) {
|
|
581
|
+
instrumentsResponse = await this.listInstruments(registryUrl, void 0, instrumentsResponse.nextPageToken);
|
|
582
|
+
instruments.push(...instrumentsResponse.instruments);
|
|
583
|
+
}
|
|
584
|
+
const instrumentAdmin = await this.getInstrumentAdmin(registryUrl);
|
|
585
|
+
return instruments.map((instrument) => ({
|
|
586
|
+
id: instrument.id,
|
|
587
|
+
displayName: instrument.name,
|
|
588
|
+
symbol: instrument.symbol,
|
|
589
|
+
registryUrl,
|
|
590
|
+
admin: instrumentAdmin
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
593
|
+
async registriesToAssets(registryUrls) {
|
|
594
|
+
const allInstruments = [];
|
|
595
|
+
for (const registryUrl of registryUrls) {
|
|
596
|
+
const instruments = await this.instrumentsToAsset(registryUrl);
|
|
597
|
+
allInstruments.push(...instruments);
|
|
598
|
+
}
|
|
599
|
+
return allInstruments;
|
|
600
|
+
}
|
|
601
|
+
async listContractsByInterface(interfaceId, partyId, limit, offset, continueUntilCompletion) {
|
|
602
|
+
return this.core.listContractsByInterface(interfaceId, partyId, limit, offset, continueUntilCompletion);
|
|
603
|
+
}
|
|
604
|
+
async listHoldingTransactions(partyId, afterOffset, beforeOffset) {
|
|
605
|
+
try {
|
|
606
|
+
this.logger.debug("Set or query offset");
|
|
607
|
+
const afterOffsetOrLatest = afterOffset ?? (await this.ledgerProvider.request({
|
|
608
|
+
method: "ledgerApi",
|
|
609
|
+
params: {
|
|
610
|
+
resource: "/v2/state/latest-pruned-offsets",
|
|
611
|
+
requestMethod: "get"
|
|
612
|
+
}
|
|
613
|
+
})).participantPrunedUpToInclusive;
|
|
614
|
+
const beforeOffsetOrLatest = beforeOffset ?? (await this.ledgerProvider.request({
|
|
615
|
+
method: "ledgerApi",
|
|
616
|
+
params: {
|
|
617
|
+
resource: "/v2/state/ledger-end",
|
|
618
|
+
requestMethod: "get"
|
|
619
|
+
}
|
|
620
|
+
})).offset;
|
|
621
|
+
this.logger.debug(afterOffsetOrLatest, "Using offset");
|
|
622
|
+
const updatesResponse = await this.ledgerProvider.request({
|
|
623
|
+
method: "ledgerApi",
|
|
624
|
+
params: {
|
|
625
|
+
resource: "/v2/updates/flats",
|
|
626
|
+
requestMethod: "post",
|
|
627
|
+
query: {},
|
|
628
|
+
body: {
|
|
629
|
+
updateFormat: { includeTransactions: {
|
|
630
|
+
eventFormat: EventFilterBySetup({
|
|
631
|
+
interfaceIds: TokenStandardTransactionInterfaces,
|
|
632
|
+
isMasterUser: this.isMasterUser,
|
|
633
|
+
partyId,
|
|
634
|
+
includeWildcard: true
|
|
635
|
+
}),
|
|
636
|
+
transactionShape: "TRANSACTION_SHAPE_LEDGER_EFFECTS"
|
|
637
|
+
} },
|
|
638
|
+
beginExclusive: afterOffsetOrLatest,
|
|
639
|
+
endInclusive: beforeOffsetOrLatest,
|
|
640
|
+
verbose: false
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
return this.core.toPrettyTransactions(updatesResponse, partyId);
|
|
645
|
+
} catch (err) {
|
|
646
|
+
this.logger.error("Failed to list holding transactions.", err);
|
|
647
|
+
throw err;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
async getTransactionById(updateId, partyId) {
|
|
651
|
+
const transactionFormat = {
|
|
652
|
+
eventFormat: EventFilterBySetup({
|
|
653
|
+
interfaceIds: TokenStandardTransactionInterfaces,
|
|
654
|
+
isMasterUser: this.isMasterUser,
|
|
655
|
+
partyId,
|
|
656
|
+
includeWildcard: true
|
|
657
|
+
}),
|
|
658
|
+
transactionShape: "TRANSACTION_SHAPE_LEDGER_EFFECTS"
|
|
659
|
+
};
|
|
660
|
+
const getTransactionResponse = await this.ledgerProvider.request({
|
|
661
|
+
method: "ledgerApi",
|
|
662
|
+
params: {
|
|
663
|
+
resource: "/v2/updates/transaction-by-id",
|
|
664
|
+
requestMethod: "post",
|
|
665
|
+
body: {
|
|
666
|
+
updateId,
|
|
667
|
+
transactionFormat
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
return this.core.toPrettyTransaction(getTransactionResponse, partyId);
|
|
672
|
+
}
|
|
673
|
+
async getTransferObjectsById(updateId, partyId) {
|
|
674
|
+
const transactionFormat = {
|
|
675
|
+
eventFormat: EventFilterBySetup({
|
|
676
|
+
interfaceIds: TokenStandardTransactionInterfaces,
|
|
677
|
+
isMasterUser: this.isMasterUser,
|
|
678
|
+
partyId,
|
|
679
|
+
includeWildcard: true
|
|
680
|
+
}),
|
|
681
|
+
transactionShape: "TRANSACTION_SHAPE_LEDGER_EFFECTS"
|
|
682
|
+
};
|
|
683
|
+
const getTransactionResponse = await this.ledgerProvider.request({
|
|
684
|
+
method: "ledgerApi",
|
|
685
|
+
params: {
|
|
686
|
+
resource: "/v2/updates/transaction-by-id",
|
|
687
|
+
requestMethod: "post",
|
|
688
|
+
body: {
|
|
689
|
+
updateId,
|
|
690
|
+
transactionFormat
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
return this.core.toPrettyTransferObjects(getTransactionResponse, partyId);
|
|
695
|
+
}
|
|
696
|
+
async getInputHoldingsCids(sender, inputUtxos, amount) {
|
|
697
|
+
if (amount) return this.core.getInputHoldingsCids({
|
|
698
|
+
sender,
|
|
699
|
+
inputUtxos: inputUtxos ?? [],
|
|
700
|
+
amount
|
|
701
|
+
});
|
|
702
|
+
else return this.core.getInputHoldingsCids({
|
|
703
|
+
sender,
|
|
704
|
+
inputUtxos: inputUtxos ?? []
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
async createDelegateProxyTransfer(sender, receiver, amount, instrumentAdmin, instrumentId, registryUrl, featuredAppRightCid, proxyCid, beneficiaries, inputUtxos, memo, expiryDate, meta) {
|
|
708
|
+
const [transferCommand, disclosedContracts] = await this.transfer.createTransfer(sender, receiver, amount, instrumentAdmin, instrumentId, registryUrl, inputUtxos, memo, expiryDate, meta);
|
|
709
|
+
if (beneficiaries.reduce((totalWeight, beneficiary) => totalWeight + beneficiary.weight, 0) > 1) throw new Error("Sum of beneficiary weights is larger than 1.");
|
|
710
|
+
const choiceArgs = {
|
|
711
|
+
cid: transferCommand.contractId,
|
|
712
|
+
proxyArg: {
|
|
713
|
+
featuredAppRightCid,
|
|
714
|
+
beneficiaries,
|
|
715
|
+
choiceArg: transferCommand.choiceArgument
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
return [{
|
|
719
|
+
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
720
|
+
contractId: proxyCid,
|
|
721
|
+
choice: "DelegateProxy_TransferFactory_Transfer",
|
|
722
|
+
choiceArgument: choiceArgs
|
|
723
|
+
}, disclosedContracts];
|
|
724
|
+
}
|
|
725
|
+
async exerciseDelegateProxyTransferInstructionAccept(exchangeParty, proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid) {
|
|
726
|
+
const [acceptTransferInstructionContext, disclosedContracts] = await this.transfer.createAcceptTransferInstruction(transferInstructionCid, registryUrl);
|
|
727
|
+
return [{
|
|
728
|
+
templateId: "#splice-util-featured-app-proxies:Splice.Util.FeaturedApp.DelegateProxy:DelegateProxy",
|
|
729
|
+
contractId: proxyCid,
|
|
730
|
+
choice: "DelegateProxy_TransferInstruction_Accept",
|
|
731
|
+
choiceArgument: {
|
|
732
|
+
cid: acceptTransferInstructionContext.contractId,
|
|
733
|
+
proxyArg: {
|
|
734
|
+
featuredAppRightCid,
|
|
735
|
+
beneficiaries: [{
|
|
736
|
+
beneficiary: exchangeParty,
|
|
737
|
+
weight: 1
|
|
738
|
+
}],
|
|
739
|
+
choiceArg: acceptTransferInstructionContext.choiceArgument
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}, disclosedContracts];
|
|
743
|
+
}
|
|
744
|
+
static isHoldingLocked(holding, currentTime = /* @__PURE__ */ new Date()) {
|
|
745
|
+
const lock = holding.lock;
|
|
746
|
+
if (!lock) return false;
|
|
747
|
+
const expiresAt = lock.expiresAt;
|
|
748
|
+
if (!expiresAt) return true;
|
|
749
|
+
return currentTime < new Date(expiresAt);
|
|
750
|
+
}
|
|
1145
751
|
};
|
|
1146
|
-
|
|
1147
|
-
|
|
752
|
+
_defineProperty(TokenStandardService, "MEMO_KEY", "splice.lfdecentralizedtrust.org/reason");
|
|
753
|
+
//#endregion
|
|
1148
754
|
export { CoreService, TokenStandardService };
|
|
1149
|
-
|
|
755
|
+
|
|
1150
756
|
//# sourceMappingURL=index.js.map
|