@canton-network/core-ledger-client 0.27.3 → 0.29.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/acs/acs-helper.d.ts +2 -2
- package/dist/index.cjs +134 -2359
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +134 -2348
- package/dist/index.js.map +1 -1
- package/dist/ledger-api-utils.d.ts +0 -33
- package/dist/ledger-api-utils.d.ts.map +1 -1
- package/dist/ledger-client.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/amulet-service.d.ts +0 -37
- package/dist/amulet-service.d.ts.map +0 -1
- package/dist/constants.d.ts +0 -14
- package/dist/constants.d.ts.map +0 -1
- package/dist/token-standard-service.d.ts +0 -142
- package/dist/token-standard-service.d.ts.map +0 -1
- package/dist/txparse/instrumentmap.d.ts +0 -18
- package/dist/txparse/instrumentmap.d.ts.map +0 -1
- package/dist/txparse/parser.d.ts +0 -27
- package/dist/txparse/parser.d.ts.map +0 -1
- package/dist/txparse/strict.d.ts +0 -3
- package/dist/txparse/strict.d.ts.map +0 -1
- package/dist/txparse/test-data/mock/eventsByContractIdResponses.d.ts +0 -1046
- package/dist/txparse/test-data/mock/eventsByContractIdResponses.d.ts.map +0 -1
- package/dist/txparse/types.d.ts +0 -159
- package/dist/txparse/types.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,2367 +1,149 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
function matchInterfaceIds(a, b) {
|
|
24
|
-
const aParts = splitInterfaceId(a);
|
|
25
|
-
const bParts = splitInterfaceId(b);
|
|
26
|
-
return aParts !== null && bParts !== null && aParts.moduleName === bParts.moduleName && aParts.entityName === bParts.entityName;
|
|
27
|
-
}
|
|
28
|
-
var TokenStandardTransactionInterfaces = [
|
|
29
|
-
HOLDING_INTERFACE_ID,
|
|
30
|
-
TRANSFER_FACTORY_INTERFACE_ID,
|
|
31
|
-
TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
32
|
-
ALLOCATION_FACTORY_INTERFACE_ID,
|
|
33
|
-
ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
34
|
-
ALLOCATION_INTERFACE_ID,
|
|
35
|
-
ALLOCATION_REQUEST_INTERFACE_ID
|
|
36
|
-
];
|
|
37
|
-
var SpliceMetaKeyPrefix = "splice.lfdecentralizedtrust.org/";
|
|
38
|
-
var TxKindMetaKey = `${SpliceMetaKeyPrefix}tx-kind`;
|
|
39
|
-
var SenderMetaKey = `${SpliceMetaKeyPrefix}sender`;
|
|
40
|
-
var ReasonMetaKey = `${SpliceMetaKeyPrefix}reason`;
|
|
41
|
-
var BurnedMetaKey = `${SpliceMetaKeyPrefix}burned`;
|
|
42
|
-
var AllKnownMetaKeys = [
|
|
43
|
-
TxKindMetaKey,
|
|
44
|
-
SenderMetaKey,
|
|
45
|
-
ReasonMetaKey,
|
|
46
|
-
BurnedMetaKey
|
|
47
|
-
];
|
|
48
|
-
function hasInterface(interfaceId, event) {
|
|
49
|
-
return (event.implementedInterfaces || []).some(
|
|
50
|
-
(id) => matchInterfaceIds(id, interfaceId)
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
function getInterfaceView(createdEvent) {
|
|
54
|
-
const interfaceViews = createdEvent.interfaceViews || null;
|
|
55
|
-
return interfaceViews && interfaceViews[0] || null;
|
|
56
|
-
}
|
|
57
|
-
function getKnownInterfaceView(createdEvent) {
|
|
58
|
-
const interfaceView = getInterfaceView(createdEvent);
|
|
59
|
-
if (!interfaceView) {
|
|
60
|
-
return null;
|
|
61
|
-
} else if (matchInterfaceIds(HOLDING_INTERFACE_ID, interfaceView.interfaceId)) {
|
|
62
|
-
return {
|
|
63
|
-
type: "Holding",
|
|
64
|
-
viewValue: interfaceView.viewValue
|
|
65
|
-
};
|
|
66
|
-
} else if (matchInterfaceIds(
|
|
67
|
-
TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
68
|
-
interfaceView.interfaceId
|
|
69
|
-
)) {
|
|
70
|
-
return {
|
|
71
|
-
type: "TransferInstruction",
|
|
72
|
-
viewValue: interfaceView.viewValue
|
|
73
|
-
};
|
|
74
|
-
} else {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
function ensureInterfaceViewIsPresent(createdEvent, interfaceId) {
|
|
79
|
-
const interfaceView = getInterfaceView(createdEvent);
|
|
80
|
-
if (!interfaceView) {
|
|
81
|
-
throw new Error(
|
|
82
|
-
`Expected to have interface views, but didn't: ${JSON.stringify(
|
|
83
|
-
createdEvent
|
|
84
|
-
)}`
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
if (!matchInterfaceIds(interfaceId, interfaceView.interfaceId)) {
|
|
88
|
-
throw new Error(
|
|
89
|
-
`Not a ${interfaceId} but a ${interfaceView.interfaceId}: ${JSON.stringify(createdEvent)}`
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
return interfaceView;
|
|
93
|
-
}
|
|
94
|
-
function mergeMetas(event, extra) {
|
|
95
|
-
const choiceArgument = event.choiceArgument;
|
|
96
|
-
const lastWriteWins = [
|
|
97
|
-
choiceArgument?.transfer?.meta,
|
|
98
|
-
choiceArgument?.extraArgs?.meta,
|
|
99
|
-
choiceArgument?.meta,
|
|
100
|
-
extra,
|
|
101
|
-
event.exerciseResult?.meta
|
|
102
|
-
];
|
|
103
|
-
const result = {};
|
|
104
|
-
lastWriteWins.forEach((meta) => {
|
|
105
|
-
const values = meta?.values || {};
|
|
106
|
-
Object.entries(values).forEach(([k, v]) => {
|
|
107
|
-
result[k] = v;
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
if (Object.keys(result).length === 0) {
|
|
111
|
-
return void 0;
|
|
112
|
-
} else {
|
|
113
|
-
return { values: result };
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
function getMetaKeyValue(key, meta) {
|
|
117
|
-
return (meta?.values || {})[key] || null;
|
|
118
|
-
}
|
|
119
|
-
function removeParsedMetaKeys(meta) {
|
|
120
|
-
return {
|
|
121
|
-
values: Object.fromEntries(
|
|
122
|
-
Object.entries(meta?.values || {}).filter(
|
|
123
|
-
([k]) => !AllKnownMetaKeys.includes(k)
|
|
124
|
-
)
|
|
125
|
-
)
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
var COMPLETIONS_LIMIT = "100";
|
|
129
|
-
var COMPLETIONS_STREAM_IDLE_TIMEOUT_MS = "1000";
|
|
130
|
-
async function awaitCompletion(ledgerClient, ledgerEnd, partyId, userId, commandIdOrSubmissionId) {
|
|
131
|
-
const responses = await ledgerClient.postWithRetry(
|
|
132
|
-
"/v2/commands/completions",
|
|
133
|
-
{
|
|
134
|
-
userId,
|
|
135
|
-
parties: [partyId],
|
|
136
|
-
beginExclusive: ledgerEnd
|
|
137
|
-
},
|
|
138
|
-
defaultRetryableOptions,
|
|
139
|
-
{
|
|
140
|
-
query: {
|
|
141
|
-
limit: COMPLETIONS_LIMIT,
|
|
142
|
-
stream_idle_timeout_ms: COMPLETIONS_STREAM_IDLE_TIMEOUT_MS
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
);
|
|
146
|
-
const completions = responses.filter(
|
|
147
|
-
(r) => "Completion" in r.completionResponse
|
|
148
|
-
);
|
|
149
|
-
const wantedCompletion = responses.find((r) => {
|
|
150
|
-
if ("Completion" in r.completionResponse) {
|
|
151
|
-
const completion = r.completionResponse.Completion.value;
|
|
152
|
-
return completion.userId === userId && (completion.commandId === commandIdOrSubmissionId || completion.submissionId === commandIdOrSubmissionId);
|
|
153
|
-
}
|
|
154
|
-
return false;
|
|
155
|
-
});
|
|
156
|
-
if (wantedCompletion && "Completion" in wantedCompletion.completionResponse) {
|
|
157
|
-
const completion = wantedCompletion.completionResponse.Completion.value;
|
|
158
|
-
const status = completion.status;
|
|
159
|
-
if (status && status.code !== 0) {
|
|
160
|
-
throw asGrpcError(status);
|
|
161
|
-
}
|
|
162
|
-
return completion;
|
|
163
|
-
} else {
|
|
164
|
-
const lastCompletion = completions[completions.length - 1];
|
|
165
|
-
const newLedgerEnd = lastCompletion && "Completion" in lastCompletion.completionResponse ? lastCompletion.completionResponse.Completion.value.offset : void 0;
|
|
166
|
-
return awaitCompletion(
|
|
167
|
-
ledgerClient,
|
|
168
|
-
newLedgerEnd || ledgerEnd,
|
|
169
|
-
// !newLedgerEnd implies response was empty
|
|
170
|
-
partyId,
|
|
171
|
-
userId,
|
|
172
|
-
commandIdOrSubmissionId
|
|
173
|
-
);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
async function promiseWithTimeout(promise, timeoutMs, errorMessage) {
|
|
177
|
-
let timeoutPid = null;
|
|
178
|
-
const timeoutPromise = new Promise((_resolve, reject) => {
|
|
179
|
-
timeoutPid = setTimeout(() => reject(errorMessage), timeoutMs);
|
|
180
|
-
});
|
|
181
|
-
try {
|
|
182
|
-
return await Promise.race([promise, timeoutPromise]);
|
|
183
|
-
} finally {
|
|
184
|
-
if (timeoutPid) {
|
|
185
|
-
clearTimeout(timeoutPid);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
var defaultRetryableOptions = {
|
|
190
|
-
retries: 5,
|
|
191
|
-
delayMs: 3e3,
|
|
192
|
-
cantonErrorKeys: [
|
|
193
|
-
"SEQUENCER_REQUEST_FAILED",
|
|
194
|
-
"SEQUENCER_BACKPRESSURE",
|
|
195
|
-
"SUBMISSION_ALREADY_IN_FLIGHT",
|
|
196
|
-
"LOCAL_VERDICT_TIMEOUT",
|
|
197
|
-
"NOT_SEQUENCED_TIMEOUT",
|
|
198
|
-
"NO_VIEW_WITH_VALID_RECIPIENTS"
|
|
199
|
-
]
|
|
200
|
-
};
|
|
201
|
-
async function retryable(fn, retryableOptions2, logger) {
|
|
202
|
-
for (let attempts = 1; attempts <= retryableOptions2.retries; attempts++) {
|
|
203
|
-
try {
|
|
204
|
-
return await fn();
|
|
205
|
-
} catch (err) {
|
|
206
|
-
const grpcError = asGrpcError(err);
|
|
207
|
-
const message = grpcError.message;
|
|
208
|
-
const shouldRetry = retryableOptions2.cantonErrorKeys.length === 0 || retryableOptions2.cantonErrorKeys.some(
|
|
209
|
-
(key) => message.includes(key)
|
|
210
|
-
);
|
|
211
|
-
if (attempts < retryableOptions2.retries && shouldRetry) {
|
|
212
|
-
logger?.warn(
|
|
213
|
-
`Caught retryiable error: ${message}. Retrying attempt ${attempts} of ${retryableOptions2.retries}...`
|
|
214
|
-
);
|
|
215
|
-
await new Promise(
|
|
216
|
-
(res) => setTimeout(res, retryableOptions2.delayMs)
|
|
217
|
-
);
|
|
218
|
-
} else {
|
|
219
|
-
throw grpcError;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
throw new Error("retryable: Exceeded maximum retries without throwing.");
|
|
224
|
-
}
|
|
225
|
-
var isJsCantonError = (e) => typeof e === "object" && e !== null && "code" in e && "cause" in e && "errorCategory" in e;
|
|
226
|
-
var asJsCantonError = (e) => {
|
|
227
|
-
if (isJsCantonError(e)) {
|
|
228
|
-
return e;
|
|
229
|
-
} else {
|
|
230
|
-
throw e;
|
|
231
|
-
}
|
|
232
|
-
};
|
|
233
|
-
var asGrpcError = (e) => {
|
|
234
|
-
let errorInfo = void 0;
|
|
235
|
-
let retryInfo = void 0;
|
|
236
|
-
let code = 500;
|
|
237
|
-
let message = e?.message || "";
|
|
238
|
-
if (typeof e === "object" && e !== null && "cause" in e) {
|
|
239
|
-
message = e.cause;
|
|
240
|
-
} else if (typeof e === "object" && e !== null && "message" in e) {
|
|
241
|
-
message = e.message;
|
|
242
|
-
}
|
|
243
|
-
if (typeof e === "object" && e !== null && "code" in e && "message" in e && "details" in e && Array.isArray(e.details)) {
|
|
244
|
-
code = e.code;
|
|
245
|
-
message = e.message;
|
|
246
|
-
for (const detail of e.details) {
|
|
247
|
-
if (detail.typeUrl === "type.googleapis.com/google.rpc.ErrorInfo" && typeof detail.value === "string") {
|
|
248
|
-
try {
|
|
249
|
-
errorInfo = ErrorInfo.fromBinary(
|
|
250
|
-
Buffer.from(detail.value, "base64")
|
|
251
|
-
);
|
|
252
|
-
} catch {
|
|
253
|
-
}
|
|
254
|
-
} else if (detail.typeUrl === "type.googleapis.com/google.rpc.RetryInfo" && typeof detail.value === "string") {
|
|
255
|
-
try {
|
|
256
|
-
retryInfo = RetryInfo.fromBinary(
|
|
257
|
-
Buffer.from(detail.value, "base64")
|
|
258
|
-
);
|
|
259
|
-
} catch {
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
} else {
|
|
264
|
-
throw e;
|
|
265
|
-
}
|
|
266
|
-
return { code, message, errorInfo, retryInfo };
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
// src/txparse/instrumentmap.ts
|
|
270
|
-
var InstrumentMap = class {
|
|
271
|
-
constructor() {
|
|
272
|
-
__publicField(this, "map");
|
|
273
|
-
this.map = /* @__PURE__ */ new Map();
|
|
274
|
-
}
|
|
275
|
-
encodeKey(instrumentId) {
|
|
276
|
-
return JSON.stringify([instrumentId.admin, instrumentId.id]);
|
|
277
|
-
}
|
|
278
|
-
decodeKey(key) {
|
|
279
|
-
const [admin, id] = JSON.parse(key);
|
|
280
|
-
return { admin, id };
|
|
281
|
-
}
|
|
282
|
-
set(key, value) {
|
|
283
|
-
this.map.set(this.encodeKey(key), value);
|
|
284
|
-
}
|
|
285
|
-
get(key) {
|
|
286
|
-
return this.map.get(this.encodeKey(key));
|
|
287
|
-
}
|
|
288
|
-
has(key) {
|
|
289
|
-
return this.map.has(this.encodeKey(key));
|
|
290
|
-
}
|
|
291
|
-
delete(key) {
|
|
292
|
-
return this.map.delete(this.encodeKey(key));
|
|
293
|
-
}
|
|
294
|
-
*entries() {
|
|
295
|
-
for (const [key, value] of this.map.entries()) {
|
|
296
|
-
yield [this.decodeKey(key), value];
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
function currentStatusFromChoiceOrResult(choice, resultTag) {
|
|
301
|
-
if (resultTag === "TransferInstructionResult_Failed") return "Failed";
|
|
302
|
-
if (resultTag === "TransferInstructionResult_Completed") return "Completed";
|
|
303
|
-
switch (choice) {
|
|
304
|
-
case "TransferInstruction_Reject":
|
|
305
|
-
return "Rejected";
|
|
306
|
-
case "TransferInstruction_Withdraw":
|
|
307
|
-
return "Withdrawn";
|
|
308
|
-
case "TransferInstruction_Accept":
|
|
309
|
-
case "TransferInstruction_Update":
|
|
310
|
-
return "Pending";
|
|
311
|
-
default:
|
|
312
|
-
return "Pending";
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
function getCorrelationIdFromTransferInstruction(currentInstructionCid, originalInstructionCid) {
|
|
316
|
-
return originalInstructionCid ?? currentInstructionCid;
|
|
317
|
-
}
|
|
318
|
-
function getPendingTransferInstructionCid(exercisedEvent) {
|
|
319
|
-
const output = exercisedEvent.exerciseResult?.output;
|
|
320
|
-
if (output?.tag !== "TransferInstructionResult_Pending") return void 0;
|
|
321
|
-
const cid = output.value?.transferInstructionCid;
|
|
322
|
-
return cid ?? void 0;
|
|
323
|
-
}
|
|
324
|
-
var TransactionParser = class {
|
|
325
|
-
constructor(transaction, ledgerClient, partyId, isMasterUser) {
|
|
326
|
-
__publicField(this, "ledgerClient");
|
|
327
|
-
__publicField(this, "partyId");
|
|
328
|
-
__publicField(this, "transaction");
|
|
329
|
-
__publicField(this, "isMasterUser");
|
|
330
|
-
this.ledgerClient = ledgerClient;
|
|
331
|
-
this.partyId = partyId;
|
|
332
|
-
this.transaction = transaction;
|
|
333
|
-
this.isMasterUser = isMasterUser;
|
|
334
|
-
}
|
|
335
|
-
async parseTransaction() {
|
|
336
|
-
const tx = this.transaction;
|
|
337
|
-
const events = await this.parseEvents([...tx.events || []].reverse());
|
|
338
|
-
return {
|
|
339
|
-
updateId: tx.updateId,
|
|
340
|
-
offset: tx.offset,
|
|
341
|
-
recordTime: tx.recordTime,
|
|
342
|
-
synchronizerId: tx.synchronizerId,
|
|
343
|
-
events
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
async parseEvents(eventsStack) {
|
|
347
|
-
let callStack = [];
|
|
348
|
-
let continueAfterNodeId = -1;
|
|
349
|
-
const result = [];
|
|
350
|
-
while (eventsStack.length > 0) {
|
|
351
|
-
const currentEvent = eventsStack.pop();
|
|
352
|
-
const { nodeId, createdEvent, archivedEvent, exercisedEvent } = getNodeIdAndEvent(currentEvent);
|
|
353
|
-
callStack = callStack.filter((s) => s.untilNodeId <= nodeId);
|
|
354
|
-
const parentChoice = callStack[callStack.length - 1] && callStack[callStack.length - 1].parentChoiceName || "none (root node)";
|
|
355
|
-
let parsed;
|
|
356
|
-
if (nodeId <= continueAfterNodeId) {
|
|
357
|
-
parsed = null;
|
|
358
|
-
} else if (createdEvent) {
|
|
359
|
-
parsed = this.parseRawCreate(createdEvent, parentChoice);
|
|
360
|
-
} else if (archivedEvent) {
|
|
361
|
-
parsed = await this.parseRawArchive(archivedEvent, parentChoice);
|
|
362
|
-
} else if (exercisedEvent) {
|
|
363
|
-
parsed = await this.parseExercise(exercisedEvent);
|
|
364
|
-
} else {
|
|
365
|
-
throw new Error(
|
|
366
|
-
`Impossible event: ${JSON.stringify(currentEvent)}`
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
if (parsed && isLeafEventNode(parsed)) {
|
|
370
|
-
if (holdingChangesNonEmpty(parsed.event)) {
|
|
371
|
-
result.push({
|
|
372
|
-
...parsed.event,
|
|
373
|
-
label: {
|
|
374
|
-
...parsed.event.label,
|
|
375
|
-
meta: removeParsedMetaKeys(parsed.event.label.meta)
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
continueAfterNodeId = parsed.continueAfterNodeId;
|
|
380
|
-
} else if (parsed) {
|
|
381
|
-
callStack.push({
|
|
382
|
-
parentChoiceName: parsed.parentChoiceName,
|
|
383
|
-
untilNodeId: parsed.lastDescendantNodeId
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
return result;
|
|
388
|
-
}
|
|
389
|
-
parseRawCreate(create, parentChoice) {
|
|
390
|
-
return this.buildRawEvent(create, create.nodeId, (result) => {
|
|
391
|
-
return {
|
|
392
|
-
// TODO: this code currently only looks at the first instrument
|
|
393
|
-
// to determine the type of the Event.
|
|
394
|
-
type: Number(
|
|
395
|
-
result.lockedHoldingsChangeSummaries[0]?.amountChange
|
|
396
|
-
) > 0 ? "Lock" : "Create",
|
|
397
|
-
parentChoice,
|
|
398
|
-
contractId: create.contractId,
|
|
399
|
-
offset: create.offset,
|
|
400
|
-
templateId: create.templateId,
|
|
401
|
-
payload: result.payload,
|
|
402
|
-
packageName: create.packageName,
|
|
403
|
-
meta: void 0
|
|
404
|
-
};
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
async parseRawArchive(archive, parentChoice) {
|
|
408
|
-
const events = await this.getEventsForArchive(archive);
|
|
409
|
-
if (!events) {
|
|
410
|
-
return null;
|
|
411
|
-
}
|
|
412
|
-
return this.buildRawEvent(
|
|
413
|
-
events.created.createdEvent,
|
|
414
|
-
archive.nodeId,
|
|
415
|
-
(result) => {
|
|
416
|
-
return {
|
|
417
|
-
type: "Archive",
|
|
418
|
-
parentChoice,
|
|
419
|
-
contractId: archive.contractId,
|
|
420
|
-
offset: archive.offset,
|
|
421
|
-
templateId: archive.templateId,
|
|
422
|
-
packageName: archive.packageName,
|
|
423
|
-
actingParties: archive.actingParties || [],
|
|
424
|
-
payload: result.payload,
|
|
425
|
-
meta: void 0
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
buildRawEvent(originalCreate, nodeId, buildLabel) {
|
|
431
|
-
const view = getKnownInterfaceView(originalCreate);
|
|
432
|
-
let result;
|
|
433
|
-
switch (view?.type) {
|
|
434
|
-
case "Holding": {
|
|
435
|
-
const holdingView = view.viewValue;
|
|
436
|
-
if (this.partyId !== holdingView.owner) {
|
|
437
|
-
result = null;
|
|
438
|
-
} else {
|
|
439
|
-
const isLocked = !!holdingView.lock;
|
|
440
|
-
const summary = {
|
|
441
|
-
instrumentId: holdingView.instrumentId,
|
|
442
|
-
amountChange: holdingView.amount,
|
|
443
|
-
numInputs: 0,
|
|
444
|
-
inputAmount: "0",
|
|
445
|
-
numOutputs: 1,
|
|
446
|
-
outputAmount: holdingView.amount
|
|
447
|
-
};
|
|
448
|
-
const lockedHoldingsChangeSummaries = isLocked ? [summary] : [];
|
|
449
|
-
const unlockedHoldingsChangeSummaries = isLocked ? [] : [summary];
|
|
450
|
-
result = {
|
|
451
|
-
payload: holdingView,
|
|
452
|
-
unlockedHoldingsChange: {
|
|
453
|
-
creates: isLocked ? [] : [holdingView],
|
|
454
|
-
archives: []
|
|
455
|
-
},
|
|
456
|
-
lockedHoldingsChange: {
|
|
457
|
-
creates: isLocked ? [holdingView] : [],
|
|
458
|
-
archives: []
|
|
459
|
-
},
|
|
460
|
-
lockedHoldingsChangeSummaries,
|
|
461
|
-
lockedHoldingsChangeSummary: lockedHoldingsChangeSummaries[0] ?? emptyHoldingsChangeSummary,
|
|
462
|
-
unlockedHoldingsChangeSummaries,
|
|
463
|
-
unlockedHoldingsChangeSummary: unlockedHoldingsChangeSummaries[0] ?? emptyHoldingsChangeSummary,
|
|
464
|
-
transferInstruction: null
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
break;
|
|
468
|
-
}
|
|
469
|
-
case "TransferInstruction": {
|
|
470
|
-
const transferInstructionView = view.viewValue;
|
|
471
|
-
if (![
|
|
472
|
-
transferInstructionView.transfer.sender,
|
|
473
|
-
transferInstructionView.transfer.receiver
|
|
474
|
-
].some((stakeholder) => stakeholder === this.partyId)) {
|
|
475
|
-
result = null;
|
|
476
|
-
} else {
|
|
477
|
-
const multiStepCorrelationId = getCorrelationIdFromTransferInstruction(
|
|
478
|
-
originalCreate.contractId,
|
|
479
|
-
transferInstructionView.originalInstructionCid ?? null
|
|
480
|
-
);
|
|
481
|
-
result = {
|
|
482
|
-
payload: transferInstructionView,
|
|
483
|
-
transferInstruction: {
|
|
484
|
-
originalInstructionCid: transferInstructionView.originalInstructionCid,
|
|
485
|
-
transfer: transferInstructionView.transfer,
|
|
486
|
-
meta: transferInstructionView.meta,
|
|
487
|
-
status: {
|
|
488
|
-
before: transferInstructionView.status,
|
|
489
|
-
// raw DAML pending sub-state
|
|
490
|
-
current: { tag: "Pending", value: {} }
|
|
491
|
-
// normalized
|
|
492
|
-
},
|
|
493
|
-
multiStepCorrelationId
|
|
494
|
-
},
|
|
495
|
-
unlockedHoldingsChange: { creates: [], archives: [] },
|
|
496
|
-
lockedHoldingsChange: { creates: [], archives: [] },
|
|
497
|
-
unlockedHoldingsChangeSummaries: [],
|
|
498
|
-
unlockedHoldingsChangeSummary: emptyHoldingsChangeSummary,
|
|
499
|
-
lockedHoldingsChangeSummaries: [],
|
|
500
|
-
lockedHoldingsChangeSummary: emptyHoldingsChangeSummary
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
break;
|
|
504
|
-
}
|
|
505
|
-
default:
|
|
506
|
-
result = null;
|
|
507
|
-
}
|
|
508
|
-
return result && {
|
|
509
|
-
continueAfterNodeId: nodeId,
|
|
510
|
-
event: {
|
|
511
|
-
label: buildLabel(result),
|
|
512
|
-
unlockedHoldingsChange: result.unlockedHoldingsChange,
|
|
513
|
-
lockedHoldingsChange: result.lockedHoldingsChange,
|
|
514
|
-
lockedHoldingsChangeSummaries: result.lockedHoldingsChangeSummaries,
|
|
515
|
-
lockedHoldingsChangeSummary: result.lockedHoldingsChangeSummary,
|
|
516
|
-
unlockedHoldingsChangeSummaries: result.unlockedHoldingsChangeSummaries,
|
|
517
|
-
unlockedHoldingsChangeSummary: result.unlockedHoldingsChangeSummary,
|
|
518
|
-
transferInstruction: result.transferInstruction
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
async parseExercise(exercise) {
|
|
523
|
-
let result = null;
|
|
524
|
-
const tokenStandardChoice = {
|
|
525
|
-
name: exercise.choice,
|
|
526
|
-
choiceArgument: exercise.choiceArgument,
|
|
527
|
-
exerciseResult: exercise.exerciseResult
|
|
528
|
-
};
|
|
529
|
-
switch (exercise.choice) {
|
|
530
|
-
case "TransferRule_Transfer":
|
|
531
|
-
case "TransferFactory_Transfer":
|
|
532
|
-
result = await this.buildTransfer(exercise, tokenStandardChoice);
|
|
533
|
-
break;
|
|
534
|
-
case "TransferInstruction_Accept":
|
|
535
|
-
case "TransferInstruction_Reject":
|
|
536
|
-
case "TransferInstruction_Withdraw":
|
|
537
|
-
case "TransferInstruction_Update":
|
|
538
|
-
result = await this.buildFromTransferInstructionExercise(
|
|
539
|
-
exercise,
|
|
540
|
-
tokenStandardChoice
|
|
541
|
-
);
|
|
542
|
-
break;
|
|
543
|
-
case "BurnMintFactory_BurnMint":
|
|
544
|
-
result = await this.buildMergeSplit(
|
|
545
|
-
exercise,
|
|
546
|
-
tokenStandardChoice
|
|
547
|
-
);
|
|
548
|
-
break;
|
|
549
|
-
default: {
|
|
550
|
-
const meta = mergeMetas(exercise);
|
|
551
|
-
const txKind = getMetaKeyValue(TxKindMetaKey, meta);
|
|
552
|
-
if (txKind) {
|
|
553
|
-
result = await this.parseViaTxKind(exercise, txKind);
|
|
554
|
-
}
|
|
555
|
-
break;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
if (!result) {
|
|
559
|
-
return {
|
|
560
|
-
lastDescendantNodeId: exercise.lastDescendantNodeId,
|
|
561
|
-
parentChoiceName: exercise.choice
|
|
562
|
-
};
|
|
563
|
-
} else {
|
|
564
|
-
const lockedHoldingsChange = {
|
|
565
|
-
creates: result.children.creates.filter(
|
|
566
|
-
(h) => !!h.lock && h.owner === this.partyId
|
|
567
|
-
),
|
|
568
|
-
archives: result.children.archives.filter(
|
|
569
|
-
(h) => !!h.lock && h.owner === this.partyId
|
|
570
|
-
)
|
|
571
|
-
};
|
|
572
|
-
const unlockedHoldingsChange = {
|
|
573
|
-
creates: result.children.creates.filter(
|
|
574
|
-
(h) => !h.lock && h.owner === this.partyId
|
|
575
|
-
),
|
|
576
|
-
archives: result.children.archives.filter(
|
|
577
|
-
(h) => !h.lock && h.owner === this.partyId
|
|
578
|
-
)
|
|
579
|
-
};
|
|
580
|
-
const lockedHoldingsChangeSummaries = computeSummaries(
|
|
581
|
-
lockedHoldingsChange,
|
|
582
|
-
this.partyId
|
|
583
|
-
);
|
|
584
|
-
const unlockedHoldingsChangeSummaries = computeSummaries(
|
|
585
|
-
unlockedHoldingsChange,
|
|
586
|
-
this.partyId
|
|
587
|
-
);
|
|
588
|
-
return {
|
|
589
|
-
event: {
|
|
590
|
-
label: result.label,
|
|
591
|
-
lockedHoldingsChange,
|
|
592
|
-
lockedHoldingsChangeSummaries,
|
|
593
|
-
lockedHoldingsChangeSummary: lockedHoldingsChangeSummaries[0] ?? emptyHoldingsChangeSummary,
|
|
594
|
-
unlockedHoldingsChange,
|
|
595
|
-
unlockedHoldingsChangeSummaries,
|
|
596
|
-
unlockedHoldingsChangeSummary: unlockedHoldingsChangeSummaries[0] ?? emptyHoldingsChangeSummary,
|
|
597
|
-
transferInstruction: result.transferInstruction
|
|
598
|
-
},
|
|
599
|
-
continueAfterNodeId: exercise.lastDescendantNodeId
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
async parseViaTxKind(exercisedEvent, txKind) {
|
|
604
|
-
switch (txKind) {
|
|
605
|
-
case "transfer":
|
|
606
|
-
return await this.buildTransfer(exercisedEvent, null);
|
|
607
|
-
case "merge-split":
|
|
608
|
-
case "burn":
|
|
609
|
-
case "mint":
|
|
610
|
-
return await this.buildMergeSplit(exercisedEvent, null);
|
|
611
|
-
case "unlock":
|
|
612
|
-
return await this.buildBasic(exercisedEvent, "Unlock", null);
|
|
613
|
-
case "expire-dust":
|
|
614
|
-
return await this.buildBasic(exercisedEvent, "ExpireDust", null);
|
|
615
|
-
default:
|
|
616
|
-
throw new Error(
|
|
617
|
-
`Unknown tx-kind '${txKind}' in ${JSON.stringify(exercisedEvent)}`
|
|
618
|
-
);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
async buildTransfer(exercisedEvent, tokenStandardChoice, transferInstructions) {
|
|
622
|
-
const meta = mergeMetas(
|
|
623
|
-
exercisedEvent,
|
|
624
|
-
transferInstructions?.transfer?.meta
|
|
625
|
-
);
|
|
626
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
627
|
-
const choiceArgumentTransfer = exercisedEvent.choiceArgument.transfer;
|
|
628
|
-
const sender = transferInstructions?.transfer?.sender || getMetaKeyValue(SenderMetaKey, meta) || choiceArgumentTransfer.sender;
|
|
629
|
-
if (!sender) {
|
|
630
|
-
console.error(
|
|
631
|
-
`Malformed transfer didn't contain sender. Will instead attempt to parse the children.
|
|
632
|
-
Transfer: ${JSON.stringify(exercisedEvent)}`
|
|
633
|
-
);
|
|
634
|
-
return null;
|
|
635
|
-
}
|
|
636
|
-
const resultTag = exercisedEvent.exerciseResult?.output?.tag || void 0;
|
|
637
|
-
const pendingCid = getPendingTransferInstructionCid(exercisedEvent);
|
|
638
|
-
const currentTag = currentStatusFromChoiceOrResult(
|
|
639
|
-
exercisedEvent.choice,
|
|
640
|
-
resultTag
|
|
641
|
-
);
|
|
642
|
-
const children = await this.getChildren(exercisedEvent);
|
|
643
|
-
const receiverAmounts = /* @__PURE__ */ new Map();
|
|
644
|
-
children.creates.filter((h) => h.owner !== sender).forEach(
|
|
645
|
-
(holding) => receiverAmounts.set(
|
|
646
|
-
holding.owner,
|
|
647
|
-
(receiverAmounts.get(holding.owner) || BigNumber("0")).plus(
|
|
648
|
-
BigNumber(holding.amount)
|
|
649
|
-
)
|
|
650
|
-
)
|
|
651
|
-
);
|
|
652
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
653
|
-
let label;
|
|
654
|
-
if (receiverAmounts.size === 0) {
|
|
655
|
-
label = {
|
|
656
|
-
...amountChanges,
|
|
657
|
-
type: "MergeSplit",
|
|
658
|
-
tokenStandardChoice,
|
|
659
|
-
reason,
|
|
660
|
-
meta
|
|
661
|
-
};
|
|
662
|
-
} else if (sender === this.partyId) {
|
|
663
|
-
label = {
|
|
664
|
-
...amountChanges,
|
|
665
|
-
type: "TransferOut",
|
|
666
|
-
receiverAmounts: [...receiverAmounts].map(([k, v]) => {
|
|
667
|
-
return { receiver: k, amount: v.toString() };
|
|
668
|
-
}),
|
|
669
|
-
tokenStandardChoice,
|
|
670
|
-
reason,
|
|
671
|
-
meta
|
|
672
|
-
};
|
|
673
|
-
} else {
|
|
674
|
-
label = {
|
|
675
|
-
type: "TransferIn",
|
|
676
|
-
// for Transfers, the burn/mint is always 0 for the receiving party (i.e., 0 for TransferIn)
|
|
677
|
-
burnAmount: "0",
|
|
678
|
-
mintAmount: "0",
|
|
679
|
-
sender,
|
|
680
|
-
tokenStandardChoice,
|
|
681
|
-
reason,
|
|
682
|
-
meta
|
|
683
|
-
};
|
|
684
|
-
}
|
|
685
|
-
if (transferInstructions) {
|
|
686
|
-
transferInstructions.status.current = transferInstructions.status.current || { tag: currentTag, value: {} };
|
|
687
|
-
return {
|
|
688
|
-
label,
|
|
689
|
-
children,
|
|
690
|
-
transferInstruction: transferInstructions
|
|
691
|
-
};
|
|
692
|
-
}
|
|
693
|
-
const transferInstruction = {
|
|
694
|
-
originalInstructionCid: null,
|
|
695
|
-
...choiceArgumentTransfer !== void 0 && {
|
|
696
|
-
transfer: choiceArgumentTransfer
|
|
697
|
-
},
|
|
698
|
-
status: {
|
|
699
|
-
before: null,
|
|
700
|
-
current: { tag: currentTag, value: {} }
|
|
701
|
-
},
|
|
702
|
-
meta: null,
|
|
703
|
-
...pendingCid ? { multiStepCorrelationId: pendingCid } : {}
|
|
704
|
-
};
|
|
705
|
-
return {
|
|
706
|
-
label,
|
|
707
|
-
children,
|
|
708
|
-
transferInstruction
|
|
709
|
-
};
|
|
710
|
-
}
|
|
711
|
-
async buildMergeSplit(exercisedEvent, tokenStandardChoice) {
|
|
712
|
-
let type;
|
|
713
|
-
const meta = mergeMetas(exercisedEvent);
|
|
714
|
-
switch (getMetaKeyValue(TxKindMetaKey, meta)) {
|
|
715
|
-
case "burn":
|
|
716
|
-
type = "Burn";
|
|
717
|
-
break;
|
|
718
|
-
case "mint":
|
|
719
|
-
type = "Mint";
|
|
720
|
-
break;
|
|
721
|
-
default:
|
|
722
|
-
type = "MergeSplit";
|
|
723
|
-
}
|
|
724
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
725
|
-
const children = await this.getChildren(exercisedEvent);
|
|
726
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
727
|
-
const label = {
|
|
728
|
-
...amountChanges,
|
|
729
|
-
type,
|
|
730
|
-
tokenStandardChoice,
|
|
731
|
-
reason,
|
|
732
|
-
meta
|
|
733
|
-
};
|
|
734
|
-
return {
|
|
735
|
-
label,
|
|
736
|
-
children,
|
|
737
|
-
transferInstruction: null
|
|
738
|
-
};
|
|
739
|
-
}
|
|
740
|
-
async buildFromTransferInstructionExercise(exercisedEvent, tokenStandardChoice) {
|
|
741
|
-
const instructionCid = exercisedEvent.contractId;
|
|
742
|
-
const transferInstructionEvents = await this.getEventsForArchive(exercisedEvent);
|
|
743
|
-
if (!transferInstructionEvents) {
|
|
744
|
-
return null;
|
|
745
|
-
}
|
|
746
|
-
const transferInstructionView = ensureInterfaceViewIsPresent(
|
|
747
|
-
transferInstructionEvents.created.createdEvent,
|
|
748
|
-
TRANSFER_INSTRUCTION_INTERFACE_ID
|
|
749
|
-
).viewValue;
|
|
750
|
-
const multiStepCorrelationId = getCorrelationIdFromTransferInstruction(
|
|
751
|
-
instructionCid,
|
|
752
|
-
transferInstructionView.originalInstructionCid ?? null
|
|
753
|
-
);
|
|
754
|
-
const resultTag = exercisedEvent.exerciseResult?.output?.tag || void 0;
|
|
755
|
-
const currentTag = currentStatusFromChoiceOrResult(
|
|
756
|
-
exercisedEvent.choice,
|
|
757
|
-
resultTag
|
|
758
|
-
);
|
|
759
|
-
const transferInstruction = {
|
|
760
|
-
originalInstructionCid: transferInstructionView.originalInstructionCid,
|
|
761
|
-
multiStepCorrelationId,
|
|
762
|
-
transfer: transferInstructionView.transfer,
|
|
763
|
-
meta: transferInstructionView.meta,
|
|
764
|
-
status: {
|
|
765
|
-
before: transferInstructionView.status,
|
|
766
|
-
current: { tag: currentTag, value: {} }
|
|
767
|
-
}
|
|
768
|
-
};
|
|
769
|
-
const exerciseResultOutputTag = resultTag;
|
|
770
|
-
let result = null;
|
|
771
|
-
switch (exerciseResultOutputTag) {
|
|
772
|
-
case "TransferInstructionResult_Failed":
|
|
773
|
-
case "TransferInstructionResult_Pending":
|
|
774
|
-
result = await this.buildMergeSplit(
|
|
775
|
-
exercisedEvent,
|
|
776
|
-
tokenStandardChoice
|
|
777
|
-
);
|
|
778
|
-
break;
|
|
779
|
-
case "TransferInstructionResult_Completed":
|
|
780
|
-
result = await this.buildTransfer(
|
|
781
|
-
exercisedEvent,
|
|
782
|
-
tokenStandardChoice,
|
|
783
|
-
transferInstruction
|
|
784
|
-
);
|
|
785
|
-
break;
|
|
786
|
-
default:
|
|
787
|
-
throw new Error(
|
|
788
|
-
`Unknown TransferInstructionResult: ${exerciseResultOutputTag}`
|
|
789
|
-
);
|
|
790
|
-
}
|
|
791
|
-
return result && {
|
|
792
|
-
...result,
|
|
793
|
-
transferInstruction
|
|
794
|
-
};
|
|
795
|
-
}
|
|
796
|
-
async buildBasic(exercisedEvent, type, tokenStandardChoice) {
|
|
797
|
-
const children = await this.getChildren(exercisedEvent);
|
|
798
|
-
const meta = mergeMetas(exercisedEvent);
|
|
799
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
800
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
801
|
-
return {
|
|
802
|
-
label: {
|
|
803
|
-
...amountChanges,
|
|
804
|
-
type,
|
|
805
|
-
tokenStandardChoice,
|
|
806
|
-
reason,
|
|
807
|
-
meta
|
|
808
|
-
},
|
|
809
|
-
children,
|
|
810
|
-
transferInstruction: null
|
|
811
|
-
};
|
|
812
|
-
}
|
|
813
|
-
async getChildren(exercisedEvent) {
|
|
814
|
-
const mutatingResult = { creates: [], archives: [] };
|
|
815
|
-
const childrenEventsSlice = (this.transaction.events || []).map(getNodeIdAndEvent).filter(
|
|
816
|
-
({ nodeId }) => nodeId > exercisedEvent.nodeId && nodeId <= exercisedEvent.lastDescendantNodeId
|
|
817
|
-
);
|
|
818
|
-
if (exercisedEvent.consuming && hasInterface(HOLDING_INTERFACE_ID, exercisedEvent)) {
|
|
819
|
-
const selfEvent = await this.getEventsForArchive(exercisedEvent);
|
|
820
|
-
if (selfEvent) {
|
|
821
|
-
const holdingView = ensureInterfaceViewIsPresent(
|
|
822
|
-
selfEvent.created.createdEvent,
|
|
823
|
-
HOLDING_INTERFACE_ID
|
|
824
|
-
).viewValue;
|
|
825
|
-
mutatingResult.archives.push({
|
|
826
|
-
amount: holdingView.amount,
|
|
827
|
-
instrumentId: holdingView.instrumentId,
|
|
828
|
-
contractId: exercisedEvent.contractId,
|
|
829
|
-
owner: holdingView.owner,
|
|
830
|
-
meta: holdingView.meta,
|
|
831
|
-
lock: holdingView.lock
|
|
832
|
-
});
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
for (const {
|
|
836
|
-
createdEvent,
|
|
837
|
-
archivedEvent,
|
|
838
|
-
exercisedEvent: exercisedEvent2
|
|
839
|
-
} of childrenEventsSlice) {
|
|
840
|
-
if (createdEvent) {
|
|
841
|
-
const interfaceView = getInterfaceView(createdEvent);
|
|
842
|
-
if (interfaceView && matchInterfaceIds(
|
|
843
|
-
HOLDING_INTERFACE_ID,
|
|
844
|
-
interfaceView.interfaceId
|
|
845
|
-
)) {
|
|
846
|
-
const holdingView = interfaceView.viewValue;
|
|
847
|
-
mutatingResult.creates.push({
|
|
848
|
-
amount: holdingView.amount,
|
|
849
|
-
instrumentId: holdingView.instrumentId,
|
|
850
|
-
contractId: createdEvent.contractId,
|
|
851
|
-
owner: holdingView.owner,
|
|
852
|
-
meta: holdingView.meta,
|
|
853
|
-
lock: holdingView.lock
|
|
854
|
-
});
|
|
855
|
-
}
|
|
856
|
-
} else if (archivedEvent && hasInterface(HOLDING_INTERFACE_ID, archivedEvent) || exercisedEvent2 && exercisedEvent2.consuming && hasInterface(HOLDING_INTERFACE_ID, exercisedEvent2)) {
|
|
857
|
-
const contractEvents = await this.getEventsForArchive(
|
|
858
|
-
archivedEvent || exercisedEvent2
|
|
859
|
-
);
|
|
860
|
-
if (contractEvents) {
|
|
861
|
-
const holdingView = ensureInterfaceViewIsPresent(
|
|
862
|
-
contractEvents.created?.createdEvent,
|
|
863
|
-
HOLDING_INTERFACE_ID
|
|
864
|
-
).viewValue;
|
|
865
|
-
mutatingResult.archives.push({
|
|
866
|
-
amount: holdingView.amount,
|
|
867
|
-
instrumentId: holdingView.instrumentId,
|
|
868
|
-
contractId: archivedEvent?.contractId || exercisedEvent2.contractId,
|
|
869
|
-
owner: holdingView.owner,
|
|
870
|
-
meta: holdingView.meta,
|
|
871
|
-
lock: holdingView.lock
|
|
872
|
-
});
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
return {
|
|
877
|
-
// remove transient contracts
|
|
878
|
-
creates: mutatingResult.creates.filter(
|
|
879
|
-
(create) => !mutatingResult.archives.some(
|
|
880
|
-
(archive) => create.contractId === archive.contractId
|
|
881
|
-
)
|
|
882
|
-
),
|
|
883
|
-
archives: mutatingResult.archives.filter(
|
|
884
|
-
(archive) => !mutatingResult.creates.some(
|
|
885
|
-
(create) => create.contractId === archive.contractId
|
|
886
|
-
)
|
|
887
|
-
)
|
|
888
|
-
};
|
|
889
|
-
}
|
|
890
|
-
async getEventsForArchive(archivedEvent) {
|
|
891
|
-
if (!(archivedEvent.witnessParties || []).includes(this.partyId)) {
|
|
892
|
-
return null;
|
|
893
|
-
}
|
|
894
|
-
const basePayload = {
|
|
895
|
-
contractId: archivedEvent.contractId,
|
|
896
|
-
eventFormat: EventFilterBySetup({
|
|
897
|
-
interfaceIds: [
|
|
898
|
-
HOLDING_INTERFACE_ID,
|
|
899
|
-
TRANSFER_INSTRUCTION_INTERFACE_ID
|
|
900
|
-
],
|
|
901
|
-
isMasterUser: this.isMasterUser,
|
|
902
|
-
partyId: this.partyId,
|
|
903
|
-
verbose: true
|
|
904
|
-
})
|
|
905
|
-
};
|
|
906
|
-
const payload = this.ledgerClient.getCurrentClientVersion() === "3.3" ? { ...basePayload, requestingParties: [] } : basePayload;
|
|
907
|
-
const events = await this.ledgerClient.postWithRetry("/v2/events/events-by-contract-id", payload).catch((err) => {
|
|
908
|
-
if (err.code === "CONTRACT_EVENTS_NOT_FOUND") {
|
|
909
|
-
return null;
|
|
910
|
-
} else {
|
|
911
|
-
throw err;
|
|
912
|
-
}
|
|
913
|
-
});
|
|
914
|
-
if (!events) {
|
|
915
|
-
return null;
|
|
916
|
-
}
|
|
917
|
-
const created = events.created;
|
|
918
|
-
const archived = events.archived;
|
|
919
|
-
if (!created || !archived) {
|
|
920
|
-
throw new Error(
|
|
921
|
-
`Archival of ${archivedEvent.contractId} does not have a corresponding create/archive event: ${JSON.stringify(
|
|
922
|
-
events
|
|
923
|
-
)}`
|
|
924
|
-
);
|
|
925
|
-
}
|
|
926
|
-
return { created, archived };
|
|
927
|
-
}
|
|
928
|
-
};
|
|
929
|
-
function isLeafEventNode(result) {
|
|
930
|
-
return !!result.event;
|
|
931
|
-
}
|
|
932
|
-
function getNodeIdAndEvent(event) {
|
|
933
|
-
if ("ExercisedEvent" in event) {
|
|
934
|
-
if (event.ExercisedEvent.choice === "Archive") {
|
|
935
|
-
return {
|
|
936
|
-
nodeId: event.ExercisedEvent.nodeId,
|
|
937
|
-
archivedEvent: event.ExercisedEvent
|
|
938
|
-
};
|
|
939
|
-
} else {
|
|
940
|
-
return {
|
|
941
|
-
nodeId: event.ExercisedEvent.nodeId,
|
|
942
|
-
exercisedEvent: event.ExercisedEvent
|
|
943
|
-
};
|
|
944
|
-
}
|
|
945
|
-
} else if ("CreatedEvent" in event) {
|
|
946
|
-
return {
|
|
947
|
-
nodeId: event.CreatedEvent.nodeId,
|
|
948
|
-
createdEvent: event.CreatedEvent
|
|
949
|
-
};
|
|
950
|
-
} else if ("ArchivedEvent" in event) {
|
|
951
|
-
return {
|
|
952
|
-
nodeId: event.ArchivedEvent.nodeId,
|
|
953
|
-
archivedEvent: event.ArchivedEvent
|
|
954
|
-
};
|
|
955
|
-
} else {
|
|
956
|
-
throw new Error(`Impossible event type: ${event}`);
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
function sumHoldingsChange(change, filter) {
|
|
960
|
-
return sumHoldings(
|
|
961
|
-
change.creates.filter((create) => filter(create.owner, create.lock))
|
|
962
|
-
).minus(
|
|
963
|
-
sumHoldings(
|
|
964
|
-
change.archives.filter(
|
|
965
|
-
(archive) => filter(archive.owner, archive.lock)
|
|
966
|
-
)
|
|
967
|
-
)
|
|
968
|
-
);
|
|
969
|
-
}
|
|
970
|
-
function sumHoldings(holdings) {
|
|
971
|
-
if (holdings.length > 0) {
|
|
972
|
-
const instrumentId = holdings[0].instrumentId;
|
|
973
|
-
for (const holding of holdings) {
|
|
974
|
-
if (holding.instrumentId.admin !== instrumentId.admin || holding.instrumentId.id !== instrumentId.id) {
|
|
975
|
-
throw new Error(
|
|
976
|
-
`Attempted to call sumHoldings on heterogeneous instruments: ${JSON.stringify(instrumentId)} != ${JSON.stringify(holding.instrumentId)}`
|
|
977
|
-
);
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
return BigNumber.sum(
|
|
982
|
-
...holdings.map((h) => h.amount).concat(["0"])
|
|
983
|
-
// avoid NaN
|
|
984
|
-
);
|
|
985
|
-
}
|
|
986
|
-
function computeAmountChanges(children, meta, partyId) {
|
|
987
|
-
const burnAmount = BigNumber(getMetaKeyValue(BurnedMetaKey, meta) || "0");
|
|
988
|
-
const partyHoldingAmountChange = sumHoldingsChange(
|
|
989
|
-
children,
|
|
990
|
-
(owner) => owner === partyId
|
|
991
|
-
);
|
|
992
|
-
const otherPartiesHoldingAmountChange = sumHoldingsChange(
|
|
993
|
-
children,
|
|
994
|
-
(owner) => owner !== partyId
|
|
995
|
-
);
|
|
996
|
-
const mintAmount = partyHoldingAmountChange.plus(burnAmount).plus(otherPartiesHoldingAmountChange);
|
|
997
|
-
return {
|
|
998
|
-
burnAmount: burnAmount.toString(),
|
|
999
|
-
mintAmount: mintAmount.toString()
|
|
1000
|
-
};
|
|
1001
|
-
}
|
|
1002
|
-
function computeSummary(instrumentId, changes, partyId) {
|
|
1003
|
-
const amountChange = sumHoldingsChange(
|
|
1004
|
-
changes,
|
|
1005
|
-
(owner) => owner === partyId
|
|
1006
|
-
);
|
|
1007
|
-
const outputAmount = sumHoldings(changes.creates);
|
|
1008
|
-
const inputAmount = sumHoldings(changes.archives);
|
|
1009
|
-
return {
|
|
1010
|
-
instrumentId,
|
|
1011
|
-
amountChange: amountChange.toString(),
|
|
1012
|
-
numOutputs: changes.creates.length,
|
|
1013
|
-
outputAmount: outputAmount.toString(),
|
|
1014
|
-
numInputs: changes.archives.length,
|
|
1015
|
-
inputAmount: inputAmount.toString()
|
|
1016
|
-
};
|
|
1017
|
-
}
|
|
1018
|
-
function holdingsChangeByInstrument(changes) {
|
|
1019
|
-
const map = new InstrumentMap();
|
|
1020
|
-
for (const create of changes.creates) {
|
|
1021
|
-
if (map.has(create.instrumentId)) {
|
|
1022
|
-
map.get(create.instrumentId).creates.push(create);
|
|
1023
|
-
} else {
|
|
1024
|
-
map.set(create.instrumentId, { creates: [create], archives: [] });
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
for (const archive of changes.archives) {
|
|
1028
|
-
if (map.has(archive.instrumentId)) {
|
|
1029
|
-
map.get(archive.instrumentId).archives.push(archive);
|
|
1030
|
-
} else {
|
|
1031
|
-
map.set(archive.instrumentId, { creates: [], archives: [archive] });
|
|
1032
|
-
}
|
|
1033
|
-
}
|
|
1034
|
-
return map;
|
|
1035
|
-
}
|
|
1036
|
-
function computeSummaries(changes, partyId) {
|
|
1037
|
-
const byInstrument = holdingsChangeByInstrument(changes);
|
|
1038
|
-
return [...byInstrument.entries()].map(
|
|
1039
|
-
([instrumentId, change]) => computeSummary(instrumentId, change, partyId)
|
|
1040
|
-
);
|
|
1041
|
-
}
|
|
1042
|
-
function holdingChangesNonEmpty(event) {
|
|
1043
|
-
return event.unlockedHoldingsChange.creates.length > 0 || event.unlockedHoldingsChange.archives.length > 0 || event.lockedHoldingsChange.creates.length > 0 || event.lockedHoldingsChange.archives.length > 0;
|
|
1044
|
-
}
|
|
1045
|
-
var emptyHoldingsChangeSummary = {
|
|
1046
|
-
// This is obviously incorrect, but the field was introduced at the same
|
|
1047
|
-
// time at which we introduced the more correct per-instrument summaries,
|
|
1048
|
-
// so we know that old code couldn't use this (broken) field, and new code
|
|
1049
|
-
// should use the correct summaries.
|
|
1050
|
-
instrumentId: { admin: "", id: "" },
|
|
1051
|
-
numInputs: 0,
|
|
1052
|
-
numOutputs: 0,
|
|
1053
|
-
inputAmount: "0",
|
|
1054
|
-
outputAmount: "0",
|
|
1055
|
-
amountChange: "0"
|
|
1056
|
-
};
|
|
1057
|
-
|
|
1058
|
-
// src/txparse/types.ts
|
|
1059
|
-
var renderTransaction = (t) => {
|
|
1060
|
-
return { ...t, events: t.events.map(renderTransactionEvent) };
|
|
1061
|
-
};
|
|
1062
|
-
var renderTransactionEvent = (e) => {
|
|
1063
|
-
const lockedHoldingsChangeSummaries = e.lockedHoldingsChangeSummaries.map(renderHoldingsChangeSummary).filter((s) => s !== null);
|
|
1064
|
-
const unlockedHoldingsChangeSummaries = e.unlockedHoldingsChangeSummaries.map(renderHoldingsChangeSummary).filter((s) => s !== null);
|
|
1065
|
-
const lockedHoldingsChange = renderHoldingsChange(e.lockedHoldingsChange);
|
|
1066
|
-
const unlockedHoldingsChange = renderHoldingsChange(
|
|
1067
|
-
e.unlockedHoldingsChange
|
|
1068
|
-
);
|
|
1069
|
-
return {
|
|
1070
|
-
...e,
|
|
1071
|
-
lockedHoldingsChange,
|
|
1072
|
-
unlockedHoldingsChange,
|
|
1073
|
-
lockedHoldingsChangeSummaries,
|
|
1074
|
-
// Deprecated
|
|
1075
|
-
lockedHoldingsChangeSummary: renderHoldingsChangeSummary(
|
|
1076
|
-
e.lockedHoldingsChangeSummary
|
|
1077
|
-
),
|
|
1078
|
-
unlockedHoldingsChangeSummaries,
|
|
1079
|
-
// Deprecated
|
|
1080
|
-
unlockedHoldingsChangeSummary: renderHoldingsChangeSummary(
|
|
1081
|
-
e.unlockedHoldingsChangeSummary
|
|
1082
|
-
)
|
|
1083
|
-
};
|
|
1084
|
-
};
|
|
1085
|
-
var renderHoldingsChangeSummary = (s) => {
|
|
1086
|
-
if (s.numInputs === 0 && s.numOutputs === 0 && s.inputAmount === "0" && s.outputAmount === "0" && s.amountChange === "0") {
|
|
1087
|
-
return null;
|
|
1088
|
-
}
|
|
1089
|
-
return {
|
|
1090
|
-
...(s.instrumentId.admin !== "" || s.instrumentId.id !== "") && {
|
|
1091
|
-
instrumentId: s.instrumentId
|
|
1092
|
-
},
|
|
1093
|
-
...s.numInputs !== 0 && { numInputs: s.numInputs },
|
|
1094
|
-
...s.inputAmount !== "0" && { inputAmount: s.inputAmount },
|
|
1095
|
-
...s.numOutputs !== 0 && { numOutputs: s.numOutputs },
|
|
1096
|
-
...s.outputAmount !== "0" && { outputAmount: s.outputAmount },
|
|
1097
|
-
...s.amountChange !== "0" && { amountChange: s.amountChange }
|
|
1098
|
-
};
|
|
1099
|
-
};
|
|
1100
|
-
var renderHoldingsChange = (c) => {
|
|
1101
|
-
if (c.creates.length === 0 && c.archives.length === 0) {
|
|
1102
|
-
return null;
|
|
1103
|
-
}
|
|
1104
|
-
return {
|
|
1105
|
-
...c.creates.length !== 0 && { creates: c.creates },
|
|
1106
|
-
...c.archives.length !== 0 && { archives: c.archives }
|
|
1107
|
-
};
|
|
1108
|
-
};
|
|
1109
|
-
|
|
1110
|
-
// src/token-standard-service.ts
|
|
1111
|
-
var REQUESTED_AT_SKEW_MS = 6e4;
|
|
1112
|
-
var EMPTY_META = { values: {} };
|
|
1113
|
-
var CoreService = class _CoreService {
|
|
1114
|
-
constructor(ledgerClient, logger, accessTokenProvider, isMasterUser, isAdmin = false, accessToken = "") {
|
|
1115
|
-
this.ledgerClient = ledgerClient;
|
|
1116
|
-
this.logger = logger;
|
|
1117
|
-
this.accessTokenProvider = accessTokenProvider;
|
|
1118
|
-
this.isMasterUser = isMasterUser;
|
|
1119
|
-
this.isAdmin = isAdmin;
|
|
1120
|
-
this.accessToken = accessToken;
|
|
1121
|
-
}
|
|
1122
|
-
getTokenStandardClient(registryUrl) {
|
|
1123
|
-
return new TokenStandardClient(
|
|
1124
|
-
registryUrl,
|
|
1125
|
-
this.logger,
|
|
1126
|
-
this.isAdmin,
|
|
1127
|
-
this.accessToken,
|
|
1128
|
-
this.accessTokenProvider
|
|
1129
|
-
);
|
|
1130
|
-
}
|
|
1131
|
-
// TODO: probably needs a filter by instrument ID as well?
|
|
1132
|
-
async getInputHoldingsCids(sender, inputUtxos, amount, continueUntilCompletion) {
|
|
1133
|
-
const now = /* @__PURE__ */ new Date();
|
|
1134
|
-
if (inputUtxos && inputUtxos.length > 0) {
|
|
1135
|
-
return inputUtxos;
|
|
1136
|
-
}
|
|
1137
|
-
const senderHoldings = await this.listContractsByInterface(
|
|
1138
|
-
HOLDING_INTERFACE_ID,
|
|
1139
|
-
sender,
|
|
1140
|
-
void 0,
|
|
1141
|
-
void 0,
|
|
1142
|
-
continueUntilCompletion
|
|
1143
|
-
);
|
|
1144
|
-
if (senderHoldings.length === 0) {
|
|
1145
|
-
throw new Error(
|
|
1146
|
-
"Sender has no holdings, so transfer can't be executed."
|
|
1147
|
-
);
|
|
1148
|
-
}
|
|
1149
|
-
const unlockedSenderHoldings = senderHoldings.filter((utxo) => {
|
|
1150
|
-
const lock = utxo.interfaceViewValue.lock;
|
|
1151
|
-
if (!lock) return true;
|
|
1152
|
-
const expiresAt = lock.expiresAt;
|
|
1153
|
-
if (!expiresAt) return false;
|
|
1154
|
-
const expiresAtDate = new Date(expiresAt);
|
|
1155
|
-
return expiresAtDate <= now;
|
|
1156
|
-
});
|
|
1157
|
-
if (unlockedSenderHoldings.length > 100) {
|
|
1158
|
-
this.logger.warn(`Sender has more than 100 unlocked utxos.`);
|
|
1159
|
-
}
|
|
1160
|
-
if (amount) {
|
|
1161
|
-
return _CoreService.getInputHoldingsCidsForAmount(
|
|
1162
|
-
amount,
|
|
1163
|
-
unlockedSenderHoldings
|
|
1164
|
-
);
|
|
1165
|
-
} else {
|
|
1166
|
-
return unlockedSenderHoldings.map((h) => h.contractId);
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
static async getInputHoldingsCidsForAmount(amount, unlockedSenderHoldings) {
|
|
1170
|
-
const exactAmount = unlockedSenderHoldings.find(
|
|
1171
|
-
(holding) => parseFloat(holding.interfaceViewValue.amount) === amount
|
|
1172
|
-
);
|
|
1173
|
-
if (exactAmount) {
|
|
1174
|
-
return [exactAmount.contractId];
|
|
1175
|
-
}
|
|
1176
|
-
const sortedUnlockedSenderHoldings = unlockedSenderHoldings.toSorted(
|
|
1177
|
-
(a, b) => parseFloat(a.interfaceViewValue.amount) - parseFloat(b.interfaceViewValue.amount)
|
|
1178
|
-
);
|
|
1179
|
-
const largestHoldingAmount = sortedUnlockedSenderHoldings.pop();
|
|
1180
|
-
if (!largestHoldingAmount) {
|
|
1181
|
-
throw new Error(`Sender doesn't have any unlocked holdings`);
|
|
1182
|
-
}
|
|
1183
|
-
let currentSum = parseFloat(
|
|
1184
|
-
largestHoldingAmount.interfaceViewValue.amount
|
|
1185
|
-
);
|
|
1186
|
-
const cIds = [largestHoldingAmount.contractId];
|
|
1187
|
-
for (const h of sortedUnlockedSenderHoldings) {
|
|
1188
|
-
if (currentSum >= amount) {
|
|
1189
|
-
break;
|
|
1190
|
-
}
|
|
1191
|
-
const currentHoldingAmount = parseFloat(h.interfaceViewValue.amount);
|
|
1192
|
-
currentSum += currentHoldingAmount;
|
|
1193
|
-
cIds.push(h.contractId);
|
|
1194
|
-
}
|
|
1195
|
-
if (currentSum < amount) {
|
|
1196
|
-
throw new Error(
|
|
1197
|
-
`Sender doesn't have sufficient funds for this transfer. Missing amount: ${amount - currentSum}`
|
|
1198
|
-
);
|
|
1199
|
-
}
|
|
1200
|
-
if (cIds.length > 100) {
|
|
1201
|
-
throw new Error(
|
|
1202
|
-
`Exceeded the maximum of 100 utxos in 1 transaction`
|
|
1203
|
-
);
|
|
1204
|
-
}
|
|
1205
|
-
return cIds;
|
|
1206
|
-
}
|
|
1207
|
-
async listContractsByInterface(interfaceId, partyId, limit, offset, continueUntilCompletion) {
|
|
1208
|
-
try {
|
|
1209
|
-
const ledgerEnd = offset ?? (await this.ledgerClient.getWithRetry("/v2/state/ledger-end")).offset;
|
|
1210
|
-
const options = {
|
|
1211
|
-
offset: ledgerEnd,
|
|
1212
|
-
interfaceIds: [interfaceId],
|
|
1213
|
-
parties: [partyId],
|
|
1214
|
-
filterByParty: true,
|
|
1215
|
-
continueUntilCompletion: Boolean(continueUntilCompletion)
|
|
1216
|
-
};
|
|
1217
|
-
if (limit !== void 0) {
|
|
1218
|
-
options.limit = limit;
|
|
1219
|
-
}
|
|
1220
|
-
const acsResponses = await this.ledgerClient.activeContracts(options);
|
|
1221
|
-
const isActiveContractEntry = (acsResponse) => "JsActiveContract" in acsResponse.contractEntry && !!acsResponse.contractEntry.JsActiveContract?.createdEvent;
|
|
1222
|
-
const activeContractEntries = acsResponses.filter(
|
|
1223
|
-
isActiveContractEntry
|
|
1224
|
-
);
|
|
1225
|
-
return activeContractEntries.map(
|
|
1226
|
-
(response) => this.toPrettyContract(interfaceId, response, ledgerEnd)
|
|
1227
|
-
);
|
|
1228
|
-
} catch (err) {
|
|
1229
|
-
this.logger.error(
|
|
1230
|
-
`Failed to list contracts of interface ${interfaceId}`,
|
|
1231
|
-
err
|
|
1232
|
-
);
|
|
1233
|
-
throw err;
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
async toPrettyTransactions(updates, partyId, ledgerClient) {
|
|
1237
|
-
const isOffsetCheckpointUpdate = (updateResponse) => "OffsetCheckpoint" in updateResponse.update;
|
|
1238
|
-
const isTransactionUpdate = (updateResponse) => "Transaction" in updateResponse.update && !!updateResponse.update.Transaction?.value;
|
|
1239
|
-
const offsetCheckpoints = updates.filter(isOffsetCheckpointUpdate).map((update) => update.update.OffsetCheckpoint.value.offset);
|
|
1240
|
-
const latestCheckpointOffset = Math.max(...offsetCheckpoints);
|
|
1241
|
-
const transactions = await Promise.all(
|
|
1242
|
-
updates.filter(isTransactionUpdate).map(async (update) => {
|
|
1243
|
-
const tx = update.update.Transaction.value;
|
|
1244
|
-
const parser = new TransactionParser(
|
|
1245
|
-
tx,
|
|
1246
|
-
ledgerClient,
|
|
1247
|
-
partyId,
|
|
1248
|
-
this.isMasterUser
|
|
1249
|
-
);
|
|
1250
|
-
return await parser.parseTransaction();
|
|
1251
|
-
})
|
|
1252
|
-
);
|
|
1253
|
-
return {
|
|
1254
|
-
// OffsetCheckpoint can be anywhere... or not at all, maybe
|
|
1255
|
-
nextOffset: Math.max(
|
|
1256
|
-
latestCheckpointOffset,
|
|
1257
|
-
...transactions.map((tx) => tx.offset)
|
|
1258
|
-
),
|
|
1259
|
-
transactions: transactions.filter((tx) => tx.events.length > 0).map(renderTransaction)
|
|
1260
|
-
};
|
|
1261
|
-
}
|
|
1262
|
-
async toPrettyTransaction(getTransactionResponse, partyId, ledgerClient) {
|
|
1263
|
-
const tx = getTransactionResponse.transaction;
|
|
1264
|
-
const parser = new TransactionParser(
|
|
1265
|
-
tx,
|
|
1266
|
-
ledgerClient,
|
|
1267
|
-
partyId,
|
|
1268
|
-
this.isMasterUser
|
|
1269
|
-
);
|
|
1270
|
-
const parsedTx = await parser.parseTransaction();
|
|
1271
|
-
return renderTransaction(parsedTx);
|
|
1272
|
-
}
|
|
1273
|
-
async toPrettyTransactionsPerParty(updates, parties, ledgerClient) {
|
|
1274
|
-
const all = await Promise.all(
|
|
1275
|
-
parties.map(
|
|
1276
|
-
async (partyId) => [
|
|
1277
|
-
partyId,
|
|
1278
|
-
await this.toPrettyTransactions(
|
|
1279
|
-
updates,
|
|
1280
|
-
partyId,
|
|
1281
|
-
ledgerClient
|
|
1282
|
-
)
|
|
1283
|
-
]
|
|
1284
|
-
)
|
|
1285
|
-
);
|
|
1286
|
-
return new Map(all);
|
|
1287
|
-
}
|
|
1288
|
-
// returns object with JsActiveContract content
|
|
1289
|
-
// and contractId and interface view value extracted from it as separate fields for convenience
|
|
1290
|
-
toPrettyContract(interfaceId, response, offset) {
|
|
1291
|
-
const activeContract = response.contractEntry.JsActiveContract;
|
|
1292
|
-
const { createdEvent } = activeContract;
|
|
1293
|
-
return {
|
|
1294
|
-
contractId: createdEvent.contractId,
|
|
1295
|
-
activeContract,
|
|
1296
|
-
interfaceViewValue: ensureInterfaceViewIsPresent(
|
|
1297
|
-
createdEvent,
|
|
1298
|
-
interfaceId
|
|
1299
|
-
).viewValue,
|
|
1300
|
-
fetchedAtOffset: offset
|
|
1301
|
-
};
|
|
1302
|
-
}
|
|
1303
|
-
toQualifiedMemberId(memberId) {
|
|
1304
|
-
if (!memberId) throw new Error("memberId is required");
|
|
1305
|
-
return /^(PAR|MED)::/.test(memberId) ? memberId : `PAR::${memberId}`;
|
|
1306
|
-
}
|
|
1307
|
-
};
|
|
1308
|
-
var AllocationService = class {
|
|
1309
|
-
constructor(core, logger) {
|
|
1310
|
-
this.core = core;
|
|
1311
|
-
this.logger = logger;
|
|
1312
|
-
}
|
|
1313
|
-
async buildAllocationFactoryChoiceArgs(allocationSpecification, expectedAdmin, inputUtxos, requestedAt) {
|
|
1314
|
-
const allocationSpecificationNormalized = {
|
|
1315
|
-
...allocationSpecification,
|
|
1316
|
-
settlement: {
|
|
1317
|
-
...allocationSpecification.settlement,
|
|
1318
|
-
meta: allocationSpecification.settlement.meta ?? { values: {} }
|
|
1319
|
-
},
|
|
1320
|
-
transferLeg: {
|
|
1321
|
-
...allocationSpecification.transferLeg,
|
|
1322
|
-
meta: allocationSpecification.transferLeg.meta ?? {
|
|
1323
|
-
values: {}
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
};
|
|
1327
|
-
const inputHoldingCids = await this.core.getInputHoldingsCids(
|
|
1328
|
-
allocationSpecificationNormalized.transferLeg.sender,
|
|
1329
|
-
inputUtxos
|
|
1330
|
-
);
|
|
1331
|
-
return {
|
|
1332
|
-
expectedAdmin,
|
|
1333
|
-
allocation: allocationSpecificationNormalized,
|
|
1334
|
-
requestedAt: requestedAt ?? new Date(Date.now() - REQUESTED_AT_SKEW_MS).toISOString(),
|
|
1335
|
-
inputHoldingCids,
|
|
1336
|
-
extraArgs: {
|
|
1337
|
-
context: { values: {} },
|
|
1338
|
-
meta: { values: {} }
|
|
1339
|
-
}
|
|
1340
|
-
};
|
|
1341
|
-
}
|
|
1342
|
-
async fetchAllocationFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
|
|
1343
|
-
return this.core.getTokenStandardClient(registryUrl).post("/registry/allocation-instruction/v1/allocation-factory", {
|
|
1344
|
-
choiceArguments: choiceArgs,
|
|
1345
|
-
excludeDebugFields
|
|
1346
|
-
});
|
|
1347
|
-
}
|
|
1348
|
-
async createAllocationInstructionFromContext(factoryId, choiceArgs, choiceContext) {
|
|
1349
|
-
choiceArgs.extraArgs.context = {
|
|
1350
|
-
...choiceContext.choiceContextData,
|
|
1351
|
-
values: choiceContext.choiceContextData?.values ?? {}
|
|
1352
|
-
};
|
|
1353
|
-
const exercise = {
|
|
1354
|
-
templateId: ALLOCATION_FACTORY_INTERFACE_ID,
|
|
1355
|
-
contractId: factoryId,
|
|
1356
|
-
choice: "AllocationFactory_Allocate",
|
|
1357
|
-
choiceArgument: choiceArgs
|
|
1358
|
-
};
|
|
1359
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
1360
|
-
}
|
|
1361
|
-
async createAllocationInstruction(allocationSpecification, expectedAdmin, registryUrl, inputUtxos, requestedAt, prefetchedRegistryChoiceContext) {
|
|
1362
|
-
const choiceArgs = await this.buildAllocationFactoryChoiceArgs(
|
|
1363
|
-
allocationSpecification,
|
|
1364
|
-
expectedAdmin,
|
|
1365
|
-
inputUtxos,
|
|
1366
|
-
requestedAt
|
|
1367
|
-
);
|
|
1368
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1369
|
-
return this.createAllocationInstructionFromContext(
|
|
1370
|
-
prefetchedRegistryChoiceContext.factoryId,
|
|
1371
|
-
choiceArgs,
|
|
1372
|
-
prefetchedRegistryChoiceContext.choiceContext
|
|
1373
|
-
);
|
|
1374
|
-
}
|
|
1375
|
-
const { factoryId, choiceContext } = await this.fetchAllocationFactoryChoiceContext(
|
|
1376
|
-
registryUrl,
|
|
1377
|
-
choiceArgs
|
|
1378
|
-
);
|
|
1379
|
-
return this.createAllocationInstructionFromContext(
|
|
1380
|
-
factoryId,
|
|
1381
|
-
choiceArgs,
|
|
1382
|
-
choiceContext
|
|
1383
|
-
);
|
|
1384
|
-
}
|
|
1385
|
-
buildAllocationExerciseWithContext(templateId, contractId, choice, choiceContext) {
|
|
1386
|
-
const exercise = {
|
|
1387
|
-
templateId,
|
|
1388
|
-
contractId,
|
|
1389
|
-
choice,
|
|
1390
|
-
choiceArgument: {
|
|
1391
|
-
extraArgs: {
|
|
1392
|
-
context: choiceContext.choiceContextData,
|
|
1393
|
-
meta: EMPTY_META
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
};
|
|
1397
|
-
return [exercise, choiceContext.disclosedContracts ?? []];
|
|
1398
|
-
}
|
|
1399
|
-
async fetchExecuteTransferChoiceContext(allocationId, registryUrl) {
|
|
1400
|
-
return this.core.getTokenStandardClient(registryUrl).post(
|
|
1401
|
-
"/registry/allocations/v1/{allocationId}/choice-contexts/execute-transfer",
|
|
1402
|
-
{
|
|
1403
|
-
excludeDebugFields: true
|
|
1404
|
-
},
|
|
1405
|
-
{
|
|
1406
|
-
path: {
|
|
1407
|
-
allocationId
|
|
1408
|
-
}
|
|
1409
|
-
}
|
|
1410
|
-
);
|
|
1411
|
-
}
|
|
1412
|
-
createExecuteTransferAllocationFromContext(allocationCid, choiceContext) {
|
|
1413
|
-
return this.buildAllocationExerciseWithContext(
|
|
1414
|
-
ALLOCATION_INTERFACE_ID,
|
|
1415
|
-
allocationCid,
|
|
1416
|
-
"Allocation_ExecuteTransfer",
|
|
1417
|
-
choiceContext
|
|
1418
|
-
);
|
|
1419
|
-
}
|
|
1420
|
-
async createExecuteTransferAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
1421
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1422
|
-
return this.createExecuteTransferAllocationFromContext(
|
|
1423
|
-
allocationCid,
|
|
1424
|
-
prefetchedRegistryChoiceContext
|
|
1425
|
-
);
|
|
1426
|
-
}
|
|
1427
|
-
const choiceContext = await this.fetchExecuteTransferChoiceContext(
|
|
1428
|
-
allocationCid,
|
|
1429
|
-
registryUrl
|
|
1430
|
-
);
|
|
1431
|
-
return this.createExecuteTransferAllocationFromContext(
|
|
1432
|
-
allocationCid,
|
|
1433
|
-
choiceContext
|
|
1434
|
-
);
|
|
1435
|
-
}
|
|
1436
|
-
async fetchWithdrawAllocationChoiceContext(allocationCid, registryUrl) {
|
|
1437
|
-
return this.core.getTokenStandardClient(registryUrl).post(
|
|
1438
|
-
"/registry/allocations/v1/{allocationId}/choice-contexts/withdraw",
|
|
1439
|
-
{
|
|
1440
|
-
excludeDebugFields: true
|
|
1441
|
-
},
|
|
1442
|
-
{ path: { allocationId: allocationCid } }
|
|
1443
|
-
);
|
|
1444
|
-
}
|
|
1445
|
-
createWithdrawAllocationFromContext(allocationCid, choiceContext) {
|
|
1446
|
-
return this.buildAllocationExerciseWithContext(
|
|
1447
|
-
ALLOCATION_INTERFACE_ID,
|
|
1448
|
-
allocationCid,
|
|
1449
|
-
"Allocation_Withdraw",
|
|
1450
|
-
choiceContext
|
|
1451
|
-
);
|
|
1452
|
-
}
|
|
1453
|
-
async createWithdrawAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
1454
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1455
|
-
return this.createWithdrawAllocationFromContext(
|
|
1456
|
-
allocationCid,
|
|
1457
|
-
prefetchedRegistryChoiceContext
|
|
1458
|
-
);
|
|
1459
|
-
}
|
|
1460
|
-
const choiceContext = await this.fetchWithdrawAllocationChoiceContext(
|
|
1461
|
-
allocationCid,
|
|
1462
|
-
registryUrl
|
|
1463
|
-
);
|
|
1464
|
-
return this.createWithdrawAllocationFromContext(
|
|
1465
|
-
allocationCid,
|
|
1466
|
-
choiceContext
|
|
1467
|
-
);
|
|
1468
|
-
}
|
|
1469
|
-
async fetchCancelAllocationChoiceContext(allocationCid, registryUrl) {
|
|
1470
|
-
return this.core.getTokenStandardClient(registryUrl).post(
|
|
1471
|
-
"/registry/allocations/v1/{allocationId}/choice-contexts/cancel",
|
|
1472
|
-
{
|
|
1473
|
-
excludeDebugFields: true
|
|
1474
|
-
},
|
|
1475
|
-
{ path: { allocationId: allocationCid } }
|
|
1476
|
-
);
|
|
1477
|
-
}
|
|
1478
|
-
createCancelAllocationFromContext(allocationCid, choiceContext) {
|
|
1479
|
-
return this.buildAllocationExerciseWithContext(
|
|
1480
|
-
ALLOCATION_INTERFACE_ID,
|
|
1481
|
-
allocationCid,
|
|
1482
|
-
"Allocation_Cancel",
|
|
1483
|
-
choiceContext
|
|
1484
|
-
);
|
|
1485
|
-
}
|
|
1486
|
-
async createCancelAllocation(allocationCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
1487
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1488
|
-
return this.createCancelAllocationFromContext(
|
|
1489
|
-
allocationCid,
|
|
1490
|
-
prefetchedRegistryChoiceContext
|
|
1491
|
-
);
|
|
1492
|
-
}
|
|
1493
|
-
const choiceContext = await this.fetchCancelAllocationChoiceContext(
|
|
1494
|
-
allocationCid,
|
|
1495
|
-
registryUrl
|
|
1496
|
-
);
|
|
1497
|
-
return this.createCancelAllocationFromContext(
|
|
1498
|
-
allocationCid,
|
|
1499
|
-
choiceContext
|
|
1500
|
-
);
|
|
1501
|
-
}
|
|
1502
|
-
async createWithdrawAllocationInstruction(allocationInstructionCid) {
|
|
1503
|
-
const exercise = {
|
|
1504
|
-
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
1505
|
-
contractId: allocationInstructionCid,
|
|
1506
|
-
choice: "AllocationInstruction_Withdraw",
|
|
1507
|
-
choiceArgument: {
|
|
1508
|
-
extraArgs: {
|
|
1509
|
-
context: { values: {} },
|
|
1510
|
-
meta: { values: {} }
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
};
|
|
1514
|
-
return [exercise, []];
|
|
1515
|
-
}
|
|
1516
|
-
async createUpdateAllocationInstruction(allocationInstructionCid, extraActors = [], extraArgsContext = {}, extraArgsMeta = {}) {
|
|
1517
|
-
const exercise = {
|
|
1518
|
-
templateId: ALLOCATION_INSTRUCTION_INTERFACE_ID,
|
|
1519
|
-
contractId: allocationInstructionCid,
|
|
1520
|
-
choice: "AllocationInstruction_Update",
|
|
1521
|
-
choiceArgument: {
|
|
1522
|
-
extraActors,
|
|
1523
|
-
extraArgs: {
|
|
1524
|
-
context: { values: extraArgsContext },
|
|
1525
|
-
meta: { values: extraArgsMeta }
|
|
1526
|
-
}
|
|
1527
|
-
}
|
|
1528
|
-
};
|
|
1529
|
-
return [exercise, []];
|
|
1530
|
-
}
|
|
1531
|
-
async createRejectAllocationRequest(allocationRequestCid, actor) {
|
|
1532
|
-
const exercise = {
|
|
1533
|
-
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
1534
|
-
contractId: allocationRequestCid,
|
|
1535
|
-
choice: "AllocationRequest_Reject",
|
|
1536
|
-
choiceArgument: {
|
|
1537
|
-
actor,
|
|
1538
|
-
extraArgs: {
|
|
1539
|
-
context: { values: {} },
|
|
1540
|
-
meta: { values: {} }
|
|
1541
|
-
}
|
|
1542
|
-
}
|
|
1543
|
-
};
|
|
1544
|
-
return [exercise, []];
|
|
1545
|
-
}
|
|
1546
|
-
async createWithdrawAllocationRequest(allocationRequestCid) {
|
|
1547
|
-
const exercise = {
|
|
1548
|
-
templateId: ALLOCATION_REQUEST_INTERFACE_ID,
|
|
1549
|
-
contractId: allocationRequestCid,
|
|
1550
|
-
choice: "AllocationRequest_Withdraw",
|
|
1551
|
-
choiceArgument: {
|
|
1552
|
-
extraArgs: {
|
|
1553
|
-
context: { values: {} },
|
|
1554
|
-
meta: { values: {} }
|
|
1555
|
-
}
|
|
1556
|
-
}
|
|
1557
|
-
};
|
|
1558
|
-
return [exercise, []];
|
|
1559
|
-
}
|
|
1560
|
-
};
|
|
1561
|
-
var TransferService = class {
|
|
1562
|
-
constructor(core, logger) {
|
|
1563
|
-
this.core = core;
|
|
1564
|
-
this.logger = logger;
|
|
1565
|
-
}
|
|
1566
|
-
async buildTransferChoiceArgs(sender, receiver, amount, instrumentAdmin, instrumentId, inputUtxos, memo, expiryDate, meta, continueUntilCompletion) {
|
|
1567
|
-
const inputHoldingCids = await this.core.getInputHoldingsCids(
|
|
1568
|
-
sender,
|
|
1569
|
-
inputUtxos,
|
|
1570
|
-
parseFloat(amount),
|
|
1571
|
-
continueUntilCompletion
|
|
1572
|
-
);
|
|
1573
|
-
return {
|
|
1574
|
-
expectedAdmin: instrumentAdmin,
|
|
1575
|
-
transfer: {
|
|
1576
|
-
sender,
|
|
1577
|
-
receiver,
|
|
1578
|
-
amount,
|
|
1579
|
-
instrumentId: { admin: instrumentAdmin, id: instrumentId },
|
|
1580
|
-
requestedAt: new Date(
|
|
1581
|
-
Date.now() - REQUESTED_AT_SKEW_MS
|
|
1582
|
-
).toISOString(),
|
|
1583
|
-
executeBefore: (expiryDate ?? new Date(Date.now() + 24 * 60 * 60 * 1e3)).toISOString(),
|
|
1584
|
-
inputHoldingCids,
|
|
1585
|
-
meta: {
|
|
1586
|
-
values: {
|
|
1587
|
-
[TokenStandardService.MEMO_KEY]: memo || "",
|
|
1588
|
-
...meta?.values
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
|
-
},
|
|
1592
|
-
extraArgs: {
|
|
1593
|
-
context: { values: {} },
|
|
1594
|
-
meta: { values: {} }
|
|
1595
|
-
}
|
|
1596
|
-
};
|
|
1597
|
-
}
|
|
1598
|
-
async fetchTransferFactoryChoiceContext(registryUrl, choiceArgs, excludeDebugFields = true) {
|
|
1599
|
-
return await this.core.getTokenStandardClient(registryUrl).post("/registry/transfer-instruction/v1/transfer-factory", {
|
|
1600
|
-
choiceArguments: choiceArgs,
|
|
1601
|
-
excludeDebugFields
|
|
1602
|
-
});
|
|
1603
|
-
}
|
|
1604
|
-
async createTransferFromContext(factoryId, choiceArgs, choiceContext) {
|
|
1605
|
-
this.logger.debug("Creating transfer from pre-fetched context...");
|
|
1606
|
-
choiceArgs.extraArgs.context = {
|
|
1607
|
-
...choiceContext.choiceContextData,
|
|
1608
|
-
values: choiceContext.choiceContextData?.values ?? {}
|
|
1609
|
-
};
|
|
1610
|
-
const exercise = {
|
|
1611
|
-
templateId: TRANSFER_FACTORY_INTERFACE_ID,
|
|
1612
|
-
contractId: factoryId,
|
|
1613
|
-
choice: "TransferFactory_Transfer",
|
|
1614
|
-
choiceArgument: choiceArgs
|
|
1615
|
-
};
|
|
1616
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
1617
|
-
}
|
|
1618
|
-
// TODO: use named parameters
|
|
1619
|
-
async createTransfer(sender, receiver, amount, instrumentAdmin, instrumentId, registryUrl, inputUtxos, memo, expiryDate, meta, prefetchedRegistryChoiceContext, continueUntilCompletion) {
|
|
1620
|
-
try {
|
|
1621
|
-
const choiceArgs = await this.buildTransferChoiceArgs(
|
|
1622
|
-
sender,
|
|
1623
|
-
receiver,
|
|
1624
|
-
amount,
|
|
1625
|
-
instrumentAdmin,
|
|
1626
|
-
instrumentId,
|
|
1627
|
-
inputUtxos,
|
|
1628
|
-
memo,
|
|
1629
|
-
expiryDate,
|
|
1630
|
-
meta,
|
|
1631
|
-
continueUntilCompletion
|
|
1632
|
-
);
|
|
1633
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1634
|
-
return this.createTransferFromContext(
|
|
1635
|
-
prefetchedRegistryChoiceContext.factoryId,
|
|
1636
|
-
choiceArgs,
|
|
1637
|
-
prefetchedRegistryChoiceContext.choiceContext
|
|
1638
|
-
);
|
|
1639
|
-
}
|
|
1640
|
-
const { factoryId, choiceContext } = await this.fetchTransferFactoryChoiceContext(
|
|
1641
|
-
registryUrl,
|
|
1642
|
-
choiceArgs
|
|
1643
|
-
);
|
|
1644
|
-
return this.createTransferFromContext(
|
|
1645
|
-
factoryId,
|
|
1646
|
-
choiceArgs,
|
|
1647
|
-
choiceContext
|
|
1648
|
-
);
|
|
1649
|
-
} catch (e) {
|
|
1650
|
-
this.logger.error("Failed to execute transfer:", e);
|
|
1651
|
-
throw e;
|
|
1652
|
-
}
|
|
1653
|
-
}
|
|
1654
|
-
async fetchAcceptTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
1655
|
-
const client = this.core.getTokenStandardClient(registryUrl);
|
|
1656
|
-
const choiceContext = await client.post(
|
|
1657
|
-
"/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/accept",
|
|
1658
|
-
{
|
|
1659
|
-
excludeDebugFields: true
|
|
1660
|
-
},
|
|
1661
|
-
{
|
|
1662
|
-
path: {
|
|
1663
|
-
transferInstructionId: transferInstructionCid
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1666
|
-
);
|
|
1667
|
-
return {
|
|
1668
|
-
choiceContextData: choiceContext.choiceContextData,
|
|
1669
|
-
disclosedContracts: choiceContext.disclosedContracts
|
|
1670
|
-
};
|
|
1671
|
-
}
|
|
1672
|
-
async createAcceptTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
1673
|
-
try {
|
|
1674
|
-
const exercise = {
|
|
1675
|
-
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
1676
|
-
contractId: transferInstructionCid,
|
|
1677
|
-
choice: "TransferInstruction_Accept",
|
|
1678
|
-
choiceArgument: {
|
|
1679
|
-
extraArgs: {
|
|
1680
|
-
context: choiceContext.choiceContextData,
|
|
1681
|
-
meta: { values: {} }
|
|
1682
|
-
}
|
|
1683
|
-
}
|
|
1684
|
-
};
|
|
1685
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
1686
|
-
} catch (e) {
|
|
1687
|
-
this.logger.error(
|
|
1688
|
-
"Failed to create accept transfer instruction:",
|
|
1689
|
-
e
|
|
1690
|
-
);
|
|
1691
|
-
throw e;
|
|
1692
|
-
}
|
|
1693
|
-
}
|
|
1694
|
-
async exerciseDelegateProxyTransferInstructionAccept(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
1695
|
-
const [acceptTransferInstructionContext, disclosedContracts] = await this.createAcceptTransferInstruction(
|
|
1696
|
-
transferInstructionCid,
|
|
1697
|
-
registryUrl.href
|
|
1698
|
-
);
|
|
1699
|
-
const choiceArgs = {
|
|
1700
|
-
cid: acceptTransferInstructionContext.contractId,
|
|
1701
|
-
proxyArg: {
|
|
1702
|
-
featuredAppRightCid,
|
|
1703
|
-
beneficiaries,
|
|
1704
|
-
choiceArg: acceptTransferInstructionContext.choiceArgument
|
|
1705
|
-
}
|
|
1706
|
-
};
|
|
1707
|
-
return [
|
|
1708
|
-
{
|
|
1709
|
-
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
1710
|
-
contractId: proxyCid,
|
|
1711
|
-
choice: "DelegateProxy_TransferInstruction_Accept",
|
|
1712
|
-
choiceArgument: choiceArgs
|
|
1713
|
-
},
|
|
1714
|
-
disclosedContracts
|
|
1715
|
-
];
|
|
1716
|
-
}
|
|
1717
|
-
async exerciseDelegateProxyTransferInstructionReject(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
1718
|
-
const [rejectTransferInstructionContext, disclosedContracts] = await this.createRejectTransferInstruction(
|
|
1719
|
-
transferInstructionCid,
|
|
1720
|
-
registryUrl.href
|
|
1721
|
-
);
|
|
1722
|
-
const choiceArgs = {
|
|
1723
|
-
cid: rejectTransferInstructionContext.contractId,
|
|
1724
|
-
proxyArg: {
|
|
1725
|
-
featuredAppRightCid,
|
|
1726
|
-
beneficiaries,
|
|
1727
|
-
choiceArg: rejectTransferInstructionContext.choiceArgument
|
|
1728
|
-
}
|
|
1729
|
-
};
|
|
1730
|
-
return [
|
|
1731
|
-
{
|
|
1732
|
-
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
1733
|
-
contractId: proxyCid,
|
|
1734
|
-
choice: "DelegateProxy_TransferInstruction_Reject",
|
|
1735
|
-
choiceArgument: choiceArgs
|
|
1736
|
-
},
|
|
1737
|
-
disclosedContracts
|
|
1738
|
-
];
|
|
1739
|
-
}
|
|
1740
|
-
async exerciseDelegateProxyTransferInstructioWithdraw(proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid, beneficiaries) {
|
|
1741
|
-
const [withdrawTransferInstructionContext, disclosedContracts] = await this.createWithdrawTransferInstruction(
|
|
1742
|
-
transferInstructionCid,
|
|
1743
|
-
registryUrl.href
|
|
1744
|
-
);
|
|
1745
|
-
const sumOfWeights = beneficiaries.reduce(
|
|
1746
|
-
(totalWeight, beneficiary) => totalWeight + beneficiary.weight,
|
|
1747
|
-
0
|
|
1748
|
-
);
|
|
1749
|
-
if (sumOfWeights > 1) {
|
|
1750
|
-
throw new Error("Sum of beneficiary weights is larger than 1.");
|
|
1751
|
-
}
|
|
1752
|
-
const choiceArgs = {
|
|
1753
|
-
cid: withdrawTransferInstructionContext.contractId,
|
|
1754
|
-
proxyArg: {
|
|
1755
|
-
featuredAppRightCid,
|
|
1756
|
-
beneficiaries,
|
|
1757
|
-
choiceArg: withdrawTransferInstructionContext.choiceArgument
|
|
1758
|
-
}
|
|
1759
|
-
};
|
|
1760
|
-
return [
|
|
1761
|
-
{
|
|
1762
|
-
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
1763
|
-
contractId: proxyCid,
|
|
1764
|
-
choice: "DelegateProxy_TransferInstruction_Withdraw",
|
|
1765
|
-
choiceArgument: choiceArgs
|
|
1766
|
-
},
|
|
1767
|
-
disclosedContracts
|
|
1768
|
-
];
|
|
1769
|
-
}
|
|
1770
|
-
async createAcceptTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
1771
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1772
|
-
return this.createAcceptTransferInstructionFromContext(
|
|
1773
|
-
transferInstructionCid,
|
|
1774
|
-
{
|
|
1775
|
-
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
1776
|
-
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
1777
|
-
}
|
|
1778
|
-
);
|
|
1779
|
-
}
|
|
1780
|
-
const choiceContext = await this.fetchAcceptTransferInstructionChoiceContext(
|
|
1781
|
-
transferInstructionCid,
|
|
1782
|
-
registryUrl
|
|
1783
|
-
);
|
|
1784
|
-
return this.createAcceptTransferInstructionFromContext(
|
|
1785
|
-
transferInstructionCid,
|
|
1786
|
-
choiceContext
|
|
1787
|
-
);
|
|
1788
|
-
}
|
|
1789
|
-
async fetchRejectTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
1790
|
-
const client = this.core.getTokenStandardClient(registryUrl);
|
|
1791
|
-
const choiceContext = await client.post(
|
|
1792
|
-
"/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/reject",
|
|
1793
|
-
{
|
|
1794
|
-
excludeDebugFields: true
|
|
1795
|
-
},
|
|
1796
|
-
{
|
|
1797
|
-
path: {
|
|
1798
|
-
transferInstructionId: transferInstructionCid
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
);
|
|
1802
|
-
return {
|
|
1803
|
-
choiceContextData: choiceContext.choiceContextData,
|
|
1804
|
-
disclosedContracts: choiceContext.disclosedContracts
|
|
1805
|
-
};
|
|
1806
|
-
}
|
|
1807
|
-
async createRejectTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
1808
|
-
try {
|
|
1809
|
-
const exercise = {
|
|
1810
|
-
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
1811
|
-
contractId: transferInstructionCid,
|
|
1812
|
-
choice: "TransferInstruction_Reject",
|
|
1813
|
-
choiceArgument: {
|
|
1814
|
-
extraArgs: {
|
|
1815
|
-
context: choiceContext.choiceContextData,
|
|
1816
|
-
meta: { values: {} }
|
|
1817
|
-
}
|
|
1818
|
-
}
|
|
1819
|
-
};
|
|
1820
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
1821
|
-
} catch (e) {
|
|
1822
|
-
this.logger.error(
|
|
1823
|
-
"Failed to create reject transfer instruction:",
|
|
1824
|
-
e
|
|
1825
|
-
);
|
|
1826
|
-
throw e;
|
|
1827
|
-
}
|
|
1828
|
-
}
|
|
1829
|
-
async createRejectTransferInstruction(transferInstructionCid, registryUrl, prefetchedRegistryChoiceContext) {
|
|
1830
|
-
if (prefetchedRegistryChoiceContext) {
|
|
1831
|
-
return this.createRejectTransferInstructionFromContext(
|
|
1832
|
-
transferInstructionCid,
|
|
1833
|
-
{
|
|
1834
|
-
choiceContextData: prefetchedRegistryChoiceContext.choiceContextData,
|
|
1835
|
-
disclosedContracts: prefetchedRegistryChoiceContext.disclosedContracts
|
|
1836
|
-
}
|
|
1837
|
-
);
|
|
1838
|
-
}
|
|
1839
|
-
const choiceContext = await this.fetchRejectTransferInstructionChoiceContext(
|
|
1840
|
-
transferInstructionCid,
|
|
1841
|
-
registryUrl
|
|
1842
|
-
);
|
|
1843
|
-
return this.createRejectTransferInstructionFromContext(
|
|
1844
|
-
transferInstructionCid,
|
|
1845
|
-
choiceContext
|
|
1846
|
-
);
|
|
1847
|
-
}
|
|
1848
|
-
async fetchWithdrawTransferInstructionChoiceContext(transferInstructionCid, registryUrl) {
|
|
1849
|
-
const client = this.core.getTokenStandardClient(registryUrl);
|
|
1850
|
-
const choiceContext = await client.post(
|
|
1851
|
-
"/registry/transfer-instruction/v1/{transferInstructionId}/choice-contexts/withdraw",
|
|
1852
|
-
{
|
|
1853
|
-
excludeDebugFields: true
|
|
1854
|
-
},
|
|
1855
|
-
{
|
|
1856
|
-
path: {
|
|
1857
|
-
transferInstructionId: transferInstructionCid
|
|
1858
|
-
}
|
|
1
|
+
import createClient from 'openapi-fetch';
|
|
2
|
+
import { ErrorInfo, RetryInfo } from '@canton-network/core-ledger-proto';
|
|
3
|
+
import { LRUCache } from 'typescript-lru-cache';
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
var COMPLETIONS_LIMIT = "100";
|
|
9
|
+
var COMPLETIONS_STREAM_IDLE_TIMEOUT_MS = "1000";
|
|
10
|
+
async function awaitCompletion(ledgerClient, ledgerEnd, partyId, userId, commandIdOrSubmissionId) {
|
|
11
|
+
const responses = await ledgerClient.postWithRetry(
|
|
12
|
+
"/v2/commands/completions",
|
|
13
|
+
{
|
|
14
|
+
userId,
|
|
15
|
+
parties: [partyId],
|
|
16
|
+
beginExclusive: ledgerEnd
|
|
17
|
+
},
|
|
18
|
+
defaultRetryableOptions,
|
|
19
|
+
{
|
|
20
|
+
query: {
|
|
21
|
+
limit: COMPLETIONS_LIMIT,
|
|
22
|
+
stream_idle_timeout_ms: COMPLETIONS_STREAM_IDLE_TIMEOUT_MS
|
|
1859
23
|
}
|
|
1860
|
-
);
|
|
1861
|
-
return {
|
|
1862
|
-
choiceContextData: choiceContext.choiceContextData,
|
|
1863
|
-
disclosedContracts: choiceContext.disclosedContracts
|
|
1864
|
-
};
|
|
1865
|
-
}
|
|
1866
|
-
async createWithdrawTransferInstructionFromContext(transferInstructionCid, choiceContext) {
|
|
1867
|
-
try {
|
|
1868
|
-
const exercise = {
|
|
1869
|
-
templateId: TRANSFER_INSTRUCTION_INTERFACE_ID,
|
|
1870
|
-
contractId: transferInstructionCid,
|
|
1871
|
-
choice: "TransferInstruction_Withdraw",
|
|
1872
|
-
choiceArgument: {
|
|
1873
|
-
extraArgs: {
|
|
1874
|
-
context: choiceContext.choiceContextData,
|
|
1875
|
-
meta: { values: {} }
|
|
1876
|
-
}
|
|
1877
|
-
}
|
|
1878
|
-
};
|
|
1879
|
-
return [exercise, choiceContext.disclosedContracts];
|
|
1880
|
-
} catch (e) {
|
|
1881
|
-
this.logger.error(
|
|
1882
|
-
"Failed to create withdraw transfer instruction:",
|
|
1883
|
-
e
|
|
1884
|
-
);
|
|
1885
|
-
throw e;
|
|
1886
24
|
}
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
}
|
|
1896
|
-
);
|
|
25
|
+
);
|
|
26
|
+
const completions = responses.filter(
|
|
27
|
+
(r) => "Completion" in r.completionResponse
|
|
28
|
+
);
|
|
29
|
+
const wantedCompletion = responses.find((r) => {
|
|
30
|
+
if ("Completion" in r.completionResponse) {
|
|
31
|
+
const completion = r.completionResponse.Completion.value;
|
|
32
|
+
return completion.userId === userId && (completion.commandId === commandIdOrSubmissionId || completion.submissionId === commandIdOrSubmissionId);
|
|
1897
33
|
}
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
);
|
|
1906
|
-
}
|
|
1907
|
-
async createTransferInstruction(transferInstructionCid, registryUrl, instructionChoice, prefetchedRegistryChoiceContext) {
|
|
1908
|
-
switch (instructionChoice) {
|
|
1909
|
-
case "Accept":
|
|
1910
|
-
return this.createAcceptTransferInstruction(
|
|
1911
|
-
transferInstructionCid,
|
|
1912
|
-
registryUrl,
|
|
1913
|
-
prefetchedRegistryChoiceContext
|
|
1914
|
-
);
|
|
1915
|
-
case "Reject":
|
|
1916
|
-
return this.createRejectTransferInstruction(
|
|
1917
|
-
transferInstructionCid,
|
|
1918
|
-
registryUrl,
|
|
1919
|
-
prefetchedRegistryChoiceContext
|
|
1920
|
-
);
|
|
1921
|
-
case "Withdraw":
|
|
1922
|
-
return this.createWithdrawTransferInstruction(
|
|
1923
|
-
transferInstructionCid,
|
|
1924
|
-
registryUrl,
|
|
1925
|
-
prefetchedRegistryChoiceContext
|
|
1926
|
-
);
|
|
34
|
+
return false;
|
|
35
|
+
});
|
|
36
|
+
if (wantedCompletion && "Completion" in wantedCompletion.completionResponse) {
|
|
37
|
+
const completion = wantedCompletion.completionResponse.Completion.value;
|
|
38
|
+
const status = completion.status;
|
|
39
|
+
if (status && status.code !== 0) {
|
|
40
|
+
throw asGrpcError(status);
|
|
1927
41
|
}
|
|
1928
|
-
|
|
1929
|
-
}
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
this.logger = logger;
|
|
1934
|
-
this.accessTokenProvider = accessTokenProvider;
|
|
1935
|
-
this.isMasterUser = isMasterUser;
|
|
1936
|
-
__publicField(this, "core");
|
|
1937
|
-
__publicField(this, "allocation");
|
|
1938
|
-
__publicField(this, "transfer");
|
|
1939
|
-
this.core = new CoreService(
|
|
42
|
+
return completion;
|
|
43
|
+
} else {
|
|
44
|
+
const lastCompletion = completions[completions.length - 1];
|
|
45
|
+
const newLedgerEnd = lastCompletion && "Completion" in lastCompletion.completionResponse ? lastCompletion.completionResponse.Completion.value.offset : void 0;
|
|
46
|
+
return awaitCompletion(
|
|
1940
47
|
ledgerClient,
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
48
|
+
newLedgerEnd || ledgerEnd,
|
|
49
|
+
// !newLedgerEnd implies response was empty
|
|
50
|
+
partyId,
|
|
51
|
+
userId,
|
|
52
|
+
commandIdOrSubmissionId
|
|
1946
53
|
);
|
|
1947
|
-
this.allocation = new AllocationService(this.core, this.logger);
|
|
1948
|
-
this.transfer = new TransferService(this.core, this.logger);
|
|
1949
54
|
}
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
);
|
|
1962
|
-
} catch (e) {
|
|
1963
|
-
this.logger.error(e);
|
|
1964
|
-
throw new Error(
|
|
1965
|
-
`Instrument id ${instrumentId} does not exist for this instrument admin.`
|
|
1966
|
-
);
|
|
55
|
+
}
|
|
56
|
+
async function promiseWithTimeout(promise, timeoutMs, errorMessage) {
|
|
57
|
+
let timeoutPid = null;
|
|
58
|
+
const timeoutPromise = new Promise((_resolve, reject) => {
|
|
59
|
+
timeoutPid = setTimeout(() => reject(errorMessage), timeoutMs);
|
|
60
|
+
});
|
|
61
|
+
try {
|
|
62
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
63
|
+
} finally {
|
|
64
|
+
if (timeoutPid) {
|
|
65
|
+
clearTimeout(timeoutPid);
|
|
1967
66
|
}
|
|
1968
67
|
}
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
// i.e. when querying by TransferInstruction interfaceId, <T> would be TransferInstructionView from daml codegen
|
|
1985
|
-
async listContractsByInterface(interfaceId, partyId, limit, offset, continueUntilCompletion) {
|
|
1986
|
-
return this.core.listContractsByInterface(
|
|
1987
|
-
interfaceId,
|
|
1988
|
-
partyId,
|
|
1989
|
-
limit,
|
|
1990
|
-
offset,
|
|
1991
|
-
continueUntilCompletion
|
|
1992
|
-
);
|
|
1993
|
-
}
|
|
1994
|
-
async listHoldingTransactions(partyId, afterOffset, beforeOffset) {
|
|
68
|
+
}
|
|
69
|
+
var defaultRetryableOptions = {
|
|
70
|
+
retries: 5,
|
|
71
|
+
delayMs: 3e3,
|
|
72
|
+
cantonErrorKeys: [
|
|
73
|
+
"SEQUENCER_REQUEST_FAILED",
|
|
74
|
+
"SEQUENCER_BACKPRESSURE",
|
|
75
|
+
"SUBMISSION_ALREADY_IN_FLIGHT",
|
|
76
|
+
"LOCAL_VERDICT_TIMEOUT",
|
|
77
|
+
"NOT_SEQUENCED_TIMEOUT",
|
|
78
|
+
"NO_VIEW_WITH_VALID_RECIPIENTS"
|
|
79
|
+
]
|
|
80
|
+
};
|
|
81
|
+
async function retryable(fn, retryableOptions2, logger) {
|
|
82
|
+
for (let attempts = 1; attempts <= retryableOptions2.retries; attempts++) {
|
|
1995
83
|
try {
|
|
1996
|
-
|
|
1997
|
-
const afterOffsetOrLatest = Number(afterOffset) || (await this.ledgerClient.getWithRetry(
|
|
1998
|
-
"/v2/state/latest-pruned-offsets"
|
|
1999
|
-
)).participantPrunedUpToInclusive;
|
|
2000
|
-
const beforeOffsetOrLatest = Number(beforeOffset) || (await this.ledgerClient.getWithRetry("/v2/state/ledger-end")).offset;
|
|
2001
|
-
this.logger.debug(afterOffsetOrLatest, "Using offset");
|
|
2002
|
-
const updatesResponse = await this.ledgerClient.postWithRetry("/v2/updates/flats", {
|
|
2003
|
-
updateFormat: {
|
|
2004
|
-
includeTransactions: {
|
|
2005
|
-
eventFormat: EventFilterBySetup({
|
|
2006
|
-
interfaceIds: TokenStandardTransactionInterfaces,
|
|
2007
|
-
isMasterUser: this.isMasterUser,
|
|
2008
|
-
partyId,
|
|
2009
|
-
includeWildcard: true
|
|
2010
|
-
}),
|
|
2011
|
-
transactionShape: "TRANSACTION_SHAPE_LEDGER_EFFECTS"
|
|
2012
|
-
}
|
|
2013
|
-
},
|
|
2014
|
-
beginExclusive: afterOffsetOrLatest,
|
|
2015
|
-
endInclusive: beforeOffsetOrLatest,
|
|
2016
|
-
verbose: false
|
|
2017
|
-
});
|
|
2018
|
-
return this.core.toPrettyTransactions(
|
|
2019
|
-
updatesResponse,
|
|
2020
|
-
partyId,
|
|
2021
|
-
this.ledgerClient
|
|
2022
|
-
);
|
|
84
|
+
return await fn();
|
|
2023
85
|
} catch (err) {
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
const getTransactionResponse = await this.ledgerClient.postWithRetry(
|
|
2039
|
-
"/v2/updates/transaction-by-id",
|
|
2040
|
-
{
|
|
2041
|
-
updateId,
|
|
2042
|
-
transactionFormat
|
|
86
|
+
const grpcError = asGrpcError(err);
|
|
87
|
+
const message = grpcError.message;
|
|
88
|
+
const shouldRetry = retryableOptions2.cantonErrorKeys.length === 0 || retryableOptions2.cantonErrorKeys.some(
|
|
89
|
+
(key) => message.includes(key)
|
|
90
|
+
);
|
|
91
|
+
if (attempts < retryableOptions2.retries && shouldRetry) {
|
|
92
|
+
logger?.warn(
|
|
93
|
+
`Caught retryiable error: ${message}. Retrying attempt ${attempts} of ${retryableOptions2.retries}...`
|
|
94
|
+
);
|
|
95
|
+
await new Promise(
|
|
96
|
+
(res) => setTimeout(res, retryableOptions2.delayMs)
|
|
97
|
+
);
|
|
98
|
+
} else {
|
|
99
|
+
throw grpcError;
|
|
2043
100
|
}
|
|
2044
|
-
);
|
|
2045
|
-
return this.core.toPrettyTransaction(
|
|
2046
|
-
getTransactionResponse,
|
|
2047
|
-
partyId,
|
|
2048
|
-
this.ledgerClient
|
|
2049
|
-
);
|
|
2050
|
-
}
|
|
2051
|
-
async getInputHoldingsCids(sender, inputUtxos, amount) {
|
|
2052
|
-
return this.core.getInputHoldingsCids(sender, inputUtxos, amount);
|
|
2053
|
-
}
|
|
2054
|
-
async createDelegateProxyTranfser(sender, receiver, amount, instrumentAdmin, instrumentId, registryUrl, featuredAppRightCid, proxyCid, beneficiaries, inputUtxos, memo, expiryDate, meta) {
|
|
2055
|
-
const [transferCommand, disclosedContracts] = await this.transfer.createTransfer(
|
|
2056
|
-
sender,
|
|
2057
|
-
receiver,
|
|
2058
|
-
amount,
|
|
2059
|
-
instrumentAdmin,
|
|
2060
|
-
instrumentId,
|
|
2061
|
-
registryUrl,
|
|
2062
|
-
inputUtxos,
|
|
2063
|
-
memo,
|
|
2064
|
-
expiryDate,
|
|
2065
|
-
meta
|
|
2066
|
-
);
|
|
2067
|
-
const sumOfWeights = beneficiaries.reduce(
|
|
2068
|
-
(totalWeight, beneficiary) => totalWeight + beneficiary.weight,
|
|
2069
|
-
0
|
|
2070
|
-
);
|
|
2071
|
-
if (sumOfWeights > 1) {
|
|
2072
|
-
throw new Error("Sum of beneficiary weights is larger than 1.");
|
|
2073
101
|
}
|
|
2074
|
-
const choiceArgs = {
|
|
2075
|
-
cid: transferCommand.contractId,
|
|
2076
|
-
proxyArg: {
|
|
2077
|
-
featuredAppRightCid,
|
|
2078
|
-
beneficiaries,
|
|
2079
|
-
choiceArg: transferCommand.choiceArgument
|
|
2080
|
-
}
|
|
2081
|
-
};
|
|
2082
|
-
const exercise = {
|
|
2083
|
-
templateId: FEATURED_APP_DELEGATE_PROXY_INTERFACE_ID,
|
|
2084
|
-
contractId: proxyCid,
|
|
2085
|
-
choice: "DelegateProxy_TransferFactory_Transfer",
|
|
2086
|
-
choiceArgument: choiceArgs
|
|
2087
|
-
};
|
|
2088
|
-
return [exercise, disclosedContracts];
|
|
2089
|
-
}
|
|
2090
|
-
async exerciseDelegateProxyTransferInstructionAccept(exchangeParty, proxyCid, transferInstructionCid, registryUrl, featuredAppRightCid) {
|
|
2091
|
-
const [acceptTransferInstructionContext, disclosedContracts] = await this.transfer.createAcceptTransferInstruction(
|
|
2092
|
-
transferInstructionCid,
|
|
2093
|
-
registryUrl
|
|
2094
|
-
);
|
|
2095
|
-
const choiceArgs = {
|
|
2096
|
-
cid: acceptTransferInstructionContext.contractId,
|
|
2097
|
-
proxyArg: {
|
|
2098
|
-
featuredAppRightCid,
|
|
2099
|
-
beneficiaries: [
|
|
2100
|
-
{
|
|
2101
|
-
beneficiary: exchangeParty,
|
|
2102
|
-
weight: 1
|
|
2103
|
-
}
|
|
2104
|
-
],
|
|
2105
|
-
choiceArg: acceptTransferInstructionContext.choiceArgument
|
|
2106
|
-
}
|
|
2107
|
-
};
|
|
2108
|
-
return [
|
|
2109
|
-
{
|
|
2110
|
-
templateId: "#splice-util-featured-app-proxies:Splice.Util.FeaturedApp.DelegateProxy:DelegateProxy",
|
|
2111
|
-
contractId: proxyCid,
|
|
2112
|
-
choice: "DelegateProxy_TransferInstruction_Accept",
|
|
2113
|
-
choiceArgument: choiceArgs
|
|
2114
|
-
},
|
|
2115
|
-
disclosedContracts
|
|
2116
|
-
];
|
|
2117
102
|
}
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
103
|
+
throw new Error("retryable: Exceeded maximum retries without throwing.");
|
|
104
|
+
}
|
|
105
|
+
var isJsCantonError = (e) => typeof e === "object" && e !== null && "code" in e && "cause" in e && "errorCategory" in e;
|
|
106
|
+
var asJsCantonError = (e) => {
|
|
107
|
+
if (isJsCantonError(e)) {
|
|
108
|
+
return e;
|
|
109
|
+
} else {
|
|
110
|
+
throw e;
|
|
2125
111
|
}
|
|
2126
112
|
};
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
}
|
|
2137
|
-
async getTransferPreApprovalByParty(partyId) {
|
|
2138
|
-
const { transfer_preapproval } = await this.scanProxyClient.get(
|
|
2139
|
-
"/v0/scan-proxy/transfer-preapprovals/by-party/{party}",
|
|
2140
|
-
{
|
|
2141
|
-
path: {
|
|
2142
|
-
party: partyId
|
|
2143
|
-
}
|
|
2144
|
-
}
|
|
2145
|
-
);
|
|
2146
|
-
return transfer_preapproval;
|
|
2147
|
-
}
|
|
2148
|
-
async getFeaturedAppsByParty(partyId) {
|
|
2149
|
-
const { featured_app_right } = await this.scanProxyClient.get(
|
|
2150
|
-
"/v0/scan-proxy/featured-apps/{provider_party_id}",
|
|
2151
|
-
{
|
|
2152
|
-
path: {
|
|
2153
|
-
provider_party_id: partyId
|
|
2154
|
-
}
|
|
2155
|
-
}
|
|
2156
|
-
);
|
|
2157
|
-
return featured_app_right;
|
|
2158
|
-
}
|
|
2159
|
-
async buyMemberTraffic(dso, provider, trafficAmount, synchronizerId, memberId, migrationId, inputUtxos) {
|
|
2160
|
-
const amuletRules = await this.scanProxyClient.getAmuletRules();
|
|
2161
|
-
const activeRound = await this.scanProxyClient.getActiveOpenMiningRound();
|
|
2162
|
-
const inputHoldings = await this.tokenStandard.getInputHoldingsCids(
|
|
2163
|
-
provider,
|
|
2164
|
-
inputUtxos,
|
|
2165
|
-
trafficAmount
|
|
2166
|
-
);
|
|
2167
|
-
if (!amuletRules) {
|
|
2168
|
-
throw new Error("AmuletRules contract not found");
|
|
2169
|
-
}
|
|
2170
|
-
if (!activeRound) {
|
|
2171
|
-
throw new Error(
|
|
2172
|
-
"OpenMiningRound active at current moment not found"
|
|
2173
|
-
);
|
|
2174
|
-
}
|
|
2175
|
-
const disclosed = [
|
|
2176
|
-
{
|
|
2177
|
-
templateId: amuletRules.template_id,
|
|
2178
|
-
contractId: amuletRules.contract_id,
|
|
2179
|
-
createdEventBlob: amuletRules.created_event_blob,
|
|
2180
|
-
synchronizerId
|
|
2181
|
-
},
|
|
2182
|
-
{
|
|
2183
|
-
templateId: activeRound.template_id,
|
|
2184
|
-
contractId: activeRound.contract_id,
|
|
2185
|
-
createdEventBlob: activeRound.created_event_blob,
|
|
2186
|
-
synchronizerId
|
|
2187
|
-
}
|
|
2188
|
-
];
|
|
2189
|
-
const context = {
|
|
2190
|
-
openMiningRound: activeRound.contract_id,
|
|
2191
|
-
issuingMiningRounds: [],
|
|
2192
|
-
validatorRights: [],
|
|
2193
|
-
featuredAppRight: null
|
|
2194
|
-
};
|
|
2195
|
-
const choiceArgs = {
|
|
2196
|
-
context,
|
|
2197
|
-
inputs: inputHoldings.map((cid) => ({
|
|
2198
|
-
tag: "InputAmulet",
|
|
2199
|
-
value: cid
|
|
2200
|
-
})),
|
|
2201
|
-
provider,
|
|
2202
|
-
memberId: this.tokenStandard.core.toQualifiedMemberId(memberId),
|
|
2203
|
-
synchronizerId,
|
|
2204
|
-
migrationId,
|
|
2205
|
-
trafficAmount,
|
|
2206
|
-
expectedDso: dso
|
|
2207
|
-
};
|
|
2208
|
-
const exercise = {
|
|
2209
|
-
templateId: "#splice-amulet:Splice.AmuletRules:AmuletRules",
|
|
2210
|
-
contractId: amuletRules.contract_id,
|
|
2211
|
-
choice: "AmuletRules_BuyMemberTraffic",
|
|
2212
|
-
choiceArgument: choiceArgs
|
|
2213
|
-
};
|
|
2214
|
-
return [exercise, disclosed];
|
|
113
|
+
var asGrpcError = (e) => {
|
|
114
|
+
let errorInfo = void 0;
|
|
115
|
+
let retryInfo = void 0;
|
|
116
|
+
let code = 500;
|
|
117
|
+
let message = e?.message || "";
|
|
118
|
+
if (typeof e === "object" && e !== null && "cause" in e) {
|
|
119
|
+
message = e.cause;
|
|
120
|
+
} else if (typeof e === "object" && e !== null && "message" in e) {
|
|
121
|
+
message = e.message;
|
|
2215
122
|
}
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
123
|
+
if (typeof e === "object" && e !== null && "code" in e && "message" in e && "details" in e && Array.isArray(e.details)) {
|
|
124
|
+
code = e.code;
|
|
125
|
+
message = e.message;
|
|
126
|
+
for (const detail of e.details) {
|
|
127
|
+
if (detail.typeUrl === "type.googleapis.com/google.rpc.ErrorInfo" && typeof detail.value === "string") {
|
|
128
|
+
try {
|
|
129
|
+
errorInfo = ErrorInfo.fromBinary(
|
|
130
|
+
Buffer.from(detail.value, "base64")
|
|
131
|
+
);
|
|
132
|
+
} catch {
|
|
2226
133
|
}
|
|
2227
|
-
}
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
templateId: amuletRules.template_id,
|
|
2234
|
-
contractId: amuletRules.contract_id,
|
|
2235
|
-
createdEventBlob: amuletRules.created_event_blob,
|
|
2236
|
-
synchronizerId
|
|
2237
|
-
};
|
|
2238
|
-
return [
|
|
2239
|
-
{
|
|
2240
|
-
templateId: amuletRules.template_id,
|
|
2241
|
-
contractId: amuletRules.contract_id,
|
|
2242
|
-
choice: "AmuletRules_DevNet_FeatureApp",
|
|
2243
|
-
choiceArgument: {
|
|
2244
|
-
provider: providerPartyId
|
|
134
|
+
} else if (detail.typeUrl === "type.googleapis.com/google.rpc.RetryInfo" && typeof detail.value === "string") {
|
|
135
|
+
try {
|
|
136
|
+
retryInfo = RetryInfo.fromBinary(
|
|
137
|
+
Buffer.from(detail.value, "base64")
|
|
138
|
+
);
|
|
139
|
+
} catch {
|
|
2245
140
|
}
|
|
2246
|
-
},
|
|
2247
|
-
[disclosedContracts]
|
|
2248
|
-
];
|
|
2249
|
-
}
|
|
2250
|
-
async cancelTransferPreapproval(contractId, templateId, actor) {
|
|
2251
|
-
const exercise = {
|
|
2252
|
-
templateId,
|
|
2253
|
-
contractId,
|
|
2254
|
-
choice: "TransferPreapproval_Cancel",
|
|
2255
|
-
choiceArgument: { p: actor }
|
|
2256
|
-
};
|
|
2257
|
-
return [exercise, []];
|
|
2258
|
-
}
|
|
2259
|
-
async renewTransferPreapproval(contractId, templateId, provider, synchronizerId, newExpiresAt, inputUtxos) {
|
|
2260
|
-
const amuletRules = await this.scanProxyClient.getAmuletRules();
|
|
2261
|
-
const activeRound = await this.scanProxyClient.getActiveOpenMiningRound();
|
|
2262
|
-
if (!amuletRules) {
|
|
2263
|
-
throw new Error("AmuletRules contract not found");
|
|
2264
|
-
}
|
|
2265
|
-
if (!activeRound) {
|
|
2266
|
-
throw new Error(
|
|
2267
|
-
"OpenMiningRound active at current moment not found"
|
|
2268
|
-
);
|
|
2269
|
-
}
|
|
2270
|
-
const disclosed = [
|
|
2271
|
-
{
|
|
2272
|
-
templateId: amuletRules.template_id,
|
|
2273
|
-
contractId: amuletRules.contract_id,
|
|
2274
|
-
createdEventBlob: amuletRules.created_event_blob,
|
|
2275
|
-
synchronizerId
|
|
2276
|
-
},
|
|
2277
|
-
{
|
|
2278
|
-
templateId: activeRound.template_id,
|
|
2279
|
-
contractId: activeRound.contract_id,
|
|
2280
|
-
createdEventBlob: activeRound.created_event_blob,
|
|
2281
|
-
synchronizerId
|
|
2282
|
-
}
|
|
2283
|
-
];
|
|
2284
|
-
const inputHoldings = await this.tokenStandard.getInputHoldingsCids(
|
|
2285
|
-
provider,
|
|
2286
|
-
inputUtxos
|
|
2287
|
-
);
|
|
2288
|
-
const context = {
|
|
2289
|
-
context: {
|
|
2290
|
-
openMiningRound: activeRound.contract_id,
|
|
2291
|
-
issuingMiningRounds: [],
|
|
2292
|
-
validatorRights: [],
|
|
2293
|
-
featuredAppRight: null
|
|
2294
|
-
},
|
|
2295
|
-
amuletRules: amuletRules.contract_id
|
|
2296
|
-
};
|
|
2297
|
-
const effectiveNewExpiresAt = newExpiresAt ?? new Date(Date.now() + 90 * 24 * 3600 * 1e3);
|
|
2298
|
-
const exercise = {
|
|
2299
|
-
templateId,
|
|
2300
|
-
contractId,
|
|
2301
|
-
choice: "TransferPreapproval_Renew",
|
|
2302
|
-
choiceArgument: {
|
|
2303
|
-
context,
|
|
2304
|
-
inputs: inputHoldings.map((cid) => ({
|
|
2305
|
-
tag: "InputAmulet",
|
|
2306
|
-
value: cid
|
|
2307
|
-
})),
|
|
2308
|
-
newExpiresAt: effectiveNewExpiresAt.toISOString()
|
|
2309
|
-
}
|
|
2310
|
-
};
|
|
2311
|
-
return [exercise, disclosed];
|
|
2312
|
-
}
|
|
2313
|
-
async createTap(receiver, amount, instrumentAdmin, instrumentId, registryUrl) {
|
|
2314
|
-
const now = /* @__PURE__ */ new Date();
|
|
2315
|
-
const tomorrow = new Date(now);
|
|
2316
|
-
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
2317
|
-
const choiceArgs = {
|
|
2318
|
-
expectedAdmin: instrumentAdmin,
|
|
2319
|
-
transfer: {
|
|
2320
|
-
sender: instrumentAdmin,
|
|
2321
|
-
receiver,
|
|
2322
|
-
amount,
|
|
2323
|
-
instrumentId: { admin: instrumentAdmin, id: instrumentId },
|
|
2324
|
-
lock: null,
|
|
2325
|
-
requestedAt: new Date(
|
|
2326
|
-
Date.now() - REQUESTED_AT_SKEW_MS2
|
|
2327
|
-
).toISOString(),
|
|
2328
|
-
executeBefore: tomorrow.toISOString(),
|
|
2329
|
-
inputHoldingCids: [],
|
|
2330
|
-
meta: { values: {} }
|
|
2331
|
-
},
|
|
2332
|
-
extraArgs: {
|
|
2333
|
-
context: { values: {} },
|
|
2334
|
-
meta: { values: {} }
|
|
2335
141
|
}
|
|
2336
|
-
};
|
|
2337
|
-
const disclosedContracts = (await this.tokenStandard.transfer.fetchTransferFactoryChoiceContext(
|
|
2338
|
-
registryUrl,
|
|
2339
|
-
choiceArgs
|
|
2340
|
-
)).choiceContext.disclosedContracts;
|
|
2341
|
-
const amuletRules = await this.scanProxyClient.getAmuletRules();
|
|
2342
|
-
if (!amuletRules) {
|
|
2343
|
-
throw new Error("AmuletRules contract not found");
|
|
2344
142
|
}
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
throw new Error(
|
|
2348
|
-
"OpenMiningRound active at current moment not found"
|
|
2349
|
-
);
|
|
2350
|
-
}
|
|
2351
|
-
return [
|
|
2352
|
-
{
|
|
2353
|
-
templateId: amuletRules.template_id,
|
|
2354
|
-
contractId: amuletRules.contract_id,
|
|
2355
|
-
choice: "AmuletRules_DevNet_Tap",
|
|
2356
|
-
choiceArgument: {
|
|
2357
|
-
receiver,
|
|
2358
|
-
amount,
|
|
2359
|
-
openRound: latestOpenMiningRound.contract_id
|
|
2360
|
-
}
|
|
2361
|
-
},
|
|
2362
|
-
disclosedContracts
|
|
2363
|
-
];
|
|
143
|
+
} else {
|
|
144
|
+
throw e;
|
|
2364
145
|
}
|
|
146
|
+
return { code, message, errorInfo, retryInfo };
|
|
2365
147
|
};
|
|
2366
148
|
|
|
2367
149
|
// src/acs/acs-container.ts
|
|
@@ -2882,11 +664,9 @@ var LedgerClient = class {
|
|
|
2882
664
|
}
|
|
2883
665
|
async init() {
|
|
2884
666
|
if (!this.initialized) {
|
|
2885
|
-
this.logger.debug(
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
this.baseUrl.href
|
|
2889
|
-
);
|
|
667
|
+
this.logger.debug({
|
|
668
|
+
message: `Initializing LedgerClient with version ${this.clientVersion} for url ${this.baseUrl.href}`
|
|
669
|
+
});
|
|
2890
670
|
const versionFromClient = await this.currentClient.GET("/v2/version");
|
|
2891
671
|
this.logger.debug(versionFromClient, "getV2Version response");
|
|
2892
672
|
this.clientVersion = this.parseSupportedVersions(
|
|
@@ -3263,6 +1043,12 @@ var LedgerClient = class {
|
|
|
3263
1043
|
).filter((event) => event.consuming);
|
|
3264
1044
|
const createdEvents = data.events?.filter((event) => "CreatedEvent" in event).map(
|
|
3265
1045
|
(event) => event.CreatedEvent
|
|
1046
|
+
).filter(
|
|
1047
|
+
(event) => Object.keys(
|
|
1048
|
+
activeContractsArgs.filter?.filtersByParty ?? {}
|
|
1049
|
+
).includes(
|
|
1050
|
+
event.createArgument?.owner ?? ""
|
|
1051
|
+
)
|
|
3266
1052
|
);
|
|
3267
1053
|
exercisedEvents?.forEach((event) => {
|
|
3268
1054
|
exercisedContracts.add(event.contractId);
|
|
@@ -3448,6 +1234,6 @@ var WSSupportWithBackOffSwitch = class {
|
|
|
3448
1234
|
}
|
|
3449
1235
|
};
|
|
3450
1236
|
|
|
3451
|
-
export { ACSContainer, ACS_UPDATE_CONFIG,
|
|
1237
|
+
export { ACSContainer, ACS_UPDATE_CONFIG, LedgerClient, WSSupportWithBackOffSwitch, asJsCantonError, awaitCompletion, defaultRetryableOptions, isJsCantonError, promiseWithTimeout, supportedVersions };
|
|
3452
1238
|
//# sourceMappingURL=index.js.map
|
|
3453
1239
|
//# sourceMappingURL=index.js.map
|