@txnlab/deflex 1.0.0-beta.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.
@@ -0,0 +1,973 @@
1
+ import { AlgorandClient } from "@algorandfoundation/algokit-utils";
2
+ import algosdk, { Transaction, TransactionSigner } from "algosdk";
3
+
4
+ //#region src/constants.d.ts
5
+ /**
6
+ * Supported DEX protocols for swap routing
7
+ */
8
+ declare enum Protocol {
9
+ TinymanV2 = "TinymanV2",
10
+ Algofi = "Algofi",
11
+ Algomint = "Algomint",
12
+ Pact = "Pact",
13
+ Folks = "Folks",
14
+ TAlgo = "TAlgo",
15
+ }
16
+ /**
17
+ * Deprecated protocols that are automatically excluded from routing
18
+ *
19
+ * @internal
20
+ */
21
+ declare const DEPRECATED_PROTOCOLS: readonly ["Humble", "Tinyman"];
22
+ /** Default Algod node URI for mainnet */
23
+ declare const DEFAULT_ALGOD_URI = "https://mainnet-api.4160.nodely.dev/";
24
+ /** Default Algod node token (empty for public nodes) */
25
+ declare const DEFAULT_ALGOD_TOKEN = "";
26
+ /** Default Algod node port */
27
+ declare const DEFAULT_ALGOD_PORT = 443;
28
+ /** Default Deflex API base URL */
29
+ declare const DEFAULT_API_BASE_URL = "https://deflex.txnlab.dev/api";
30
+ /** Default fee in basis points (0.15%) */
31
+ declare const DEFAULT_FEE_BPS = 15;
32
+ /** Maximum allowed fee in basis points (3.00%) */
33
+ declare const MAX_FEE_BPS = 300;
34
+ /** Default maximum transaction group size */
35
+ declare const DEFAULT_MAX_GROUP_SIZE = 16;
36
+ /** Default maximum routing depth (number of hops) */
37
+ declare const DEFAULT_MAX_DEPTH = 4;
38
+ /** Default auto opt-in setting (automatic asset/app opt-in detection) */
39
+ declare const DEFAULT_AUTO_OPT_IN = false;
40
+ /** Default number of rounds to wait for transaction confirmation */
41
+ declare const DEFAULT_CONFIRMATION_ROUNDS = 4;
42
+ //#endregion
43
+ //#region src/types.d.ts
44
+ /**
45
+ * Configuration parameters for initializing DeflexClient
46
+ */
47
+ interface DeflexConfigParams {
48
+ /** API key for Deflex API (required) */
49
+ readonly apiKey: string;
50
+ /** Algod node URI (default: https://mainnet-api.4160.nodely.dev/) */
51
+ readonly algodUri?: string;
52
+ /** Algod node token (can be empty string for public nodes) */
53
+ readonly algodToken?: string;
54
+ /** Algod node port (default: 443) */
55
+ readonly algodPort?: string | number;
56
+ /** Referrer address for fee sharing (receives 25% of swap fees) */
57
+ readonly referrerAddress?: string;
58
+ /** Fee in basis points (default: 15 = 0.15%, max: 300 = 3.00%) */
59
+ readonly feeBps?: number;
60
+ /** Automatically detect and add required opt-in transactions (default: false) */
61
+ readonly autoOptIn?: boolean;
62
+ }
63
+ /**
64
+ * Internal configuration with all required fields validated
65
+ *
66
+ * @internal
67
+ */
68
+ type DeflexConfig = Omit<Required<DeflexConfigParams>, 'referrerAddress'> & {
69
+ readonly referrerAddress: string | undefined;
70
+ };
71
+ /**
72
+ * Swap quote type determining which amount is fixed
73
+ */
74
+ type QuoteType = 'fixed-input' | 'fixed-output';
75
+ /**
76
+ * Parameters for requesting a swap quote from the Deflex API (raw API method)
77
+ */
78
+ interface FetchQuoteParams {
79
+ /** Input asset ID */
80
+ readonly fromASAID: bigint | number;
81
+ /** Output asset ID */
82
+ readonly toASAID: bigint | number;
83
+ /** Amount to swap (in base units) */
84
+ readonly amount: bigint | number;
85
+ /** Quote type (default: 'fixed-input') */
86
+ readonly type?: QuoteType;
87
+ /** Protocols to exclude from routing (default: []) */
88
+ readonly disabledProtocols?: readonly Protocol[];
89
+ /** Maximum transaction group size (default: 16) */
90
+ readonly maxGroupSize?: number;
91
+ /** Maximum depth of the route (default: 4) */
92
+ readonly maxDepth?: number;
93
+ /** Whether to include asset opt-in transaction (overrides config.autoOptIn if set) */
94
+ readonly optIn?: boolean;
95
+ /** Address of the account that will perform the swap (required if autoOptIn is enabled) */
96
+ readonly address?: string;
97
+ }
98
+ /**
99
+ * Parameters for requesting a swap quote (class-based method)
100
+ */
101
+ interface QuoteParams {
102
+ /** Input asset ID */
103
+ readonly fromAssetId: bigint | number;
104
+ /** Output asset ID */
105
+ readonly toAssetId: bigint | number;
106
+ /** Amount to swap (in base units) */
107
+ readonly amount: bigint | number;
108
+ /** Quote type (default: 'fixed-input') */
109
+ readonly type?: QuoteType;
110
+ /** Protocols to exclude from routing (default: []) */
111
+ readonly disabledProtocols?: readonly Protocol[];
112
+ /** Maximum transaction group size (default: 16) */
113
+ readonly maxGroupSize?: number;
114
+ /** Maximum depth of the route (default: 4) */
115
+ readonly maxDepth?: number;
116
+ /** Whether to include asset opt-in transaction (overrides config.autoOptIn if set) */
117
+ readonly optIn?: boolean;
118
+ /** Address of the account that will perform the swap (required if autoOptIn is enabled) */
119
+ readonly address?: string;
120
+ }
121
+ /**
122
+ * Asset information from the Deflex API
123
+ */
124
+ interface Asset {
125
+ /** Asset ID */
126
+ readonly id: number;
127
+ /** Number of decimal places */
128
+ readonly decimals: number;
129
+ /** Unit name */
130
+ readonly unit_name: string;
131
+ /** Full asset name */
132
+ readonly name: string;
133
+ /** Price in ALGO */
134
+ readonly price_algo: number;
135
+ /** Price in USD */
136
+ readonly price_usd: number;
137
+ }
138
+ /**
139
+ * Profit information for a swap
140
+ */
141
+ interface Profit {
142
+ /** Profit amount in base units */
143
+ readonly amount: number;
144
+ /** The asset in which profit is denominated */
145
+ readonly asa: Asset;
146
+ }
147
+ /**
148
+ * A single step in a swap route path
149
+ */
150
+ interface PathElement {
151
+ /** Protocol name and fee tier */
152
+ readonly name: string;
153
+ /** Protocol class identifier */
154
+ readonly class: string[][];
155
+ /** Input asset */
156
+ readonly in: Asset;
157
+ /** Output asset */
158
+ readonly out: Asset;
159
+ }
160
+ /**
161
+ * A route with its percentage of the total swap amount
162
+ */
163
+ interface Route {
164
+ /** Percentage of total swap amount routed through this path */
165
+ readonly percentage: number;
166
+ /** The sequence of swaps in this route */
167
+ readonly path: PathElement[];
168
+ }
169
+ /**
170
+ * Quote from a specific DEX protocol
171
+ */
172
+ interface DexQuote {
173
+ /** Protocol name and fee tier */
174
+ readonly name: string;
175
+ /** Protocol class */
176
+ readonly class: string;
177
+ /** Quoted output value */
178
+ readonly value: number;
179
+ }
180
+ /**
181
+ * Encrypted transaction payload from the quote
182
+ */
183
+ interface TxnPayload {
184
+ /** Initialization vector for decryption */
185
+ readonly iv: string;
186
+ /** Encrypted transaction data */
187
+ readonly data: string;
188
+ }
189
+ /**
190
+ * Quote response from the Deflex API
191
+ */
192
+ interface FetchQuoteResponse {
193
+ /** The quoted output amount or input amount (depending on quote type) */
194
+ readonly quote: string | number;
195
+ /** Profit information for the swap */
196
+ readonly profit: Profit;
197
+ /** Baseline price without fees */
198
+ readonly priceBaseline: number;
199
+ /** Price impact for the user */
200
+ readonly userPriceImpact?: number;
201
+ /** Overall market price impact */
202
+ readonly marketPriceImpact?: number;
203
+ /** USD value of input amount */
204
+ readonly usdIn: number;
205
+ /** USD value of output amount */
206
+ readonly usdOut: number;
207
+ /** The routing path(s) for the swap */
208
+ readonly route: Route[];
209
+ /** Flattened view of routing percentages by protocol */
210
+ readonly flattenedRoute: Record<string, number>;
211
+ /** Individual quotes from each DEX */
212
+ readonly quotes: DexQuote[];
213
+ /** App IDs that require opt-in for this swap */
214
+ readonly requiredAppOptIns: number[];
215
+ /** Encrypted transaction payload for generating swap transactions */
216
+ readonly txnPayload: TxnPayload | null;
217
+ /** Fees charged by each protocol */
218
+ readonly protocolFees: Record<string, number>;
219
+ /** Input asset ID */
220
+ readonly fromASAID: number;
221
+ /** Output asset ID */
222
+ readonly toASAID: number;
223
+ /** Quote type */
224
+ readonly type: string;
225
+ /** Performance timing data */
226
+ readonly timing?: unknown;
227
+ }
228
+ /**
229
+ * Transaction signature from the Deflex API
230
+ */
231
+ interface DeflexSignature {
232
+ /** Signature type */
233
+ readonly type: 'logic_signature' | 'secret_key';
234
+ /** Signature data */
235
+ readonly value: unknown;
236
+ }
237
+ /**
238
+ * Transaction data from the Deflex API
239
+ */
240
+ interface DeflexTransaction {
241
+ /** Base64-encoded transaction data */
242
+ readonly data: string;
243
+ /** Group ID for the transaction */
244
+ readonly group: string;
245
+ /** Logic signature blob (if applicable) */
246
+ readonly logicSigBlob: unknown | false;
247
+ /** Signature data (false if user must sign) */
248
+ readonly signature: DeflexSignature | false;
249
+ }
250
+ /**
251
+ * Parameters for fetching executable swap transactions
252
+ */
253
+ interface FetchSwapTxnsParams {
254
+ /** Quote response from fetchQuote() */
255
+ readonly quote: FetchQuoteResponse;
256
+ /** Algorand address that will sign the transactions */
257
+ readonly address: string;
258
+ /** Slippage tolerance as a percentage (e.g., 1 for 1%) */
259
+ readonly slippage: number;
260
+ }
261
+ /**
262
+ * Request body for fetching swap transactions
263
+ *
264
+ * @internal
265
+ */
266
+ interface FetchSwapTxnsBody {
267
+ /** API key for authentication */
268
+ readonly apiKey: string;
269
+ /** Address of the account that will sign the transactions */
270
+ readonly address: string;
271
+ /** Encrypted transaction payload from the quote */
272
+ readonly txnPayloadJSON: TxnPayload | null;
273
+ /** Slippage tolerance as a percentage */
274
+ readonly slippage: number;
275
+ }
276
+ /**
277
+ * Response from fetching swap transactions
278
+ */
279
+ interface FetchSwapTxnsResponse {
280
+ /** Array of transaction data */
281
+ readonly txns: DeflexTransaction[];
282
+ }
283
+ /**
284
+ * Processed transaction with optional pre-signature
285
+ *
286
+ * @internal
287
+ */
288
+ interface SwapTransaction {
289
+ /** The Algorand transaction */
290
+ readonly txn: algosdk.Transaction;
291
+ /** Pre-signature from Deflex (if applicable) */
292
+ readonly deflexSignature?: DeflexSignature;
293
+ }
294
+ //#endregion
295
+ //#region src/composer.d.ts
296
+ /**
297
+ * A signer function for signing transaction groups
298
+ *
299
+ * Can be either:
300
+ * - A TransactionSigner: algosdk.TransactionSigner
301
+ * - A simpler inline function that only accepts the transaction group
302
+ */
303
+ type SignerFunction = TransactionSigner | ((txns: Transaction[]) => Promise<Uint8Array[]>);
304
+ /**
305
+ * Status of the SwapComposer transaction group lifecycle
306
+ */
307
+ declare enum SwapComposerStatus {
308
+ /** The atomic group is still under construction. */
309
+ BUILDING = 0,
310
+ /** The atomic group has been finalized, but not yet signed. */
311
+ BUILT = 1,
312
+ /** The atomic group has been finalized and signed, but not yet submitted to the network. */
313
+ SIGNED = 2,
314
+ /** The atomic group has been finalized, signed, and submitted to the network. */
315
+ SUBMITTED = 3,
316
+ /** The atomic group has been finalized, signed, submitted, and successfully committed to a block. */
317
+ COMMITTED = 4,
318
+ }
319
+ /**
320
+ * Configuration for creating a SwapComposer instance
321
+ */
322
+ interface SwapComposerConfig {
323
+ /** The quote response from fetchQuote() */
324
+ readonly quote: FetchQuoteResponse;
325
+ /** The swap transactions from fetchSwapTransactions() */
326
+ readonly deflexTxns: DeflexTransaction[];
327
+ /** AlgorandClient instance for blockchain operations */
328
+ readonly algorand: AlgorandClient;
329
+ /** The address of the account that will sign transactions */
330
+ readonly address: string;
331
+ /** Transaction signer function */
332
+ readonly signer: SignerFunction;
333
+ }
334
+ /**
335
+ * Composer for building and executing atomic swap transaction groups
336
+ *
337
+ * The SwapComposer allows you to build complex transaction groups by adding custom
338
+ * transactions before and after swap transactions. It handles pre-signed transactions,
339
+ * automatic app opt-ins, and provides a fluent API for transaction group construction.
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * const quote = await deflex.fetchQuote({ ... })
344
+ * const composer = await deflex.newSwap({ quote, address, slippage })
345
+ *
346
+ * await composer
347
+ * .addTransaction(customTxn)
348
+ * .addSwapTransactions()
349
+ * .execute(signer)
350
+ * ```
351
+ */
352
+ declare class SwapComposer {
353
+ /** The maximum size of an atomic transaction group. */
354
+ static MAX_GROUP_SIZE: number;
355
+ private status;
356
+ private transactions;
357
+ private swapTransactionsAdded;
358
+ private signedTxns;
359
+ private txIds;
360
+ private readonly quote;
361
+ private readonly deflexTxns;
362
+ private readonly algorand;
363
+ private readonly address;
364
+ private readonly signer;
365
+ /**
366
+ * Create a new SwapComposer instance
367
+ *
368
+ * Note: Most developers should use DeflexClient.newSwap() instead of constructing
369
+ * this directly, as the factory method handles fetching swap transactions automatically.
370
+ *
371
+ * @param config - Configuration for the composer
372
+ * @param config.quote - The quote response from fetchQuote()
373
+ * @param config.deflexTxns - The swap transactions from fetchSwapTransactions()
374
+ * @param config.algorand - AlgorandClient instance for blockchain operations
375
+ * @param config.address - The address of the account that will sign transactions
376
+ * @param config.signer - Transaction signer function
377
+ */
378
+ constructor(config: SwapComposerConfig);
379
+ /**
380
+ * Get the status of this composer's transaction group
381
+ *
382
+ * @returns The current status of the transaction group
383
+ */
384
+ getStatus(): SwapComposerStatus;
385
+ /**
386
+ * Get the number of transactions currently in this atomic group
387
+ *
388
+ * @returns The number of transactions in the group
389
+ */
390
+ count(): number;
391
+ /**
392
+ * Add a transaction to the atomic group
393
+ *
394
+ * Transactions are added in the order methods are called. For example:
395
+ * ```typescript
396
+ * composer
397
+ * .addTransaction(txn1) // Added first
398
+ * .addSwapTransactions() // Added second
399
+ * .addTransaction(txn2) // Added third
400
+ * ```
401
+ *
402
+ * @param transaction - The transaction to add
403
+ * @returns This composer instance for chaining
404
+ * @throws Error if the composer is not in the BUILDING status
405
+ * @throws Error if the maximum group size is exceeded
406
+ */
407
+ addTransaction(transaction: Transaction): this;
408
+ /**
409
+ * Add swap transactions to the atomic group
410
+ *
411
+ * This method automatically processes required app opt-ins and adds all swap
412
+ * transactions from the quote. Can only be called once per composer instance.
413
+ *
414
+ * @returns This composer instance for chaining
415
+ * @throws Error if the swap transactions have already been added
416
+ * @throws Error if the composer is not in the BUILDING status
417
+ * @throws Error if the maximum group size is exceeded
418
+ */
419
+ addSwapTransactions(): Promise<this>;
420
+ /**
421
+ * Sign the transaction group
422
+ *
423
+ * Automatically adds swap transactions if not already added, builds the atomic group,
424
+ * and signs all transactions using the configured signer.
425
+ *
426
+ * @returns A promise that resolves to an array of signed transaction blobs
427
+ *
428
+ * @example
429
+ * ```typescript
430
+ * const signedTxns = await composer.sign()
431
+ * ```
432
+ */
433
+ sign(): Promise<Uint8Array[]>;
434
+ /**
435
+ * Submit the signed transactions to the network
436
+ *
437
+ * This method signs the transaction group (if not already signed) and submits
438
+ * it to the Algorand network. Does not wait for confirmation.
439
+ *
440
+ * @returns The transaction IDs
441
+ * @throws Error if the transaction group has already been submitted
442
+ *
443
+ * @example
444
+ * ```typescript
445
+ * const txIds = await composer.submit()
446
+ * console.log('Submitted transactions:', txIds)
447
+ * ```
448
+ */
449
+ submit(): Promise<string[]>;
450
+ /**
451
+ * Execute the swap
452
+ *
453
+ * Signs the transaction group, submits it to the network, and waits for confirmation.
454
+ * This is the primary method for executing swaps and combines sign(), submit(), and
455
+ * waitForConfirmation() into a single call.
456
+ *
457
+ * @param waitRounds - The number of rounds to wait for confirmation (default: 4)
458
+ * @returns Object containing the confirmed round and transaction IDs
459
+ * @throws Error if the transaction group has already been committed
460
+ *
461
+ * @example
462
+ * ```typescript
463
+ * const result = await composer.execute()
464
+ * console.log(`Confirmed in round ${result.confirmedRound}`)
465
+ * console.log('Transaction IDs:', result.txIds)
466
+ * ```
467
+ */
468
+ execute(waitRounds?: number): Promise<{
469
+ confirmedRound: bigint;
470
+ txIds: string[];
471
+ }>;
472
+ /**
473
+ * Validates an Algorand address
474
+ */
475
+ private validateAddress;
476
+ /**
477
+ * Processes app opt-ins and decodes swap transactions from API response
478
+ */
479
+ private processSwapTransactions;
480
+ /**
481
+ * Creates opt-in transactions for apps the user hasn't opted into yet
482
+ */
483
+ private processRequiredAppOptIns;
484
+ /**
485
+ * Finalizes the transaction group by assigning group IDs
486
+ *
487
+ * The composer's status will be at least BUILT after executing this method.
488
+ */
489
+ private buildGroup;
490
+ /**
491
+ * Re-signs a Deflex transaction using the provided logic signature or secret key
492
+ */
493
+ private signDeflexTransaction;
494
+ }
495
+ //#endregion
496
+ //#region src/quote.d.ts
497
+ /**
498
+ * Configuration for creating a DeflexQuote instance
499
+ */
500
+ interface DeflexQuoteConfig {
501
+ /** The raw quote response from the Deflex API */
502
+ response: FetchQuoteResponse;
503
+ /** The original amount from the quote request */
504
+ amount: bigint | number;
505
+ /** Optional address parameter from the quote request */
506
+ address?: string;
507
+ }
508
+ /**
509
+ * Wrapper class for Deflex quote responses with convenience methods
510
+ *
511
+ * The DeflexQuote class provides a developer-friendly interface for working with
512
+ * swap quotes from the Deflex API. It exposes all quote properties via getters
513
+ * and provides additional convenience methods for derived data.
514
+ *
515
+ * @example
516
+ * ```typescript
517
+ * const quote = await deflex.newQuote({
518
+ * address: 'ABC...',
519
+ * fromAssetId: 0,
520
+ * toAssetId: 31566704,
521
+ * amount: 1_000_000,
522
+ * })
523
+ *
524
+ * // Access quote properties directly
525
+ * console.log(quote.quote) // bigint (quoted amount)
526
+ * console.log(quote.fromAssetId) // number
527
+ * console.log(quote.toAssetId) // number
528
+ *
529
+ * // Access metadata
530
+ * console.log(quote.amount) // bigint (original request amount)
531
+ * console.log(quote.address) // string | undefined
532
+ * console.log(quote.createdAt) // number
533
+ *
534
+ * // Use convenience methods for derived data
535
+ * const minReceivedAmount = quote.getSlippageAmount(slippage) // fixed-input swap
536
+ * const maxSentAmount = quote.getSlippageAmount(slippage) // fixed-output swap
537
+ * ```
538
+ */
539
+ declare class DeflexQuote {
540
+ private readonly _response;
541
+ private readonly _amount;
542
+ private readonly _address?;
543
+ private readonly _createdAt;
544
+ /**
545
+ * Create a new DeflexQuote instance
546
+ *
547
+ * Note: Most developers should use DeflexClient.newQuote() instead of constructing
548
+ * this directly, as the factory method handles fetching the quote automatically.
549
+ *
550
+ * @param config - Configuration for the quote instance
551
+ * @param config.response - The raw quote response from the Deflex API
552
+ * @param config.amount - The original amount from the quote request
553
+ * @param config.address - Optional address parameter from the quote request
554
+ */
555
+ constructor(config: DeflexQuoteConfig);
556
+ /**
557
+ * Get the raw quote response from the Deflex API
558
+ *
559
+ * @returns The raw quote response from the Deflex API
560
+ */
561
+ get response(): FetchQuoteResponse;
562
+ /**
563
+ * The original amount from the quote request (in base units)
564
+ *
565
+ * This is the amount that was provided when fetching the quote.
566
+ * For fixed-input swaps, this is the input amount.
567
+ * For fixed-output swaps, this is the desired output amount.
568
+ *
569
+ * @example
570
+ * ```typescript
571
+ * const quote = await deflex.newQuote({
572
+ * fromAssetId: 0,
573
+ * toAssetId: 31566704,
574
+ * amount: 1_000_000, // 1 ALGO
575
+ * type: 'fixed-input'
576
+ * })
577
+ * console.log(quote.amount) // 1000000n (input amount)
578
+ * console.log(quote.quote) // 5000000n (output amount quoted)
579
+ * ```
580
+ */
581
+ get amount(): bigint;
582
+ /**
583
+ * The address parameter from the quote request
584
+ *
585
+ * This is the address that was provided when fetching the quote (if any).
586
+ * Useful for tracking which account the quote was fetched for, especially
587
+ * when using autoOptIn functionality.
588
+ */
589
+ get address(): string | undefined;
590
+ /**
591
+ * Timestamp when the quote was created (in milliseconds)
592
+ *
593
+ * Can be used to determine quote freshness. Quotes may become stale
594
+ * over time as market conditions change.
595
+ *
596
+ * @example
597
+ * ```typescript
598
+ * const ageInSeconds = (Date.now() - quote.createdAt) / 1000
599
+ * if (ageInSeconds > 30) {
600
+ * console.log('Quote may be stale, consider refreshing')
601
+ * }
602
+ * ```
603
+ */
604
+ get createdAt(): number;
605
+ /**
606
+ * The quoted output amount or input amount (depending on quote type)
607
+ *
608
+ * For fixed-input swaps: This is the output amount you'll receive
609
+ * For fixed-output swaps: This is the input amount you'll need to provide
610
+ *
611
+ * @returns The quote amount as a bigint in base units
612
+ */
613
+ get quote(): bigint;
614
+ /**
615
+ * Profit information for the swap
616
+ */
617
+ get profit(): Profit;
618
+ /**
619
+ * Baseline price without fees
620
+ */
621
+ get priceBaseline(): number;
622
+ /**
623
+ * Price impact for the user
624
+ */
625
+ get userPriceImpact(): number | undefined;
626
+ /**
627
+ * Overall market price impact
628
+ */
629
+ get marketPriceImpact(): number | undefined;
630
+ /**
631
+ * USD value of input amount
632
+ */
633
+ get usdIn(): number;
634
+ /**
635
+ * USD value of output amount
636
+ */
637
+ get usdOut(): number;
638
+ /**
639
+ * The routing path(s) for the swap
640
+ */
641
+ get route(): Route[];
642
+ /**
643
+ * Flattened view of routing percentages by protocol
644
+ */
645
+ get flattenedRoute(): Record<string, number>;
646
+ /**
647
+ * Individual quotes from each DEX
648
+ */
649
+ get quotes(): DexQuote[];
650
+ /**
651
+ * App IDs that require opt-in for this swap
652
+ */
653
+ get requiredAppOptIns(): number[];
654
+ /**
655
+ * Encrypted transaction payload for generating swap transactions
656
+ */
657
+ get txnPayload(): TxnPayload | null;
658
+ /**
659
+ * Fees charged by each protocol
660
+ */
661
+ get protocolFees(): Record<string, number>;
662
+ /**
663
+ * Input asset ID
664
+ */
665
+ get fromAssetId(): number;
666
+ /**
667
+ * Output asset ID
668
+ */
669
+ get toAssetId(): number;
670
+ /**
671
+ * Quote type
672
+ */
673
+ get type(): string;
674
+ /**
675
+ * Performance timing data
676
+ */
677
+ get timing(): unknown | undefined;
678
+ /**
679
+ * Calculate the slippage-adjusted amount
680
+ *
681
+ * For fixed-input swaps: Returns the minimum amount you'll receive after slippage
682
+ * For fixed-output swaps: Returns the maximum amount you'll need to send after slippage
683
+ *
684
+ * @param slippage - Slippage tolerance as a percentage (e.g., 1 for 1%)
685
+ * @returns The slippage-adjusted amount as a bigint
686
+ *
687
+ * @example
688
+ * ```typescript
689
+ * // Fixed-input swap: 1 ALGO -> USDC
690
+ * const quote = await deflex.newQuote({
691
+ * fromAssetId: 0,
692
+ * toAssetId: 31566704,
693
+ * amount: 1_000_000,
694
+ * type: 'fixed-input'
695
+ * })
696
+ *
697
+ * // Get minimum output with 1% slippage
698
+ * const minOutput = quote.getSlippageAmount(1)
699
+ * console.log(`Minimum you'll receive: ${minOutput}`)
700
+ * ```
701
+ *
702
+ * @example
703
+ * ```typescript
704
+ * // Fixed-output swap: ALGO -> 10 USDC
705
+ * const quote = await deflex.newQuote({
706
+ * fromAssetId: 0,
707
+ * toAssetId: 31566704,
708
+ * amount: 10_000_000,
709
+ * type: 'fixed-output'
710
+ * })
711
+ *
712
+ * // Get maximum input with 1% slippage
713
+ * const maxInput = quote.getSlippageAmount(1)
714
+ * console.log(`Maximum you'll need to send: ${maxInput}`)
715
+ * ```
716
+ */
717
+ getSlippageAmount(slippage: number): bigint;
718
+ }
719
+ //#endregion
720
+ //#region src/client.d.ts
721
+ /**
722
+ * Client for interacting with the Deflex order router API
723
+ *
724
+ * The DeflexClient provides methods to fetch swap quotes and create transaction composers
725
+ * for executing swaps on the Algorand blockchain. It handles API communication, transaction
726
+ * validation, and automatic asset/app opt-in detection.
727
+ *
728
+ * @example
729
+ * ```typescript
730
+ * const deflex = new DeflexClient({
731
+ * apiKey: 'your-api-key',
732
+ * })
733
+ * ```
734
+ *
735
+ * @example
736
+ * ```typescript
737
+ * const deflex = new DeflexClient({
738
+ * apiKey: 'your-api-key',
739
+ * algodUri: 'https://mainnet-api.4160.nodely.dev/',
740
+ * algodToken: '',
741
+ * algodPort: 443,
742
+ * referrerAddress: 'REFERRER_ADDRESS...',
743
+ * feeBps: 15,
744
+ * autoOptIn: false,
745
+ * })
746
+ * ```
747
+ */
748
+ declare class DeflexClient {
749
+ private readonly baseUrl;
750
+ private readonly config;
751
+ private readonly algorand;
752
+ /**
753
+ * Create a new DeflexClient instance
754
+ *
755
+ * @param config - Configuration parameters
756
+ * @param config.apiKey - API key for Deflex API (required)
757
+ * @param config.algodUri - Algod node URI (default: https://mainnet-api.4160.nodely.dev/)
758
+ * @param config.algodToken - Algod node token (default: '')
759
+ * @param config.algodPort - Algod node port (default: 443)
760
+ * @param config.referrerAddress - Referrer address for fee sharing (receives 25% of swap fees)
761
+ * @param config.feeBps - Fee in basis points (default: 15 = 0.15%, max: 300 = 3.00%)
762
+ * @param config.autoOptIn - Automatically detect and add required opt-in transactions (default: false)
763
+ */
764
+ constructor(config: DeflexConfigParams);
765
+ /**
766
+ * Fetch a swap quote from the Deflex API
767
+ *
768
+ * Requests optimal swap routing from the Deflex API. The quote includes routing
769
+ * information, price impact, required opt-ins, and an encrypted transaction payload.
770
+ *
771
+ * @param params - Parameters for the quote request
772
+ * @param params.fromASAID - The ID of the asset to swap from
773
+ * @param params.toASAID - The ID of the asset to swap to
774
+ * @param params.amount - The amount of the asset to swap in base units
775
+ * @param params.type - The type of the quote (default: 'fixed-input')
776
+ * @param params.disabledProtocols - List of protocols to disable (default: [])
777
+ * @param params.maxGroupSize - The maximum group size (default: 16)
778
+ * @param params.maxDepth - The maximum depth (default: 4)
779
+ * @param params.address - The address of the account that will perform the swap (recommended when using `config.autoOptIn` or `params.optIn`)
780
+ * @param params.optIn - Whether to include asset opt-in transaction
781
+ * - If true: API reduces maxGroupSize by 1 and includes opt-in (always included, even if not needed)
782
+ * - If false: No opt-in transaction included
783
+ * - If undefined: Falls back to `config.autoOptIn` behavior with account check (if `params.address` is provided)
784
+ * @returns A FetchQuoteResponse object with routing information
785
+ *
786
+ * @example
787
+ * ```typescript
788
+ * const quote = await deflex.fetchQuote({
789
+ * address: 'ABC...',
790
+ * fromASAID: 0, // ALGO
791
+ * toASAID: 31566704, // USDC
792
+ * amount: 1_000_000, // 1 ALGO
793
+ * })
794
+ * ```
795
+ */
796
+ fetchQuote(params: FetchQuoteParams): Promise<FetchQuoteResponse>;
797
+ /**
798
+ * Check if asset opt-in is required for the output asset
799
+ *
800
+ * Convenience method to determine if an address needs to opt into the output asset
801
+ * of a swap. This is useful when you want to get a quote without requiring wallet
802
+ * connection upfront, but need to know whether to set `optIn: true` in fetchQuote()
803
+ * to ensure proper routing (as opt-ins reduce maxGroupSize by 1).
804
+ *
805
+ * Note: If you enable `config.autoOptIn`, this check is handled automatically when
806
+ * an address is provided to fetchQuote().
807
+ *
808
+ * @param address - The address to check
809
+ * @param assetId - The output asset ID to check
810
+ * @returns True if asset opt-in is required, false otherwise (always false for ALGO)
811
+ *
812
+ * @example
813
+ * ```typescript
814
+ * // Check if opt-in needed for output asset before fetching quote
815
+ * const needsOptIn = await deflex.needsAssetOptIn(userAddress, toAssetId)
816
+ * const quote = await deflex.fetchQuote({
817
+ * fromAssetId,
818
+ * toAssetId,
819
+ * amount,
820
+ * optIn: needsOptIn,
821
+ * })
822
+ * ```
823
+ */
824
+ needsAssetOptIn(address: string, assetId: number | bigint): Promise<boolean>;
825
+ /**
826
+ * Fetch swap transactions from the Deflex API
827
+ *
828
+ * Decrypts the quote payload and generates executable swap transactions for the
829
+ * specified signer address with the given slippage tolerance.
830
+ *
831
+ * @param params - Parameters for the swap transaction request
832
+ * @param params.quote - The quote response from fetchQuote()
833
+ * @param params.address - The address of the signer
834
+ * @param params.slippage - The slippage tolerance as a percentage (e.g., 1 for 1%)
835
+ * @returns A FetchSwapTxnsResponse object with transaction data
836
+ */
837
+ fetchSwapTransactions(params: FetchSwapTxnsParams): Promise<FetchSwapTxnsResponse>;
838
+ /**
839
+ * Fetch a quote and return a DeflexQuote wrapper instance
840
+ *
841
+ * This is the recommended way to fetch quotes. It wraps the raw API response
842
+ * in a DeflexQuote class that provides convenient methods for accessing quote data.
843
+ *
844
+ * @param params - Parameters for the quote request
845
+ * @param params.fromAssetId - The ID of the asset to swap from
846
+ * @param params.toAssetId - The ID of the asset to swap to
847
+ * @param params.amount - The amount of the asset to swap in base units
848
+ * @param params.type - The type of the quote (default: 'fixed-input')
849
+ * @param params.disabledProtocols - List of protocols to disable (default: [])
850
+ * @param params.maxGroupSize - The maximum group size (default: 16)
851
+ * @param params.maxDepth - The maximum depth (default: 4)
852
+ * @param params.address - The address of the account that will perform the swap (recommended when using `config.autoOptIn` or `params.optIn`)
853
+ * @param params.optIn - Whether to include asset opt-in transaction
854
+ * @returns A DeflexQuote instance with convenience methods
855
+ *
856
+ * @example
857
+ * ```typescript
858
+ * const quote = await deflex.newQuote({
859
+ * address: 'ABC...',
860
+ * fromAssetId: 0,
861
+ * toAssetId: 31566704,
862
+ * amount: 1_000_000,
863
+ * })
864
+ *
865
+ * // Access quote data
866
+ * console.log(quote.quote) // bigint (quoted amount)
867
+ * console.log(quote.fromAssetId) // number
868
+ * console.log(quote.toAssetId) // number
869
+ *
870
+ * // Access metadata
871
+ * console.log(quote.amount) // bigint (original request amount)
872
+ * console.log(quote.address) // string | undefined
873
+ * console.log(quote.createdAt) // number
874
+ *
875
+ * // Use convenience methods
876
+ * const minReceivedAmount = quote.getSlippageAmount(slippage) // fixed-input swap
877
+ * const maxSentAmount = quote.getSlippageAmount(slippage) // fixed-output swap
878
+ * ```
879
+ */
880
+ newQuote(params: QuoteParams): Promise<DeflexQuote>;
881
+ /**
882
+ * Create a SwapComposer instance
883
+ *
884
+ * This factory method creates a composer that allows you to add custom transactions
885
+ * before and after the swap transactions, with automatic handling of pre-signed transactions
886
+ * and opt-ins.
887
+ *
888
+ * @param config.quote - The quote response from fetchQuote() or a DeflexQuote instance
889
+ * @param config.address - The address of the signer
890
+ * @param config.slippage - The slippage tolerance
891
+ * @param config.signer - Transaction signer function
892
+ * @returns A SwapComposer instance ready for building transaction groups
893
+ *
894
+ * @example
895
+ * ```typescript
896
+ * // Basic swap
897
+ * const quote = await deflex.newQuote({ ... })
898
+ * await deflex.newSwap({ quote, address, slippage, signer })
899
+ * .execute()
900
+ * ```
901
+ *
902
+ * @example
903
+ * ```typescript
904
+ * // Advanced swap with custom transactions
905
+ * const quote = await deflex.newQuote({ ... })
906
+ * const swap = await deflex.newSwap({
907
+ * quote,
908
+ * address,
909
+ * slippage,
910
+ * signer,
911
+ * })
912
+ *
913
+ * console.log(swap.getStatus()) // BUILDING
914
+ *
915
+ * const signedTxns = await swap
916
+ * .addTransaction(beforeTxn)
917
+ * .addSwapTransactions() // Adds swap transactions to the group
918
+ * .addTransaction(afterTxn)
919
+ * .sign()
920
+ *
921
+ * console.log(swap.getStatus()) // SIGNED
922
+ *
923
+ * const result = await swap.execute(waitRounds)
924
+ * console.log(result.confirmedRound, result.txIds)
925
+ *
926
+ * console.log(swap.getStatus()) // COMMITTED
927
+ * ```
928
+ */
929
+ newSwap(config: {
930
+ quote: DeflexQuote | FetchQuoteResponse;
931
+ address: string;
932
+ slippage: number;
933
+ signer: SignerFunction;
934
+ }): Promise<SwapComposer>;
935
+ /**
936
+ * Validates the API key
937
+ */
938
+ private validateApiKey;
939
+ /**
940
+ * Validates an Algorand address
941
+ */
942
+ private validateAddress;
943
+ /**
944
+ * Validates the fee in basis points (max 300 = 3.00%)
945
+ */
946
+ private validateFeeBps;
947
+ }
948
+ //#endregion
949
+ //#region src/utils.d.ts
950
+ /**
951
+ * HTTP error with status code and response data
952
+ */
953
+ declare class HTTPError extends Error {
954
+ status: number;
955
+ statusText: string;
956
+ data: unknown;
957
+ constructor(status: number, statusText: string, data: unknown);
958
+ }
959
+ /**
960
+ * Make an HTTP request and parse JSON response
961
+ *
962
+ * Simple wrapper around native fetch for API calls. Throws HTTPError for
963
+ * non-2xx responses.
964
+ *
965
+ * @param url - The URL to request
966
+ * @param options - Fetch options
967
+ * @returns Parsed JSON response
968
+ * @throws HTTPError if the response status is not ok
969
+ */
970
+ declare function request<T>(url: string, options?: RequestInit): Promise<T>;
971
+ //#endregion
972
+ export { Asset, DEFAULT_ALGOD_PORT, DEFAULT_ALGOD_TOKEN, DEFAULT_ALGOD_URI, DEFAULT_API_BASE_URL, DEFAULT_AUTO_OPT_IN, DEFAULT_CONFIRMATION_ROUNDS, DEFAULT_FEE_BPS, DEFAULT_MAX_DEPTH, DEFAULT_MAX_GROUP_SIZE, DEPRECATED_PROTOCOLS, DeflexClient, DeflexConfig, DeflexConfigParams, DeflexQuote, DeflexQuoteConfig, DeflexSignature, DeflexTransaction, DexQuote, FetchQuoteParams, FetchQuoteResponse, FetchSwapTxnsBody, FetchSwapTxnsParams, FetchSwapTxnsResponse, HTTPError, MAX_FEE_BPS, PathElement, Profit, Protocol, QuoteParams, QuoteType, Route, SignerFunction, SwapComposer, SwapComposerConfig, SwapComposerStatus, SwapTransaction, TxnPayload, request };
973
+ //# sourceMappingURL=index.d.ts.map