@cetusprotocol/dlmm-sdk 0.0.4 → 0.0.6
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.
- package/README.md +376 -51
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/.turbo/turbo-build.log +0 -10485
- package/src/config/index.ts +0 -2
- package/src/config/mainnet.ts +0 -25
- package/src/config/testnet.ts +0 -30
- package/src/errors/errors.ts +0 -40
- package/src/index.ts +0 -8
- package/src/modules/configModule.ts +0 -184
- package/src/modules/index.ts +0 -1
- package/src/modules/partnerModule.ts +0 -302
- package/src/modules/poolModule.ts +0 -625
- package/src/modules/positionModule.ts +0 -901
- package/src/modules/rewardModule.ts +0 -174
- package/src/modules/swapModule.ts +0 -129
- package/src/sdk.ts +0 -88
- package/src/types/constants.ts +0 -23
- package/src/types/dlmm.ts +0 -466
- package/src/types/index.ts +0 -2
- package/src/utils/binUtils.ts +0 -552
- package/src/utils/feeUtils.ts +0 -92
- package/src/utils/index.ts +0 -5
- package/src/utils/parseData.ts +0 -556
- package/src/utils/strategyUtils.ts +0 -202
- package/src/utils/weightUtils.ts +0 -674
- package/tests/add_liquidity_bidask.test.ts +0 -192
- package/tests/add_liquidity_curve.test.ts +0 -244
- package/tests/add_liquidity_spot.test.ts +0 -314
- package/tests/bin.test.ts +0 -80
- package/tests/config.test.ts +0 -73
- package/tests/partner.test.ts +0 -74
- package/tests/pool.test.ts +0 -184
- package/tests/position.test.ts +0 -76
- package/tests/remove_liquidity.test.ts +0 -137
- package/tests/swap.test.ts +0 -96
- package/tests/tsconfig.json +0 -26
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -9
package/README.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
The SDK provides a DLMM (Dynamic Liquidity Market Maker) module for specialized liquidity operations with different modes to suit various trading strategies. This module enables users to perform complex liquidity operations with flexibility in how they want to manage their positions.
|
|
4
4
|
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Multiple Liquidity Strategies**: Spot, BidAsk, and Curve strategies for different trading approaches
|
|
8
|
+
- **Comprehensive Pool Management**: Create, query, and manage DLMM pools
|
|
9
|
+
- **Advanced Position Management**: Add/remove liquidity, collect fees and rewards
|
|
10
|
+
- **Flexible Swap Operations**: Support for both A-to-B and B-to-A swaps
|
|
11
|
+
- **Partner Integration**: Built-in partner and referral system
|
|
12
|
+
- **Rich Utility Functions**: Bin calculations, price conversions, and liquidity management tools
|
|
13
|
+
- **Multi-Network Support**: Works with both mainnet and testnet
|
|
14
|
+
|
|
5
15
|
## Getting Started
|
|
6
16
|
|
|
7
17
|
## How to Use the DLMM SDK?
|
|
@@ -26,27 +36,34 @@ import { CetusDlmmSDK } from '@cetusprotocol/dlmm-sdk'
|
|
|
26
36
|
|
|
27
37
|
Initialize the SDK with the required configuration parameters. This typically includes setting up the network if needed.
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
**Option 1: Use default mainnet configuration**
|
|
31
40
|
```typescript
|
|
32
41
|
const sdk = CetusDlmmSDK.createSDK()
|
|
33
42
|
```
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
**Option 2: Specify network environment**
|
|
37
45
|
```typescript
|
|
38
|
-
|
|
39
|
-
const
|
|
46
|
+
// For mainnet
|
|
47
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'mainnet' })
|
|
40
48
|
|
|
41
|
-
|
|
49
|
+
// For testnet
|
|
50
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'testnet' })
|
|
42
51
|
```
|
|
43
52
|
|
|
44
|
-
|
|
53
|
+
**Option 3: Use custom RPC URL**
|
|
54
|
+
```typescript
|
|
55
|
+
const sdk = CetusDlmmSDK.createSDK({
|
|
56
|
+
env: 'mainnet',
|
|
57
|
+
full_rpc_url: 'YOUR_FULL_NODE_URL'
|
|
58
|
+
})
|
|
59
|
+
```
|
|
45
60
|
|
|
61
|
+
**Option 4: Use custom SuiClient**
|
|
46
62
|
```typescript
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
63
|
+
import { SuiClient } from '@mysten/sui/client'
|
|
64
|
+
|
|
65
|
+
const suiClient = new SuiClient({ url: 'YOUR_RPC_URL' })
|
|
66
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'mainnet', sui_client: suiClient })
|
|
50
67
|
```
|
|
51
68
|
|
|
52
69
|
## Usage
|
|
@@ -55,15 +72,13 @@ After linking your wallet, if you need use your wallet address to do something,
|
|
|
55
72
|
|
|
56
73
|
```typescript
|
|
57
74
|
const wallet = 'YOUR_WALLET_ADDRESS'
|
|
58
|
-
|
|
59
75
|
sdk.setSenderAddress(wallet)
|
|
60
76
|
```
|
|
61
77
|
|
|
62
|
-
|
|
78
|
+
If you need to change your RPC URL, you can do so as follows:
|
|
63
79
|
|
|
64
80
|
```typescript
|
|
65
81
|
const new_rpc_url = 'YOUR_NEW_FULL_NODE_URL'
|
|
66
|
-
|
|
67
82
|
sdk.updateFullRpcUrl(new_rpc_url)
|
|
68
83
|
```
|
|
69
84
|
|
|
@@ -75,6 +90,53 @@ sdk.updateFullRpcUrl(new_rpc_url)
|
|
|
75
90
|
- `coin_type_a` & `coin_type_b`: Coin type identifiers for the trading pair
|
|
76
91
|
- `coin_decimal_a` & `coin_decimal_b`: Decimal places for each coin type
|
|
77
92
|
|
|
93
|
+
### Default Fee Options
|
|
94
|
+
|
|
95
|
+
The SDK provides predefined fee configurations for different types of trading pairs. These configurations are optimized based on asset volatility and market characteristics:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const dlmmDefaultFeeOptions = [
|
|
99
|
+
{ binStep: 1, baseFactor: 10000, fee: '0.0001' },
|
|
100
|
+
{ binStep: 1, baseFactor: 20000, fee: '0.0002' },
|
|
101
|
+
{ binStep: 2, baseFactor: 15000, fee: '0.0003' },
|
|
102
|
+
{ binStep: 2, baseFactor: 20000, fee: '0.0004' },
|
|
103
|
+
{ binStep: 5, baseFactor: 10000, fee: '0.0005' },
|
|
104
|
+
{ binStep: 10, baseFactor: 10000, fee: '0.001' },
|
|
105
|
+
{ binStep: 15, baseFactor: 10000, fee: '0.0015' },
|
|
106
|
+
{ binStep: 20, baseFactor: 10000, fee: '0.002' },
|
|
107
|
+
{ binStep: 25, baseFactor: 10000, fee: '0.0025' },
|
|
108
|
+
{ binStep: 30, baseFactor: 10000, fee: '0.003' },
|
|
109
|
+
{ binStep: 50, baseFactor: 8000, fee: '0.004' },
|
|
110
|
+
{ binStep: 80, baseFactor: 7500, fee: '0.006' },
|
|
111
|
+
{ binStep: 100, baseFactor: 8000, fee: '0.008' },
|
|
112
|
+
{ binStep: 100, baseFactor: 10000, fee: '0.01' },
|
|
113
|
+
{ binStep: 200, baseFactor: 10000, fee: '0.02' },
|
|
114
|
+
{ binStep: 400, baseFactor: 10000, fee: '0.04' }
|
|
115
|
+
]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Parameter Explanations:**
|
|
119
|
+
|
|
120
|
+
- **`binStep`**: The step size between bins, determining the price granularity of the pool. Smaller values provide finer price resolution but require more computational resources.
|
|
121
|
+
- **`baseFactor`**: A multiplier used in fee calculations, affecting the overall fee structure of the pool.
|
|
122
|
+
- **`fee`**: The trading fee rate as a decimal (e.g., '0.0001' = 0.01%). This is the fee charged for each swap transaction.
|
|
123
|
+
|
|
124
|
+
**Fee Tier Recommendations:**
|
|
125
|
+
|
|
126
|
+
- **Low fees (0.01% - 0.05%)**: Best for stable pairs or low-volatility mainstream assets
|
|
127
|
+
- **Medium fees (0.1% - 0.3%)**: Suitable for medium-volatility assets or mainstream trading pairs
|
|
128
|
+
- **High fees (0.4% - 4%)**: Recommended for high-volatility assets, altcoins, or small market cap pairs
|
|
129
|
+
|
|
130
|
+
When creating a new pool, choose the fee configuration that best matches your trading pair's characteristics:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// Example: Create a pool for a stable pair (USDC/USDT)
|
|
134
|
+
const stablePairConfig = { binStep: 1, baseFactor: 10000, fee: '0.0001' }
|
|
135
|
+
|
|
136
|
+
// Example: Create a pool for a volatile altcoin pair
|
|
137
|
+
const altcoinConfig = { binStep: 100, baseFactor: 10000, fee: '0.01' }
|
|
138
|
+
```
|
|
139
|
+
|
|
78
140
|
### 1. Pool Operations
|
|
79
141
|
|
|
80
142
|
#### Get Pool Information
|
|
@@ -201,24 +263,22 @@ const { feeData, rewardData } = await sdk.Position.fetchPositionFeeAndReward([
|
|
|
201
263
|
},
|
|
202
264
|
])
|
|
203
265
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
// }
|
|
221
|
-
// }
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
#### Fee Rate Calculations
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// Get total fee rate for a pool
|
|
272
|
+
const totalFeeRate = await sdk.Pool.getTotalFeeRate({
|
|
273
|
+
pool_id,
|
|
274
|
+
coin_type_a,
|
|
275
|
+
coin_type_b
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
// Get variable fee from pool parameters
|
|
279
|
+
const variableFee = FeeUtils.getVariableFee(pool.variable_parameters)
|
|
280
|
+
console.log('Variable fee:', variableFee)
|
|
281
|
+
console.log('Variable fee percentage:', d(variableFee).div(d(FEE_PRECISION)).toString())
|
|
222
282
|
```
|
|
223
283
|
|
|
224
284
|
#### Fee Collection
|
|
@@ -244,6 +304,174 @@ const sim_result = await sdk.FullClient.sendSimulationTransaction(tx, wallet)
|
|
|
244
304
|
|
|
245
305
|
#### Add Liquidity
|
|
246
306
|
|
|
307
|
+
The DLMM SDK provides two main methods for adding liquidity:
|
|
308
|
+
|
|
309
|
+
1. **`calculateAddLiquidityInfo(option)`** - Calculates the liquidity distribution across bins
|
|
310
|
+
2. **`addLiquidityPayload(option, tx?)`** - Creates the transaction payload for adding liquidity
|
|
311
|
+
|
|
312
|
+
**Method 1: `calculateAddLiquidityInfo(option: CalculateAddLiquidityOption | CalculateAddLiquidityAutoFillOption)`**
|
|
313
|
+
|
|
314
|
+
This method calculates how liquidity should be distributed across different bins based on your strategy and parameters.
|
|
315
|
+
|
|
316
|
+
**Parameters for `CalculateAddLiquidityOption`:**
|
|
317
|
+
```typescript
|
|
318
|
+
interface CalculateAddLiquidityOption {
|
|
319
|
+
pool_id: string // Pool ID
|
|
320
|
+
amount_a: string // Amount of token A to add
|
|
321
|
+
amount_b: string // Amount of token B to add
|
|
322
|
+
active_id: number // Current active bin ID
|
|
323
|
+
bin_step: number // Bin step size
|
|
324
|
+
lower_bin_id: number // Lower bound of bin range
|
|
325
|
+
upper_bin_id: number // Upper bound of bin range
|
|
326
|
+
active_bin_of_pool?: { // Active bin amounts (if active bin is in range)
|
|
327
|
+
amount_a: string
|
|
328
|
+
amount_b: string
|
|
329
|
+
}
|
|
330
|
+
strategy_type: StrategyType // Liquidity strategy (Spot, BidAsk, Curve)
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Parameters for `CalculateAddLiquidityAutoFillOption`:**
|
|
335
|
+
```typescript
|
|
336
|
+
interface CalculateAddLiquidityAutoFillOption {
|
|
337
|
+
pool_id: string // Pool ID
|
|
338
|
+
coin_amount: string // Fixed amount of one token
|
|
339
|
+
fix_amount_a: boolean // true for token A, false for token B
|
|
340
|
+
active_id: number // Current active bin ID
|
|
341
|
+
bin_step: number // Bin step size
|
|
342
|
+
lower_bin_id: number // Lower bound of bin range
|
|
343
|
+
upper_bin_id: number // Upper bound of bin range
|
|
344
|
+
active_bin_of_pool?: { // Active bin amounts (if active bin is in range)
|
|
345
|
+
amount_a: string
|
|
346
|
+
amount_b: string
|
|
347
|
+
}
|
|
348
|
+
strategy_type: StrategyType // Liquidity strategy (Spot, BidAsk, Curve)
|
|
349
|
+
}
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
**Method 2: `addLiquidityPayload(option: AddLiquidityOption | OpenAndAddLiquidityOption, tx?: Transaction)`**
|
|
353
|
+
|
|
354
|
+
This method creates the transaction payload for adding liquidity to an existing position or opening a new position.
|
|
355
|
+
|
|
356
|
+
**Parameters for `AddLiquidityOption` (existing position):**
|
|
357
|
+
```typescript
|
|
358
|
+
interface AddLiquidityOption {
|
|
359
|
+
pool_id: string // Pool ID
|
|
360
|
+
bin_infos: BinLiquidityInfo // Calculated bin distribution from calculateAddLiquidityInfo
|
|
361
|
+
coin_type_a: string // Token A coin type
|
|
362
|
+
coin_type_b: string // Token B coin type
|
|
363
|
+
active_id: number // Current active bin ID
|
|
364
|
+
position_id: string // Existing position ID
|
|
365
|
+
collect_fee: boolean // Whether to collect fees
|
|
366
|
+
reward_coins: string[] // Reward coin types
|
|
367
|
+
strategy_type: StrategyType // Liquidity strategy
|
|
368
|
+
use_bin_infos: boolean // Whether to use calculated bin_infos
|
|
369
|
+
max_price_slippage: number // Maximum price slippage tolerance
|
|
370
|
+
bin_step: number // Bin step size
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
**Parameters for `OpenAndAddLiquidityOption` (new position):**
|
|
375
|
+
```typescript
|
|
376
|
+
interface OpenAndAddLiquidityOption {
|
|
377
|
+
pool_id: string // Pool ID
|
|
378
|
+
bin_infos: BinLiquidityInfo // Calculated bin distribution from calculateAddLiquidityInfo
|
|
379
|
+
coin_type_a: string // Token A coin type
|
|
380
|
+
coin_type_b: string // Token B coin type
|
|
381
|
+
lower_bin_id: number // Lower bound of bin range
|
|
382
|
+
upper_bin_id: number // Upper bound of bin range
|
|
383
|
+
active_id: number // Current active bin ID
|
|
384
|
+
strategy_type: StrategyType // Liquidity strategy
|
|
385
|
+
use_bin_infos: boolean // Whether to use calculated bin_infos
|
|
386
|
+
max_price_slippage: number // Maximum price slippage tolerance
|
|
387
|
+
bin_step: number // Bin step size
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
**Complete Example - Adding Liquidity to Existing Position:**
|
|
392
|
+
|
|
393
|
+
```typescript
|
|
394
|
+
// Step 1: Calculate liquidity distribution
|
|
395
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
396
|
+
pool_id: '0x...',
|
|
397
|
+
amount_a: '1000000',
|
|
398
|
+
amount_b: '1200000',
|
|
399
|
+
active_id: 100,
|
|
400
|
+
bin_step: 2,
|
|
401
|
+
lower_bin_id: 90,
|
|
402
|
+
upper_bin_id: 110,
|
|
403
|
+
active_bin_of_pool: amounts_in_active_bin, // Optional: if active bin is in range
|
|
404
|
+
strategy_type: StrategyType.Spot
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const bin_infos = await sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
408
|
+
|
|
409
|
+
// Step 2: Create transaction payload
|
|
410
|
+
const addLiquidityOption: AddLiquidityOption = {
|
|
411
|
+
pool_id: '0x...',
|
|
412
|
+
bin_infos: bin_infos,
|
|
413
|
+
coin_type_a: '0x...::usdc::USDC',
|
|
414
|
+
coin_type_b: '0x...::usdt::USDT',
|
|
415
|
+
active_id: 100,
|
|
416
|
+
position_id: '0x...',
|
|
417
|
+
collect_fee: true,
|
|
418
|
+
reward_coins: [],
|
|
419
|
+
strategy_type: StrategyType.Spot,
|
|
420
|
+
use_bin_infos: false,
|
|
421
|
+
max_price_slippage: 0.01,
|
|
422
|
+
bin_step: 2
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const tx = sdk.Position.addLiquidityPayload(addLiquidityOption)
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
**Complete Example - Opening New Position:**
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
// Step 1: Calculate liquidity distribution
|
|
432
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
433
|
+
pool_id: '0x...',
|
|
434
|
+
amount_a: '1000000',
|
|
435
|
+
amount_b: '1200000',
|
|
436
|
+
active_id: 100,
|
|
437
|
+
bin_step: 2,
|
|
438
|
+
lower_bin_id: 90,
|
|
439
|
+
upper_bin_id: 110,
|
|
440
|
+
active_bin_of_pool: amounts_in_active_bin,
|
|
441
|
+
strategy_type: StrategyType.Spot
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const bin_infos = await sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
445
|
+
|
|
446
|
+
// Step 2: Create transaction payload for new position
|
|
447
|
+
const openPositionOption: OpenAndAddLiquidityOption = {
|
|
448
|
+
pool_id: '0x...',
|
|
449
|
+
bin_infos: bin_infos,
|
|
450
|
+
coin_type_a: '0x...::usdc::USDC',
|
|
451
|
+
coin_type_b: '0x...::usdt::USDT',
|
|
452
|
+
lower_bin_id: 90,
|
|
453
|
+
upper_bin_id: 110,
|
|
454
|
+
active_id: 100,
|
|
455
|
+
strategy_type: StrategyType.Spot,
|
|
456
|
+
use_bin_infos: false,
|
|
457
|
+
max_price_slippage: 0.01,
|
|
458
|
+
bin_step: 2
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const tx = sdk.Position.addLiquidityPayload(openPositionOption)
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
**Important Parameter Notes:**
|
|
465
|
+
|
|
466
|
+
- **`active_bin_of_pool`**: This parameter is crucial when the active bin falls within your position's range. Use `getActiveBinIfInRange()` to get the correct values.
|
|
467
|
+
- **`use_bin_infos`**: When `false`, the contract calculates liquidity distribution internally; when `true`, it uses the provided `bin_infos`.
|
|
468
|
+
- **`max_price_slippage`**: Protects against price movements during transaction execution (e.g., 0.01 = 1% slippage tolerance).
|
|
469
|
+
- **`collect_fee`**: Only applicable for existing positions; determines whether to collect accumulated fees when adding liquidity.
|
|
470
|
+
- **`strategy_type`**: Affects how liquidity is distributed across bins:
|
|
471
|
+
- `Spot`: Even distribution around current price
|
|
472
|
+
- `BidAsk`: Concentrated at specific price levels
|
|
473
|
+
- `Curve`: Smooth distribution following a curve
|
|
474
|
+
|
|
247
475
|
There are three strategies for adding liquidity: Spot, BidAsk, and Curve. Here's how to use each:
|
|
248
476
|
|
|
249
477
|
1. **Spot Strategy**:
|
|
@@ -263,6 +491,7 @@ const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
|
263
491
|
|
|
264
492
|
// Calculate liquidity distribution
|
|
265
493
|
const calculateOption = {
|
|
494
|
+
pool_id,
|
|
266
495
|
amount_a: '1000000',
|
|
267
496
|
amount_b: '1200000',
|
|
268
497
|
active_id,
|
|
@@ -273,30 +502,36 @@ const calculateOption = {
|
|
|
273
502
|
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
274
503
|
strategy_type: StrategyType.Spot
|
|
275
504
|
}
|
|
276
|
-
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
505
|
+
const bin_infos = await sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
277
506
|
|
|
278
507
|
// For new position
|
|
279
508
|
const addOption = {
|
|
280
509
|
pool_id,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
510
|
+
amount_a,
|
|
511
|
+
amount_b,
|
|
512
|
+
active_id,
|
|
513
|
+
bin_step,
|
|
514
|
+
lower_bin_id,
|
|
515
|
+
upper_bin_id,
|
|
516
|
+
active_bin_of_pool: amounts_in_active_bin,
|
|
517
|
+
strategy_type: StrategyType.Spot,
|
|
287
518
|
}
|
|
288
519
|
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
289
520
|
|
|
290
521
|
// For existing position
|
|
291
522
|
const addOption = {
|
|
292
523
|
pool_id,
|
|
293
|
-
bin_infos,
|
|
294
|
-
coin_type_a
|
|
295
|
-
coin_type_b
|
|
524
|
+
bin_infos: bin_infos,
|
|
525
|
+
coin_type_a,
|
|
526
|
+
coin_type_b,
|
|
296
527
|
active_id,
|
|
297
528
|
position_id,
|
|
298
529
|
collect_fee: true,
|
|
299
|
-
reward_coins: []
|
|
530
|
+
reward_coins: [],
|
|
531
|
+
strategy_type: StrategyType.Spot,
|
|
532
|
+
use_bin_infos: false,
|
|
533
|
+
max_price_slippage: 0.01,
|
|
534
|
+
bin_step,
|
|
300
535
|
}
|
|
301
536
|
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
302
537
|
```
|
|
@@ -556,9 +791,31 @@ const whitelistTx = sdk.Reward.updateRewardWhiteListPayload({
|
|
|
556
791
|
})
|
|
557
792
|
```
|
|
558
793
|
|
|
559
|
-
### 8.
|
|
794
|
+
### 8. Configuration Management
|
|
795
|
+
|
|
796
|
+
The SDK provides configuration management through the `Config` module:
|
|
797
|
+
|
|
798
|
+
```typescript
|
|
799
|
+
// Get global DLMM configuration
|
|
800
|
+
const globalConfig = await sdk.Config.getDlmmGlobalConfig()
|
|
801
|
+
|
|
802
|
+
// Get bin step configuration list
|
|
803
|
+
const binStepConfigs = await sdk.Config.getBinStepConfigList(pool_id)
|
|
804
|
+
|
|
805
|
+
// Fetch all SDK configurations
|
|
806
|
+
const sdkConfigs = await sdk.Config.fetchDlmmSdkConfigs()
|
|
807
|
+
|
|
808
|
+
// Get reward period emission data
|
|
809
|
+
const rewardEmission = await sdk.Reward.getRewardPeriodEmission(
|
|
810
|
+
reward_manager_id,
|
|
811
|
+
reward_period,
|
|
812
|
+
current_time
|
|
813
|
+
)
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
### 9. Bin Operations
|
|
560
817
|
|
|
561
|
-
The SDK provides utility functions for working with bins and prices through the `BinUtils` class:
|
|
818
|
+
The SDK provides comprehensive utility functions for working with bins and prices through the `BinUtils` class:
|
|
562
819
|
|
|
563
820
|
```typescript
|
|
564
821
|
import { BinUtils } from '@cetusprotocol/dlmm-sdk'
|
|
@@ -598,15 +855,15 @@ const amountA = BinUtils.getAmountAFromLiquidity('4101094304427826916657468', '1
|
|
|
598
855
|
// Get amount B from liquidity
|
|
599
856
|
const amountB = BinUtils.getAmountBFromLiquidity('4919119455159831291232256')
|
|
600
857
|
|
|
601
|
-
// Get amounts from liquidity with ratio
|
|
602
|
-
const [amountA, amountB] = BinUtils.getAmountsFromLiquidityWithRatio(
|
|
603
|
-
'4510099798813469403250688',
|
|
604
|
-
'18446744073709551616',
|
|
605
|
-
0.9 // ratio
|
|
606
|
-
)
|
|
607
858
|
|
|
608
859
|
// Split bin liquidity info
|
|
609
860
|
const split_bin_infos = BinUtils.splitBinLiquidityInfo(bin_infos, 0, 70)
|
|
861
|
+
|
|
862
|
+
// Get position count between bin ranges
|
|
863
|
+
const positionCount = BinUtils.getPositionCount(-750, 845)
|
|
864
|
+
|
|
865
|
+
// Find min/max bin ID for a given bin step
|
|
866
|
+
const { minBinId, maxBinId } = BinUtils.findMinMaxBinId(10)
|
|
610
867
|
```
|
|
611
868
|
|
|
612
869
|
These utility functions are particularly useful when:
|
|
@@ -614,6 +871,7 @@ These utility functions are particularly useful when:
|
|
|
614
871
|
- Calculating optimal bin ranges for trading strategies
|
|
615
872
|
- Converting between different price representations
|
|
616
873
|
- Managing liquidity distributions across bins
|
|
874
|
+
- Analyzing position density and distribution
|
|
617
875
|
|
|
618
876
|
### 9. Advanced Operations
|
|
619
877
|
|
|
@@ -640,6 +898,73 @@ await sdk.Position.updatePositionFeeAndRewards({
|
|
|
640
898
|
})
|
|
641
899
|
```
|
|
642
900
|
|
|
901
|
+
## Troubleshooting
|
|
902
|
+
|
|
903
|
+
### Common Issues
|
|
904
|
+
|
|
905
|
+
**1. Transaction Simulation Failures**
|
|
906
|
+
```typescript
|
|
907
|
+
// Always simulate transactions before sending
|
|
908
|
+
const simResult = await sdk.FullClient.sendSimulationTransaction(tx, wallet)
|
|
909
|
+
if (simResult.effects.status.status === 'failure') {
|
|
910
|
+
console.error('Transaction simulation failed:', simResult.effects.status.error)
|
|
911
|
+
}
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
**2. Insufficient Gas Budget**
|
|
915
|
+
```typescript
|
|
916
|
+
// Set appropriate gas budget for complex operations
|
|
917
|
+
tx.setGasBudget(10000000000) // 10 SUI
|
|
918
|
+
```
|
|
919
|
+
|
|
920
|
+
**3. Active Bin Range Validation**
|
|
921
|
+
```typescript
|
|
922
|
+
// Always check if active bin is within your position range
|
|
923
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
924
|
+
pool.bin_manager.bin_manager_handle,
|
|
925
|
+
lower_bin_id,
|
|
926
|
+
upper_bin_id,
|
|
927
|
+
active_id,
|
|
928
|
+
bin_step
|
|
929
|
+
)
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
**4. Price Slippage Protection**
|
|
933
|
+
```typescript
|
|
934
|
+
// Use appropriate slippage protection
|
|
935
|
+
const isValid = await sdk.Position.validateActiveIdSlippage({
|
|
936
|
+
pool_id,
|
|
937
|
+
active_id,
|
|
938
|
+
max_bin_slippage: 0.01 // 1% slippage tolerance
|
|
939
|
+
})
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
### Debugging Tips
|
|
943
|
+
|
|
944
|
+
**1. Print Transaction Details**
|
|
945
|
+
```typescript
|
|
946
|
+
import { printTransaction } from '@cetusprotocol/common-sdk'
|
|
947
|
+
|
|
948
|
+
// Print transaction for debugging
|
|
949
|
+
printTransaction(tx)
|
|
950
|
+
```
|
|
951
|
+
|
|
952
|
+
**2. Check Pool State**
|
|
953
|
+
```typescript
|
|
954
|
+
// Always verify pool state before operations
|
|
955
|
+
const pool = await sdk.Pool.getPool(pool_id)
|
|
956
|
+
console.log('Pool active_id:', pool.active_id)
|
|
957
|
+
console.log('Pool bin_step:', pool.bin_step)
|
|
958
|
+
```
|
|
959
|
+
|
|
960
|
+
**3. Validate Position Data**
|
|
961
|
+
```typescript
|
|
962
|
+
// Parse and validate position liquidity shares
|
|
963
|
+
const active_bin = await sdk.Pool.getBinInfo(pool.bin_manager.bin_manager_handle, pool.active_id, pool.bin_step)
|
|
964
|
+
const liquidity_shares_data = parseLiquidityShares(position.liquidity_shares, pool.bin_step, position.lower_bin_id, active_bin)
|
|
965
|
+
console.log('Liquidity shares data:', liquidity_shares_data)
|
|
966
|
+
```
|
|
967
|
+
|
|
643
968
|
## More About Cetus
|
|
644
969
|
|
|
645
970
|
Use the following links to learn more about Cetus:
|