@cetusprotocol/dlmm-sdk 0.0.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.
- package/.turbo/turbo-build.log +10423 -0
- package/README.md +646 -0
- package/dist/index.d.mts +1015 -0
- package/dist/index.d.ts +1015 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +13 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +35 -0
- package/src/config/index.ts +2 -0
- package/src/config/mainnet.ts +25 -0
- package/src/config/testnet.ts +30 -0
- package/src/errors/errors.ts +40 -0
- package/src/index.ts +8 -0
- package/src/modules/configModule.ts +184 -0
- package/src/modules/index.ts +1 -0
- package/src/modules/partnerModule.ts +302 -0
- package/src/modules/poolModule.ts +578 -0
- package/src/modules/positionModule.ts +888 -0
- package/src/modules/rewardModule.ts +175 -0
- package/src/modules/swapModule.ts +129 -0
- package/src/sdk.ts +88 -0
- package/src/types/constants.ts +23 -0
- package/src/types/dlmm.ts +445 -0
- package/src/types/index.ts +2 -0
- package/src/utils/binUtils.ts +552 -0
- package/src/utils/feeUtils.ts +92 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/parseData.ts +519 -0
- package/src/utils/strategyUtils.ts +121 -0
- package/src/utils/weightUtils.ts +510 -0
- package/tests/add_liquidity_bidask.test.ts +180 -0
- package/tests/add_liquidity_curve.test.ts +244 -0
- package/tests/add_liquidity_spot.test.ts +262 -0
- package/tests/bin.test.ts +80 -0
- package/tests/config.test.ts +51 -0
- package/tests/partner.test.ts +74 -0
- package/tests/pool.test.ts +174 -0
- package/tests/position.test.ts +76 -0
- package/tests/remove_liquidity.test.ts +137 -0
- package/tests/swap.test.ts +96 -0
- package/tests/tsconfig.json +26 -0
- package/tsconfig.json +5 -0
- package/tsup.config.ts +9 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// buildTestAccount
|
|
2
|
+
import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519'
|
|
3
|
+
import { buildTestAccount } from '@cetusprotocol/test-utils'
|
|
4
|
+
import { CetusDlmmSDK } from '../src/sdk'
|
|
5
|
+
import {
|
|
6
|
+
AddLiquidityOption,
|
|
7
|
+
CalculateAddLiquidityAutoFillOption,
|
|
8
|
+
CalculateAddLiquidityOption,
|
|
9
|
+
DlmmPool,
|
|
10
|
+
OpenAndAddLiquidityOption,
|
|
11
|
+
StrategyType,
|
|
12
|
+
} from '../src/types/dlmm'
|
|
13
|
+
import { printTransaction } from '@cetusprotocol/common-sdk'
|
|
14
|
+
|
|
15
|
+
const pool_id = '0x5156119a3cc1143ac65c24f3e7e0b339b74ea057ff5e3a1532b407a749193394'
|
|
16
|
+
const position_id = '0xbf3a7a03dbb80e37c44b3dac20cf675b69e5c1ca866538853522ccec934ef554'
|
|
17
|
+
|
|
18
|
+
describe('dlmm add liquidity curve', () => {
|
|
19
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'testnet', full_rpc_url: 'https://rpc-testnet.suiscan.xyz' })
|
|
20
|
+
let send_key_pair: Ed25519Keypair
|
|
21
|
+
let account: string
|
|
22
|
+
let pool: DlmmPool
|
|
23
|
+
|
|
24
|
+
beforeEach(async () => {
|
|
25
|
+
send_key_pair = buildTestAccount()
|
|
26
|
+
account = send_key_pair.getPublicKey().toSuiAddress()
|
|
27
|
+
sdk.setSenderAddress(account)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('0 curve both amounts open multiple positions and add liquidity', async () => {
|
|
31
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
32
|
+
console.log('🚀 ~ beforeEach ~ pool:', pool)
|
|
33
|
+
const { active_id, bin_step } = pool
|
|
34
|
+
const amount_a = '10000000'
|
|
35
|
+
const amount_b = '10000000'
|
|
36
|
+
const lower_bin_id = active_id - 100
|
|
37
|
+
const upper_bin_id = active_id + 1300
|
|
38
|
+
|
|
39
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
40
|
+
pool.bin_manager.bin_manager_handle,
|
|
41
|
+
lower_bin_id,
|
|
42
|
+
upper_bin_id,
|
|
43
|
+
active_id,
|
|
44
|
+
bin_step
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
48
|
+
amount_a,
|
|
49
|
+
amount_b,
|
|
50
|
+
active_id,
|
|
51
|
+
bin_step,
|
|
52
|
+
lower_bin_id,
|
|
53
|
+
upper_bin_id,
|
|
54
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
55
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
56
|
+
strategy_type: StrategyType.Curve,
|
|
57
|
+
}
|
|
58
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
59
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
60
|
+
|
|
61
|
+
const addOption: OpenAndAddLiquidityOption = {
|
|
62
|
+
pool_id,
|
|
63
|
+
bin_infos: bin_infos,
|
|
64
|
+
coin_type_a: pool.coin_type_a,
|
|
65
|
+
coin_type_b: pool.coin_type_b,
|
|
66
|
+
lower_bin_id,
|
|
67
|
+
upper_bin_id,
|
|
68
|
+
active_id,
|
|
69
|
+
strategy_type: StrategyType.Curve,
|
|
70
|
+
max_price_slippage: 0.001,
|
|
71
|
+
bin_step,
|
|
72
|
+
use_bin_infos: false,
|
|
73
|
+
}
|
|
74
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
75
|
+
tx.setGasBudget(10000000000)
|
|
76
|
+
printTransaction(tx)
|
|
77
|
+
|
|
78
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, true)
|
|
79
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('1 curve both amounts open and add liquidity', async () => {
|
|
83
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
84
|
+
console.log('🚀 ~ beforeEach ~ pool:', pool)
|
|
85
|
+
const { active_id, bin_step } = pool
|
|
86
|
+
const amount_a = '1000000'
|
|
87
|
+
const amount_b = '1200000'
|
|
88
|
+
const lower_bin_id = -10
|
|
89
|
+
const upper_bin_id = 10
|
|
90
|
+
|
|
91
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
92
|
+
pool.bin_manager.bin_manager_handle,
|
|
93
|
+
lower_bin_id,
|
|
94
|
+
upper_bin_id,
|
|
95
|
+
active_id,
|
|
96
|
+
bin_step
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
100
|
+
amount_a,
|
|
101
|
+
amount_b,
|
|
102
|
+
active_id,
|
|
103
|
+
bin_step,
|
|
104
|
+
lower_bin_id,
|
|
105
|
+
upper_bin_id,
|
|
106
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
107
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
108
|
+
strategy_type: StrategyType.Curve,
|
|
109
|
+
}
|
|
110
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
111
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
112
|
+
|
|
113
|
+
const addOption: OpenAndAddLiquidityOption = {
|
|
114
|
+
pool_id,
|
|
115
|
+
bin_infos: bin_infos,
|
|
116
|
+
coin_type_a: pool.coin_type_a,
|
|
117
|
+
coin_type_b: pool.coin_type_b,
|
|
118
|
+
lower_bin_id,
|
|
119
|
+
upper_bin_id,
|
|
120
|
+
active_id,
|
|
121
|
+
strategy_type: StrategyType.Curve,
|
|
122
|
+
max_price_slippage: 0.001,
|
|
123
|
+
bin_step,
|
|
124
|
+
use_bin_infos: false,
|
|
125
|
+
}
|
|
126
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
127
|
+
|
|
128
|
+
printTransaction(tx)
|
|
129
|
+
|
|
130
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
131
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test('2 curve strategy both amounts add liquidity', async () => {
|
|
135
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
136
|
+
const { active_id, bin_step, bin_manager, coin_type_a, coin_type_b } = pool
|
|
137
|
+
console.log('🚀 ~ pool:', pool)
|
|
138
|
+
|
|
139
|
+
const position = await sdk.Position.getPosition(position_id)
|
|
140
|
+
const { lower_bin_id, upper_bin_id } = position
|
|
141
|
+
console.log('🚀 ~ position:', position)
|
|
142
|
+
|
|
143
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
144
|
+
bin_manager.bin_manager_handle,
|
|
145
|
+
lower_bin_id,
|
|
146
|
+
upper_bin_id,
|
|
147
|
+
active_id,
|
|
148
|
+
bin_step
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
const amount_a = '1000000'
|
|
152
|
+
const amount_b = '1200000'
|
|
153
|
+
|
|
154
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
155
|
+
amount_a,
|
|
156
|
+
amount_b,
|
|
157
|
+
active_id,
|
|
158
|
+
bin_step,
|
|
159
|
+
lower_bin_id,
|
|
160
|
+
upper_bin_id,
|
|
161
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
162
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
163
|
+
strategy_type: StrategyType.Curve,
|
|
164
|
+
}
|
|
165
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
166
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
167
|
+
|
|
168
|
+
const addOption: AddLiquidityOption = {
|
|
169
|
+
pool_id,
|
|
170
|
+
bin_infos: bin_infos,
|
|
171
|
+
coin_type_a,
|
|
172
|
+
coin_type_b,
|
|
173
|
+
active_id,
|
|
174
|
+
position_id,
|
|
175
|
+
collect_fee: true,
|
|
176
|
+
reward_coins: [],
|
|
177
|
+
strategy_type: StrategyType.Curve,
|
|
178
|
+
max_price_slippage: 0.001,
|
|
179
|
+
bin_step,
|
|
180
|
+
use_bin_infos: false,
|
|
181
|
+
}
|
|
182
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
183
|
+
|
|
184
|
+
printTransaction(tx)
|
|
185
|
+
|
|
186
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
187
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('3 curve strategy fix coin a add liquidity', async () => {
|
|
191
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
192
|
+
const { active_id, bin_step, bin_manager, coin_type_a, coin_type_b } = pool
|
|
193
|
+
console.log('🚀 ~ pool:', pool)
|
|
194
|
+
|
|
195
|
+
const position = await sdk.Position.getPosition(position_id)
|
|
196
|
+
const { lower_bin_id, upper_bin_id } = position
|
|
197
|
+
console.log('🚀 ~ position:', position)
|
|
198
|
+
|
|
199
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
200
|
+
bin_manager.bin_manager_handle,
|
|
201
|
+
lower_bin_id,
|
|
202
|
+
upper_bin_id,
|
|
203
|
+
active_id,
|
|
204
|
+
bin_step
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
const coin_amount = '2000000'
|
|
208
|
+
|
|
209
|
+
const calculateOption: CalculateAddLiquidityAutoFillOption = {
|
|
210
|
+
coin_amount,
|
|
211
|
+
fix_amount_a: true,
|
|
212
|
+
active_id,
|
|
213
|
+
bin_step,
|
|
214
|
+
lower_bin_id,
|
|
215
|
+
upper_bin_id,
|
|
216
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
217
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
218
|
+
strategy_type: StrategyType.Curve,
|
|
219
|
+
}
|
|
220
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
221
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
222
|
+
|
|
223
|
+
const addOption: AddLiquidityOption = {
|
|
224
|
+
pool_id,
|
|
225
|
+
bin_infos: bin_infos,
|
|
226
|
+
coin_type_a,
|
|
227
|
+
coin_type_b,
|
|
228
|
+
active_id,
|
|
229
|
+
position_id,
|
|
230
|
+
collect_fee: true,
|
|
231
|
+
reward_coins: [],
|
|
232
|
+
strategy_type: StrategyType.Curve,
|
|
233
|
+
max_price_slippage: 0.001,
|
|
234
|
+
bin_step,
|
|
235
|
+
use_bin_infos: false,
|
|
236
|
+
}
|
|
237
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
238
|
+
|
|
239
|
+
printTransaction(tx)
|
|
240
|
+
|
|
241
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
242
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
243
|
+
})
|
|
244
|
+
})
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// buildTestAccount
|
|
2
|
+
import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519'
|
|
3
|
+
import { buildTestAccount } from '@cetusprotocol/test-utils'
|
|
4
|
+
import { CetusDlmmSDK } from '../src/sdk'
|
|
5
|
+
import {
|
|
6
|
+
AddLiquidityOption,
|
|
7
|
+
CalculateAddLiquidityAutoFillOption,
|
|
8
|
+
CalculateAddLiquidityOption,
|
|
9
|
+
DlmmPool,
|
|
10
|
+
OpenAndAddLiquidityOption,
|
|
11
|
+
OpenAndAddLiquidityWithPriceOption,
|
|
12
|
+
StrategyType,
|
|
13
|
+
} from '../src/types/dlmm'
|
|
14
|
+
import { printTransaction, toDecimalsAmount } from '@cetusprotocol/common-sdk'
|
|
15
|
+
import { BinUtils } from '../src/utils/binUtils'
|
|
16
|
+
|
|
17
|
+
const pool_id = '0x5156119a3cc1143ac65c24f3e7e0b339b74ea057ff5e3a1532b407a749193394'
|
|
18
|
+
const position_id = '0xf5139870fbc926d1ca1afdc536b4ab457a9c2a696440d10955572f04b95d9e29'
|
|
19
|
+
|
|
20
|
+
describe('dlmm add liquidity spot', () => {
|
|
21
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'testnet', full_rpc_url: 'https://rpc-testnet.suiscan.xyz:443' })
|
|
22
|
+
let send_key_pair: Ed25519Keypair
|
|
23
|
+
let account: string
|
|
24
|
+
let pool: DlmmPool
|
|
25
|
+
|
|
26
|
+
beforeEach(async () => {
|
|
27
|
+
send_key_pair = buildTestAccount()
|
|
28
|
+
account = send_key_pair.getPublicKey().toSuiAddress()
|
|
29
|
+
sdk.setSenderAddress(account)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('0 spot both amounts open and add liquidity with price', async () => {
|
|
33
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
34
|
+
console.log('🚀 ~ beforeEach ~ pool:', pool)
|
|
35
|
+
const { active_id, bin_step } = pool
|
|
36
|
+
const amount_a = '1000000'
|
|
37
|
+
const amount_b = '1200000'
|
|
38
|
+
|
|
39
|
+
const lower_price = '0.99'
|
|
40
|
+
const upper_price = '1.01'
|
|
41
|
+
const price = BinUtils.getPriceFromBinId(active_id, bin_step, 6, 6)
|
|
42
|
+
|
|
43
|
+
const lower_bin_id = BinUtils.getBinIdFromPrice(lower_price, bin_step, true, 6, 6)
|
|
44
|
+
const upper_bin_id = BinUtils.getBinIdFromPrice(upper_price, bin_step, true, 6, 6)
|
|
45
|
+
|
|
46
|
+
console.log('🚀 ~ test ~ lower_bin_id:', lower_bin_id)
|
|
47
|
+
console.log('🚀 ~ test ~ upper_bin_id:', upper_bin_id)
|
|
48
|
+
|
|
49
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
50
|
+
pool.bin_manager.bin_manager_handle,
|
|
51
|
+
lower_bin_id,
|
|
52
|
+
upper_bin_id,
|
|
53
|
+
active_id,
|
|
54
|
+
bin_step
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
58
|
+
amount_a,
|
|
59
|
+
amount_b,
|
|
60
|
+
active_id,
|
|
61
|
+
bin_step,
|
|
62
|
+
lower_bin_id,
|
|
63
|
+
upper_bin_id,
|
|
64
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
65
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
66
|
+
strategy_type: StrategyType.Spot,
|
|
67
|
+
}
|
|
68
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
69
|
+
// console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
70
|
+
|
|
71
|
+
const addOption: OpenAndAddLiquidityWithPriceOption = {
|
|
72
|
+
pool_id,
|
|
73
|
+
bin_infos: bin_infos,
|
|
74
|
+
coin_type_a: pool.coin_type_a,
|
|
75
|
+
coin_type_b: pool.coin_type_b,
|
|
76
|
+
price_base_coin: 'coin_a',
|
|
77
|
+
price: price.toString(),
|
|
78
|
+
lower_price,
|
|
79
|
+
upper_price,
|
|
80
|
+
bin_step,
|
|
81
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
82
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
83
|
+
strategy_type: StrategyType.Spot,
|
|
84
|
+
decimals_a: 6,
|
|
85
|
+
decimals_b: 6,
|
|
86
|
+
max_price_slippage: 0.01,
|
|
87
|
+
active_id,
|
|
88
|
+
}
|
|
89
|
+
const tx = sdk.Position.addLiquidityWithPricePayload(addOption)
|
|
90
|
+
|
|
91
|
+
printTransaction(tx)
|
|
92
|
+
|
|
93
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
94
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('1 spot both amounts open and add liquidity', async () => {
|
|
98
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
99
|
+
console.log('🚀 ~ beforeEach ~ pool:', pool)
|
|
100
|
+
const { active_id, bin_step } = pool
|
|
101
|
+
const amount_a = '10000000'
|
|
102
|
+
const amount_b = '0'
|
|
103
|
+
const lower_bin_id = active_id
|
|
104
|
+
const upper_bin_id = active_id + 10
|
|
105
|
+
|
|
106
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
107
|
+
pool.bin_manager.bin_manager_handle,
|
|
108
|
+
lower_bin_id,
|
|
109
|
+
upper_bin_id,
|
|
110
|
+
active_id,
|
|
111
|
+
bin_step
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
115
|
+
amount_a,
|
|
116
|
+
amount_b,
|
|
117
|
+
active_id,
|
|
118
|
+
bin_step,
|
|
119
|
+
lower_bin_id,
|
|
120
|
+
upper_bin_id,
|
|
121
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
122
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
123
|
+
strategy_type: StrategyType.Spot,
|
|
124
|
+
}
|
|
125
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
126
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
127
|
+
|
|
128
|
+
const addOption: OpenAndAddLiquidityOption = {
|
|
129
|
+
pool_id,
|
|
130
|
+
bin_infos: bin_infos,
|
|
131
|
+
coin_type_a: pool.coin_type_a,
|
|
132
|
+
coin_type_b: pool.coin_type_b,
|
|
133
|
+
lower_bin_id,
|
|
134
|
+
upper_bin_id,
|
|
135
|
+
active_id,
|
|
136
|
+
strategy_type: StrategyType.Spot,
|
|
137
|
+
use_bin_infos: false,
|
|
138
|
+
max_price_slippage: 0.01,
|
|
139
|
+
bin_step,
|
|
140
|
+
}
|
|
141
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
142
|
+
tx.setGasBudget(10000000000)
|
|
143
|
+
printTransaction(tx)
|
|
144
|
+
|
|
145
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
146
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('2 spot strategy both amounts add liquidity', async () => {
|
|
150
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
151
|
+
const { active_id, bin_step, bin_manager, coin_type_a, coin_type_b } = pool
|
|
152
|
+
console.log('🚀 ~ pool:', pool)
|
|
153
|
+
|
|
154
|
+
const position = await sdk.Position.getPosition(position_id)
|
|
155
|
+
const { lower_bin_id, upper_bin_id } = position
|
|
156
|
+
console.log('🚀 ~ position:', position)
|
|
157
|
+
|
|
158
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
159
|
+
bin_manager.bin_manager_handle,
|
|
160
|
+
lower_bin_id,
|
|
161
|
+
upper_bin_id,
|
|
162
|
+
active_id,
|
|
163
|
+
bin_step
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
const amount_a = '1000000'
|
|
167
|
+
const amount_b = '0'
|
|
168
|
+
|
|
169
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
170
|
+
amount_a,
|
|
171
|
+
amount_b,
|
|
172
|
+
active_id,
|
|
173
|
+
bin_step,
|
|
174
|
+
lower_bin_id,
|
|
175
|
+
upper_bin_id,
|
|
176
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
177
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
178
|
+
strategy_type: StrategyType.Spot,
|
|
179
|
+
}
|
|
180
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
181
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
182
|
+
|
|
183
|
+
const addOption: AddLiquidityOption = {
|
|
184
|
+
pool_id,
|
|
185
|
+
bin_infos: bin_infos,
|
|
186
|
+
coin_type_a,
|
|
187
|
+
coin_type_b,
|
|
188
|
+
active_id,
|
|
189
|
+
position_id,
|
|
190
|
+
collect_fee: true,
|
|
191
|
+
reward_coins: [],
|
|
192
|
+
strategy_type: StrategyType.Spot,
|
|
193
|
+
use_bin_infos: false,
|
|
194
|
+
max_price_slippage: 0.01,
|
|
195
|
+
bin_step,
|
|
196
|
+
}
|
|
197
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
198
|
+
|
|
199
|
+
printTransaction(tx)
|
|
200
|
+
|
|
201
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, false)
|
|
202
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test('3 spot strategy fix coin a add liquidity', async () => {
|
|
206
|
+
pool = await sdk.Pool.getPool(pool_id)
|
|
207
|
+
const { active_id, bin_step, bin_manager, coin_type_a, coin_type_b } = pool
|
|
208
|
+
console.log('🚀 ~ pool:', pool)
|
|
209
|
+
|
|
210
|
+
const position = await sdk.Position.getPosition(position_id)
|
|
211
|
+
const { lower_bin_id, upper_bin_id } = position
|
|
212
|
+
console.log('🚀 ~ position:', position)
|
|
213
|
+
|
|
214
|
+
// const lower_bin_id = active_id + 1
|
|
215
|
+
// const upper_bin_id = active_id + 1
|
|
216
|
+
|
|
217
|
+
const amounts_in_active_bin = await sdk.Position.getActiveBinIfInRange(
|
|
218
|
+
bin_manager.bin_manager_handle,
|
|
219
|
+
lower_bin_id,
|
|
220
|
+
upper_bin_id,
|
|
221
|
+
active_id,
|
|
222
|
+
bin_step
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
const coin_amount = toDecimalsAmount(1, 9)
|
|
226
|
+
|
|
227
|
+
const calculateOption: CalculateAddLiquidityAutoFillOption = {
|
|
228
|
+
coin_amount,
|
|
229
|
+
fix_amount_a: false,
|
|
230
|
+
active_id,
|
|
231
|
+
bin_step,
|
|
232
|
+
lower_bin_id,
|
|
233
|
+
upper_bin_id,
|
|
234
|
+
amount_a_in_active_bin: amounts_in_active_bin?.amount_a || '0',
|
|
235
|
+
amount_b_in_active_bin: amounts_in_active_bin?.amount_b || '0',
|
|
236
|
+
strategy_type: StrategyType.Spot,
|
|
237
|
+
}
|
|
238
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
239
|
+
console.log('🚀 ~ test ~ bin_infos:', bin_infos)
|
|
240
|
+
|
|
241
|
+
const addOption: AddLiquidityOption = {
|
|
242
|
+
pool_id,
|
|
243
|
+
bin_infos: bin_infos,
|
|
244
|
+
coin_type_a,
|
|
245
|
+
coin_type_b,
|
|
246
|
+
active_id,
|
|
247
|
+
position_id,
|
|
248
|
+
collect_fee: false,
|
|
249
|
+
reward_coins: [],
|
|
250
|
+
use_bin_infos: false,
|
|
251
|
+
strategy_type: StrategyType.Spot,
|
|
252
|
+
max_price_slippage: 0.01,
|
|
253
|
+
bin_step,
|
|
254
|
+
}
|
|
255
|
+
const tx = sdk.Position.addLiquidityPayload(addOption)
|
|
256
|
+
tx.setGasBudget(10000000000)
|
|
257
|
+
printTransaction(tx)
|
|
258
|
+
|
|
259
|
+
const res = await sdk.FullClient.executeTx(send_key_pair, tx, true)
|
|
260
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
261
|
+
})
|
|
262
|
+
})
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// buildTestAccount
|
|
2
|
+
import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519'
|
|
3
|
+
import { buildTestAccount } from '@cetusprotocol/test-utils'
|
|
4
|
+
import { CetusDlmmSDK } from '../src/sdk'
|
|
5
|
+
import { BinUtils } from '../src/utils/binUtils'
|
|
6
|
+
import { Transaction } from '@mysten/sui/transactions'
|
|
7
|
+
import { asUintN, d, printTransaction } from '@cetusprotocol/common-sdk'
|
|
8
|
+
import { CalculateAddLiquidityOption, StrategyType } from '../src/types/dlmm'
|
|
9
|
+
import { safeMulAmount } from '../src/utils'
|
|
10
|
+
|
|
11
|
+
describe('dlmm bin', () => {
|
|
12
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'mainnet' })
|
|
13
|
+
let send_key_pair: Ed25519Keypair
|
|
14
|
+
let account: string
|
|
15
|
+
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
send_key_pair = buildTestAccount()
|
|
18
|
+
account = send_key_pair.getPublicKey().toSuiAddress()
|
|
19
|
+
sdk.setSenderAddress(account)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('getBinIdFromPrice', async () => {
|
|
23
|
+
const binId = BinUtils.getBinIdFromPrice(d(1).div('0.0013360404960738484928').toString(), 20, false, 9, 6)
|
|
24
|
+
console.log('🚀 ~ test ~ binId:', binId)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('getPriceFromBinId', async () => {
|
|
28
|
+
const price = BinUtils.getPriceFromBinId(1113, 400, 6, 6)
|
|
29
|
+
console.log('🚀 ~ test ~ price:', price)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('getPricePerLamportFromQPrice', async () => {
|
|
33
|
+
const q_price = BinUtils.getQPriceFromId(-4400, 100)
|
|
34
|
+
console.log('🚀 ~ test ~ q_price:', q_price)
|
|
35
|
+
const price_per_lamport = BinUtils.getPricePerLamportFromQPrice(q_price)
|
|
36
|
+
console.log('🚀 ~ test ~ price_per_lamport:', price_per_lamport)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('getPositionCount', async () => {
|
|
40
|
+
const positionCount = BinUtils.getPositionCount(-750, 845)
|
|
41
|
+
console.log('🚀 ~ test ~ positionCount:', positionCount)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('getLiquidity', async () => {
|
|
45
|
+
const liquidity = BinUtils.getLiquidity('0', '266666', '18431994054197767090')
|
|
46
|
+
console.log('🚀 ~ test ~ liquidity:', liquidity)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('getAmountAFromLiquidity', async () => {
|
|
50
|
+
const amountA = BinUtils.getAmountAFromLiquidity('4101094304427826916657468', '18461505896777422276')
|
|
51
|
+
console.log('🚀 ~ test ~ amountA:', amountA)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('getAmountBFromLiquidity', async () => {
|
|
55
|
+
const amountB = BinUtils.getAmountBFromLiquidity('4919119455159831291232256')
|
|
56
|
+
console.log('🚀 ~ test ~ amountB:', amountB)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('calculateAddLiquidityInfo', async () => {
|
|
60
|
+
const calculateOption: CalculateAddLiquidityOption = {
|
|
61
|
+
amount_a: '100000000',
|
|
62
|
+
amount_b: '100000000',
|
|
63
|
+
active_id: 0,
|
|
64
|
+
bin_step: 2,
|
|
65
|
+
lower_bin_id: 0,
|
|
66
|
+
upper_bin_id: 70,
|
|
67
|
+
amount_a_in_active_bin: '0',
|
|
68
|
+
amount_b_in_active_bin: '0',
|
|
69
|
+
strategy_type: StrategyType.Spot,
|
|
70
|
+
}
|
|
71
|
+
const bin_infos = sdk.Position.calculateAddLiquidityInfo(calculateOption)
|
|
72
|
+
console.log('🚀 ~ test ~ split_bin_infos:', bin_infos.bins.length)
|
|
73
|
+
const split_bin_infos = BinUtils.splitBinLiquidityInfo(bin_infos, 0, 70)
|
|
74
|
+
console.log('🚀 ~ test ~ split_bin_infos:', split_bin_infos.length)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('findMinMaxBinId', async () => {
|
|
78
|
+
console.log('bin step 10 : ', BinUtils.findMinMaxBinId(10))
|
|
79
|
+
})
|
|
80
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// buildTestAccount
|
|
2
|
+
import type { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519'
|
|
3
|
+
import { buildTestAccount } from '@cetusprotocol/test-utils'
|
|
4
|
+
import { CetusDlmmSDK } from '../src/sdk'
|
|
5
|
+
import { parseCurrentRewardPeriodEmission, parseRewardPeriodEmission } from '../src/utils/parseData'
|
|
6
|
+
|
|
7
|
+
describe('config', () => {
|
|
8
|
+
const sdk = CetusDlmmSDK.createSDK({ env: 'mainnet' })
|
|
9
|
+
let send_key_pair: Ed25519Keypair
|
|
10
|
+
let account: string
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
send_key_pair = buildTestAccount()
|
|
14
|
+
account = send_key_pair.getPublicKey().toSuiAddress()
|
|
15
|
+
sdk.setSenderAddress(account)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('getDlmmGlobalConfig', async () => {
|
|
19
|
+
const res = await sdk.Config.getDlmmGlobalConfig()
|
|
20
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('getBinStepConfigList', async () => {
|
|
24
|
+
const res = await sdk.Config.getBinStepConfigList('0xc00e4dbc372948b2b72ae5554c7296b39e107a92821baff08aa640aba0b07aed')
|
|
25
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('fetchDlmmSdkConfigs', async () => {
|
|
29
|
+
const res = await sdk.Config.fetchDlmmSdkConfigs()
|
|
30
|
+
console.log('🚀 ~ test ~ res:', res)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('getPool', async () => {
|
|
34
|
+
const pool = await sdk.Pool.getPool('0x8b131bccc1b4da0b463c19fa8cacdb71fc9f2ff632841769650886db2f0995f8')
|
|
35
|
+
console.log('🚀 ~ test ~ pool:', JSON.stringify(pool, null, 2))
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('getRewardPeriodEmission', async () => {
|
|
39
|
+
const currentTime = new Date().getTime() / 1000
|
|
40
|
+
const res = await sdk.Reward.getRewardPeriodEmission(
|
|
41
|
+
'0x59fbe11899d46c36b597b9899d86f62b880f0aa05727e25454a1158a52bf290d',
|
|
42
|
+
'22080.13295225819507789012012891061154107319453032687306404113769',
|
|
43
|
+
1756354616
|
|
44
|
+
)
|
|
45
|
+
console.log('🚀 ~ test ~ res:', JSON.stringify(res, null, 2))
|
|
46
|
+
const result = parseRewardPeriodEmission(res, currentTime, currentTime + 60 * 60 * 24 * 20, 60 * 60 * 24)
|
|
47
|
+
console.log('🚀 ~ test ~ result:', JSON.stringify(result, null, 2))
|
|
48
|
+
const currentEmission = parseCurrentRewardPeriodEmission(res)
|
|
49
|
+
console.log('🚀 ~ test ~ currentEmission:', currentEmission)
|
|
50
|
+
})
|
|
51
|
+
})
|