@layerzerolabs/gated-transaction 0.0.61 → 0.0.63

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.
Files changed (40) hide show
  1. package/.turbo/turbo-build.log +27 -43
  2. package/.turbo/turbo-test.log +5 -6
  3. package/dist/{VH2LMV2W.js → SGAQYA4D.js} +2 -2
  4. package/dist/{VH2LMV2W.js.map → SGAQYA4D.js.map} +1 -1
  5. package/dist/{UVQ5QQ4U.cjs → UV63UNW4.cjs} +2 -2
  6. package/dist/{UVQ5QQ4U.cjs.map → UV63UNW4.cjs.map} +1 -1
  7. package/dist/index.cjs +10 -32
  8. package/dist/index.d.ts +0 -2
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -3
  11. package/dist/schemata.cjs +10 -10
  12. package/dist/schemata.d.ts +0 -4
  13. package/dist/schemata.d.ts.map +1 -1
  14. package/dist/schemata.js +1 -1
  15. package/package.json +8 -11
  16. package/src/index.ts +0 -2
  17. package/src/schemata.ts +0 -5
  18. package/dist/CZKVKTL4.cjs +0 -107
  19. package/dist/CZKVKTL4.cjs.map +0 -1
  20. package/dist/JMMYK2SL.js +0 -103
  21. package/dist/JMMYK2SL.js.map +0 -1
  22. package/dist/ONVFWJXG.js +0 -250
  23. package/dist/ONVFWJXG.js.map +0 -1
  24. package/dist/XROB6QHI.cjs +0 -257
  25. package/dist/XROB6QHI.cjs.map +0 -1
  26. package/dist/gatedTransactionSignalLock.cjs +0 -22
  27. package/dist/gatedTransactionSignalLock.cjs.map +0 -1
  28. package/dist/gatedTransactionSignalLock.d.ts +0 -20
  29. package/dist/gatedTransactionSignalLock.d.ts.map +0 -1
  30. package/dist/gatedTransactionSignalLock.js +0 -5
  31. package/dist/gatedTransactionSignalLock.js.map +0 -1
  32. package/dist/resolver.cjs +0 -19
  33. package/dist/resolver.cjs.map +0 -1
  34. package/dist/resolver.d.ts +0 -24
  35. package/dist/resolver.d.ts.map +0 -1
  36. package/dist/resolver.js +0 -6
  37. package/dist/resolver.js.map +0 -1
  38. package/src/gatedTransactionSignalLock.ts +0 -143
  39. package/src/resolver.ts +0 -448
  40. package/test/resolver.test.ts +0 -425
package/src/resolver.ts DELETED
@@ -1,448 +0,0 @@
1
- import isEqual from 'lodash.isequal';
2
-
3
- import type { ActivityRegistry } from '@layerzerolabs/common-activities';
4
- import type {
5
- Transaction,
6
- TransactionResult,
7
- TransactionWithResult,
8
- } from '@layerzerolabs/common-chain-model';
9
- import type { WorkflowFunctions } from '@layerzerolabs/common-workflow';
10
- import type { Logger } from '@layerzerolabs/logger-node';
11
-
12
- import { getIdForGatedTransaction } from './gatedTx';
13
- import type {
14
- BaseGatedTransactionFor,
15
- GatedTransaction,
16
- GatedTransactionId,
17
- IGatedTransactionCache,
18
- InferOnChainDataTypeFromGatedTransaction,
19
- ResolvedGatedTransaction,
20
- ResolvedGatedTransactionDependencies,
21
- UserInteractionCallbacks,
22
- } from './schemata';
23
- import { GatedTransactionStatus } from './schemata';
24
-
25
- type OnChainCheckResult<DataType = any> = {
26
- expectationMet: boolean;
27
- onChainData: DataType;
28
- };
29
-
30
- const checkGatedTransaction = async <
31
- GatedTx extends GatedTransaction,
32
- TxWithResult extends TransactionWithResult,
33
- >({
34
- registry,
35
- gatedTransaction,
36
- cachedCheckResult,
37
- submittedTransaction,
38
- }: {
39
- registry: ActivityRegistry;
40
- gatedTransaction: GatedTx;
41
- cachedCheckResult?: InferOnChainDataTypeFromGatedTransaction<GatedTx>;
42
- submittedTransaction?: TxWithResult;
43
- }): Promise<OnChainCheckResult<InferOnChainDataTypeFromGatedTransaction<GatedTx>>> => {
44
- let onChainData: InferOnChainDataTypeFromGatedTransaction<GatedTx>;
45
- if (cachedCheckResult !== undefined) {
46
- onChainData = cachedCheckResult;
47
- } else {
48
- // Always inject the processed transaction as the last parameter when available
49
- let params = gatedTransaction.check.params;
50
- if (submittedTransaction !== undefined) {
51
- params = [...gatedTransaction.check.params, submittedTransaction];
52
- }
53
-
54
- onChainData = await registry.callByPointer(gatedTransaction.check.functionPointer, params);
55
- }
56
-
57
- let expectationMet: boolean;
58
-
59
- switch (gatedTransaction.check.expectedResult.operator) {
60
- case '=':
61
- expectationMet = isEqual(
62
- onChainData,
63
- gatedTransaction.check.expectedResult.comparisonValue,
64
- );
65
- break;
66
- case '!=':
67
- expectationMet = !isEqual(
68
- onChainData,
69
- gatedTransaction.check.expectedResult.comparisonValue,
70
- );
71
- break;
72
- case '>':
73
- expectationMet =
74
- (onChainData as number) >
75
- (gatedTransaction.check.expectedResult.comparisonValue as number);
76
- break;
77
- case '>=':
78
- expectationMet =
79
- (onChainData as number) >=
80
- (gatedTransaction.check.expectedResult.comparisonValue as number);
81
- break;
82
- case '<':
83
- expectationMet =
84
- (onChainData as number) <
85
- (gatedTransaction.check.expectedResult.comparisonValue as number);
86
- break;
87
- case '<=':
88
- expectationMet =
89
- (onChainData as number) <=
90
- (gatedTransaction.check.expectedResult.comparisonValue as number);
91
- break;
92
- default:
93
- throw new Error(
94
- `Gated transaction operator "${gatedTransaction.check.expectedResult.operator}" is not supported`,
95
- );
96
- }
97
-
98
- return {
99
- expectationMet,
100
- onChainData,
101
- };
102
- };
103
-
104
- const extractGatedTransactionUnderlyingTransaction = async <
105
- GatedTx extends GatedTransaction,
106
- TxType extends Transaction,
107
- >({
108
- registry,
109
- gatedTransaction,
110
- deps,
111
- }: {
112
- registry: ActivityRegistry;
113
- gatedTransaction: GatedTx;
114
- deps: ResolvedGatedTransactionDependencies<GatedTx>;
115
- }): Promise<TxType> => {
116
- let tx: TxType;
117
- if (gatedTransaction.transactionType === 'literal') {
118
- tx = gatedTransaction.transaction as TxType;
119
- } else {
120
- tx = await registry.callByPointer(gatedTransaction.getTransaction.functionPointer, [
121
- deps,
122
- ...gatedTransaction.getTransaction.params,
123
- ]);
124
- }
125
-
126
- if (!tx.parallelizingKey) {
127
- tx.parallelizingKey = `MISSING_PARALLELIZING_KEY-${getIdForGatedTransaction(gatedTransaction)}`;
128
- }
129
-
130
- return tx;
131
- };
132
-
133
- const resolveGatedTransaction = async <
134
- TxType extends Transaction,
135
- GatedTx extends GatedTransaction<string, TxType, any>,
136
- ProcessTx extends (transaction: TxType) => Promise<TransactionResult>,
137
- >({
138
- activityRegistry,
139
- gatedTx,
140
- getTransactionResult,
141
- processTx,
142
- confirmationCallback,
143
- fn,
144
- cache,
145
- logger,
146
- }: {
147
- activityRegistry: ActivityRegistry;
148
- gatedTx: GatedTx;
149
- processTx: ProcessTx;
150
- getTransactionResult: (gtxId: GatedTransactionId) =>
151
- | {
152
- resolved?: ResolvedGatedTransaction;
153
- checkResult: { onChainData?: any; expectationMet: boolean };
154
- }
155
- | undefined;
156
- confirmationCallback: NonNullable<UserInteractionCallbacks['confirmationCallback']>;
157
- fn: WorkflowFunctions;
158
- cache?: IGatedTransactionCache;
159
- logger: Logger;
160
- }): Promise<{
161
- resolvedGatedTransaction: ResolvedGatedTransaction<GatedTx>;
162
- lastCheckResult: OnChainCheckResult | null;
163
- }> => {
164
- const baseGatedTx = {
165
- name: gatedTx.name,
166
- chainName: gatedTx.chainName,
167
- bundleName: gatedTx.bundleName,
168
- check: gatedTx.check,
169
- dependencies: gatedTx.dependencies,
170
- cacheable: gatedTx.cacheable,
171
- uniqueIdKeys: gatedTx.uniqueIdKeys,
172
- metadata: gatedTx.metadata,
173
- description: gatedTx.description,
174
- } as BaseGatedTransactionFor<GatedTx>;
175
-
176
- const dependencyIds = (gatedTx.dependencies ?? []).map((tx) => getIdForGatedTransaction(tx));
177
-
178
- const dependenciesAreAllProcessed = () => dependencyIds.every((id) => getTransactionResult(id));
179
-
180
- await fn.condition(dependenciesAreAllProcessed);
181
-
182
- //check that the dependencies succeeded
183
- const dependenciesWereSuccessful = dependencyIds.every(
184
- (id) => getTransactionResult(id)?.checkResult.expectationMet,
185
- );
186
-
187
- if (!dependenciesWereSuccessful) {
188
- logger.error(
189
- `Some dependencies of gated transaction ${gatedTx.name} failed: ${JSON.stringify(
190
- Object.fromEntries(dependencyIds.map((id) => [id, getTransactionResult(id)])),
191
- undefined,
192
- 2,
193
- )}`,
194
- );
195
- return {
196
- resolvedGatedTransaction: {
197
- ...baseGatedTx,
198
- result: {
199
- status: GatedTransactionStatus.DEPENDENCY_FAILED,
200
- },
201
- },
202
- lastCheckResult: null,
203
- };
204
- }
205
-
206
- const initialCheckResult = await checkGatedTransaction({
207
- gatedTransaction: gatedTx,
208
- registry: activityRegistry,
209
- cachedCheckResult: await cache?.getCachedTxCheckData?.(gatedTx),
210
- });
211
-
212
- await cache?.cacheSuccessfulTxCheckData?.(gatedTx, initialCheckResult.onChainData);
213
-
214
- if (initialCheckResult.expectationMet) {
215
- //already met
216
- return {
217
- resolvedGatedTransaction: {
218
- ...baseGatedTx,
219
- result: {
220
- status: GatedTransactionStatus.NO_OP,
221
- },
222
- },
223
- lastCheckResult: initialCheckResult,
224
- };
225
- }
226
-
227
- //get confirmation before sending
228
- const confirmation = await confirmationCallback(gatedTx);
229
- if (!confirmation) {
230
- return {
231
- resolvedGatedTransaction: {
232
- ...baseGatedTx,
233
- result: {
234
- status: GatedTransactionStatus.DENIED,
235
- },
236
- },
237
- lastCheckResult: initialCheckResult,
238
- };
239
- }
240
-
241
- //send the tx
242
- try {
243
- const depsResults = gatedTx.dependencies.map(
244
- (dep) => getTransactionResult(getIdForGatedTransaction(dep))!.resolved!,
245
- ) as ResolvedGatedTransactionDependencies<GatedTx>;
246
- // get the underlying calldata
247
- const underlying = await extractGatedTransactionUnderlyingTransaction<GatedTx, TxType>({
248
- registry: activityRegistry,
249
- gatedTransaction: gatedTx,
250
- deps: depsResults,
251
- });
252
- const processResult = await processTx(underlying);
253
-
254
- if (processResult.type === 'error') {
255
- throw new Error(`Sending gtx ${gatedTx.name} failed: ${processResult.error}`);
256
- }
257
-
258
- const submittedTransaction = { ...underlying, result: processResult };
259
- const finalCheckResult = await checkGatedTransaction({
260
- registry: activityRegistry,
261
- gatedTransaction: gatedTx,
262
- submittedTransaction,
263
- });
264
-
265
- await cache?.cacheSuccessfulTxCheckData?.(gatedTx, finalCheckResult.onChainData);
266
-
267
- if (finalCheckResult.expectationMet) {
268
- //sent and successful
269
- return {
270
- resolvedGatedTransaction: {
271
- ...baseGatedTx,
272
- result: {
273
- status: GatedTransactionStatus.SUCCESS,
274
- submittedTransaction,
275
- },
276
- },
277
- lastCheckResult: finalCheckResult,
278
- };
279
- } else {
280
- //sent and unsuccessful
281
- const stringify = (o: any) =>
282
- JSON.stringify(
283
- o,
284
- (_, v) =>
285
- v === undefined ? 'undefined' : typeof v === 'bigint' ? v.toString() : v,
286
- 2,
287
- );
288
- logger.error(
289
- `Gated transaction ${gatedTx.name} final check failed:\nExpected result ${stringify(gatedTx.check.expectedResult.comparisonValue)}, but got:\n${stringify(finalCheckResult.onChainData)}, txHash: ${submittedTransaction.result.minedTxHash}`,
290
- );
291
- return {
292
- resolvedGatedTransaction: {
293
- ...baseGatedTx,
294
- result: {
295
- status: GatedTransactionStatus.FINAL_CHECK_FAILED,
296
- submittedTransaction,
297
- finalOnChainState: finalCheckResult.onChainData,
298
- },
299
- },
300
- lastCheckResult: finalCheckResult,
301
- };
302
- }
303
- } catch (e) {
304
- logger.error('Gated transaction failed', e);
305
- //sent and failed
306
- return {
307
- resolvedGatedTransaction: {
308
- ...baseGatedTx,
309
- result: {
310
- status: GatedTransactionStatus.TRANSACTION_FAILED,
311
- transactionError: e,
312
- },
313
- },
314
- lastCheckResult: initialCheckResult,
315
- };
316
- }
317
- };
318
-
319
- export const resolveGatedTransactions = async <
320
- TxType extends Transaction,
321
- GatedTx extends GatedTransaction<string, TxType, any>,
322
- ProcessTx extends (transaction: TxType) => Promise<TransactionResult>,
323
- >({
324
- activityRegistry,
325
- gatedTxes,
326
- processTx,
327
- fn,
328
- cache,
329
- userInteractionCallbacks,
330
- logger,
331
- }: {
332
- activityRegistry: ActivityRegistry;
333
- gatedTxes: GatedTx[];
334
- processTx: ProcessTx;
335
- fn: WorkflowFunctions;
336
- cache?: IGatedTransactionCache;
337
- userInteractionCallbacks?: UserInteractionCallbacks;
338
- logger: Logger;
339
- }): Promise<ResolvedGatedTransaction[]> => {
340
- //compute deps
341
- const allInternalIds = gatedTxes.map((tx) => getIdForGatedTransaction(tx));
342
- const allDeps = gatedTxes.flatMap((tx) => tx.dependencies ?? []);
343
- const allExternalDeps = allDeps.filter(
344
- (tx) => !allInternalIds.includes(getIdForGatedTransaction(tx)),
345
- );
346
-
347
- //prepopulate results for external dependencies
348
- //this map is only used for internal status tracking
349
- const results: Record<
350
- GatedTransactionId,
351
- {
352
- resolved?: ResolvedGatedTransaction;
353
- checkResult: { onChainData?: any; expectationMet: boolean };
354
- }
355
- > = Object.fromEntries(
356
- await Promise.all(
357
- allExternalDeps.map(async (tx) => {
358
- const checkResult = await checkGatedTransaction({
359
- gatedTransaction: tx,
360
- registry: activityRegistry,
361
- cachedCheckResult: await cache?.getCachedTxCheckData?.(tx),
362
- });
363
-
364
- return [
365
- getIdForGatedTransaction(tx),
366
- {
367
- checkResult,
368
- },
369
- ];
370
- }),
371
- ),
372
- );
373
-
374
- const getTransactionResult = (gtxId: GatedTransactionId) => results[gtxId];
375
-
376
- const txFunctions = gatedTxes.map((tx) => async () => {
377
- const { resolvedGatedTransaction, lastCheckResult } = await resolveGatedTransaction<
378
- TxType,
379
- GatedTx,
380
- ProcessTx
381
- >({
382
- gatedTx: tx,
383
- activityRegistry,
384
- getTransactionResult,
385
- processTx,
386
- confirmationCallback:
387
- userInteractionCallbacks?.confirmationCallback ?? (async () => true),
388
- fn,
389
- cache,
390
- logger,
391
- });
392
-
393
- const resId = getIdForGatedTransaction(resolvedGatedTransaction);
394
- results[resId] = {
395
- resolved: resolvedGatedTransaction,
396
- checkResult: {
397
- expectationMet: [
398
- GatedTransactionStatus.SUCCESS,
399
- GatedTransactionStatus.NO_OP,
400
- ].includes(resolvedGatedTransaction.result.status),
401
- onChainData: lastCheckResult?.onChainData ?? undefined,
402
- },
403
- };
404
-
405
- userInteractionCallbacks?.onResultCallback?.(resolvedGatedTransaction);
406
- return resolvedGatedTransaction;
407
- });
408
-
409
- return await Promise.all(txFunctions.map((func) => func()));
410
- };
411
-
412
- export const resolveSequencedGatedTransactions = async <
413
- TxType extends Transaction,
414
- GatedTx extends GatedTransaction<string, TxType, any>,
415
- ProcessTx extends (transaction: TxType) => Promise<TransactionResult>,
416
- >({
417
- activityRegistry,
418
- gatedTxes,
419
- processTx,
420
- cache,
421
- userInteractionCallbacks,
422
- fn,
423
- logger,
424
- }: {
425
- activityRegistry: ActivityRegistry;
426
- gatedTxes: GatedTx[][];
427
- processTx: ProcessTx;
428
- fn: WorkflowFunctions;
429
- cache?: IGatedTransactionCache;
430
- userInteractionCallbacks?: UserInteractionCallbacks;
431
- logger: Logger;
432
- }) => {
433
- const results = [];
434
- for (const seq of gatedTxes) {
435
- const result = await resolveGatedTransactions<TxType, GatedTx, ProcessTx>({
436
- gatedTxes: seq,
437
- activityRegistry,
438
- processTx,
439
- cache,
440
- userInteractionCallbacks,
441
- fn,
442
- logger,
443
- });
444
- results.push(result);
445
- }
446
-
447
- return results;
448
- };