@agirails/sdk 2.5.3 → 2.5.5

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 (169) hide show
  1. package/dist/ACTPClient.d.ts +18 -0
  2. package/dist/ACTPClient.d.ts.map +1 -1
  3. package/dist/ACTPClient.js +72 -23
  4. package/dist/ACTPClient.js.map +1 -1
  5. package/dist/adapters/BasicAdapter.d.ts +15 -0
  6. package/dist/adapters/BasicAdapter.d.ts.map +1 -1
  7. package/dist/adapters/BasicAdapter.js +33 -4
  8. package/dist/adapters/BasicAdapter.js.map +1 -1
  9. package/dist/adapters/StandardAdapter.d.ts +20 -3
  10. package/dist/adapters/StandardAdapter.d.ts.map +1 -1
  11. package/dist/adapters/StandardAdapter.js +90 -12
  12. package/dist/adapters/StandardAdapter.js.map +1 -1
  13. package/dist/cli/commands/publish.js +16 -4
  14. package/dist/cli/commands/publish.js.map +1 -1
  15. package/dist/cli/commands/register.js +16 -4
  16. package/dist/cli/commands/register.js.map +1 -1
  17. package/dist/cli/commands/tx.js +31 -3
  18. package/dist/cli/commands/tx.js.map +1 -1
  19. package/dist/config/networks.d.ts +10 -2
  20. package/dist/config/networks.d.ts.map +1 -1
  21. package/dist/config/networks.js +31 -22
  22. package/dist/config/networks.js.map +1 -1
  23. package/dist/level0/request.d.ts.map +1 -1
  24. package/dist/level0/request.js +2 -1
  25. package/dist/level0/request.js.map +1 -1
  26. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
  27. package/dist/runtime/BlockchainRuntime.js +11 -5
  28. package/dist/runtime/BlockchainRuntime.js.map +1 -1
  29. package/dist/utils/IPFSClient.d.ts +3 -1
  30. package/dist/utils/IPFSClient.d.ts.map +1 -1
  31. package/dist/utils/IPFSClient.js +27 -7
  32. package/dist/utils/IPFSClient.js.map +1 -1
  33. package/dist/wallet/AutoWalletProvider.d.ts +11 -1
  34. package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
  35. package/dist/wallet/AutoWalletProvider.js +84 -19
  36. package/dist/wallet/AutoWalletProvider.js.map +1 -1
  37. package/dist/wallet/IWalletProvider.d.ts +34 -0
  38. package/dist/wallet/IWalletProvider.d.ts.map +1 -1
  39. package/dist/wallet/SmartWalletRouter.d.ts +128 -0
  40. package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
  41. package/dist/wallet/SmartWalletRouter.js +248 -0
  42. package/dist/wallet/SmartWalletRouter.js.map +1 -0
  43. package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
  44. package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
  45. package/dist/wallet/aa/DualNonceManager.js +140 -6
  46. package/dist/wallet/aa/DualNonceManager.js.map +1 -1
  47. package/package.json +3 -6
  48. package/src/ACTPClient.ts +0 -1579
  49. package/src/abi/ACTPKernel.json +0 -1356
  50. package/src/abi/AgentRegistry.json +0 -915
  51. package/src/abi/ERC20.json +0 -40
  52. package/src/abi/EscrowVault.json +0 -134
  53. package/src/abi/IdentityRegistry.json +0 -316
  54. package/src/adapters/AdapterRegistry.ts +0 -173
  55. package/src/adapters/AdapterRouter.ts +0 -416
  56. package/src/adapters/BaseAdapter.ts +0 -498
  57. package/src/adapters/BasicAdapter.ts +0 -514
  58. package/src/adapters/IAdapter.ts +0 -292
  59. package/src/adapters/StandardAdapter.ts +0 -555
  60. package/src/adapters/X402Adapter.ts +0 -731
  61. package/src/adapters/index.ts +0 -60
  62. package/src/builders/DeliveryProofBuilder.ts +0 -327
  63. package/src/builders/QuoteBuilder.ts +0 -483
  64. package/src/builders/index.ts +0 -17
  65. package/src/cli/commands/balance.ts +0 -110
  66. package/src/cli/commands/batch.ts +0 -487
  67. package/src/cli/commands/config.ts +0 -231
  68. package/src/cli/commands/deploy-check.ts +0 -364
  69. package/src/cli/commands/deploy-env.ts +0 -120
  70. package/src/cli/commands/diff.ts +0 -141
  71. package/src/cli/commands/init.ts +0 -469
  72. package/src/cli/commands/mint.ts +0 -116
  73. package/src/cli/commands/pay.ts +0 -113
  74. package/src/cli/commands/publish.ts +0 -475
  75. package/src/cli/commands/pull.ts +0 -124
  76. package/src/cli/commands/register.ts +0 -247
  77. package/src/cli/commands/simulate.ts +0 -345
  78. package/src/cli/commands/time.ts +0 -302
  79. package/src/cli/commands/tx.ts +0 -448
  80. package/src/cli/commands/watch.ts +0 -211
  81. package/src/cli/index.ts +0 -134
  82. package/src/cli/utils/client.ts +0 -252
  83. package/src/cli/utils/config.ts +0 -389
  84. package/src/cli/utils/output.ts +0 -465
  85. package/src/cli/utils/wallet.ts +0 -109
  86. package/src/config/agirailsmd.ts +0 -262
  87. package/src/config/networks.ts +0 -275
  88. package/src/config/pendingPublish.ts +0 -237
  89. package/src/config/publishPipeline.ts +0 -359
  90. package/src/config/syncOperations.ts +0 -279
  91. package/src/erc8004/ERC8004Bridge.ts +0 -462
  92. package/src/erc8004/ReputationReporter.ts +0 -468
  93. package/src/erc8004/index.ts +0 -61
  94. package/src/errors/index.ts +0 -427
  95. package/src/index.ts +0 -364
  96. package/src/level0/Provider.ts +0 -117
  97. package/src/level0/ServiceDirectory.ts +0 -131
  98. package/src/level0/index.ts +0 -10
  99. package/src/level0/provide.ts +0 -132
  100. package/src/level0/request.ts +0 -432
  101. package/src/level1/Agent.ts +0 -1426
  102. package/src/level1/index.ts +0 -10
  103. package/src/level1/pricing/PriceCalculator.ts +0 -255
  104. package/src/level1/pricing/PricingStrategy.ts +0 -198
  105. package/src/level1/types/Job.ts +0 -179
  106. package/src/level1/types/Options.ts +0 -291
  107. package/src/level1/types/index.ts +0 -8
  108. package/src/protocol/ACTPKernel.ts +0 -808
  109. package/src/protocol/AgentRegistry.ts +0 -559
  110. package/src/protocol/DIDManager.ts +0 -629
  111. package/src/protocol/DIDResolver.ts +0 -554
  112. package/src/protocol/EASHelper.ts +0 -378
  113. package/src/protocol/EscrowVault.ts +0 -255
  114. package/src/protocol/EventMonitor.ts +0 -204
  115. package/src/protocol/MessageSigner.ts +0 -510
  116. package/src/protocol/ProofGenerator.ts +0 -339
  117. package/src/protocol/QuoteBuilder.ts +0 -15
  118. package/src/registry/AgentRegistryClient.ts +0 -202
  119. package/src/runtime/BlockchainRuntime.ts +0 -1015
  120. package/src/runtime/IACTPRuntime.ts +0 -306
  121. package/src/runtime/MockRuntime.ts +0 -1298
  122. package/src/runtime/MockStateManager.ts +0 -577
  123. package/src/runtime/index.ts +0 -25
  124. package/src/runtime/types/MockState.ts +0 -237
  125. package/src/storage/ArchiveBundleBuilder.ts +0 -561
  126. package/src/storage/ArweaveClient.ts +0 -946
  127. package/src/storage/FilebaseClient.ts +0 -790
  128. package/src/storage/index.ts +0 -96
  129. package/src/storage/types.ts +0 -348
  130. package/src/types/adapter.ts +0 -310
  131. package/src/types/agent.ts +0 -79
  132. package/src/types/did.ts +0 -223
  133. package/src/types/eip712.ts +0 -175
  134. package/src/types/erc8004.ts +0 -293
  135. package/src/types/escrow.ts +0 -27
  136. package/src/types/index.ts +0 -17
  137. package/src/types/message.ts +0 -145
  138. package/src/types/state.ts +0 -87
  139. package/src/types/transaction.ts +0 -69
  140. package/src/types/x402.ts +0 -251
  141. package/src/utils/ErrorRecoveryGuide.ts +0 -676
  142. package/src/utils/Helpers.ts +0 -688
  143. package/src/utils/IPFSClient.ts +0 -368
  144. package/src/utils/Logger.ts +0 -484
  145. package/src/utils/NonceManager.ts +0 -591
  146. package/src/utils/RateLimiter.ts +0 -534
  147. package/src/utils/ReceivedNonceTracker.ts +0 -567
  148. package/src/utils/SDKLifecycle.ts +0 -416
  149. package/src/utils/SecureNonce.ts +0 -78
  150. package/src/utils/Semaphore.ts +0 -276
  151. package/src/utils/UsedAttestationTracker.ts +0 -385
  152. package/src/utils/canonicalJson.ts +0 -38
  153. package/src/utils/circuitBreaker.ts +0 -324
  154. package/src/utils/computeTypeHash.ts +0 -48
  155. package/src/utils/fsSafe.ts +0 -80
  156. package/src/utils/index.ts +0 -80
  157. package/src/utils/retry.ts +0 -364
  158. package/src/utils/security.ts +0 -418
  159. package/src/utils/validation.ts +0 -540
  160. package/src/wallet/AutoWalletProvider.ts +0 -299
  161. package/src/wallet/EOAWalletProvider.ts +0 -69
  162. package/src/wallet/IWalletProvider.ts +0 -135
  163. package/src/wallet/aa/BundlerClient.ts +0 -274
  164. package/src/wallet/aa/DualNonceManager.ts +0 -173
  165. package/src/wallet/aa/PaymasterClient.ts +0 -174
  166. package/src/wallet/aa/TransactionBatcher.ts +0 -353
  167. package/src/wallet/aa/UserOpBuilder.ts +0 -246
  168. package/src/wallet/aa/constants.ts +0 -60
  169. package/src/wallet/keystore.ts +0 -240
@@ -1,448 +0,0 @@
1
- /**
2
- * Transaction Commands - tx subcommand group
3
- *
4
- * Commands for managing ACTP transactions:
5
- * - tx create: Create a new transaction (standard API)
6
- * - tx status: Check transaction status
7
- * - tx list: List all transactions
8
- * - tx deliver: Mark transaction as delivered
9
- * - tx settle: Release escrow funds
10
- * - tx cancel: Cancel a transaction
11
- *
12
- * @module cli/commands/tx
13
- */
14
-
15
- import { Command } from 'commander';
16
- import { Output, ExitCode, TransactionDisplay } from '../utils/output';
17
- import { createClient, mapError, isValidTxId } from '../utils/client';
18
- import { TransactionState, MockTransaction } from '../../runtime/types/MockState';
19
-
20
- // ============================================================================
21
- // Main tx Command
22
- // ============================================================================
23
-
24
- export function createTxCommand(): Command {
25
- const cmd = new Command('tx')
26
- .description('Transaction management commands');
27
-
28
- cmd.addCommand(createTxCreateCommand());
29
- cmd.addCommand(createTxStatusCommand());
30
- cmd.addCommand(createTxListCommand());
31
- cmd.addCommand(createTxDeliverCommand());
32
- cmd.addCommand(createTxSettleCommand());
33
- cmd.addCommand(createTxCancelCommand());
34
-
35
- return cmd;
36
- }
37
-
38
- // ============================================================================
39
- // tx create
40
- // ============================================================================
41
-
42
- function createTxCreateCommand(): Command {
43
- return new Command('create')
44
- .description('Create a new transaction (without auto-funding)')
45
- .argument('<provider>', 'Provider address')
46
- .argument('<amount>', 'Amount to pay')
47
- .option('-d, --deadline <deadline>', 'Deadline (+24h, +7d, or Unix timestamp)', '+24h')
48
- .option('-w, --dispute-window <seconds>', 'Dispute window in seconds', '172800')
49
- .option('--description <text>', 'Service description')
50
- .option('--fund', 'Automatically fund the escrow after creation')
51
- .option('--json', 'Output as JSON')
52
- .option('-q, --quiet', 'Output only the transaction ID')
53
- .action(async (provider, amount, options) => {
54
- const output = new Output(
55
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
56
- );
57
-
58
- try {
59
- const client = await createClient();
60
-
61
- let deadline: string | number = options.deadline;
62
- if (/^\d+$/.test(options.deadline)) {
63
- deadline = parseInt(options.deadline, 10);
64
- }
65
-
66
- const disputeWindow = parseInt(options.disputeWindow, 10);
67
-
68
- // Create transaction
69
- const txId = await client.standard.createTransaction({
70
- provider,
71
- amount,
72
- deadline,
73
- disputeWindow,
74
- serviceDescription: options.description,
75
- });
76
-
77
- // Optionally fund
78
- let escrowId: string | undefined;
79
- if (options.fund) {
80
- escrowId = await client.standard.linkEscrow(txId);
81
- }
82
-
83
- const tx = await client.standard.getTransaction(txId);
84
- if (!tx) throw new Error('Transaction not found after creation');
85
-
86
- output.result(
87
- {
88
- txId,
89
- state: tx.state,
90
- provider: tx.provider,
91
- requester: tx.requester,
92
- amount: `${formatUsdc(tx.amount)} USDC`,
93
- deadline: new Date(tx.deadline * 1000).toISOString(),
94
- escrowId: escrowId || null,
95
- },
96
- { quietKey: 'txId' }
97
- );
98
-
99
- if (!options.fund) {
100
- output.blank();
101
- output.info('Transaction created but not funded.');
102
- output.print(' Fund it: actp tx fund ' + txId.slice(0, 10) + '...');
103
- }
104
- } catch (error) {
105
- const structuredError = mapError(error);
106
- output.errorResult({
107
- code: structuredError.code,
108
- message: structuredError.message,
109
- details: structuredError.details,
110
- });
111
- process.exit(ExitCode.ERROR);
112
- }
113
- });
114
- }
115
-
116
- // ============================================================================
117
- // tx status
118
- // ============================================================================
119
-
120
- function createTxStatusCommand(): Command {
121
- return new Command('status')
122
- .description('Check transaction status')
123
- .argument('<txId>', 'Transaction ID')
124
- .option('--json', 'Output as JSON')
125
- .option('-q, --quiet', 'Output only the state')
126
- .action(async (txId, options) => {
127
- const output = new Output(
128
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
129
- );
130
-
131
- try {
132
- if (!isValidTxId(txId)) {
133
- throw new Error(`Invalid transaction ID format: "${txId}"`);
134
- }
135
-
136
- const client = await createClient();
137
- const tx = await client.standard.getTransaction(txId);
138
-
139
- if (!tx) {
140
- throw new Error(`Transaction not found: ${txId}`);
141
- }
142
-
143
- const status = await client.basic.checkStatus(txId);
144
-
145
- if (options.quiet) {
146
- output.raw(tx.state);
147
- return;
148
- }
149
-
150
- const display: TransactionDisplay = {
151
- txId: tx.id,
152
- state: tx.state,
153
- requester: tx.requester,
154
- provider: tx.provider,
155
- amount: `${formatUsdc(tx.amount)} USDC`,
156
- deadline: new Date(tx.deadline * 1000).toISOString(),
157
- escrowId: tx.escrowId,
158
- createdAt: new Date(tx.createdAt * 1000).toISOString(),
159
- };
160
-
161
- if (options.json) {
162
- output.result({
163
- ...display,
164
- actions: {
165
- canAccept: status.canAccept,
166
- canComplete: status.canComplete,
167
- canDispute: status.canDispute,
168
- },
169
- });
170
- } else {
171
- output.transaction(display);
172
- output.blank();
173
- output.print('Available Actions:');
174
- output.keyValue(' Can Accept', status.canAccept);
175
- output.keyValue(' Can Complete', status.canComplete);
176
- output.keyValue(' Can Dispute', status.canDispute);
177
- }
178
- } catch (error) {
179
- const structuredError = mapError(error);
180
- output.errorResult({
181
- code: structuredError.code,
182
- message: structuredError.message,
183
- details: structuredError.details,
184
- });
185
- process.exit(ExitCode.ERROR);
186
- }
187
- });
188
- }
189
-
190
- // ============================================================================
191
- // tx list
192
- // ============================================================================
193
-
194
- function createTxListCommand(): Command {
195
- return new Command('list')
196
- .description('List all transactions')
197
- .option('-s, --state <state>', 'Filter by state')
198
- .option('-l, --limit <n>', 'Limit number of results', '50')
199
- .option('--json', 'Output as JSON')
200
- .option('-q, --quiet', 'Output only transaction IDs (one per line)')
201
- .action(async (options) => {
202
- const output = new Output(
203
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
204
- );
205
-
206
- try {
207
- const client = await createClient();
208
- let transactions: MockTransaction[] = await client.advanced.getAllTransactions();
209
-
210
- // Filter by state if specified
211
- if (options.state) {
212
- const stateFilter = options.state.toUpperCase();
213
- transactions = transactions.filter((tx: MockTransaction) => tx.state === stateFilter);
214
- }
215
-
216
- // Sort by createdAt descending (newest first)
217
- transactions.sort((a: MockTransaction, b: MockTransaction) => b.createdAt - a.createdAt);
218
-
219
- // Apply limit
220
- const limit = parseInt(options.limit, 10);
221
- transactions = transactions.slice(0, limit);
222
-
223
- if (options.quiet) {
224
- for (const tx of transactions) {
225
- console.log(tx.id);
226
- }
227
- return;
228
- }
229
-
230
- if (options.json) {
231
- output.result({
232
- count: transactions.length,
233
- transactions: transactions.map((tx: MockTransaction) => ({
234
- txId: tx.id,
235
- state: tx.state,
236
- requester: tx.requester,
237
- provider: tx.provider,
238
- amount: tx.amount,
239
- deadline: new Date(tx.deadline * 1000).toISOString(),
240
- createdAt: new Date(tx.createdAt * 1000).toISOString(),
241
- })),
242
- });
243
- return;
244
- }
245
-
246
- output.section(`Transactions (${transactions.length})`);
247
- output.transactionTable(
248
- transactions.map((tx: MockTransaction) => ({
249
- txId: tx.id,
250
- state: tx.state,
251
- requester: tx.requester,
252
- provider: tx.provider,
253
- amount: `${formatUsdc(tx.amount)} USDC`,
254
- deadline: new Date(tx.deadline * 1000).toISOString(),
255
- }))
256
- );
257
- } catch (error) {
258
- const structuredError = mapError(error);
259
- output.errorResult({
260
- code: structuredError.code,
261
- message: structuredError.message,
262
- details: structuredError.details,
263
- });
264
- process.exit(ExitCode.ERROR);
265
- }
266
- });
267
- }
268
-
269
- // ============================================================================
270
- // tx deliver
271
- // ============================================================================
272
-
273
- function createTxDeliverCommand(): Command {
274
- return new Command('deliver')
275
- .description('Mark transaction as delivered (provider action)')
276
- .argument('<txId>', 'Transaction ID')
277
- .option('--json', 'Output as JSON')
278
- .option('-q, --quiet', 'Minimal output')
279
- .action(async (txId, options) => {
280
- const output = new Output(
281
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
282
- );
283
-
284
- try {
285
- if (!isValidTxId(txId)) {
286
- throw new Error(`Invalid transaction ID format: "${txId}"`);
287
- }
288
-
289
- const client = await createClient();
290
-
291
- // Transition to DELIVERED
292
- await client.standard.transitionState(txId, 'DELIVERED' as TransactionState);
293
-
294
- const tx = await client.standard.getTransaction(txId);
295
- if (!tx) throw new Error('Transaction not found');
296
-
297
- output.result(
298
- {
299
- txId,
300
- state: tx.state,
301
- completedAt: tx.completedAt
302
- ? new Date(tx.completedAt * 1000).toISOString()
303
- : null,
304
- },
305
- { quietKey: 'state' }
306
- );
307
-
308
- output.success('Transaction marked as delivered!');
309
- output.info(`Dispute window: ${tx.disputeWindow} seconds`);
310
- output.print('');
311
- output.print('After dispute window expires, settle with:');
312
- output.print(' actp tx settle ' + txId.slice(0, 10) + '...');
313
- } catch (error) {
314
- const structuredError = mapError(error);
315
- output.errorResult({
316
- code: structuredError.code,
317
- message: structuredError.message,
318
- details: structuredError.details,
319
- });
320
- process.exit(ExitCode.ERROR);
321
- }
322
- });
323
- }
324
-
325
- // ============================================================================
326
- // tx settle
327
- // ============================================================================
328
-
329
- function createTxSettleCommand(): Command {
330
- return new Command('settle')
331
- .description('Release escrow funds to provider')
332
- .argument('<txId>', 'Transaction ID')
333
- .option('--json', 'Output as JSON')
334
- .option('-q, --quiet', 'Minimal output')
335
- .action(async (txId, options) => {
336
- const output = new Output(
337
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
338
- );
339
-
340
- try {
341
- if (!isValidTxId(txId)) {
342
- throw new Error(`Invalid transaction ID format: "${txId}"`);
343
- }
344
-
345
- const client = await createClient();
346
-
347
- // Get transaction to find escrow
348
- const tx = await client.standard.getTransaction(txId);
349
- if (!tx) throw new Error(`Transaction not found: ${txId}`);
350
- if (!tx.escrowId) throw new Error('Transaction has no linked escrow');
351
-
352
- // Release escrow
353
- await client.standard.releaseEscrow(tx.escrowId);
354
-
355
- // Get updated transaction
356
- const updatedTx = await client.standard.getTransaction(txId);
357
- if (!updatedTx) throw new Error('Transaction not found');
358
-
359
- output.result(
360
- {
361
- txId,
362
- state: updatedTx.state,
363
- provider: updatedTx.provider,
364
- amount: `${formatUsdc(updatedTx.amount)} USDC`,
365
- },
366
- { quietKey: 'state' }
367
- );
368
-
369
- output.success('Escrow released! Funds sent to provider.');
370
- } catch (error) {
371
- const structuredError = mapError(error);
372
- output.errorResult({
373
- code: structuredError.code,
374
- message: structuredError.message,
375
- details: structuredError.details,
376
- });
377
- process.exit(ExitCode.ERROR);
378
- }
379
- });
380
- }
381
-
382
- // ============================================================================
383
- // tx cancel
384
- // ============================================================================
385
-
386
- function createTxCancelCommand(): Command {
387
- return new Command('cancel')
388
- .description('Cancel a transaction (before delivery)')
389
- .argument('<txId>', 'Transaction ID')
390
- .option('--json', 'Output as JSON')
391
- .option('-q, --quiet', 'Minimal output')
392
- .action(async (txId, options) => {
393
- const output = new Output(
394
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
395
- );
396
-
397
- try {
398
- if (!isValidTxId(txId)) {
399
- throw new Error(`Invalid transaction ID format: "${txId}"`);
400
- }
401
-
402
- const client = await createClient();
403
-
404
- // Transition to CANCELLED
405
- await client.standard.transitionState(txId, 'CANCELLED' as TransactionState);
406
-
407
- const tx = await client.standard.getTransaction(txId);
408
- if (!tx) throw new Error('Transaction not found');
409
-
410
- output.result(
411
- {
412
- txId,
413
- state: tx.state,
414
- refunded: tx.escrowId !== null,
415
- },
416
- { quietKey: 'state' }
417
- );
418
-
419
- output.success('Transaction cancelled!');
420
- if (tx.escrowId) {
421
- output.info('Funds have been refunded to requester.');
422
- }
423
- } catch (error) {
424
- const structuredError = mapError(error);
425
- output.errorResult({
426
- code: structuredError.code,
427
- message: structuredError.message,
428
- details: structuredError.details,
429
- });
430
- process.exit(ExitCode.ERROR);
431
- }
432
- });
433
- }
434
-
435
- // ============================================================================
436
- // Helpers
437
- // ============================================================================
438
-
439
- /**
440
- * Format USDC amount from wei to decimal
441
- */
442
- function formatUsdc(weiAmount: string): string {
443
- const amount = BigInt(weiAmount);
444
- const whole = amount / 1_000_000n;
445
- const decimal = amount % 1_000_000n;
446
- const decimalStr = decimal.toString().padStart(6, '0').slice(0, 2);
447
- return `${whole}.${decimalStr}`;
448
- }
@@ -1,211 +0,0 @@
1
- /**
2
- * Watch Command - Stream transaction state changes
3
- *
4
- * Agent-first feature: Real-time monitoring of transaction state.
5
- * Outputs state changes as they happen, perfect for scripts
6
- * that need to react to transaction lifecycle events.
7
- *
8
- * @module cli/commands/watch
9
- */
10
-
11
- import { Command } from 'commander';
12
- import { Output, ExitCode, formatState, fmt } from '../utils/output';
13
- import { createClient, mapError, isValidTxId } from '../utils/client';
14
- import { TransactionState } from '../../runtime/types/MockState';
15
-
16
- // ============================================================================
17
- // Command Definition
18
- // ============================================================================
19
-
20
- export function createWatchCommand(): Command {
21
- const cmd = new Command('watch')
22
- .description('Watch a transaction for state changes (agent-first feature)')
23
- .argument('<txId>', 'Transaction ID to watch')
24
- .option('-t, --timeout <seconds>', 'Exit after timeout (default: indefinite)', '0')
25
- .option('-i, --interval <ms>', 'Polling interval in milliseconds', '1000')
26
- .option('--until <state>', 'Exit when transaction reaches this state')
27
- .option('--json', 'Output state changes as JSON lines (NDJSON)')
28
- .option('-q, --quiet', 'Output only state names on change')
29
- .action(async (txId, options) => {
30
- const output = new Output(
31
- options.json ? 'json' : options.quiet ? 'quiet' : 'human'
32
- );
33
-
34
- try {
35
- await runWatch(txId, options, output);
36
- } catch (error) {
37
- const structuredError = mapError(error);
38
- output.errorResult({
39
- code: structuredError.code,
40
- message: structuredError.message,
41
- details: structuredError.details,
42
- });
43
- process.exit(ExitCode.ERROR);
44
- }
45
- });
46
-
47
- return cmd;
48
- }
49
-
50
- // ============================================================================
51
- // Implementation
52
- // ============================================================================
53
-
54
- interface WatchOptions {
55
- timeout: string;
56
- interval: string;
57
- until?: string;
58
- }
59
-
60
- async function runWatch(
61
- txId: string,
62
- options: WatchOptions,
63
- output: Output
64
- ): Promise<void> {
65
- if (!isValidTxId(txId)) {
66
- throw new Error(`Invalid transaction ID format: "${txId}"`);
67
- }
68
-
69
- const client = await createClient();
70
- const pollIntervalMs = parseInt(options.interval, 10);
71
- const timeoutSeconds = parseInt(options.timeout, 10);
72
- const untilState = options.until?.toUpperCase() as TransactionState | undefined;
73
-
74
- // Validate until state if provided
75
- const validStates: TransactionState[] = [
76
- 'INITIATED', 'QUOTED', 'COMMITTED', 'IN_PROGRESS',
77
- 'DELIVERED', 'SETTLED', 'DISPUTED', 'CANCELLED',
78
- ];
79
-
80
- if (untilState && !validStates.includes(untilState)) {
81
- throw new Error(
82
- `Invalid state: "${options.until}"\n` +
83
- `Valid states: ${validStates.join(', ')}`
84
- );
85
- }
86
-
87
- // Get initial state
88
- const tx = await client.standard.getTransaction(txId);
89
- if (!tx) {
90
- throw new Error(`Transaction not found: ${txId}`);
91
- }
92
-
93
- let currentState = tx.state;
94
- const startTime = Date.now();
95
-
96
- // Output initial state
97
- emitStateChange(output, txId, null, currentState, tx.updatedAt);
98
-
99
- // Check if already at target state
100
- if (untilState && currentState === untilState) {
101
- output.info('Transaction already at target state.');
102
- process.exit(ExitCode.SUCCESS);
103
- }
104
-
105
- // Terminal states
106
- const terminalStates: TransactionState[] = ['SETTLED', 'CANCELLED'];
107
- if (terminalStates.includes(currentState)) {
108
- output.info('Transaction is in terminal state.');
109
- process.exit(ExitCode.SUCCESS);
110
- }
111
-
112
- output.info('Watching for state changes... (Ctrl+C to stop)');
113
-
114
- // Polling loop
115
- const poll = async (): Promise<void> => {
116
- // Check timeout
117
- if (timeoutSeconds > 0) {
118
- const elapsed = (Date.now() - startTime) / 1000;
119
- if (elapsed >= timeoutSeconds) {
120
- output.warning(`Timeout reached (${timeoutSeconds}s)`);
121
- process.exit(ExitCode.TIMEOUT);
122
- }
123
- }
124
-
125
- try {
126
- const updatedTx = await client.standard.getTransaction(txId);
127
- if (!updatedTx) {
128
- output.warning('Transaction no longer exists');
129
- process.exit(ExitCode.ERROR);
130
- }
131
-
132
- if (updatedTx.state !== currentState) {
133
- const previousState = currentState;
134
- currentState = updatedTx.state;
135
- emitStateChange(output, txId, previousState, currentState, updatedTx.updatedAt);
136
-
137
- // Check if reached target state
138
- if (untilState && currentState === untilState) {
139
- output.success(`Reached target state: ${untilState}`);
140
- process.exit(ExitCode.SUCCESS);
141
- }
142
-
143
- // Check if terminal
144
- if (terminalStates.includes(currentState)) {
145
- output.info(`Transaction reached terminal state: ${currentState}`);
146
- process.exit(ExitCode.SUCCESS);
147
- }
148
- }
149
- } catch (error) {
150
- // Non-fatal: log warning and continue
151
- output.warning(`Poll error: ${(error as Error).message}`);
152
- }
153
-
154
- // Schedule next poll
155
- setTimeout(poll, pollIntervalMs);
156
- };
157
-
158
- // Start polling
159
- setTimeout(poll, pollIntervalMs);
160
-
161
- // Keep process alive
162
- await new Promise(() => {});
163
- }
164
-
165
- /**
166
- * Emit a state change event in the appropriate format
167
- */
168
- function emitStateChange(
169
- output: Output,
170
- txId: string,
171
- fromState: TransactionState | null,
172
- toState: TransactionState,
173
- timestamp: number
174
- ): void {
175
- const time = new Date(timestamp * 1000).toISOString();
176
-
177
- switch (output['mode']) {
178
- case 'json':
179
- // NDJSON format for easy parsing
180
- console.log(
181
- JSON.stringify({
182
- event: 'state_change',
183
- txId,
184
- fromState,
185
- toState,
186
- timestamp: time,
187
- unix: timestamp,
188
- })
189
- );
190
- break;
191
-
192
- case 'quiet':
193
- console.log(toState);
194
- break;
195
-
196
- case 'human':
197
- default:
198
- if (fromState) {
199
- console.log(
200
- `${fmt.dim(time)} ${formatState(fromState)} ${fmt.dim('->')} ${formatState(toState)}`
201
- );
202
- } else {
203
- console.log(
204
- `${fmt.dim(time)} ${fmt.dim('Current:')} ${formatState(toState)}`
205
- );
206
- }
207
- break;
208
- }
209
- }
210
-
211
- export { runWatch };