@atomiqlabs/sdk 8.8.3 → 8.9.1

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 (130) hide show
  1. package/api/index.d.ts +1 -0
  2. package/api/index.js +3 -0
  3. package/dist/ApiList.d.ts +37 -0
  4. package/dist/ApiList.js +30 -0
  5. package/dist/api/ApiEndpoints.d.ts +393 -0
  6. package/dist/api/ApiEndpoints.js +2 -0
  7. package/dist/api/ApiParser.d.ts +10 -0
  8. package/dist/api/ApiParser.js +134 -0
  9. package/dist/api/ApiTypes.d.ts +157 -0
  10. package/dist/api/ApiTypes.js +75 -0
  11. package/dist/api/SerializedAction.d.ts +40 -0
  12. package/dist/api/SerializedAction.js +59 -0
  13. package/dist/api/SwapperApi.d.ts +50 -0
  14. package/dist/api/SwapperApi.js +431 -0
  15. package/dist/api/index.d.ts +5 -0
  16. package/dist/api/index.js +24 -0
  17. package/dist/events/UnifiedSwapEventListener.d.ts +4 -3
  18. package/dist/events/UnifiedSwapEventListener.js +8 -2
  19. package/dist/http/HttpUtils.d.ts +4 -2
  20. package/dist/http/HttpUtils.js +10 -4
  21. package/dist/http/paramcoders/client/StreamingFetchPromise.d.ts +2 -1
  22. package/dist/http/paramcoders/client/StreamingFetchPromise.js +3 -2
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/intermediaries/IntermediaryDiscovery.d.ts +7 -2
  26. package/dist/intermediaries/IntermediaryDiscovery.js +4 -4
  27. package/dist/intermediaries/apis/IntermediaryAPI.d.ts +171 -14
  28. package/dist/intermediaries/apis/IntermediaryAPI.js +174 -28
  29. package/dist/intermediaries/auth/SignedKeyBasedAuth.d.ts +14 -0
  30. package/dist/intermediaries/auth/SignedKeyBasedAuth.js +68 -0
  31. package/dist/storage/IUnifiedStorage.d.ts +45 -3
  32. package/dist/storage/UnifiedSwapStorage.d.ts +8 -2
  33. package/dist/storage/UnifiedSwapStorage.js +46 -8
  34. package/dist/swapper/Swapper.d.ts +41 -3
  35. package/dist/swapper/Swapper.js +93 -48
  36. package/dist/swapper/SwapperUtils.d.ts +18 -2
  37. package/dist/swapper/SwapperUtils.js +39 -1
  38. package/dist/swaps/ISwap.d.ts +70 -9
  39. package/dist/swaps/ISwap.js +28 -6
  40. package/dist/swaps/ISwapWrapper.d.ts +11 -1
  41. package/dist/swaps/ISwapWrapper.js +23 -3
  42. package/dist/swaps/escrow_swaps/IEscrowSwap.d.ts +1 -1
  43. package/dist/swaps/escrow_swaps/IEscrowSwap.js +4 -2
  44. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.d.ts +2 -1
  45. package/dist/swaps/escrow_swaps/IEscrowSwapWrapper.js +2 -2
  46. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.d.ts +3 -1
  47. package/dist/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.js +3 -2
  48. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.d.ts +47 -31
  49. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.js +201 -67
  50. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.d.ts +3 -1
  51. package/dist/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.js +6 -6
  52. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.d.ts +82 -15
  53. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.js +304 -98
  54. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.d.ts +3 -1
  55. package/dist/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.js +6 -6
  56. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.d.ts +75 -42
  57. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.js +424 -87
  58. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.d.ts +3 -1
  59. package/dist/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.js +7 -7
  60. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.d.ts +54 -11
  61. package/dist/swaps/escrow_swaps/tobtc/IToBTCSwap.js +214 -41
  62. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.d.ts +2 -1
  63. package/dist/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.js +7 -8
  64. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.d.ts +3 -1
  65. package/dist/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.js +5 -5
  66. package/dist/swaps/spv_swaps/SpvFromBTCSwap.d.ts +76 -19
  67. package/dist/swaps/spv_swaps/SpvFromBTCSwap.js +290 -51
  68. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.d.ts +3 -1
  69. package/dist/swaps/spv_swaps/SpvFromBTCWrapper.js +5 -5
  70. package/dist/swaps/trusted/ln/LnForGasSwap.d.ts +53 -12
  71. package/dist/swaps/trusted/ln/LnForGasSwap.js +163 -49
  72. package/dist/swaps/trusted/ln/LnForGasWrapper.js +1 -2
  73. package/dist/swaps/trusted/onchain/OnchainForGasSwap.d.ts +14 -13
  74. package/dist/swaps/trusted/onchain/OnchainForGasSwap.js +30 -47
  75. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.d.ts +3 -1
  76. package/dist/swaps/trusted/onchain/OnchainForGasWrapper.js +4 -4
  77. package/dist/types/SwapExecutionAction.d.ts +141 -34
  78. package/dist/types/SwapExecutionAction.js +104 -0
  79. package/dist/types/SwapExecutionStep.d.ts +144 -0
  80. package/dist/types/SwapExecutionStep.js +87 -0
  81. package/dist/types/TokenAmount.d.ts +6 -0
  82. package/dist/types/TokenAmount.js +26 -1
  83. package/dist/utils/BitcoinUtils.d.ts +2 -0
  84. package/dist/utils/BitcoinUtils.js +34 -1
  85. package/dist/utils/Utils.d.ts +3 -1
  86. package/dist/utils/Utils.js +7 -1
  87. package/package.json +7 -4
  88. package/src/api/ApiEndpoints.ts +427 -0
  89. package/src/api/ApiParser.ts +138 -0
  90. package/src/api/ApiTypes.ts +229 -0
  91. package/src/api/SerializedAction.ts +97 -0
  92. package/src/api/SwapperApi.ts +545 -0
  93. package/src/api/index.ts +5 -0
  94. package/src/events/UnifiedSwapEventListener.ts +11 -3
  95. package/src/http/HttpUtils.ts +10 -4
  96. package/src/http/paramcoders/client/StreamingFetchPromise.ts +4 -2
  97. package/src/index.ts +1 -0
  98. package/src/intermediaries/IntermediaryDiscovery.ts +9 -2
  99. package/src/intermediaries/apis/IntermediaryAPI.ts +314 -30
  100. package/src/intermediaries/auth/SignedKeyBasedAuth.ts +69 -0
  101. package/src/storage/IUnifiedStorage.ts +45 -4
  102. package/src/storage/UnifiedSwapStorage.ts +42 -8
  103. package/src/swapper/Swapper.ts +134 -52
  104. package/src/swapper/SwapperUtils.ts +42 -2
  105. package/src/swaps/ISwap.ts +88 -16
  106. package/src/swaps/ISwapWrapper.ts +28 -3
  107. package/src/swaps/escrow_swaps/IEscrowSwap.ts +5 -3
  108. package/src/swaps/escrow_swaps/IEscrowSwapWrapper.ts +3 -1
  109. package/src/swaps/escrow_swaps/frombtc/IFromBTCLNWrapper.ts +4 -1
  110. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNSwap.ts +264 -67
  111. package/src/swaps/escrow_swaps/frombtc/ln/FromBTCLNWrapper.ts +6 -4
  112. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoSwap.ts +390 -89
  113. package/src/swaps/escrow_swaps/frombtc/ln_auto/FromBTCLNAutoWrapper.ts +6 -4
  114. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCSwap.ts +548 -94
  115. package/src/swaps/escrow_swaps/frombtc/onchain/FromBTCWrapper.ts +7 -5
  116. package/src/swaps/escrow_swaps/tobtc/IToBTCSwap.ts +276 -45
  117. package/src/swaps/escrow_swaps/tobtc/ln/ToBTCLNWrapper.ts +7 -6
  118. package/src/swaps/escrow_swaps/tobtc/onchain/ToBTCWrapper.ts +5 -3
  119. package/src/swaps/spv_swaps/SpvFromBTCSwap.ts +393 -57
  120. package/src/swaps/spv_swaps/SpvFromBTCWrapper.ts +5 -3
  121. package/src/swaps/trusted/ln/LnForGasSwap.ts +211 -47
  122. package/src/swaps/trusted/ln/LnForGasWrapper.ts +1 -2
  123. package/src/swaps/trusted/onchain/OnchainForGasSwap.ts +32 -51
  124. package/src/swaps/trusted/onchain/OnchainForGasWrapper.ts +5 -3
  125. package/src/types/SwapExecutionAction.ts +266 -43
  126. package/src/types/SwapExecutionStep.ts +224 -0
  127. package/src/types/TokenAmount.ts +36 -2
  128. package/src/utils/BitcoinUtils.ts +32 -0
  129. package/src/utils/Utils.ts +10 -1
  130. package/src/intermediaries/apis/TrustedIntermediaryAPI.ts +0 -258
@@ -0,0 +1,427 @@
1
+ import {SwapExecutionStep} from "../types/SwapExecutionStep";
2
+ import {SerializedAction} from "./SerializedAction";
3
+ import {SwapExecutionAction} from "../types/SwapExecutionAction";
4
+ import {ApiAmount, ApiLNURL, ApiToken} from "./ApiTypes";
5
+ import {LNURLDecodedSuccessAction} from "../types/lnurl/LNURLPay";
6
+
7
+ /**
8
+ * Base serialized swap data returned by API endpoints that expose swap details.
9
+ *
10
+ * @category API
11
+ */
12
+ export type SwapOutputBase = {
13
+ /** Unique identifier of the swap. */
14
+ swapId: string;
15
+ /** Swap type name, for example `FROM_BTC`, `TO_BTCLN`, or `FROM_BTCLN`. */
16
+ swapType: string;
17
+
18
+ /** Current swap state information. This varies for every swap type */
19
+ state: {
20
+ /** Numeric state representation of the state. */
21
+ number: number;
22
+ /** Human-readable state name. */
23
+ name: string;
24
+ /** Human-readable description of the current state. */
25
+ description: string;
26
+ };
27
+
28
+ /** Quote data captured when the swap was created. */
29
+ quote: {
30
+ /** Input source amount that will be paid by the user (including fees, but excluding source network fees). */
31
+ inputAmount: ApiAmount;
32
+ /** Output destination amount that will be paid out to the user. */
33
+ outputAmount: ApiAmount;
34
+ /** Fee breakdown included in the quote. */
35
+ fees: {
36
+ /** Swap service fee (charged in source token). */
37
+ swap: ApiAmount;
38
+ /** Swap fee to cover the transaction network fees on the destination side (charged in source token). */
39
+ networkOutput?: ApiAmount;
40
+ };
41
+ /** Quote expiration timestamp in milliseconds since Unix epoch. */
42
+ expiry: number;
43
+ /** Output address of the swap, the destination tokens will be sent here. */
44
+ outputAddress: string;
45
+ };
46
+
47
+ /** Swap creation timestamp in milliseconds since Unix epoch. */
48
+ createdAt: number;
49
+
50
+ /** Swap execution steps. */
51
+ steps: SwapExecutionStep[];
52
+
53
+ /** LNURL metadata attached to Lightning-based swaps when applicable. */
54
+ lnurl?: {
55
+ /** LNURL-withdraw link for Lightning to smart-chain flows. */
56
+ withdraw?: string;
57
+ /** LNURL-pay link for smart-chain to Lightning flows. */
58
+ pay?: string;
59
+ /** LNURL success action returned after a successful payment via LNURL-pay link, if specified in the LNURL. */
60
+ successAction?: LNURLDecodedSuccessAction;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Input for creating a new swap
66
+ *
67
+ * @category API
68
+ */
69
+ export type CreateSwapInput = {
70
+ /** Input source token identifier, always in the format of <network>-<tiker>, e.g. `BITCOIN-BTC`, `LIGHTNING-BTC`, or `STARKNET-STRK`. */
71
+ srcToken: string;
72
+ /** Output destination token identifier, always in the format of <network>-<tiker>, e.g. `BITCOIN-BTC`, `LIGHTNING-BTC`, or `STARKNET-STRK`. */
73
+ dstToken: string;
74
+ /** Swap amount in base units */
75
+ amount: bigint;
76
+ /** Whether the provided amount represents the exact input or exact output side of the quote. */
77
+ amountType: "EXACT_IN" | "EXACT_OUT";
78
+ /** Source address for flows that require it, mainly smart-chain to Bitcoin or Lightning swaps. */
79
+ srcAddress?: string;
80
+ /** Destination address, invoice, or recipient identifier for the swap output. */
81
+ dstAddress: string;
82
+ /**
83
+ * Only for smart chain to Lighting/Bitcoin swaps.
84
+ *
85
+ * Optional gas-drop amount (additional native token) to receive on the destination smart chain, in base units.
86
+ */
87
+ gasAmount?: bigint;
88
+ /**
89
+ * Only for Lightning to smart chain swaps.
90
+ *
91
+ * Optional custom swap payment hash encoded as a hexadecimal string, for when you want to generate the preimage
92
+ * and payment hash pair outside the API. When used, you need to provide the `secret` argument to the `getSwapStatus`
93
+ * endpoint when the swap HTLC is ready to be claimed (indicated by the `requiresSecretReveal` param in the
94
+ * `getSwapStatus` response)
95
+ */
96
+ paymentHash?: string;
97
+ /**
98
+ * Only for Lightning to smart chain swaps.
99
+ *
100
+ * Optional description to add to the generated Lightning network BOLT11 invoice.
101
+ */
102
+ lightningInvoiceDescription?: string;
103
+ /**
104
+ * Only for Lightning to smart chain swaps.
105
+ *
106
+ * Optional description to add to the generated Lightning network BOLT11 invoice.
107
+ */
108
+ lightningInvoiceDescriptionHash?: string;
109
+ /**
110
+ * Only for smart chain to Lightning swaps.
111
+ *
112
+ * Optional override for the HTLC timeout in seconds (default is 5 days), longer timeouts allow more lightning
113
+ * network hops to be considered when routing the destination lightning network payment, but might lead to longer
114
+ * funds lockup in case of non-cooperative LP.
115
+ */
116
+ lightningPaymentHTLCTimeout?: number;
117
+ }
118
+
119
+ /**
120
+ * Output from create swap endpoint
121
+ *
122
+ * @category API
123
+ */
124
+ export type CreateSwapOutput = SwapOutputBase;
125
+
126
+ /**
127
+ * Input for getting swap status
128
+ *
129
+ * @category API
130
+ */
131
+ export type GetSwapStatusInput = {
132
+ /** Unique identifier of the swap to query. */
133
+ swapId: string;
134
+
135
+ /**
136
+ * Swap secret pre-image revealed after the destination chain HTLC is created for Lightning to smart chain swaps,
137
+ * encoded as a hexadecimal string.
138
+ */
139
+ secret?: string;
140
+
141
+ /**
142
+ * For Bitcoin to smart chain swaps.
143
+ *
144
+ * Bitcoin address used to request a pre-funded PSBT with populated input UTXOs ready for signing and execution
145
+ */
146
+ bitcoinAddress?: string;
147
+ /**
148
+ * For Bitcoin to smart chain swaps.
149
+ *
150
+ * Bitcoin public key used together with `bitcoinAddress` to request a pre-funded PSBT with populated input UTXOs
151
+ * ready for signing and execution
152
+ */
153
+ bitcoinPublicKey?: string;
154
+ /**
155
+ * For Bitcoin to smart chain swaps.
156
+ *
157
+ * Bitcoin fee rate override used when building a pre-funded PSBT, otherwise the current economical fee rate is used.
158
+ */
159
+ bitcoinFeeRate?: number;
160
+
161
+ /** Alternative smart-chain signer to use for claim, refund, or manual settlement transactions. */
162
+ signer?: string;
163
+ }
164
+
165
+ /**
166
+ * Output from swap status getter
167
+ *
168
+ * @category API
169
+ */
170
+ export type ListSwapOutput = SwapOutputBase & {
171
+ /** Whether the swap reached a terminal state. */
172
+ isFinished: boolean;
173
+ /** Whether the swap finished successfully. */
174
+ isSuccess: boolean;
175
+ /** Whether the swap finished in a failed state. */
176
+ isFailed: boolean;
177
+ /** Whether the quote expired before completion. */
178
+ isExpired: boolean;
179
+ }
180
+
181
+ /**
182
+ * Output from swap status getter
183
+ *
184
+ * @category API
185
+ */
186
+ export type GetSwapStatusOutput = ListSwapOutput & {
187
+ /** Current actionable instruction for the client, or `null` when no action is required. */
188
+ currentAction: SerializedAction<SwapExecutionAction> | null;
189
+
190
+ /**
191
+ * For Lightning to smart chain swaps.
192
+ *
193
+ * Whether the client should provide the Lightning secret pre-image to continue execution. Returned when the HTLC
194
+ * on the destination chain is created and is ready to be claimed by the user.
195
+ */
196
+ requiresSecretReveal?: boolean;
197
+
198
+ /**
199
+ * Escrow-specific data for escrow contract based swaps (all except the non-legacy Bitcoin to smart chain swaps)
200
+ */
201
+ escrow?: {
202
+ /** Serialized escrow data. */
203
+ data: any,
204
+ /** A transaction which created the escrow, if the escrow is already created on-chain. */
205
+ initTxId?: string
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Input for listing swaps
211
+ *
212
+ * @category API
213
+ */
214
+ export type ListSwapsInput = {
215
+ /** Smart-chain signer address used to filter swaps. */
216
+ signer: string;
217
+ /** Optional smart-chain identifier used to narrow the result set, e.g. SOLANA, STARKNET, CITREA */
218
+ chainId?: string;
219
+ }
220
+
221
+ /**
222
+ * Output from swap list endpoint
223
+ *
224
+ * @category API
225
+ */
226
+ export type ListSwapsOutput = ListSwapOutput[];
227
+
228
+ /**
229
+ * Input for listing pending swaps
230
+ *
231
+ * @category API
232
+ */
233
+ export type ListPendingSwapsInput = ListSwapsInput;
234
+
235
+ /**
236
+ * Output from pending swap list endpoint
237
+ *
238
+ * @category API
239
+ */
240
+ export type ListPendingSwapsOutput = ListSwapsOutput;
241
+
242
+ /**
243
+ * Input for listing supported tokens
244
+ *
245
+ * @category API
246
+ */
247
+ export type GetSupportedTokensInput = {
248
+ /** Whether to list source-side (`INPUT`) or destination-side (`OUTPUT`) tokens. */
249
+ side: "INPUT" | "OUTPUT";
250
+ }
251
+
252
+ /**
253
+ * Output from supported token list endpoint
254
+ *
255
+ * @category API
256
+ */
257
+ export type GetSupportedTokensOutput = ApiToken[];
258
+
259
+ /**
260
+ * Input for listing swap counter-tokens for a given token
261
+ *
262
+ * @category API
263
+ */
264
+ export type GetSwapCounterTokensInput = {
265
+ /** Token identifier to find compatible counter-tokens for, e.g. BITCOIN-BTC, LIGHTNING-BTC or STARKNET-STRK */
266
+ token: string;
267
+ /** Whether the provided token should be treated as the input or output side of the swap. */
268
+ side: "INPUT" | "OUTPUT";
269
+ }
270
+
271
+ /**
272
+ * Output from swap counter-token list endpoint
273
+ *
274
+ * @category API
275
+ */
276
+ export type GetSwapCounterTokensOutput = ApiToken[];
277
+
278
+ /**
279
+ * Input for querying swap limits between two tokens
280
+ *
281
+ * @category API
282
+ */
283
+ export type GetSwapLimitsInput = {
284
+ /** Source token identifier, e.g. BITCOIN-BTC, LIGHTNING-BTC or STARKNET-STRK */
285
+ srcToken: string;
286
+ /** Destination token identifier, e.g. BITCOIN-BTC, LIGHTNING-BTC or STARKNET-STRK */
287
+ dstToken: string;
288
+ }
289
+
290
+ /**
291
+ * Output from swap limits endpoint
292
+ *
293
+ * @category API
294
+ */
295
+ export type GetSwapLimitsOutput = {
296
+ /** Limits expressed on the input side of the swap. */
297
+ input: {
298
+ /** Minimum supported input amount. */
299
+ min: ApiAmount;
300
+ /** Maximum supported input amount when bounded. */
301
+ max?: ApiAmount;
302
+ };
303
+ /** Limits expressed on the output side of the swap. */
304
+ output: {
305
+ /** Minimum supported output amount. */
306
+ min: ApiAmount;
307
+ /** Maximum supported output amount when bounded. */
308
+ max?: ApiAmount;
309
+ };
310
+ }
311
+
312
+ /**
313
+ * Input for parsing an address-like string supported by the SDK
314
+ *
315
+ * @category API
316
+ */
317
+ export type ParseAddressInput = {
318
+ /** Address-like string to parse, such as a wallet address, lightning invoice, LNURL, or URI. */
319
+ address: string;
320
+ }
321
+
322
+ /**
323
+ * Output from address parser endpoint
324
+ *
325
+ * @category API
326
+ */
327
+ export type ParseAddressOutput = {
328
+ /** Canonical parsed address or recipient identifier. */
329
+ address: string;
330
+ /** Parsed address type, e.g. BITCOIN, LIGHTNING, SOLANA, STARKNET or CITREA */
331
+ type: string;
332
+ /** Parsed LNURL metadata when the input resolves to LNURL content. */
333
+ lnurl?: ApiLNURL;
334
+ /** Minimum supported amount advertised by the parsed target, when available. */
335
+ min?: ApiAmount;
336
+ /** Maximum supported amount advertised by the parsed target, when available. */
337
+ max?: ApiAmount;
338
+ /** Amount embedded directly in the parsed target, when present. */
339
+ amount?: ApiAmount;
340
+ };
341
+
342
+ /**
343
+ * Input for querying spendable wallet balance
344
+ *
345
+ * @category API
346
+ */
347
+ export type GetSpendableBalanceInput = {
348
+ /** Wallet address to inspect. */
349
+ wallet: string;
350
+ /** Token identifier to get the balance for, e.g. BITCOIN-BTC, SOLANA-SOL or STARKNET-STRK */
351
+ token: string;
352
+ /**
353
+ * Target smart chain identifier when estimating spendable bitcoin balance. Automatically adjusts based on
354
+ * available swaps between Bitcoin and the provided chain
355
+ */
356
+ targetChain?: string;
357
+ /**
358
+ * Whether gas-drop transaction size overhead should be included for Bitcoin to smart chain swaps.
359
+ */
360
+ gasDrop?: boolean;
361
+ /**
362
+ * Manual fee-rate override used for spendable balance estimation.
363
+ */
364
+ feeRate?: string;
365
+ /** Minimum Bitcoin fee rate to enforce during estimation. */
366
+ minBitcoinFeeRate?: number;
367
+ /**
368
+ * Multiplier applied to fetched economical network fee.
369
+ */
370
+ feeMultiplier?: number;
371
+ }
372
+
373
+ /**
374
+ * Output from spendable balance endpoint
375
+ *
376
+ * @category API
377
+ */
378
+ export type GetSpendableBalanceOutput = {
379
+ /** Spendable balance after deducting estimated swap execution costs. */
380
+ balance: ApiAmount;
381
+ /** Fee rate used during estimation. Only when estimating BTC balances on Bitcoin */
382
+ feeRate?: number;
383
+ }
384
+
385
+ /**
386
+ * Input for submitting signed transactions
387
+ *
388
+ * @category API
389
+ */
390
+ export type SubmitTransactionInput = {
391
+ /** Unique identifier of the swap the transactions belong to. */
392
+ swapId: string;
393
+ /** Serialized signed transactions to submit in execution order. */
394
+ signedTxs: string[];
395
+ }
396
+
397
+ /**
398
+ * Output from submitting transactions
399
+ *
400
+ * @category API
401
+ */
402
+ export type SubmitTransactionOutput = {
403
+ /** Transaction hashes returned after successful submission. */
404
+ txHashes: string[];
405
+ }
406
+
407
+ /**
408
+ * Input for triggering an LNURL-withdraw based settlement for Lightning -> Smart chain swaps
409
+ *
410
+ * @category API
411
+ */
412
+ export type SettleWithLnurlInput = {
413
+ /** Unique identifier of the Lightning to smart-chain swap to settle. */
414
+ swapId: string;
415
+ /** LNURL-withdraw link to use when the swap was not already created with one. */
416
+ lnurlWithdraw?: string;
417
+ }
418
+
419
+ /**
420
+ * Output for triggering an LNURL-withdraw based settlement for Lightning -> Smart chain swaps
421
+ *
422
+ * @category API
423
+ */
424
+ export type SettleWithLnurlOutput = {
425
+ /** Payment hash of the Lightning payment paid by the LNURL-withdraw link, encoded as a hexadecimal string. */
426
+ paymentHash: string;
427
+ }
@@ -0,0 +1,138 @@
1
+ import {InputSchema, InputSchemaField} from "./ApiTypes";
2
+
3
+ // Errors
4
+ function invalidInput(path: string, expected: string, value: unknown): Error {
5
+ const actualType = value == null ? String(value) : Array.isArray(value) ? "array" : typeof(value);
6
+ return new Error(`Invalid input "${path}", expected ${expected}, got ${actualType}`);
7
+ }
8
+
9
+ function missingInput(path: string): Error {
10
+ return new Error(`Missing required input "${path}"`);
11
+ }
12
+
13
+ function invalidAllowedValue(path: string, allowedValues: readonly (string | number | bigint)[], value: string | number | bigint): Error {
14
+ return new Error(
15
+ `Invalid input "${path}", expected one of: ${allowedValues.map(val => val.toString()).join(", ")}, got ${value.toString()}`
16
+ );
17
+ }
18
+
19
+ // Parsers
20
+ function parseNumber(path: string, value: unknown): number {
21
+ let result: number;
22
+ if(typeof(value) === "number") {
23
+ result = value;
24
+ } else if(typeof(value) === "string") {
25
+ const trimmedValue = value.trim();
26
+ if(trimmedValue === "") throw invalidInput(path, "finite number", value);
27
+ result = Number(trimmedValue);
28
+ } else throw invalidInput(path, "finite number", value);
29
+
30
+ if(!Number.isFinite(result)) throw invalidInput(path, "finite number", value);
31
+ return result;
32
+ }
33
+
34
+ function parseBigInt(path: string, value: unknown): bigint {
35
+ if(typeof(value) === "bigint") return value;
36
+
37
+ if(typeof(value) === "number") {
38
+ if(!Number.isSafeInteger(value)) throw invalidInput(path, "safe integer", value);
39
+ return BigInt(value);
40
+ }
41
+
42
+ if(typeof(value) === "string") {
43
+ const trimmedValue = value.trim();
44
+ if(!/^[+-]?\d+$/.test(trimmedValue)) throw invalidInput(path, "integer", value);
45
+ return BigInt(trimmedValue);
46
+ }
47
+
48
+ throw invalidInput(path, "integer", value);
49
+ }
50
+
51
+ function parseBoolean(path: string, value: unknown): boolean {
52
+ Boolean(value);
53
+ if(typeof(value) === "boolean") return value;
54
+ if(value === "true") return true;
55
+ if(value === "false") return false;
56
+ throw invalidInput(path, "boolean", value);
57
+ }
58
+
59
+ // Object type check
60
+ function isObject(value: unknown): value is Record<string, unknown> {
61
+ return value != null && typeof(value) === "object" && !Array.isArray(value);
62
+ }
63
+
64
+ // Allowed values for enums
65
+ function applyAllowedValues<T extends string | number | bigint>(path: string, value: T, allowedValues?: readonly T[]): T {
66
+ if(allowedValues != null && !allowedValues.includes(value)) {
67
+ throw invalidAllowedValue(path, allowedValues, value);
68
+ }
69
+ return value;
70
+ }
71
+
72
+ function parseField<T>(field: InputSchemaField<T>, value: unknown, path: string): T {
73
+ switch(field.type) {
74
+ case "string": {
75
+ if(typeof(value) !== "string") throw invalidInput(path, "string", value);
76
+ return applyAllowedValues(path, value, field.allowedValues as readonly string[] | undefined) as T;
77
+ }
78
+ case "number": {
79
+ const parsedValue = parseNumber(path, value);
80
+ return applyAllowedValues(path, parsedValue, field.allowedValues as readonly number[] | undefined) as T;
81
+ }
82
+ case "bigint": {
83
+ const parsedValue = parseBigInt(path, value);
84
+ return applyAllowedValues(path, parsedValue, field.allowedValues as readonly bigint[] | undefined) as T;
85
+ }
86
+ case "boolean":
87
+ return parseBoolean(path, value) as T;
88
+ case "array": {
89
+ if(!Array.isArray(value)) throw invalidInput(path, "array", value);
90
+ if(field.items == null) return [...value] as T;
91
+
92
+ return value.map((item, index) => parseField(field.items as InputSchemaField, item, `${path}[${index}]`)) as T;
93
+ }
94
+ case "object": {
95
+ if(!isObject(value)) throw invalidInput(path, "object", value);
96
+ if(field.properties == null) return {...value} as T;
97
+
98
+ return _parseApiInput(field.properties as InputSchema<T>, value, path);
99
+ }
100
+ default:
101
+ throw new Error(`Unsupported input schema type for "${path}": ${(field as InputSchemaField).type}`);
102
+ }
103
+ }
104
+
105
+ function _parseApiInput<TInput>(inputSchema: InputSchema<TInput>, rawInput: unknown, parentPath: string = ""): TInput {
106
+ if(!isObject(rawInput)) {
107
+ throw invalidInput(parentPath || "input", "object", rawInput);
108
+ }
109
+
110
+ const parsedInput: Partial<TInput> = {};
111
+
112
+ for(const key in inputSchema) {
113
+ const field = inputSchema[key];
114
+ const value = rawInput[key];
115
+ const path = parentPath === "" ? key : `${parentPath}.${key}`;
116
+
117
+ if(value == null) {
118
+ if(field.required) throw missingInput(path);
119
+ continue;
120
+ }
121
+
122
+ parsedInput[key] = parseField(field as InputSchemaField<TInput[typeof key]>, value, path);
123
+ }
124
+
125
+ return parsedInput as TInput;
126
+ }
127
+
128
+ /**
129
+ * Parses raw input values according to the endpoint schema.
130
+ *
131
+ * This accepts untyped transport input such as HTTP query params, JSON request bodies,
132
+ * or CLI arguments and returns the normalized callback input type.
133
+ *
134
+ * @category API
135
+ */
136
+ export function parseApiInput<TInput>(inputSchema: InputSchema<TInput>, rawInput: unknown): TInput {
137
+ return _parseApiInput(inputSchema, rawInput);
138
+ }