-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms.txt
More file actions
408 lines (342 loc) · 12.6 KB
/
llms.txt
File metadata and controls
408 lines (342 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Soroswap SDK - Complete Documentation
## Overview
The Soroswap SDK is the official TypeScript SDK for Soroswap.Finance, a comprehensive DEX (Decentralized Exchange) and exchange aggregator built on the Stellar network using Soroban smart contracts. This SDK provides server-side access to all Soroswap functionality including trading operations, liquidity management, market data retrieval, and system information.
## Architecture
### Core Components
#### 1. SoroswapSDK (Main SDK Class)
- **Location**: `src/soroswap-sdk.ts`
- **Purpose**: Main orchestrator class that provides access to all Soroswap API functionality
- **Key Features**:
- API key-based authentication
- Centralized HTTP client management
- Network-aware operations (MAINNET/TESTNET)
- Type-safe method signatures
- Asset list transformation utilities
#### 2. HttpClient
- **Location**: `src/clients/http-client.ts`
- **Purpose**: Centralized HTTP communication with error handling and authentication
- **Key Features**:
- Automatic Bearer token authentication
- BigInt serialization support
- Comprehensive error transformation
- Request/response interceptors
- URL query parameter building
#### 3. Type System
- **Location**: `src/types/`
- **Purpose**: Comprehensive TypeScript type definitions for all SDK operations
- **Components**:
- `common.ts`: Core enums and configuration types
- `quote.ts`: Trading and quote-related types
- `pools.ts`: Pool and liquidity operation types
- `assets.ts`: Asset and asset list types
- `price.ts`: Price data types
- `send.ts`: Transaction submission types
## Authentication System
### API Key Authentication
- **Method**: Bearer token authentication
- **Format**: API keys must start with `sk_`
- **Usage**: Set once during SDK initialization
- **Security**: Server-side only, never expose to frontend
### Configuration
```typescript
interface SoroswapSDKConfig {
apiKey: string; // Required: API key starting with 'sk_'
baseUrl?: string; // Optional: Custom API base URL
defaultNetwork?: SupportedNetworks; // Optional: MAINNET or TESTNET
timeout?: number; // Optional: Request timeout (default: 30000ms)
}
```
## API Operations
### 1. Trading Operations
#### Get Protocols
- **Method**: `getProtocols(network?: SupportedNetworks)`
- **Purpose**: Retrieve available trading protocols
- **Returns**: `string[]` - Array of protocol names
- **Example**: `['sdex', 'soroswap', 'phoenix', 'aqua']`
#### Get Quote
- **Method**: `quote(quoteRequest: QuoteRequest, network?: SupportedNetworks)`
- **Purpose**: Get trading quote for token swap
- **Parameters**:
- `assetIn`: Source token contract address
- `assetOut`: Destination token contract address
- `amount`: Amount as BigInt (required)
- `tradeType`: EXACT_IN or EXACT_OUT
- `protocols`: Array of protocols to use
- `parts`: Number of parts for trade splitting (optional)
- `slippageBps`: Slippage tolerance in basis points (optional)
- `maxHops`: Maximum number of hops (optional)
- `assetList`: Supported asset lists (optional)
- `feeBps`: Platform fee in basis points (optional)
- `gaslessTrustline`: Enable gasless trustline creation (optional)
- **Returns**: `QuoteResponse` - Detailed quote information
#### Build Transaction
- **Method**: `build(buildQuoteRequest: BuildQuoteRequest, network?: SupportedNetworks)`
- **Purpose**: Convert quote into executable XDR transaction
- **Parameters**:
- `quote`: Quote response from previous call
- `from`: Source wallet address (optional)
- `to`: Destination wallet address (optional)
- `referralId`: Referral wallet address (optional)
- **Returns**: `BuildQuoteResponse` - Contains XDR string
#### Send Transaction
- **Method**: `send(xdr: string, network?: SupportedNetworks)`
- **Purpose**: Submit signed transaction to network
- **Parameters**:
- `xdr`: Signed transaction XDR string
- `network`: Network override (optional)
- **Returns**: Transaction result
### 2. Pool Operations
#### Get Pools
- **Method**: `getPools(network: SupportedNetworks, protocols: string[], assetList?: (SupportedAssetLists | string)[])`
- **Purpose**: Retrieve pools for specific protocols
- **Parameters**:
- `network`: Target network (MAINNET/TESTNET)
- `protocols`: Array of protocol names
- `assetList`: Optional asset list filter
- **Returns**: `Pool[]` - Array of pool information
#### Get Pool by Tokens
- **Method**: `getPoolByTokens(assetA: string, assetB: string, network: SupportedNetworks, protocols: string[])`
- **Purpose**: Get specific pool for token pair
- **Parameters**:
- `assetA`: First token contract address
- `assetB`: Second token contract address
- `network`: Target network
- `protocols`: Array of protocol names
- **Returns**: `Pool[]` - Array of matching pools
### 3. Liquidity Operations
#### Add Liquidity
- **Method**: `addLiquidity(liquidityData: AddLiquidityRequest, network?: SupportedNetworks)`
- **Purpose**: Add liquidity to existing pool
- **Parameters**:
- `assetA`: First token contract address
- `assetB`: Second token contract address
- `amountA`: Amount of first token (BigInt)
- `amountB`: Amount of second token (BigInt)
- `to`: Recipient address
- `slippageBps`: Slippage tolerance (optional)
- **Returns**: `LiquidityResponse` - Transaction XDR and pool info
#### Remove Liquidity
- **Method**: `removeLiquidity(liquidityData: RemoveLiquidityRequest, network?: SupportedNetworks)`
- **Purpose**: Remove liquidity from pool
- **Parameters**:
- `assetA`: First token contract address
- `assetB`: Second token contract address
- `liquidity`: LP token amount to remove (BigInt)
- `amountA`: Minimum amount of first token (BigInt)
- `amountB`: Minimum amount of second token (BigInt)
- `to`: Recipient address
- `slippageBps`: Slippage tolerance (optional)
- **Returns**: `LiquidityResponse` - Transaction XDR and pool info
#### Get User Positions
- **Method**: `getUserPositions(address: string, network?: SupportedNetworks)`
- **Purpose**: Retrieve user's liquidity positions
- **Parameters**:
- `address`: User wallet address
- `network`: Target network (optional)
- **Returns**: `UserPosition[]` - Array of user positions
### 4. Market Data Operations
#### Get Asset Lists
- **Method**: `getAssetList(name?: SupportedAssetLists)`
- **Purpose**: Get asset list metadata or specific asset list
- **Parameters**:
- `name`: Specific asset list name (optional)
- **Returns**: `AssetList[]` or `AssetListInfo[]` depending on parameters
#### Get Asset Prices
- **Method**: `getPrice(assets: string | string[], network?: SupportedNetworks)`
- **Purpose**: Retrieve current asset prices
- **Parameters**:
- `assets`: Single asset or array of asset addresses
- `network`: Target network (optional)
- **Returns**: `PriceData[]` - Array of price information
### 5. System Operations
#### Get Contract Address
- **Method**: `getContractAddress(network: SupportedNetworks, contractName: 'factory' | 'router' | 'aggregator')`
- **Purpose**: Get contract addresses for specific network
- **Parameters**:
- `network`: Target network
- `contractName`: Contract type
- **Returns**: `{address: string}` - Contract address
## Type Definitions
### Core Enums
#### SupportedNetworks
- `MAINNET`: Production Stellar network
- `TESTNET`: Test Stellar network
#### SupportedProtocols
- `SOROSWAP`: Soroswap protocol
- `PHOENIX`: Phoenix protocol
- `AQUA`: Aqua protocol
- `SDEX`: Stellar DEX
#### SupportedAssetLists
- `SOROSWAP`: Soroswap token list
- `STELLAR_EXPERT`: Stellar Expert top 50 assets
- `LOBSTR`: Lobstr curated assets
- `AQUA`: Aqua pooled tokens
#### TradeType
- `EXACT_IN`: Exact input amount trading
- `EXACT_OUT`: Exact output amount trading
#### SupportedPlatforms
- `SDEX`: Stellar DEX platform
- `AGGREGATOR`: Aggregator platform
- `ROUTER`: Router platform
### Request/Response Types
#### QuoteRequest
```typescript
interface QuoteRequest {
assetIn: string;
assetOut: string;
amount: bigint;
tradeType: TradeType;
protocols: SupportedProtocols[];
parts?: number;
slippageBps?: number;
maxHops?: number;
assetList?: (SupportedAssetLists | string)[];
feeBps?: number;
gaslessTrustline?: GaslessTrustlineType;
}
```
#### QuoteResponse
Union type supporting both EXACT_IN and EXACT_OUT trades:
```typescript
type QuoteResponse = ExactInQuoteResponse | ExactOutQuoteResponse;
interface BaseQuoteResponse {
assetIn: string;
amountIn: bigint;
assetOut: string;
amountOut: bigint;
otherAmountThreshold: bigint;
priceImpactPct: string;
platform: SupportedPlatforms;
routePlan: RoutePlan[];
trustlineInfo?: TrustlineInfo;
platformFee?: PlatformFee;
gaslessTrustline?: GaslessTrustlineType;
}
```
#### Pool
```typescript
interface Pool {
protocol: SupportedProtocols;
address: string;
tokenA: string;
tokenB: string;
reserveA: bigint;
reserveB: bigint;
ledger?: number;
reserveLp?: bigint;
stakeAddress?: string;
poolType?: string;
fee?: bigint;
totalFeeBps?: number;
// ... additional pool-specific fields
}
```
## Error Handling
### HTTP Client Error Transformation
The SDK transforms all HTTP errors into structured error objects:
```typescript
interface APIError {
message: string;
statusCode: number;
timestamp: string;
path?: string;
}
```
### Error Types
1. **Server Response Errors**: HTTP 4xx/5xx responses
2. **Network Errors**: No response received
3. **Request Errors**: Invalid request configuration
4. **Authentication Errors**: Invalid API key
### Best Practices
- Always wrap SDK calls in try-catch blocks
- Check error status codes for specific handling
- Log errors appropriately for debugging
- Handle network timeouts gracefully
## Development Configuration
### Build System
- **TypeScript**: Target ES2020, CommonJS modules
- **Output**: `dist/` directory with declaration files
- **Source Maps**: Generated for debugging
- **Strict Mode**: Enabled for type safety
### Testing Strategy
#### Unit Tests
- **Location**: `tests/`
- **Framework**: Jest
- **Approach**: Mock all external dependencies
- **Coverage**: Comprehensive coverage of SDK logic
- **Command**: `pnpm test`
#### Integration Tests
- **Location**: `tests/integration/`
- **Framework**: Jest with custom config
- **Approach**: Test against real Soroswap API
- **Requirements**: Valid API key in environment
- **Command**: `pnpm run test:integration`
### Code Quality
- **ESLint**: TypeScript-specific rules
- **Prettier**: Code formatting (implied)
- **Pre-commit**: Tests and linting before publish
## Security Considerations
### API Key Security
- Store API keys in environment variables
- Never commit API keys to version control
- Use different keys for different environments
- Rotate keys regularly
### Server-Side Only
- SDK designed for server-side use only
- API keys should never be exposed to frontend
- Create proxy endpoints for frontend integration
### Amount Handling
- All trading amounts must be BigInt
- Proper serialization in HTTP client
- Type safety enforced at TypeScript level
## Performance Considerations
### HTTP Client
- Connection pooling via axios
- Configurable timeouts
- Request/response interceptors
- Automatic retry logic (implicit)
### BigInt Serialization
- Custom JSON serialization for BigInt values
- Automatic conversion to strings for API
### Asset List Transformation
- Efficient enum-to-string conversion
- Support for both enum and string inputs
- Minimal processing overhead
## Examples and Use Cases
### Basic Trading Flow
1. Initialize SDK with API key
2. Get available protocols
3. Create quote request with BigInt amount
4. Get quote from API
5. Build transaction XDR
6. Sign transaction with external signer
7. Send signed transaction
### Liquidity Management Flow
1. Get existing pools to understand ratios
2. Calculate proportional amounts
3. Create liquidity request
4. Build transaction XDR
5. Sign and send transaction
### Market Data Retrieval
1. Get asset lists for token information
2. Retrieve current prices
3. Monitor pool reserves and ratios
4. Track user positions
## Package Information
### Dependencies
- **axios**: HTTP client library
- **TypeScript**: Type definitions and compilation
- **Jest**: Testing framework
- **ESLint**: Code linting
### Package Details
- **Name**: `@soroswap/sdk`
- **Version**: 0.3.2
- **License**: MIT
- **Node.js**: >=16.0.0
- **Main**: `dist/index.js`
- **Types**: `dist/index.d.ts`
### Repository
- **GitHub**: https://github.com/soroswap/sdk
- **Homepage**: https://soroswap.finance
- **Documentation**: https://docs.soroswap.finance
This SDK provides a comprehensive, type-safe interface to the Soroswap ecosystem, enabling developers to build sophisticated DeFi applications on the Stellar network with confidence and ease.