@canton-network/core-ledger-client 0.15.0 → 0.16.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-container.d.ts +33 -0
- package/dist/acs/acs-container.d.ts.map +1 -0
- package/dist/acs/acs-helper.d.ts +37 -0
- package/dist/acs/acs-helper.d.ts.map +1 -0
- package/dist/acs/types.d.ts +10 -0
- package/dist/acs/types.d.ts.map +1 -0
- package/dist/acs/ws-support.d.ts +18 -0
- package/dist/acs/ws-support.d.ts.map +1 -0
- package/dist/index.cjs +3317 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3297 -7
- package/dist/index.js.map +1 -0
- package/dist/ledger-api-utils.d.ts +3 -1
- package/dist/ledger-api-utils.d.ts.map +1 -1
- package/dist/ledger-client.d.ts +31 -3
- package/dist/ledger-client.d.ts.map +1 -1
- package/dist/token-standard-service.d.ts +10 -7
- package/dist/token-standard-service.d.ts.map +1 -1
- package/dist/topology-write-service.d.ts +4 -2
- package/dist/topology-write-service.d.ts.map +1 -1
- package/dist/txparse/parser.d.ts.map +1 -1
- package/dist/txparse/test-data/mock/eventsByContractIdResponses.d.ts +153 -0
- package/dist/txparse/test-data/mock/eventsByContractIdResponses.d.ts.map +1 -1
- package/dist/txparse/types.d.ts +5 -2
- package/dist/txparse/types.d.ts.map +1 -1
- package/package.json +23 -11
- package/dist/constants.js +0 -47
- package/dist/generated-clients/openapi-3.3.0-SNAPSHOT.js +0 -5
- package/dist/generated-clients/openapi-3.4.0-SNAPSHOT.js +0 -5
- package/dist/ledger-api-utils.js +0 -432
- package/dist/ledger-client.js +0 -321
- package/dist/token-standard-service.js +0 -871
- package/dist/topology-write-service.js +0 -283
- package/dist/txparse/parser.js +0 -581
- package/dist/txparse/parser.test.d.ts +0 -2
- package/dist/txparse/parser.test.d.ts.map +0 -1
- package/dist/txparse/parser.test.js +0 -97
- package/dist/txparse/strict.js +0 -19
- package/dist/txparse/test-data/expected/txs.js +0 -2303
- package/dist/txparse/test-data/mock/eventsByContractIdResponses.js +0 -2973
- package/dist/txparse/test-data/mock/txs.js +0 -6788
- package/dist/txparse/types.js +0 -58
package/dist/txparse/parser.js
DELETED
|
@@ -1,581 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
-
import { ensureInterfaceViewIsPresent, getInterfaceView, getKnownInterfaceView, getMetaKeyValue, hasInterface, mergeMetas, removeParsedMetaKeys, EventFilterBySetup, } from '../ledger-api-utils.js';
|
|
5
|
-
import { BurnedMetaKey, matchInterfaceIds, ReasonMetaKey, SenderMetaKey, TxKindMetaKey, } from '../constants.js';
|
|
6
|
-
import { EmptyHoldingsChangeSummary, } from './types.js';
|
|
7
|
-
import BigNumber from 'bignumber.js';
|
|
8
|
-
import { HOLDING_INTERFACE_ID, TRANSFER_INSTRUCTION_INTERFACE_ID, } from '@canton-network/core-token-standard';
|
|
9
|
-
export class TransactionParser {
|
|
10
|
-
ledgerClient;
|
|
11
|
-
partyId;
|
|
12
|
-
transaction;
|
|
13
|
-
isMasterUser;
|
|
14
|
-
constructor(transaction, ledgerClient, partyId, isMasterUser) {
|
|
15
|
-
this.ledgerClient = ledgerClient;
|
|
16
|
-
this.partyId = partyId;
|
|
17
|
-
this.transaction = transaction;
|
|
18
|
-
this.isMasterUser = isMasterUser;
|
|
19
|
-
}
|
|
20
|
-
async parseTransaction() {
|
|
21
|
-
const tx = this.transaction;
|
|
22
|
-
const events = await this.parseEvents([...(tx.events || [])].reverse());
|
|
23
|
-
return {
|
|
24
|
-
updateId: tx.updateId,
|
|
25
|
-
offset: tx.offset,
|
|
26
|
-
recordTime: tx.recordTime,
|
|
27
|
-
synchronizerId: tx.synchronizerId,
|
|
28
|
-
events,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
async parseEvents(eventsStack) {
|
|
32
|
-
let callStack = [];
|
|
33
|
-
let continueAfterNodeId = -1;
|
|
34
|
-
const result = [];
|
|
35
|
-
while (eventsStack.length > 0) {
|
|
36
|
-
const currentEvent = eventsStack.pop();
|
|
37
|
-
const { nodeId, createdEvent, archivedEvent, exercisedEvent } = getNodeIdAndEvent(currentEvent);
|
|
38
|
-
callStack = callStack.filter((s) => s.untilNodeId <= nodeId);
|
|
39
|
-
const parentChoice = (callStack[callStack.length - 1] &&
|
|
40
|
-
callStack[callStack.length - 1].parentChoiceName) ||
|
|
41
|
-
'none (root node)';
|
|
42
|
-
let parsed;
|
|
43
|
-
if (nodeId <= continueAfterNodeId) {
|
|
44
|
-
parsed = null;
|
|
45
|
-
}
|
|
46
|
-
else if (createdEvent) {
|
|
47
|
-
parsed = this.parseRawCreate(createdEvent, parentChoice);
|
|
48
|
-
}
|
|
49
|
-
else if (archivedEvent) {
|
|
50
|
-
parsed = await this.parseRawArchive(archivedEvent, parentChoice);
|
|
51
|
-
}
|
|
52
|
-
else if (exercisedEvent) {
|
|
53
|
-
parsed = await this.parseExercise(exercisedEvent);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
throw new Error(`Impossible event: ${JSON.stringify(currentEvent)}`);
|
|
57
|
-
}
|
|
58
|
-
if (parsed && isLeafEventNode(parsed)) {
|
|
59
|
-
// Exclude events where nothing happened
|
|
60
|
-
if (holdingChangesNonEmpty(parsed.event)) {
|
|
61
|
-
result.push({
|
|
62
|
-
...parsed.event,
|
|
63
|
-
label: {
|
|
64
|
-
...parsed.event.label,
|
|
65
|
-
meta: removeParsedMetaKeys(parsed.event.label.meta),
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
continueAfterNodeId = parsed.continueAfterNodeId;
|
|
70
|
-
}
|
|
71
|
-
else if (parsed) {
|
|
72
|
-
callStack.push({
|
|
73
|
-
parentChoiceName: parsed.parentChoiceName,
|
|
74
|
-
untilNodeId: parsed.lastDescendantNodeId,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
parseRawCreate(create, parentChoice) {
|
|
81
|
-
return this.buildRawEvent(create, create.nodeId, (payload) => {
|
|
82
|
-
return {
|
|
83
|
-
type: 'Create',
|
|
84
|
-
parentChoice,
|
|
85
|
-
contractId: create.contractId,
|
|
86
|
-
offset: create.offset,
|
|
87
|
-
templateId: create.templateId,
|
|
88
|
-
payload,
|
|
89
|
-
packageName: create.packageName,
|
|
90
|
-
meta: undefined,
|
|
91
|
-
};
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
async parseRawArchive(archive, parentChoice) {
|
|
95
|
-
const events = await this.getEventsForArchive(archive);
|
|
96
|
-
if (!events) {
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
return this.buildRawEvent(events.created.createdEvent, archive.nodeId, (payload) => {
|
|
100
|
-
return {
|
|
101
|
-
type: 'Archive',
|
|
102
|
-
parentChoice,
|
|
103
|
-
contractId: archive.contractId,
|
|
104
|
-
offset: archive.offset,
|
|
105
|
-
templateId: archive.templateId,
|
|
106
|
-
packageName: archive.packageName,
|
|
107
|
-
actingParties: archive.actingParties || [],
|
|
108
|
-
payload,
|
|
109
|
-
meta: undefined,
|
|
110
|
-
};
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
buildRawEvent(originalCreate, nodeId, buildLabel) {
|
|
114
|
-
const view = getKnownInterfaceView(originalCreate);
|
|
115
|
-
let result;
|
|
116
|
-
switch (view?.type) {
|
|
117
|
-
case 'Holding': {
|
|
118
|
-
const holdingView = view.viewValue;
|
|
119
|
-
if (this.partyId !== holdingView.owner) {
|
|
120
|
-
result = null;
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
const isLocked = !!holdingView.lock;
|
|
124
|
-
const summary = {
|
|
125
|
-
amountChange: holdingView.amount,
|
|
126
|
-
numInputs: 0,
|
|
127
|
-
inputAmount: '0',
|
|
128
|
-
numOutputs: 1,
|
|
129
|
-
outputAmount: holdingView.amount,
|
|
130
|
-
};
|
|
131
|
-
result = {
|
|
132
|
-
payload: holdingView,
|
|
133
|
-
unlockedHoldingsChange: {
|
|
134
|
-
creates: isLocked ? [] : [holdingView],
|
|
135
|
-
archives: [],
|
|
136
|
-
},
|
|
137
|
-
lockedHoldingsChange: {
|
|
138
|
-
creates: isLocked ? [holdingView] : [],
|
|
139
|
-
archives: [],
|
|
140
|
-
},
|
|
141
|
-
lockedHoldingsChangeSummary: isLocked
|
|
142
|
-
? summary
|
|
143
|
-
: EmptyHoldingsChangeSummary,
|
|
144
|
-
unlockedHoldingsChangeSummary: isLocked
|
|
145
|
-
? EmptyHoldingsChangeSummary
|
|
146
|
-
: summary,
|
|
147
|
-
transferInstruction: null,
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
case 'TransferInstruction': {
|
|
153
|
-
const transferInstructionView = view.viewValue;
|
|
154
|
-
if (![
|
|
155
|
-
transferInstructionView.transfer.sender,
|
|
156
|
-
transferInstructionView.transfer.receiver,
|
|
157
|
-
].some((stakeholder) => stakeholder === this.partyId)) {
|
|
158
|
-
result = null;
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
result = {
|
|
162
|
-
payload: transferInstructionView,
|
|
163
|
-
transferInstruction: transferInstructionView,
|
|
164
|
-
unlockedHoldingsChange: { creates: [], archives: [] },
|
|
165
|
-
lockedHoldingsChange: { creates: [], archives: [] },
|
|
166
|
-
unlockedHoldingsChangeSummary: EmptyHoldingsChangeSummary,
|
|
167
|
-
lockedHoldingsChangeSummary: EmptyHoldingsChangeSummary,
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
default:
|
|
173
|
-
result = null;
|
|
174
|
-
}
|
|
175
|
-
return (result && {
|
|
176
|
-
continueAfterNodeId: nodeId,
|
|
177
|
-
event: {
|
|
178
|
-
label: buildLabel(result.payload),
|
|
179
|
-
unlockedHoldingsChange: result.unlockedHoldingsChange,
|
|
180
|
-
lockedHoldingsChange: result.lockedHoldingsChange,
|
|
181
|
-
lockedHoldingsChangeSummary: result.lockedHoldingsChangeSummary,
|
|
182
|
-
unlockedHoldingsChangeSummary: result.unlockedHoldingsChangeSummary,
|
|
183
|
-
transferInstruction: result.transferInstruction,
|
|
184
|
-
},
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
async parseExercise(exercise) {
|
|
188
|
-
let result = null;
|
|
189
|
-
const tokenStandardChoice = {
|
|
190
|
-
name: exercise.choice,
|
|
191
|
-
choiceArgument: exercise.choiceArgument,
|
|
192
|
-
exerciseResult: exercise.exerciseResult,
|
|
193
|
-
};
|
|
194
|
-
switch (exercise.choice) {
|
|
195
|
-
case 'TransferFactory_Transfer':
|
|
196
|
-
result = await this.buildTransfer(exercise, tokenStandardChoice);
|
|
197
|
-
break;
|
|
198
|
-
case 'TransferInstruction_Accept':
|
|
199
|
-
case 'TransferInstruction_Reject':
|
|
200
|
-
case 'TransferInstruction_Withdraw':
|
|
201
|
-
case 'TransferInstruction_Update':
|
|
202
|
-
result = await this.buildFromTransferInstructionExercise(exercise, tokenStandardChoice);
|
|
203
|
-
break;
|
|
204
|
-
case 'BurnMintFactory_BurnMint':
|
|
205
|
-
result = await this.buildMergeSplit(exercise, tokenStandardChoice);
|
|
206
|
-
break;
|
|
207
|
-
default: {
|
|
208
|
-
const meta = mergeMetas(exercise);
|
|
209
|
-
const txKind = getMetaKeyValue(TxKindMetaKey, meta);
|
|
210
|
-
if (txKind) {
|
|
211
|
-
result = await this.parseViaTxKind(exercise, txKind);
|
|
212
|
-
}
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
if (!result) {
|
|
217
|
-
return {
|
|
218
|
-
lastDescendantNodeId: exercise.lastDescendantNodeId,
|
|
219
|
-
parentChoiceName: exercise.choice,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
else {
|
|
223
|
-
// only this.partyId's holdings should be included in the response
|
|
224
|
-
const lockedHoldingsChange = {
|
|
225
|
-
creates: result.children.creates.filter((h) => !!h.lock && h.owner === this.partyId),
|
|
226
|
-
archives: result.children.archives.filter((h) => !!h.lock && h.owner === this.partyId),
|
|
227
|
-
};
|
|
228
|
-
const unlockedHoldingsChange = {
|
|
229
|
-
creates: result.children.creates.filter((h) => !h.lock && h.owner === this.partyId),
|
|
230
|
-
archives: result.children.archives.filter((h) => !h.lock && h.owner === this.partyId),
|
|
231
|
-
};
|
|
232
|
-
return {
|
|
233
|
-
event: {
|
|
234
|
-
label: result.label,
|
|
235
|
-
lockedHoldingsChange,
|
|
236
|
-
lockedHoldingsChangeSummary: computeSummary(lockedHoldingsChange, this.partyId),
|
|
237
|
-
unlockedHoldingsChange,
|
|
238
|
-
unlockedHoldingsChangeSummary: computeSummary(unlockedHoldingsChange, this.partyId),
|
|
239
|
-
transferInstruction: result.transferInstruction,
|
|
240
|
-
},
|
|
241
|
-
continueAfterNodeId: exercise.lastDescendantNodeId,
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
async parseViaTxKind(exercisedEvent, txKind) {
|
|
246
|
-
switch (txKind) {
|
|
247
|
-
case 'transfer':
|
|
248
|
-
return await this.buildTransfer(exercisedEvent, null);
|
|
249
|
-
case 'merge-split':
|
|
250
|
-
case 'burn':
|
|
251
|
-
case 'mint':
|
|
252
|
-
return await this.buildMergeSplit(exercisedEvent, null);
|
|
253
|
-
case 'unlock':
|
|
254
|
-
return await this.buildBasic(exercisedEvent, 'Unlock', null);
|
|
255
|
-
case 'expire-dust':
|
|
256
|
-
return await this.buildBasic(exercisedEvent, 'ExpireDust', null);
|
|
257
|
-
default:
|
|
258
|
-
throw new Error(`Unknown tx-kind '${txKind}' in ${JSON.stringify(exercisedEvent)}`);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
async buildTransfer(exercisedEvent, tokenStandardChoice, transferInstructions) {
|
|
262
|
-
const meta = mergeMetas(exercisedEvent, transferInstructions?.transfer?.meta);
|
|
263
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
264
|
-
const choiceArgumentTransfer = exercisedEvent.choiceArgument.transfer;
|
|
265
|
-
const sender = transferInstructions?.transfer?.sender ||
|
|
266
|
-
getMetaKeyValue(SenderMetaKey, meta) ||
|
|
267
|
-
choiceArgumentTransfer.sender;
|
|
268
|
-
if (!sender) {
|
|
269
|
-
console.error(`Malformed transfer didn't contain sender. Will instead attempt to parse the children.
|
|
270
|
-
Transfer: ${JSON.stringify(exercisedEvent)}`);
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
const children = await this.getChildren(exercisedEvent);
|
|
274
|
-
const receiverAmounts = new Map();
|
|
275
|
-
children.creates
|
|
276
|
-
.filter((h) => h.owner !== sender)
|
|
277
|
-
.forEach((holding) => receiverAmounts.set(holding.owner, (receiverAmounts.get(holding.owner) || BigNumber('0')).plus(BigNumber(holding.amount))));
|
|
278
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
279
|
-
let label;
|
|
280
|
-
if (receiverAmounts.size === 0) {
|
|
281
|
-
label = {
|
|
282
|
-
...amountChanges,
|
|
283
|
-
type: 'MergeSplit',
|
|
284
|
-
tokenStandardChoice,
|
|
285
|
-
reason,
|
|
286
|
-
meta,
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
else if (sender === this.partyId) {
|
|
290
|
-
label = {
|
|
291
|
-
...amountChanges,
|
|
292
|
-
type: 'TransferOut',
|
|
293
|
-
receiverAmounts: [...receiverAmounts].map(([k, v]) => {
|
|
294
|
-
return { receiver: k, amount: v.toString() };
|
|
295
|
-
}),
|
|
296
|
-
tokenStandardChoice,
|
|
297
|
-
reason,
|
|
298
|
-
meta,
|
|
299
|
-
};
|
|
300
|
-
}
|
|
301
|
-
else {
|
|
302
|
-
label = {
|
|
303
|
-
type: 'TransferIn',
|
|
304
|
-
// for Transfers, the burn/mint is always 0 for the receiving party (i.e., 0 for TransferIn)
|
|
305
|
-
burnAmount: '0',
|
|
306
|
-
mintAmount: '0',
|
|
307
|
-
sender,
|
|
308
|
-
tokenStandardChoice,
|
|
309
|
-
reason,
|
|
310
|
-
meta,
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
const transferInstruction = {
|
|
314
|
-
originalInstructionCid: null,
|
|
315
|
-
transfer: choiceArgumentTransfer,
|
|
316
|
-
status: {
|
|
317
|
-
before: null,
|
|
318
|
-
},
|
|
319
|
-
meta: null,
|
|
320
|
-
};
|
|
321
|
-
return {
|
|
322
|
-
label,
|
|
323
|
-
children,
|
|
324
|
-
transferInstruction,
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
async buildMergeSplit(exercisedEvent, tokenStandardChoice) {
|
|
328
|
-
let type;
|
|
329
|
-
const meta = mergeMetas(exercisedEvent);
|
|
330
|
-
switch (getMetaKeyValue(TxKindMetaKey, meta)) {
|
|
331
|
-
case 'burn':
|
|
332
|
-
type = 'Burn';
|
|
333
|
-
break;
|
|
334
|
-
case 'mint':
|
|
335
|
-
type = 'Mint';
|
|
336
|
-
break;
|
|
337
|
-
default:
|
|
338
|
-
type = 'MergeSplit';
|
|
339
|
-
}
|
|
340
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
341
|
-
const children = await this.getChildren(exercisedEvent);
|
|
342
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
343
|
-
const label = {
|
|
344
|
-
...amountChanges,
|
|
345
|
-
type,
|
|
346
|
-
tokenStandardChoice,
|
|
347
|
-
reason,
|
|
348
|
-
meta,
|
|
349
|
-
};
|
|
350
|
-
return {
|
|
351
|
-
label,
|
|
352
|
-
children,
|
|
353
|
-
transferInstruction: null,
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
async buildFromTransferInstructionExercise(exercisedEvent, tokenStandardChoice) {
|
|
357
|
-
const transferInstructionEvents = await this.getEventsForArchive(exercisedEvent);
|
|
358
|
-
if (!transferInstructionEvents) {
|
|
359
|
-
// This will happen when the party observes the archive but is not a stakeholder.
|
|
360
|
-
// For example, for Amulet, a validator will see a TransferInstruction_Reject/Withdraw
|
|
361
|
-
// but will not see the create of a TransferInstruction.
|
|
362
|
-
return null;
|
|
363
|
-
}
|
|
364
|
-
const transferInstructionView = ensureInterfaceViewIsPresent(transferInstructionEvents.created.createdEvent, TRANSFER_INSTRUCTION_INTERFACE_ID).viewValue;
|
|
365
|
-
const transferInstruction = {
|
|
366
|
-
originalInstructionCid: transferInstructionView.originalInstructionCid,
|
|
367
|
-
transfer: transferInstructionView.transfer,
|
|
368
|
-
meta: transferInstructionView.meta,
|
|
369
|
-
status: {
|
|
370
|
-
before: transferInstructionView.status,
|
|
371
|
-
},
|
|
372
|
-
};
|
|
373
|
-
const exerciseResultOutputTag = exercisedEvent.exerciseResult.output.tag;
|
|
374
|
-
let result = null;
|
|
375
|
-
switch (exerciseResultOutputTag) {
|
|
376
|
-
case 'TransferInstructionResult_Failed':
|
|
377
|
-
case 'TransferInstructionResult_Pending':
|
|
378
|
-
result = await this.buildMergeSplit(exercisedEvent, tokenStandardChoice);
|
|
379
|
-
break;
|
|
380
|
-
case 'TransferInstructionResult_Completed':
|
|
381
|
-
result = await this.buildTransfer(exercisedEvent, tokenStandardChoice, transferInstruction);
|
|
382
|
-
break;
|
|
383
|
-
default:
|
|
384
|
-
throw new Error(`Unknown TransferInstructionResult: ${exerciseResultOutputTag}`);
|
|
385
|
-
}
|
|
386
|
-
return (result && {
|
|
387
|
-
...result,
|
|
388
|
-
transferInstruction,
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
async buildBasic(exercisedEvent, type, tokenStandardChoice) {
|
|
392
|
-
const children = await this.getChildren(exercisedEvent);
|
|
393
|
-
const meta = mergeMetas(exercisedEvent);
|
|
394
|
-
const amountChanges = computeAmountChanges(children, meta, this.partyId);
|
|
395
|
-
const reason = getMetaKeyValue(ReasonMetaKey, meta);
|
|
396
|
-
return {
|
|
397
|
-
label: {
|
|
398
|
-
...amountChanges,
|
|
399
|
-
type,
|
|
400
|
-
tokenStandardChoice,
|
|
401
|
-
reason,
|
|
402
|
-
meta,
|
|
403
|
-
},
|
|
404
|
-
children,
|
|
405
|
-
transferInstruction: null,
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
async getChildren(exercisedEvent) {
|
|
409
|
-
const mutatingResult = { creates: [], archives: [] };
|
|
410
|
-
const childrenEventsSlice = (this.transaction.events || [])
|
|
411
|
-
.map(getNodeIdAndEvent)
|
|
412
|
-
.filter(({ nodeId }) => nodeId > exercisedEvent.nodeId &&
|
|
413
|
-
nodeId <= exercisedEvent.lastDescendantNodeId);
|
|
414
|
-
if (exercisedEvent.consuming &&
|
|
415
|
-
hasInterface(HOLDING_INTERFACE_ID, exercisedEvent)) {
|
|
416
|
-
const selfEvent = await this.getEventsForArchive(exercisedEvent);
|
|
417
|
-
if (selfEvent) {
|
|
418
|
-
const holdingView = ensureInterfaceViewIsPresent(selfEvent.created.createdEvent, HOLDING_INTERFACE_ID).viewValue;
|
|
419
|
-
mutatingResult.archives.push({
|
|
420
|
-
amount: holdingView.amount,
|
|
421
|
-
instrumentId: holdingView.instrumentId,
|
|
422
|
-
contractId: exercisedEvent.contractId,
|
|
423
|
-
owner: holdingView.owner,
|
|
424
|
-
meta: holdingView.meta,
|
|
425
|
-
lock: holdingView.lock,
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
for (const { createdEvent, archivedEvent, exercisedEvent, } of childrenEventsSlice) {
|
|
430
|
-
if (createdEvent) {
|
|
431
|
-
const interfaceView = getInterfaceView(createdEvent);
|
|
432
|
-
if (interfaceView &&
|
|
433
|
-
matchInterfaceIds(HOLDING_INTERFACE_ID, interfaceView.interfaceId)) {
|
|
434
|
-
const holdingView = interfaceView.viewValue;
|
|
435
|
-
mutatingResult.creates.push({
|
|
436
|
-
amount: holdingView.amount,
|
|
437
|
-
instrumentId: holdingView.instrumentId,
|
|
438
|
-
contractId: createdEvent.contractId,
|
|
439
|
-
owner: holdingView.owner,
|
|
440
|
-
meta: holdingView.meta,
|
|
441
|
-
lock: holdingView.lock,
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
else if ((archivedEvent &&
|
|
446
|
-
hasInterface(HOLDING_INTERFACE_ID, archivedEvent)) ||
|
|
447
|
-
(exercisedEvent &&
|
|
448
|
-
exercisedEvent.consuming &&
|
|
449
|
-
hasInterface(HOLDING_INTERFACE_ID, exercisedEvent))) {
|
|
450
|
-
const contractEvents = await this.getEventsForArchive(archivedEvent || exercisedEvent);
|
|
451
|
-
if (contractEvents) {
|
|
452
|
-
const holdingView = ensureInterfaceViewIsPresent(contractEvents.created?.createdEvent, HOLDING_INTERFACE_ID).viewValue;
|
|
453
|
-
mutatingResult.archives.push({
|
|
454
|
-
amount: holdingView.amount,
|
|
455
|
-
instrumentId: holdingView.instrumentId,
|
|
456
|
-
contractId: archivedEvent?.contractId ||
|
|
457
|
-
exercisedEvent.contractId,
|
|
458
|
-
owner: holdingView.owner,
|
|
459
|
-
meta: holdingView.meta,
|
|
460
|
-
lock: holdingView.lock,
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return {
|
|
466
|
-
// remove transient contracts
|
|
467
|
-
creates: mutatingResult.creates.filter((create) => !mutatingResult.archives.some((archive) => create.contractId === archive.contractId)),
|
|
468
|
-
archives: mutatingResult.archives.filter((archive) => !mutatingResult.creates.some((create) => create.contractId === archive.contractId)),
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
async getEventsForArchive(archivedEvent) {
|
|
472
|
-
if (!(archivedEvent.witnessParties || []).includes(this.partyId)) {
|
|
473
|
-
return null;
|
|
474
|
-
}
|
|
475
|
-
const basePayload = {
|
|
476
|
-
contractId: archivedEvent.contractId,
|
|
477
|
-
eventFormat: EventFilterBySetup([HOLDING_INTERFACE_ID, TRANSFER_INSTRUCTION_INTERFACE_ID], {
|
|
478
|
-
isMasterUser: this.isMasterUser,
|
|
479
|
-
partyId: this.partyId,
|
|
480
|
-
verbose: true,
|
|
481
|
-
}),
|
|
482
|
-
};
|
|
483
|
-
const payload = this.ledgerClient.getCurrentClientVersion() === '3.3'
|
|
484
|
-
? { ...basePayload, requestingParties: [] }
|
|
485
|
-
: basePayload;
|
|
486
|
-
const events = await this.ledgerClient
|
|
487
|
-
.postWithRetry('/v2/events/events-by-contract-id', payload)
|
|
488
|
-
.catch((err) => {
|
|
489
|
-
// This will happen for holdings with consuming choices
|
|
490
|
-
// where the party the script is running on is an actor on the choice
|
|
491
|
-
// but not a stakeholder.
|
|
492
|
-
if (err.code === 'CONTRACT_EVENTS_NOT_FOUND') {
|
|
493
|
-
return null;
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
throw err;
|
|
497
|
-
}
|
|
498
|
-
});
|
|
499
|
-
if (!events) {
|
|
500
|
-
return null;
|
|
501
|
-
}
|
|
502
|
-
const created = events.created;
|
|
503
|
-
const archived = events.archived;
|
|
504
|
-
if (!created || !archived) {
|
|
505
|
-
throw new Error(`Archival of ${archivedEvent.contractId} does not have a corresponding create/archive event: ${JSON.stringify(events)}`);
|
|
506
|
-
}
|
|
507
|
-
return { created, archived };
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
function isLeafEventNode(result) {
|
|
511
|
-
return !!result.event;
|
|
512
|
-
}
|
|
513
|
-
function getNodeIdAndEvent(event) {
|
|
514
|
-
if (event.ExercisedEvent) {
|
|
515
|
-
// ledger API's TRANSACTION_SHAPE_LEDGER_EFFECTS does not include ArchivedEvent, instead has the choice as Archive
|
|
516
|
-
if (event.ExercisedEvent.choice === 'Archive') {
|
|
517
|
-
return {
|
|
518
|
-
nodeId: event.ExercisedEvent.nodeId,
|
|
519
|
-
archivedEvent: event.ExercisedEvent,
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
else {
|
|
523
|
-
return {
|
|
524
|
-
nodeId: event.ExercisedEvent.nodeId,
|
|
525
|
-
exercisedEvent: event.ExercisedEvent,
|
|
526
|
-
};
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
else if (event.CreatedEvent) {
|
|
530
|
-
return {
|
|
531
|
-
nodeId: event.CreatedEvent.nodeId,
|
|
532
|
-
createdEvent: event.CreatedEvent,
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
else if (event.ArchivedEvent) {
|
|
536
|
-
return {
|
|
537
|
-
nodeId: event.ArchivedEvent.nodeId,
|
|
538
|
-
archivedEvent: event.ArchivedEvent,
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
throw new Error(`Impossible event type: ${event}`);
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
function sumHoldingsChange(change, filter) {
|
|
546
|
-
return sumHoldings(change.creates.filter((create) => filter(create.owner, create.lock))).minus(sumHoldings(change.archives.filter((archive) => filter(archive.owner, archive.lock))));
|
|
547
|
-
}
|
|
548
|
-
function sumHoldings(holdings) {
|
|
549
|
-
return BigNumber.sum(...holdings.map((h) => h.amount).concat(['0']) // avoid NaN
|
|
550
|
-
);
|
|
551
|
-
}
|
|
552
|
-
function computeAmountChanges(children, meta, partyId) {
|
|
553
|
-
const burnAmount = BigNumber(getMetaKeyValue(BurnedMetaKey, meta) || '0');
|
|
554
|
-
const partyHoldingAmountChange = sumHoldingsChange(children, (owner) => owner === partyId);
|
|
555
|
-
const otherPartiesHoldingAmountChange = sumHoldingsChange(children, (owner) => owner !== partyId);
|
|
556
|
-
const mintAmount = partyHoldingAmountChange
|
|
557
|
-
.plus(burnAmount)
|
|
558
|
-
.plus(otherPartiesHoldingAmountChange);
|
|
559
|
-
return {
|
|
560
|
-
burnAmount: burnAmount.toString(),
|
|
561
|
-
mintAmount: mintAmount.toString(),
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
|
-
function computeSummary(changes, partyId) {
|
|
565
|
-
const amountChange = sumHoldingsChange(changes, (owner) => owner === partyId);
|
|
566
|
-
const outputAmount = sumHoldings(changes.creates);
|
|
567
|
-
const inputAmount = sumHoldings(changes.archives);
|
|
568
|
-
return {
|
|
569
|
-
amountChange: amountChange.toString(),
|
|
570
|
-
numOutputs: changes.creates.length,
|
|
571
|
-
outputAmount: outputAmount.toString(),
|
|
572
|
-
numInputs: changes.archives.length,
|
|
573
|
-
inputAmount: inputAmount.toString(),
|
|
574
|
-
};
|
|
575
|
-
}
|
|
576
|
-
function holdingChangesNonEmpty(event) {
|
|
577
|
-
return (event.unlockedHoldingsChange.creates.length > 0 ||
|
|
578
|
-
event.unlockedHoldingsChange.archives.length > 0 ||
|
|
579
|
-
event.lockedHoldingsChange.creates.length > 0 ||
|
|
580
|
-
event.lockedHoldingsChange.archives.length > 0);
|
|
581
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"parser.test.d.ts","sourceRoot":"","sources":["../../src/txparse/parser.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { jest, describe, it, expect, beforeEach } from '@jest/globals';
|
|
4
|
-
import { TransactionParser } from './parser.js';
|
|
5
|
-
import eventsByContractIdResponses from './test-data/mock/eventsByContractIdResponses.js';
|
|
6
|
-
import rawTxsMock from './test-data/mock/txs.js';
|
|
7
|
-
import txsExpected from './test-data/expected/txs.js';
|
|
8
|
-
const EVENTS_BY_CID_PATH = '/v2/events/events-by-contract-id';
|
|
9
|
-
const txsMock = rawTxsMock;
|
|
10
|
-
const makeLedgerClientFromEventsResponses = (responses) => {
|
|
11
|
-
const responseByCid = new Map(responses.map((response) => [
|
|
12
|
-
response.created.createdEvent.contractId,
|
|
13
|
-
response,
|
|
14
|
-
]));
|
|
15
|
-
const getCurrentClientVersion = jest.fn(() => '3.3');
|
|
16
|
-
const postWithRetry = jest.fn(async (url, body) => {
|
|
17
|
-
if (url !== EVENTS_BY_CID_PATH) {
|
|
18
|
-
throw new Error(`Unexpected URL in mock LedgerClient: ${url}`);
|
|
19
|
-
}
|
|
20
|
-
const entry = responseByCid.get(body.contractId);
|
|
21
|
-
if (!entry) {
|
|
22
|
-
throw Object.assign(new Error('Not Found'), {
|
|
23
|
-
code: 'CONTRACT_EVENTS_NOT_FOUND',
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
return entry;
|
|
27
|
-
});
|
|
28
|
-
return { postWithRetry, getCurrentClientVersion };
|
|
29
|
-
};
|
|
30
|
-
const mockLedgerClient = makeLedgerClientFromEventsResponses(eventsByContractIdResponses);
|
|
31
|
-
describe('TransactionParser', () => {
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
jest.clearAllMocks();
|
|
34
|
-
});
|
|
35
|
-
it('returns transaction header and no events when input has no events', async () => {
|
|
36
|
-
const partyId = 'Alice::122000';
|
|
37
|
-
const tx = {
|
|
38
|
-
updateId: 'update-1',
|
|
39
|
-
offset: 42,
|
|
40
|
-
recordTime: '2025-01-01T00:00:00Z',
|
|
41
|
-
synchronizerId: 'sync-1',
|
|
42
|
-
events: [],
|
|
43
|
-
};
|
|
44
|
-
const parser = new TransactionParser(tx, mockLedgerClient, partyId, false);
|
|
45
|
-
const parsed = await parser.parseTransaction();
|
|
46
|
-
const expected = {
|
|
47
|
-
updateId: 'update-1',
|
|
48
|
-
offset: 42,
|
|
49
|
-
recordTime: '2025-01-01T00:00:00Z',
|
|
50
|
-
synchronizerId: 'sync-1',
|
|
51
|
-
events: [],
|
|
52
|
-
};
|
|
53
|
-
expect(parsed).toEqual(expected);
|
|
54
|
-
expect(mockLedgerClient.postWithRetry).not.toHaveBeenCalled();
|
|
55
|
-
});
|
|
56
|
-
it('parses the full mock input and matches the expected output from JSON fixtures', async () => {
|
|
57
|
-
const partyId = 'alice::normalized';
|
|
58
|
-
const actual = await Promise.all(txsMock.map((txMock) => {
|
|
59
|
-
const parser = new TransactionParser(txMock, mockLedgerClient, partyId, false);
|
|
60
|
-
return parser.parseTransaction();
|
|
61
|
-
}));
|
|
62
|
-
expect(actual).toEqual(txsExpected);
|
|
63
|
-
expect(mockLedgerClient.postWithRetry).toHaveBeenCalled();
|
|
64
|
-
});
|
|
65
|
-
it('skips an ArchivedEvent when ledger returns CONTRACT_EVENTS_NOT_FOUND', async () => {
|
|
66
|
-
const partyId = 'alice::normalized';
|
|
67
|
-
// contractId not present in eventsByContractIdResponses that results in 404 from mock LedgerClient
|
|
68
|
-
const missingCid = 'MISSING';
|
|
69
|
-
const tx = {
|
|
70
|
-
updateId: 'u-404',
|
|
71
|
-
offset: 100,
|
|
72
|
-
recordTime: '2025-01-01T00:00:00Z',
|
|
73
|
-
synchronizerId: 'sync-404',
|
|
74
|
-
events: [
|
|
75
|
-
{
|
|
76
|
-
ArchivedEvent: {
|
|
77
|
-
contractId: missingCid,
|
|
78
|
-
nodeId: 1,
|
|
79
|
-
offset: 100,
|
|
80
|
-
packageName: 'pkg',
|
|
81
|
-
templateId: 'Pkg:Temp:Id',
|
|
82
|
-
witnessParties: [partyId],
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
};
|
|
87
|
-
const parser = new TransactionParser(tx, mockLedgerClient, partyId, false);
|
|
88
|
-
const parsed = await parser.parseTransaction();
|
|
89
|
-
expect(parsed.events).toEqual([]);
|
|
90
|
-
// ensure we actually tried to fetch and got the 404 path
|
|
91
|
-
expect(mockLedgerClient.postWithRetry.mock.calls).toContainEqual([
|
|
92
|
-
EVENTS_BY_CID_PATH,
|
|
93
|
-
expect.objectContaining({ contractId: missingCid }),
|
|
94
|
-
]);
|
|
95
|
-
await expect(mockLedgerClient.postWithRetry.mock.results[0].value).rejects.toMatchObject({ code: 'CONTRACT_EVENTS_NOT_FOUND' });
|
|
96
|
-
});
|
|
97
|
-
});
|