Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit e01f8ed

Browse files
committed
docs: enhance README with detailed features and usage examples
1 parent 0d8ce88 commit e01f8ed

File tree

2 files changed

+311
-1
lines changed

2 files changed

+311
-1
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
pnpm-lock.yaml
4+
README.md

README.md

Lines changed: 310 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,318 @@ TypeScript/JavaScript SDK for [Deflex Order Router](https://txnlab.gitbook.io/de
88
npm install @txnlab/deflex
99
```
1010

11+
## Features
12+
13+
- 🔄 **Smart Order Routing** - Automatically finds the best swap routes across multiple DEXs
14+
- 💰 **Fee Sharing** - Optional referrer address to earn 25% of swap fees
15+
- 🎯 **Slippage Protection** - Built-in slippage tolerance controls
16+
- 🔐 **Type Safety** - Full TypeScript support with comprehensive type definitions
17+
-**Atomic Swaps** - All swaps execute atomically with automatic opt-in handling
18+
- 🛠️ **Transaction Composer** - Flexible API for building complex transaction groups
19+
20+
## Quick Start
21+
22+
```typescript
23+
import { DeflexClient } from '@txnlab/deflex'
24+
import algosdk from 'algosdk'
25+
26+
// Initialize the client
27+
const deflex = new DeflexClient({
28+
apiKey: 'your-api-key',
29+
})
30+
31+
// Get a quote for swapping 1 ALGO to USDC
32+
const quote = await deflex.fetchQuote({
33+
address: 'YOUR_ADDRESS...',
34+
fromAssetId: 0, // ALGO
35+
toAssetId: 31566704, // USDC
36+
amount: 1_000_000, // 1 ALGO (in microAlgos)
37+
})
38+
39+
console.log(`You will receive approximately ${quote.quote} USDC`)
40+
41+
// Execute the swap
42+
const signer = algosdk.makeBasicAccountTransactionSigner(account)
43+
const swap = await deflex.newSwap({
44+
quote,
45+
address: account.addr,
46+
slippage: 1, // 1% slippage tolerance
47+
})
48+
const result = await swap.execute(signer)
49+
50+
console.log(`Swap completed in round ${result.confirmedRound}`)
51+
```
52+
1153
## Usage
1254

13-
Documentation and examples coming soon.
55+
### Initialize the Client
56+
57+
```typescript
58+
import { DeflexClient } from '@txnlab/deflex'
59+
60+
// Basic initialization
61+
const deflex = new DeflexClient({
62+
apiKey: 'your-api-key',
63+
})
64+
65+
// Advanced configuration
66+
const deflex = new DeflexClient({
67+
apiKey: 'your-api-key',
68+
algodUri: 'https://mainnet-api.4160.nodely.dev/',
69+
algodToken: '',
70+
algodPort: 443,
71+
referrerAddress: 'REFERRER_ADDRESS...', // Earns 25% of swap fees
72+
feeBps: 15, // 0.15% fee (max: 300 = 3%)
73+
autoOptIn: true, // Automatically handle asset opt-ins
74+
})
75+
```
76+
77+
### Get a Swap Quote
78+
79+
```typescript
80+
// Basic quote
81+
const quote = await deflex.fetchQuote({
82+
fromAssetId: 0, // ALGO
83+
toAssetId: 31566704, // USDC
84+
amount: 1_000_000, // 1 ALGO
85+
address: userAddress, // Required for auto opt-in detection
86+
})
87+
88+
// Fixed-output quote (specify exact output amount)
89+
const quote = await deflex.fetchQuote({
90+
fromAssetId: 0,
91+
toAssetId: 31566704,
92+
amount: 5_000_000, // Receive exactly 5 USDC
93+
type: 'fixed-output',
94+
address: userAddress,
95+
})
96+
97+
// Advanced routing options
98+
const quote = await deflex.fetchQuote({
99+
fromAssetId: 0,
100+
toAssetId: 31566704,
101+
amount: 1_000_000,
102+
address: userAddress,
103+
maxGroupSize: 16, // Maximum transactions in atomic group
104+
maxDepth: 4, // Maximum number of swap hops
105+
disabledProtocols: ['Algofi'], // Exclude specific protocols
106+
})
107+
```
108+
109+
### Execute a Swap
110+
111+
```typescript
112+
import algosdk from 'algosdk'
113+
114+
// Using algosdk signer
115+
const account = algosdk.generateAccount()
116+
const signer = algosdk.makeBasicAccountTransactionSigner(account)
117+
118+
const swap = await deflex.newSwap({
119+
quote,
120+
address: account.addr,
121+
slippage: 1, // 1% slippage tolerance
122+
})
123+
const result = await swap.execute(signer)
124+
125+
console.log(`Confirmed in round ${result.confirmedRound}`)
126+
console.log('Transaction IDs:', result.txIds)
127+
```
128+
129+
### Advanced Transaction Composition
130+
131+
Build complex transaction groups by adding custom transactions before and after swaps:
132+
133+
```typescript
134+
import { Transaction } from 'algosdk'
135+
136+
// Create your custom transactions
137+
const customTxn1 = new Transaction({...})
138+
const customTxn2 = new Transaction({...})
139+
140+
// Build and execute the transaction group
141+
const swap = await deflex.newSwap({
142+
quote,
143+
address: account.addr,
144+
slippage: 1,
145+
})
146+
147+
const result = await swap
148+
.addTransaction(customTxn1) // Add transaction before swap
149+
.addSwapTransactions() // Add swap transactions
150+
.addTransaction(customTxn2) // Add transaction after swap
151+
.execute(signer) // Sign and execute entire group
152+
```
153+
154+
### Transaction Group Lifecycle
155+
156+
The `SwapComposer` provides fine-grained control over the transaction lifecycle:
157+
158+
```typescript
159+
const swap = await deflex.newSwap({ quote, address, slippage })
160+
161+
console.log(swap.getStatus()) // BUILDING
162+
163+
// Add transactions to the group
164+
await swap
165+
.addTransaction(customTxn)
166+
.addSwapTransactions()
167+
168+
console.log(swap.count()) // Total number of transactions
169+
170+
// Sign the group
171+
const signedTxns = await swap.sign(signer)
172+
console.log(swap.getStatus()) // SIGNED
173+
174+
// Submit to network (without waiting for confirmation)
175+
const txIds = await swap.submit(signer)
176+
console.log(swap.getStatus()) // SUBMITTED
177+
178+
// Or execute (sign + submit + wait for confirmation)
179+
const result = await swap.execute(signer)
180+
console.log(swap.getStatus()) // COMMITTED
181+
```
182+
183+
### Manual Asset Opt-In Detection
184+
185+
If you're not using `autoOptIn: true`, you can manually check if opt-in is needed:
186+
187+
```typescript
188+
const deflex = new DeflexClient({
189+
apiKey: 'your-api-key',
190+
autoOptIn: false,
191+
})
192+
193+
// Check if user needs to opt into the output asset
194+
const needsOptIn = await deflex.needsAssetOptIn(userAddress, toAssetId)
195+
196+
// Include opt-in in quote if needed
197+
const quote = await deflex.fetchQuote({
198+
fromAssetId,
199+
toAssetId,
200+
amount,
201+
optIn: needsOptIn,
202+
})
203+
```
204+
205+
### Inspecting Quote Details
206+
207+
```typescript
208+
const quote = await deflex.fetchQuote({...})
209+
210+
// Output amount
211+
console.log(`Quote: ${quote.quote}`)
212+
213+
// Price impact
214+
console.log(`User price impact: ${quote.userPriceImpact}%`)
215+
console.log(`Market price impact: ${quote.marketPriceImpact}%`)
216+
217+
// USD values
218+
console.log(`USD in: $${quote.usdIn}`)
219+
console.log(`USD out: $${quote.usdOut}`)
220+
221+
// Routing information
222+
console.log('Route:', quote.route)
223+
console.log('Flattened route:', quote.flattenedRoute)
224+
225+
// Required opt-ins
226+
console.log('Required app opt-ins:', quote.requiredAppOptIns)
227+
228+
// Protocol fees
229+
console.log('Protocol fees:', quote.protocolFees)
230+
```
231+
232+
### Error Handling
233+
234+
```typescript
235+
try {
236+
const quote = await deflex.fetchQuote({
237+
fromAssetId: 0,
238+
toAssetId: 31566704,
239+
amount: 1_000_000,
240+
address: userAddress,
241+
})
242+
243+
const swap = await deflex.newSwap({
244+
quote,
245+
address: userAddress,
246+
slippage: 1,
247+
})
248+
const result = await swap.execute(signer)
249+
250+
console.log('Swap successful:', result)
251+
} catch (error) {
252+
console.error('Swap failed:', error.message)
253+
}
254+
```
255+
256+
## API Reference
257+
258+
### DeflexClient
259+
260+
The main client for interacting with the Deflex API.
261+
262+
#### Constructor Options
263+
264+
- `apiKey` (required): Your Deflex API key
265+
- `algodUri`: Algod node URI (default: `https://mainnet-api.4160.nodely.dev/`)
266+
- `algodToken`: Algod node token (default: `''`)
267+
- `algodPort`: Algod node port (default: `443`)
268+
- `referrerAddress`: Referrer address for fee sharing (receives 25% of swap fees)
269+
- `feeBps`: Fee in basis points (default: `15` = 0.15%, max: `300` = 3.00%)
270+
- `autoOptIn`: Auto-detect and add required opt-in transactions (default: `false`)
271+
272+
#### Methods
273+
274+
##### `fetchQuote(params)`
275+
276+
Fetch a swap quote from the Deflex API.
277+
278+
##### `fetchSwapTransactions(params)`
279+
280+
Fetch executable swap transactions for a quote.
281+
282+
##### `newSwap(config)`
283+
284+
Create a `SwapComposer` instance for building and executing swaps.
285+
286+
##### `needsAssetOptIn(address, assetId)`
287+
288+
Check if an address needs to opt into an asset.
289+
290+
### SwapComposer
291+
292+
Builder for constructing and executing atomic swap transaction groups.
293+
294+
#### Methods
295+
296+
##### `addTransaction(transaction)`
297+
298+
Add a transaction to the atomic group.
299+
300+
##### `addSwapTransactions()`
301+
302+
Add swap transactions to the group (automatically includes required app opt-ins).
303+
304+
##### `sign(signer)`
305+
306+
Sign the transaction group.
307+
308+
##### `submit(signer)`
309+
310+
Sign and submit the transaction group to the network.
311+
312+
##### `execute(signer, waitRounds?)`
313+
314+
Sign, submit, and wait for confirmation.
315+
316+
##### `getStatus()`
317+
318+
Get the current status: `BUILDING`, `BUILT`, `SIGNED`, `SUBMITTED`, or `COMMITTED`.
319+
320+
##### `count()`
321+
322+
Get the number of transactions in the group.
14323

15324
## Documentation
16325

0 commit comments

Comments
 (0)