@flashnet/sdk 0.3.40 → 0.4.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.
Files changed (43) hide show
  1. package/dist/cjs/index.d.ts +3 -2
  2. package/dist/cjs/index.d.ts.map +1 -1
  3. package/dist/cjs/index.js +10 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/src/api/client.d.ts.map +1 -1
  6. package/dist/cjs/src/api/client.js +14 -15
  7. package/dist/cjs/src/api/client.js.map +1 -1
  8. package/dist/cjs/src/api/typed-endpoints.d.ts +9 -1
  9. package/dist/cjs/src/api/typed-endpoints.d.ts.map +1 -1
  10. package/dist/cjs/src/api/typed-endpoints.js +6 -2
  11. package/dist/cjs/src/api/typed-endpoints.js.map +1 -1
  12. package/dist/cjs/src/client/FlashnetClient.d.ts +95 -1
  13. package/dist/cjs/src/client/FlashnetClient.d.ts.map +1 -1
  14. package/dist/cjs/src/client/FlashnetClient.js +422 -109
  15. package/dist/cjs/src/client/FlashnetClient.js.map +1 -1
  16. package/dist/cjs/src/types/errors.d.ts +264 -0
  17. package/dist/cjs/src/types/errors.d.ts.map +1 -0
  18. package/dist/cjs/src/types/errors.js +758 -0
  19. package/dist/cjs/src/types/errors.js.map +1 -0
  20. package/dist/cjs/src/types/index.d.ts +1 -1
  21. package/dist/cjs/src/types/index.d.ts.map +1 -1
  22. package/dist/esm/index.d.ts +3 -2
  23. package/dist/esm/index.d.ts.map +1 -1
  24. package/dist/esm/index.js +2 -1
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/src/api/client.d.ts.map +1 -1
  27. package/dist/esm/src/api/client.js +14 -15
  28. package/dist/esm/src/api/client.js.map +1 -1
  29. package/dist/esm/src/api/typed-endpoints.d.ts +9 -1
  30. package/dist/esm/src/api/typed-endpoints.d.ts.map +1 -1
  31. package/dist/esm/src/api/typed-endpoints.js +6 -2
  32. package/dist/esm/src/api/typed-endpoints.js.map +1 -1
  33. package/dist/esm/src/client/FlashnetClient.d.ts +95 -1
  34. package/dist/esm/src/client/FlashnetClient.d.ts.map +1 -1
  35. package/dist/esm/src/client/FlashnetClient.js +422 -109
  36. package/dist/esm/src/client/FlashnetClient.js.map +1 -1
  37. package/dist/esm/src/types/errors.d.ts +264 -0
  38. package/dist/esm/src/types/errors.d.ts.map +1 -0
  39. package/dist/esm/src/types/errors.js +749 -0
  40. package/dist/esm/src/types/errors.js.map +1 -0
  41. package/dist/esm/src/types/index.d.ts +1 -1
  42. package/dist/esm/src/types/index.d.ts.map +1 -1
  43. package/package.json +5 -5
@@ -0,0 +1,749 @@
1
+ /**
2
+ * Flashnet AMM Gateway Error System
3
+ *
4
+ * Error code format: FSAG-XXXX
5
+ * Categories by range:
6
+ * - 1000–1999: Validation
7
+ * - 2000–2999: Security/Auth
8
+ * - 3000–3999: Infrastructure/External
9
+ * - 4000–4999: Business/AMM Logic
10
+ * - 5000–5999: System/Service
11
+ */
12
+ /**
13
+ * Comprehensive metadata for all FSAG error codes
14
+ */
15
+ const ERROR_CODE_METADATA = {
16
+ // ===== Validation Errors (1000-1999) - Clawback Required =====
17
+ "FSAG-1000": {
18
+ httpStatus: 400,
19
+ category: "Validation",
20
+ recovery: "clawback_required",
21
+ summary: "Validation failed",
22
+ userMessage: "The request failed validation checks.",
23
+ actionHint: "Check your input parameters and try again. If you sent funds, initiate a clawback.",
24
+ isRetryable: false,
25
+ },
26
+ "FSAG-1001": {
27
+ httpStatus: 400,
28
+ category: "Validation",
29
+ recovery: "clawback_required",
30
+ summary: "Required field missing",
31
+ userMessage: "A required field is missing from your request.",
32
+ actionHint: "Ensure all required fields are provided. If you sent funds, initiate a clawback.",
33
+ isRetryable: false,
34
+ },
35
+ "FSAG-1002": {
36
+ httpStatus: 400,
37
+ category: "Validation",
38
+ recovery: "clawback_required",
39
+ summary: "Invalid field format",
40
+ userMessage: "One or more fields have an invalid format.",
41
+ actionHint: "Check the format of your inputs (addresses, amounts, etc.). If you sent funds, initiate a clawback.",
42
+ isRetryable: false,
43
+ },
44
+ "FSAG-1003": {
45
+ httpStatus: 400,
46
+ category: "Validation",
47
+ recovery: "clawback_required",
48
+ summary: "Value out of range",
49
+ userMessage: "A value is outside the acceptable range.",
50
+ actionHint: "Adjust the value to be within valid bounds. If you sent funds, initiate a clawback.",
51
+ isRetryable: false,
52
+ },
53
+ "FSAG-1004": {
54
+ httpStatus: 409,
55
+ category: "Validation",
56
+ recovery: "clawback_required",
57
+ summary: "Duplicate value",
58
+ userMessage: "This value already exists and must be unique.",
59
+ actionHint: "Use a different value or check if the operation was already completed.",
60
+ isRetryable: false,
61
+ },
62
+ // ===== Security/Auth Errors (2000-2999) - Clawback Required =====
63
+ "FSAG-2001": {
64
+ httpStatus: 403,
65
+ category: "Security",
66
+ recovery: "clawback_required",
67
+ summary: "Signature verification failed",
68
+ userMessage: "Your request signature could not be verified.",
69
+ actionHint: "Ensure you're signing with the correct key. If you sent funds, initiate a clawback.",
70
+ isRetryable: false,
71
+ },
72
+ "FSAG-2002": {
73
+ httpStatus: 403,
74
+ category: "Security",
75
+ recovery: "clawback_required",
76
+ summary: "Token identity mismatch",
77
+ userMessage: "The token identity doesn't match the expected public key.",
78
+ actionHint: "Ensure you're using the correct wallet/identity. If you sent funds, initiate a clawback.",
79
+ isRetryable: false,
80
+ },
81
+ "FSAG-2003": {
82
+ httpStatus: 401,
83
+ category: "Security",
84
+ recovery: "none",
85
+ summary: "Authorization token missing",
86
+ userMessage: "Authentication is required for this operation.",
87
+ actionHint: "Authenticate first by calling the auth flow.",
88
+ isRetryable: true,
89
+ },
90
+ "FSAG-2004": {
91
+ httpStatus: 401,
92
+ category: "Security",
93
+ recovery: "none",
94
+ summary: "Authorization token invalid or expired",
95
+ userMessage: "Your session has expired or the token is invalid.",
96
+ actionHint: "Re-authenticate to get a new access token.",
97
+ isRetryable: true,
98
+ },
99
+ "FSAG-2005": {
100
+ httpStatus: 403,
101
+ category: "Security",
102
+ recovery: "clawback_required",
103
+ summary: "Nonce verification failed",
104
+ userMessage: "The request nonce is invalid or has already been used.",
105
+ actionHint: "Generate a new nonce and retry. If you sent funds, initiate a clawback.",
106
+ isRetryable: false,
107
+ },
108
+ "FSAG-2101": {
109
+ httpStatus: 400,
110
+ category: "Security",
111
+ recovery: "clawback_required",
112
+ summary: "Public key invalid",
113
+ userMessage: "The provided public key is invalid.",
114
+ actionHint: "Check that the public key is correctly formatted. If you sent funds, initiate a clawback.",
115
+ isRetryable: false,
116
+ },
117
+ // ===== Infrastructure/External Errors (3000-3999) - Clawback Recommended =====
118
+ "FSAG-3001": {
119
+ httpStatus: 503,
120
+ category: "Infrastructure",
121
+ recovery: "clawback_recommended",
122
+ summary: "Internal server error",
123
+ userMessage: "A temporary service issue occurred. Please try again.",
124
+ actionHint: "Wait a moment and retry. If you sent funds, consider initiating a clawback.",
125
+ isRetryable: true,
126
+ },
127
+ "FSAG-3002": {
128
+ httpStatus: 500,
129
+ category: "Infrastructure",
130
+ recovery: "clawback_recommended",
131
+ summary: "Internal server error",
132
+ userMessage: "A temporary service issue occurred. Please try again.",
133
+ actionHint: "Wait a moment and retry. If you sent funds, consider initiating a clawback.",
134
+ isRetryable: true,
135
+ },
136
+ "FSAG-3101": {
137
+ httpStatus: 500,
138
+ category: "Infrastructure",
139
+ recovery: "clawback_recommended",
140
+ summary: "Internal server error",
141
+ userMessage: "A database error occurred. Please try again.",
142
+ actionHint: "Wait a moment and retry. If you sent funds, consider initiating a clawback.",
143
+ isRetryable: true,
144
+ },
145
+ "FSAG-3201": {
146
+ httpStatus: 503,
147
+ category: "Infrastructure",
148
+ recovery: "clawback_recommended",
149
+ summary: "Internal server error",
150
+ userMessage: "The settlement service is temporarily unavailable.",
151
+ actionHint: "Wait and retry. If you sent funds and they haven't been processed, consider initiating a clawback.",
152
+ isRetryable: true,
153
+ },
154
+ "FSAG-3201T1": {
155
+ httpStatus: 503,
156
+ category: "Infrastructure",
157
+ recovery: "clawback_recommended",
158
+ summary: "Internal server error",
159
+ userMessage: "The settlement service is temporarily unavailable.",
160
+ actionHint: "Wait and retry. If you sent funds and they haven't been processed, consider initiating a clawback.",
161
+ isRetryable: true,
162
+ },
163
+ "FSAG-3201T2": {
164
+ httpStatus: 503,
165
+ category: "Infrastructure",
166
+ recovery: "clawback_recommended",
167
+ summary: "Internal server error",
168
+ userMessage: "The settlement request timed out.",
169
+ actionHint: "Check if your transaction was processed. If not, you may retry or initiate a clawback.",
170
+ isRetryable: true,
171
+ },
172
+ "FSAG-3202": {
173
+ httpStatus: 503,
174
+ category: "Infrastructure",
175
+ recovery: "clawback_recommended",
176
+ summary: "Internal server error",
177
+ userMessage: "A dependent service is temporarily unavailable.",
178
+ actionHint: "Wait a moment and retry.",
179
+ isRetryable: true,
180
+ },
181
+ "FSAG-3301": {
182
+ httpStatus: 500,
183
+ category: "Infrastructure",
184
+ recovery: "clawback_recommended",
185
+ summary: "Internal server error",
186
+ userMessage: "The AMM processor couldn't receive your request.",
187
+ actionHint: "Wait and retry. If you sent funds, consider initiating a clawback.",
188
+ isRetryable: true,
189
+ },
190
+ "FSAG-3302": {
191
+ httpStatus: 503,
192
+ category: "Infrastructure",
193
+ recovery: "clawback_recommended",
194
+ summary: "Internal server error",
195
+ userMessage: "The AMM processor timed out while processing your request.",
196
+ actionHint: "Check if your transaction was processed. If not, you may retry or initiate a clawback.",
197
+ isRetryable: true,
198
+ },
199
+ "FSAG-3401": {
200
+ httpStatus: 500,
201
+ category: "Infrastructure",
202
+ recovery: "clawback_recommended",
203
+ summary: "Internal server error",
204
+ userMessage: "An internal processing error occurred.",
205
+ actionHint: "This is likely a temporary issue. Wait and retry. If you sent funds, consider initiating a clawback.",
206
+ isRetryable: true,
207
+ },
208
+ "FSAG-3402": {
209
+ httpStatus: 500,
210
+ category: "Infrastructure",
211
+ recovery: "clawback_recommended",
212
+ summary: "Internal server error",
213
+ userMessage: "An internal processing error occurred.",
214
+ actionHint: "This is likely a temporary issue. Wait and retry. If you sent funds, consider initiating a clawback.",
215
+ isRetryable: true,
216
+ },
217
+ // ===== Business/AMM Logic Errors (4000-4999) - Auto Refund =====
218
+ "FSAG-4001": {
219
+ httpStatus: 404,
220
+ category: "Business",
221
+ recovery: "auto_refund",
222
+ summary: "Pool not found",
223
+ userMessage: "The specified pool does not exist.",
224
+ actionHint: "Verify the pool ID is correct. Your funds will be automatically refunded.",
225
+ isRetryable: false,
226
+ },
227
+ "FSAG-4002": {
228
+ httpStatus: 404,
229
+ category: "Business",
230
+ recovery: "none",
231
+ summary: "Host not found",
232
+ userMessage: "The specified host namespace does not exist.",
233
+ actionHint: "Verify the host namespace is correct.",
234
+ isRetryable: false,
235
+ },
236
+ "FSAG-4101": {
237
+ httpStatus: 404,
238
+ category: "Business",
239
+ recovery: "none",
240
+ summary: "Auth session not found",
241
+ userMessage: "Your authentication session was not found or has expired.",
242
+ actionHint: "Start a new authentication flow.",
243
+ isRetryable: true,
244
+ },
245
+ "FSAG-4102": {
246
+ httpStatus: 400,
247
+ category: "Business",
248
+ recovery: "none",
249
+ summary: "Incorrect authentication flow",
250
+ userMessage: "The authentication flow was not followed correctly.",
251
+ actionHint: "Complete the authentication steps in the correct order.",
252
+ isRetryable: true,
253
+ },
254
+ "FSAG-4201": {
255
+ httpStatus: 400,
256
+ category: "Business",
257
+ recovery: "auto_refund",
258
+ summary: "Insufficient liquidity",
259
+ userMessage: "The pool doesn't have enough liquidity to complete this swap.",
260
+ actionHint: "Try a smaller amount or wait for more liquidity. Your funds will be automatically refunded.",
261
+ isRetryable: true,
262
+ },
263
+ "FSAG-4202": {
264
+ httpStatus: 400,
265
+ category: "Business",
266
+ recovery: "auto_refund",
267
+ summary: "Slippage exceeded",
268
+ userMessage: "The price moved more than your allowed slippage tolerance.",
269
+ actionHint: "Try increasing slippage tolerance, reducing trade size, or waiting for less volatile conditions. Your funds will be automatically refunded.",
270
+ isRetryable: true,
271
+ },
272
+ "FSAG-4203": {
273
+ httpStatus: 409,
274
+ category: "Business",
275
+ recovery: "auto_refund",
276
+ summary: "Operation not allowed in current phase",
277
+ userMessage: "This operation cannot be performed while the pool is in its current phase.",
278
+ actionHint: "Wait for the pool to transition to the appropriate phase. Your funds will be automatically refunded.",
279
+ isRetryable: true,
280
+ },
281
+ "FSAG-4204": {
282
+ httpStatus: 400,
283
+ category: "Business",
284
+ recovery: "none",
285
+ summary: "Insufficient LP tokens",
286
+ userMessage: "You don't have enough LP tokens to complete this withdrawal.",
287
+ actionHint: "Check your LP token balance and reduce the withdrawal amount.",
288
+ isRetryable: false,
289
+ },
290
+ "FSAG-4301": {
291
+ httpStatus: 400,
292
+ category: "Business",
293
+ recovery: "none",
294
+ summary: "Invalid fee configuration",
295
+ userMessage: "The fee configuration is invalid.",
296
+ actionHint: "Check that fee rates are within acceptable bounds.",
297
+ isRetryable: false,
298
+ },
299
+ "FSAG-4401": {
300
+ httpStatus: 409,
301
+ category: "Business",
302
+ recovery: "none",
303
+ summary: "Transfer ID already used",
304
+ userMessage: "This Spark transfer has already been used in an operation.",
305
+ actionHint: "Each transfer can only be used once. Use a new transfer for this operation.",
306
+ isRetryable: false,
307
+ },
308
+ // ===== System/Service Errors (5000-5999) - Clawback Required =====
309
+ "FSAG-5001": {
310
+ httpStatus: 500,
311
+ category: "System",
312
+ recovery: "clawback_required",
313
+ summary: "Failed to generate unique ID",
314
+ userMessage: "An internal error occurred while processing your request.",
315
+ actionHint: "Please try again. If you sent funds, initiate a clawback.",
316
+ isRetryable: true,
317
+ },
318
+ "FSAG-5002": {
319
+ httpStatus: 501,
320
+ category: "System",
321
+ recovery: "none",
322
+ summary: "Feature not implemented",
323
+ userMessage: "This feature is not yet available.",
324
+ actionHint: "This operation is not currently supported.",
325
+ isRetryable: false,
326
+ },
327
+ "FSAG-5003": {
328
+ httpStatus: 500,
329
+ category: "System",
330
+ recovery: "clawback_required",
331
+ summary: "Internal state inconsistent",
332
+ userMessage: "An internal error occurred. Please contact support if this persists.",
333
+ actionHint: "If you sent funds, initiate a clawback and contact support.",
334
+ isRetryable: false,
335
+ },
336
+ "FSAG-5004": {
337
+ httpStatus: 500,
338
+ category: "System",
339
+ recovery: "clawback_required",
340
+ summary: "Invalid configuration parameter",
341
+ userMessage: "A system configuration error occurred.",
342
+ actionHint: "This is a server-side issue. If you sent funds, initiate a clawback.",
343
+ isRetryable: false,
344
+ },
345
+ "FSAG-5100": {
346
+ httpStatus: 500,
347
+ category: "System",
348
+ recovery: "clawback_required",
349
+ summary: "Unexpected panic",
350
+ userMessage: "An unexpected error occurred on the server.",
351
+ actionHint: "Please try again later. If you sent funds, initiate a clawback immediately.",
352
+ isRetryable: false,
353
+ },
354
+ };
355
+ // ===== Helper Functions =====
356
+ /**
357
+ * Check if a string is a valid FlashnetErrorCode
358
+ */
359
+ function isFlashnetErrorCode(code) {
360
+ return code in ERROR_CODE_METADATA;
361
+ }
362
+ /**
363
+ * Get error category from error code
364
+ */
365
+ function getErrorCategory(code) {
366
+ return ERROR_CODE_METADATA[code].category;
367
+ }
368
+ /**
369
+ * Get recovery strategy from error code
370
+ */
371
+ function getErrorRecovery(code) {
372
+ return ERROR_CODE_METADATA[code].recovery;
373
+ }
374
+ /**
375
+ * Get metadata for an error code
376
+ */
377
+ function getErrorMetadata(code) {
378
+ return ERROR_CODE_METADATA[code];
379
+ }
380
+ /**
381
+ * Determine category from error code prefix (for unknown codes)
382
+ */
383
+ function getCategoryFromCodeRange(code) {
384
+ const match = code.match(/^FSAG-(\d)/);
385
+ if (!match) {
386
+ return null;
387
+ }
388
+ const prefix = match[1];
389
+ switch (prefix) {
390
+ case "1":
391
+ return "Validation";
392
+ case "2":
393
+ return "Security";
394
+ case "3":
395
+ return "Infrastructure";
396
+ case "4":
397
+ return "Business";
398
+ case "5":
399
+ return "System";
400
+ default:
401
+ return null;
402
+ }
403
+ }
404
+ /**
405
+ * Error class for Flashnet AMM Gateway errors
406
+ *
407
+ * Provides:
408
+ * - Typed error codes with full metadata
409
+ * - Recovery strategy information
410
+ * - Human-readable messages
411
+ * - Transfer tracking for clawback operations
412
+ */
413
+ class FlashnetError extends Error {
414
+ /** The FSAG error code (e.g., "FSAG-4202") */
415
+ errorCode;
416
+ /** Error category */
417
+ category;
418
+ /** Recovery strategy for this error */
419
+ recovery;
420
+ /** HTTP status code */
421
+ httpStatus;
422
+ /** Unique request ID for debugging */
423
+ requestId;
424
+ /** ISO timestamp when error occurred */
425
+ timestamp;
426
+ /** Service that generated the error */
427
+ service;
428
+ /** Error severity */
429
+ severity;
430
+ /** Additional error details */
431
+ details;
432
+ /** Server-provided remediation hint */
433
+ remediation;
434
+ /** Transfer IDs that may need clawback (unrecovered transfers) */
435
+ transferIds;
436
+ /** LP identity public key for clawback operations */
437
+ lpIdentityPublicKey;
438
+ /** Summary of automatic clawback attempts (if any were made) */
439
+ clawbackSummary;
440
+ /** Whether this error type is generally retryable */
441
+ isRetryable;
442
+ /** Human-readable summary */
443
+ summary;
444
+ /** User-friendly message explaining the error */
445
+ userMessage;
446
+ /** Suggested action for the user */
447
+ actionHint;
448
+ constructor(message, options = {}) {
449
+ super(message);
450
+ this.name = "FlashnetError";
451
+ const response = options.response;
452
+ const rawCode = response?.errorCode ?? "UNKNOWN";
453
+ // Determine if we have a known error code
454
+ if (isFlashnetErrorCode(rawCode)) {
455
+ this.errorCode = rawCode;
456
+ const metadata = ERROR_CODE_METADATA[rawCode];
457
+ this.category = metadata.category;
458
+ this.recovery = metadata.recovery;
459
+ this.httpStatus = options.httpStatus ?? metadata.httpStatus;
460
+ this.isRetryable = metadata.isRetryable;
461
+ this.summary = metadata.summary;
462
+ this.userMessage = metadata.userMessage;
463
+ // Prefer remediation from response (e.g., after successful auto-clawback)
464
+ this.actionHint = response?.remediation ?? metadata.actionHint;
465
+ }
466
+ else {
467
+ // Unknown error code - try to determine category from range
468
+ this.errorCode = rawCode;
469
+ this.category = getCategoryFromCodeRange(rawCode) ?? "System";
470
+ this.httpStatus = options.httpStatus ?? 500;
471
+ // Default recovery based on category
472
+ switch (this.category) {
473
+ case "Validation":
474
+ case "Security":
475
+ case "System":
476
+ this.recovery = "clawback_required";
477
+ break;
478
+ case "Infrastructure":
479
+ this.recovery = "clawback_recommended";
480
+ break;
481
+ case "Business":
482
+ this.recovery = "auto_refund";
483
+ break;
484
+ default:
485
+ this.recovery = "clawback_required";
486
+ }
487
+ this.isRetryable = this.category === "Infrastructure";
488
+ this.summary = response?.message ?? "Unknown error";
489
+ this.userMessage = response?.message ?? "An unexpected error occurred.";
490
+ this.actionHint =
491
+ response?.remediation ?? "Please try again or contact support.";
492
+ }
493
+ this.requestId = response?.requestId ?? "";
494
+ this.timestamp = response?.timestamp ?? new Date().toISOString();
495
+ this.service = response?.service ?? "unknown";
496
+ this.severity = response?.severity ?? "Error";
497
+ this.details = response?.details;
498
+ this.remediation = response?.remediation;
499
+ this.transferIds = options.transferIds ?? [];
500
+ this.lpIdentityPublicKey = options.lpIdentityPublicKey;
501
+ this.clawbackSummary = options.clawbackSummary;
502
+ // Maintain proper prototype chain
503
+ Object.setPrototypeOf(this, FlashnetError.prototype);
504
+ // Capture stack trace
505
+ if (Error.captureStackTrace) {
506
+ Error.captureStackTrace(this, FlashnetError);
507
+ }
508
+ }
509
+ // ===== Recovery Status Methods =====
510
+ /**
511
+ * Returns true if funds were sent and clawback is required
512
+ */
513
+ isClawbackRequired() {
514
+ return this.recovery === "clawback_required" && this.transferIds.length > 0;
515
+ }
516
+ /**
517
+ * Returns true if clawback is recommended (infrastructure errors)
518
+ */
519
+ isClawbackRecommended() {
520
+ return (this.recovery === "clawback_recommended" && this.transferIds.length > 0);
521
+ }
522
+ /**
523
+ * Returns true if clawback should be attempted (required or recommended)
524
+ */
525
+ shouldClawback() {
526
+ return this.isClawbackRequired() || this.isClawbackRecommended();
527
+ }
528
+ /**
529
+ * Returns true if funds will be automatically refunded
530
+ */
531
+ willAutoRefund() {
532
+ return this.recovery === "auto_refund";
533
+ }
534
+ /**
535
+ * Returns true if this error has associated transfers that may need recovery
536
+ */
537
+ hasTransfersAtRisk() {
538
+ return this.transferIds.length > 0 && this.recovery !== "auto_refund";
539
+ }
540
+ // ===== Category Check Methods =====
541
+ isValidationError() {
542
+ return this.category === "Validation";
543
+ }
544
+ isSecurityError() {
545
+ return this.category === "Security";
546
+ }
547
+ isInfrastructureError() {
548
+ return this.category === "Infrastructure";
549
+ }
550
+ isBusinessError() {
551
+ return this.category === "Business";
552
+ }
553
+ isSystemError() {
554
+ return this.category === "System";
555
+ }
556
+ // ===== Specific Error Checks =====
557
+ isSlippageError() {
558
+ return this.errorCode === "FSAG-4202";
559
+ }
560
+ isInsufficientLiquidityError() {
561
+ return this.errorCode === "FSAG-4201";
562
+ }
563
+ isAuthError() {
564
+ return this.errorCode === "FSAG-2003" || this.errorCode === "FSAG-2004";
565
+ }
566
+ isPoolNotFoundError() {
567
+ return this.errorCode === "FSAG-4001";
568
+ }
569
+ isTransferAlreadyUsedError() {
570
+ return this.errorCode === "FSAG-4401";
571
+ }
572
+ // ===== Clawback Status Methods =====
573
+ /**
574
+ * Returns true if automatic clawback was attempted
575
+ */
576
+ wasClawbackAttempted() {
577
+ return this.clawbackSummary?.attempted ?? false;
578
+ }
579
+ /**
580
+ * Returns true if all transfers were successfully recovered via clawback
581
+ */
582
+ wereAllTransfersRecovered() {
583
+ if (!this.clawbackSummary?.attempted) {
584
+ return false;
585
+ }
586
+ return this.clawbackSummary.failureCount === 0;
587
+ }
588
+ /**
589
+ * Returns true if some (but not all) transfers were recovered
590
+ */
591
+ werePartialTransfersRecovered() {
592
+ if (!this.clawbackSummary?.attempted) {
593
+ return false;
594
+ }
595
+ return (this.clawbackSummary.successCount > 0 &&
596
+ this.clawbackSummary.failureCount > 0);
597
+ }
598
+ /**
599
+ * Get the number of transfers that were successfully recovered
600
+ */
601
+ getRecoveredTransferCount() {
602
+ return this.clawbackSummary?.successCount ?? 0;
603
+ }
604
+ /**
605
+ * Get the transfer IDs that were successfully recovered
606
+ */
607
+ getRecoveredTransferIds() {
608
+ return this.clawbackSummary?.recoveredTransferIds ?? [];
609
+ }
610
+ /**
611
+ * Get the transfer IDs that failed to recover (still at risk)
612
+ */
613
+ getUnrecoveredTransferIds() {
614
+ return this.clawbackSummary?.unrecoveredTransferIds ?? this.transferIds;
615
+ }
616
+ // ===== Formatting Methods =====
617
+ /**
618
+ * Get a formatted string for logging
619
+ */
620
+ toLogString() {
621
+ const parts = [
622
+ `[${this.errorCode}]`,
623
+ this.message,
624
+ `(requestId: ${this.requestId})`,
625
+ ];
626
+ if (this.transferIds.length > 0) {
627
+ parts.push(`transferIds: [${this.transferIds.join(", ")}]`);
628
+ }
629
+ return parts.join(" ");
630
+ }
631
+ /**
632
+ * Get a user-friendly error description
633
+ */
634
+ getUserFriendlyMessage() {
635
+ const parts = [this.userMessage];
636
+ if (this.shouldClawback()) {
637
+ parts.push("Your funds may need to be recovered via clawback.");
638
+ }
639
+ else if (this.willAutoRefund()) {
640
+ parts.push("Your funds will be automatically refunded.");
641
+ }
642
+ return parts.join(" ");
643
+ }
644
+ /**
645
+ * Convert to a plain object for serialization
646
+ */
647
+ toJSON() {
648
+ return {
649
+ name: this.name,
650
+ message: this.message,
651
+ errorCode: this.errorCode,
652
+ category: this.category,
653
+ recovery: this.recovery,
654
+ httpStatus: this.httpStatus,
655
+ requestId: this.requestId,
656
+ timestamp: this.timestamp,
657
+ service: this.service,
658
+ severity: this.severity,
659
+ details: this.details,
660
+ remediation: this.remediation,
661
+ transferIds: this.transferIds,
662
+ lpIdentityPublicKey: this.lpIdentityPublicKey,
663
+ clawbackSummary: this.clawbackSummary,
664
+ isRetryable: this.isRetryable,
665
+ summary: this.summary,
666
+ userMessage: this.userMessage,
667
+ actionHint: this.actionHint,
668
+ };
669
+ }
670
+ /**
671
+ * Create a FlashnetError from an API error response
672
+ */
673
+ static fromResponse(response, httpStatus, options) {
674
+ return new FlashnetError(response.message, {
675
+ response,
676
+ httpStatus,
677
+ transferIds: options?.transferIds,
678
+ lpIdentityPublicKey: options?.lpIdentityPublicKey,
679
+ });
680
+ }
681
+ /**
682
+ * Create a FlashnetError from an unknown error
683
+ */
684
+ static fromUnknown(error, options) {
685
+ if (error instanceof FlashnetError) {
686
+ // If already a FlashnetError, preserve its state
687
+ // Don't override transferIds if the error indicates auto_refund (funds already safe)
688
+ const shouldPreserveTransferIds = error.recovery === "auto_refund";
689
+ if (options?.lpIdentityPublicKey && !error.lpIdentityPublicKey) {
690
+ return new FlashnetError(error.message, {
691
+ response: {
692
+ errorCode: error.errorCode,
693
+ errorCategory: error.category,
694
+ message: error.message,
695
+ details: error.details,
696
+ requestId: error.requestId,
697
+ timestamp: error.timestamp,
698
+ service: error.service,
699
+ severity: error.severity,
700
+ remediation: error.remediation,
701
+ },
702
+ httpStatus: error.httpStatus,
703
+ transferIds: shouldPreserveTransferIds
704
+ ? error.transferIds
705
+ : (options.transferIds ?? error.transferIds),
706
+ lpIdentityPublicKey: options.lpIdentityPublicKey ?? error.lpIdentityPublicKey,
707
+ cause: error,
708
+ });
709
+ }
710
+ return error;
711
+ }
712
+ if (error instanceof Error) {
713
+ // Check if it's an API error with response data
714
+ const apiError = error;
715
+ if (apiError.response?.data &&
716
+ typeof apiError.response.data === "object") {
717
+ const data = apiError.response.data;
718
+ if (data.errorCode &&
719
+ typeof data.errorCode === "string" &&
720
+ typeof data.message === "string" &&
721
+ typeof data.requestId === "string") {
722
+ return FlashnetError.fromResponse(data, apiError.response.status ?? apiError.status ?? 500, options);
723
+ }
724
+ }
725
+ // Generic error
726
+ return new FlashnetError(error.message, {
727
+ httpStatus: apiError.status ?? 500,
728
+ transferIds: options?.transferIds,
729
+ lpIdentityPublicKey: options?.lpIdentityPublicKey,
730
+ cause: error,
731
+ });
732
+ }
733
+ // Unknown error type
734
+ return new FlashnetError(String(error), {
735
+ httpStatus: 500,
736
+ transferIds: options?.transferIds,
737
+ lpIdentityPublicKey: options?.lpIdentityPublicKey,
738
+ });
739
+ }
740
+ }
741
+ /**
742
+ * Type guard to check if an error is a FlashnetError
743
+ */
744
+ function isFlashnetError(error) {
745
+ return error instanceof FlashnetError;
746
+ }
747
+
748
+ export { ERROR_CODE_METADATA, FlashnetError, getCategoryFromCodeRange, getErrorCategory, getErrorMetadata, getErrorRecovery, isFlashnetError, isFlashnetErrorCode };
749
+ //# sourceMappingURL=errors.js.map