@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.
Files changed (42) hide show
  1. package/README.md +376 -51
  2. package/dist/index.js +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +1 -1
  5. package/dist/index.mjs.map +1 -1
  6. package/package.json +1 -1
  7. package/.turbo/turbo-build.log +0 -10485
  8. package/src/config/index.ts +0 -2
  9. package/src/config/mainnet.ts +0 -25
  10. package/src/config/testnet.ts +0 -30
  11. package/src/errors/errors.ts +0 -40
  12. package/src/index.ts +0 -8
  13. package/src/modules/configModule.ts +0 -184
  14. package/src/modules/index.ts +0 -1
  15. package/src/modules/partnerModule.ts +0 -302
  16. package/src/modules/poolModule.ts +0 -625
  17. package/src/modules/positionModule.ts +0 -901
  18. package/src/modules/rewardModule.ts +0 -174
  19. package/src/modules/swapModule.ts +0 -129
  20. package/src/sdk.ts +0 -88
  21. package/src/types/constants.ts +0 -23
  22. package/src/types/dlmm.ts +0 -466
  23. package/src/types/index.ts +0 -2
  24. package/src/utils/binUtils.ts +0 -552
  25. package/src/utils/feeUtils.ts +0 -92
  26. package/src/utils/index.ts +0 -5
  27. package/src/utils/parseData.ts +0 -556
  28. package/src/utils/strategyUtils.ts +0 -202
  29. package/src/utils/weightUtils.ts +0 -674
  30. package/tests/add_liquidity_bidask.test.ts +0 -192
  31. package/tests/add_liquidity_curve.test.ts +0 -244
  32. package/tests/add_liquidity_spot.test.ts +0 -314
  33. package/tests/bin.test.ts +0 -80
  34. package/tests/config.test.ts +0 -73
  35. package/tests/partner.test.ts +0 -74
  36. package/tests/pool.test.ts +0 -184
  37. package/tests/position.test.ts +0 -76
  38. package/tests/remove_liquidity.test.ts +0 -137
  39. package/tests/swap.test.ts +0 -96
  40. package/tests/tsconfig.json +0 -26
  41. package/tsconfig.json +0 -5
  42. package/tsup.config.ts +0 -9
@@ -1,625 +0,0 @@
1
- import { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions'
2
- import {
3
- asUintN,
4
- CLOCK_ADDRESS,
5
- CoinAssist,
6
- createFullClient,
7
- d,
8
- DataPage,
9
- DETAILS_KEYS,
10
- fixCoinType,
11
- getObjectFields,
12
- getPackagerConfigs,
13
- IModule,
14
- isSortedSymbols,
15
- PageQuery,
16
- PaginationArgs,
17
- printTransaction,
18
- } from '@cetusprotocol/common-sdk'
19
- import { DlmmErrorCode, handleError } from '../errors/errors'
20
- import {
21
- BinUtils,
22
- buildPoolKey,
23
- parseBinInfo,
24
- parseBinInfoList,
25
- parseDlmmBasePool,
26
- parseDlmmPool,
27
- parsePoolTransactionInfo,
28
- } from '../utils'
29
- import { CetusDlmmSDK } from '../sdk'
30
- import {
31
- BinAmount,
32
- BinLiquidityInfo,
33
- CalculateAddLiquidityOption,
34
- CreatePoolAndAddOption,
35
- CreatePoolAndAddWithPriceOption,
36
- CreatePoolOption,
37
- DlmmBasePool,
38
- DlmmConfigs,
39
- DlmmPool,
40
- FeeRate,
41
- GetPoolBinInfoOption,
42
- GetTotalFeeRateOption,
43
- OpenAndAddLiquidityOption,
44
- PoolTransactionInfo,
45
- } from '../types/dlmm'
46
- import { MAX_BIN_PER_POSITION } from '../types/constants'
47
- import { SuiClient } from '@mysten/sui/client'
48
- import { normalizeSuiAddress } from '@mysten/sui/utils'
49
- import { bcs } from '@mysten/sui/bcs'
50
-
51
- export class PoolModule implements IModule<CetusDlmmSDK> {
52
- protected _sdk: CetusDlmmSDK
53
-
54
- constructor(sdk: CetusDlmmSDK) {
55
- this._sdk = sdk
56
- }
57
-
58
- get sdk() {
59
- return this._sdk
60
- }
61
-
62
- async getPoolAddress(coin_type_a: string, coin_type_b: string, bin_step: number, base_factor: number): Promise<string | undefined> {
63
- try {
64
- const poolKey = buildPoolKey(coin_type_a, coin_type_b, bin_step, base_factor)
65
- const { dlmm_pool } = this._sdk.sdkOptions
66
- const { pools_id } = getPackagerConfigs(dlmm_pool)
67
- const res = await this._sdk.FullClient.getDynamicFieldObject({
68
- parentId: pools_id,
69
- name: {
70
- type: '0x2::object::ID',
71
- value: poolKey,
72
- },
73
- })
74
- const fields = getObjectFields(res)
75
- return fields.value.fields.value.fields.pool_id
76
- } catch (error) {
77
- return handleError(DlmmErrorCode.FetchError, error as Error, {
78
- [DETAILS_KEYS.METHOD_NAME]: 'getPoolAddress',
79
- [DETAILS_KEYS.REQUEST_PARAMS]: {
80
- coin_type_a,
81
- coin_type_b,
82
- bin_step,
83
- base_factor,
84
- },
85
- })
86
- }
87
-
88
- return undefined
89
- }
90
-
91
- /**
92
- * Get the list of DLMM base pools
93
- * @param pagination_args - The pagination arguments
94
- * @returns The list of DLMM base pools
95
- */
96
- async getBasePoolList(pagination_args: PaginationArgs = 'all', force_refresh = false): Promise<DataPage<DlmmBasePool>> {
97
- const { dlmm_pool } = this._sdk.sdkOptions
98
- const dataPage: DataPage<DlmmBasePool> = {
99
- data: [],
100
- has_next_page: false,
101
- }
102
-
103
- const queryAll = pagination_args === 'all'
104
- const cacheAllKey = `${dlmm_pool.package_id}_getBasePoolList`
105
- if (queryAll) {
106
- const cacheDate = this._sdk.getCache<DlmmBasePool[]>(cacheAllKey, force_refresh)
107
- if (cacheDate && cacheDate.length > 0) {
108
- dataPage.data.push(...cacheDate)
109
- return dataPage
110
- }
111
- }
112
-
113
- try {
114
- const moveEventType = `${dlmm_pool.package_id}::registry::CreatePoolEvent`
115
- const res = await this._sdk.FullClient.queryEventsByPage({ MoveEventType: moveEventType }, pagination_args)
116
- dataPage.has_next_page = res.has_next_page
117
- dataPage.next_cursor = res.next_cursor
118
- res.data.forEach((object) => {
119
- const pool = parseDlmmBasePool(object)
120
- dataPage.data.push(pool)
121
- })
122
-
123
- if (queryAll) {
124
- this._sdk.updateCache(`${dlmm_pool.package_id}_getPoolImmutables`, dataPage.data)
125
- }
126
- } catch (error) {
127
- return handleError(DlmmErrorCode.FetchError, error as Error, {
128
- [DETAILS_KEYS.METHOD_NAME]: 'getBasePoolList',
129
- })
130
- }
131
-
132
- return dataPage
133
- }
134
-
135
- /**
136
- * Get the list of DLMM pools
137
- * @param pagination_args - The pagination arguments
138
- * @returns The list of DLMM pools
139
- */
140
- async getPools(pagination_args: PaginationArgs = 'all', force_refresh = false): Promise<DataPage<DlmmPool>> {
141
- const dataPage: DataPage<DlmmPool> = {
142
- data: [],
143
- has_next_page: false,
144
- }
145
-
146
- const basePoolPage = await this.getBasePoolList(pagination_args, force_refresh)
147
- if (basePoolPage.data.length === 0) {
148
- return dataPage
149
- }
150
-
151
- try {
152
- const res = await this._sdk.FullClient.batchGetObjects(
153
- basePoolPage.data.map((item) => item.id),
154
- {
155
- showContent: true,
156
- showType: true,
157
- }
158
- )
159
- dataPage.has_next_page = basePoolPage.has_next_page
160
- dataPage.next_cursor = basePoolPage.next_cursor
161
- for (const suiObj of res) {
162
- const pool = parseDlmmPool(suiObj)
163
- const cacheKey = `${pool.id}_getDlmmPool`
164
- this._sdk.updateCache(cacheKey, pool)
165
- dataPage.data.push(pool)
166
- }
167
- } catch (error) {
168
- return handleError(DlmmErrorCode.FetchError, error as Error, {
169
- [DETAILS_KEYS.METHOD_NAME]: 'getPools',
170
- })
171
- }
172
-
173
- return dataPage
174
- }
175
-
176
- /**
177
- * Get the bin info by bin id
178
- * @param bin_manager_handle - The bin manager handle
179
- * @param bin_id - The bin id
180
- * @param bin_step - The bin step
181
- * @param force_refresh - Whether to force a refresh of the cache
182
- * @returns The bin info
183
- */
184
- async getBinInfo(bin_manager_handle: string, bin_id: number, bin_step: number, force_refresh = true): Promise<BinAmount> {
185
- try {
186
- const cacheKey = `${bin_manager_handle}_getBinInfo_${bin_id}`
187
- const cacheData = this._sdk.getCache<BinAmount>(cacheKey, force_refresh)
188
- if (cacheData !== undefined) {
189
- return cacheData
190
- }
191
-
192
- const score = BinUtils.binScore(bin_id)
193
- const [groupIndex, offsetInGroup] = BinUtils.resolveBinPosition(score)
194
-
195
- const res: any = await this._sdk.FullClient.getDynamicFieldObject({
196
- parentId: bin_manager_handle,
197
- name: { type: 'u64', value: groupIndex },
198
- })
199
- const fields = res.data.content.fields.value.fields.value.fields.group.fields.bins[offsetInGroup].fields
200
- const bin_info = parseBinInfo(fields)
201
- this._sdk.updateCache(cacheKey, bin_info)
202
- return bin_info
203
- } catch (error) {
204
- return {
205
- bin_id,
206
- amount_a: '0',
207
- amount_b: '0',
208
- liquidity: '0',
209
- price_per_lamport: BinUtils.getPricePerLamportFromBinId(bin_id, bin_step),
210
- }
211
- }
212
- }
213
-
214
- async getTotalFeeRate(option: GetTotalFeeRateOption): Promise<FeeRate> {
215
- const { dlmm_pool } = this._sdk.sdkOptions
216
- const { pool_id, coin_type_a, coin_type_b } = option
217
- const tx: Transaction = new Transaction()
218
- tx.moveCall({
219
- target: `${dlmm_pool.published_at}::pool::get_total_fee_rate`,
220
- arguments: [tx.object(pool_id)],
221
- typeArguments: [coin_type_a, coin_type_b],
222
- })
223
-
224
- const res = await this._sdk.FullClient.devInspectTransactionBlock({
225
- transactionBlock: tx,
226
- sender: normalizeSuiAddress('0x0'),
227
- })
228
- const bcsFeeRate = bcs.struct('FeeRate', {
229
- base_fee_rate: bcs.u64(),
230
- var_fee_rate: bcs.u64(),
231
- total_fee_rate: bcs.u64(),
232
- })
233
-
234
- const feeRate = bcsFeeRate.parse(Uint8Array.from(res.results![0].returnValues![0][0]))
235
-
236
- return feeRate
237
- }
238
-
239
- async getPoolBinInfo(option: GetPoolBinInfoOption): Promise<BinAmount[]> {
240
- const { dlmm_pool } = this._sdk.sdkOptions
241
- const { pool_id, coin_type_a, coin_type_b } = option
242
- const limit = 1000
243
- const bin_infos: BinAmount[] = []
244
- let start_bin_id: number | undefined = undefined
245
- let hasNext = true
246
- while (hasNext) {
247
- const tx: Transaction = new Transaction()
248
- let start_bin
249
- if (start_bin_id !== undefined) {
250
- start_bin = tx.moveCall({
251
- target: `0x1::option::some`,
252
- arguments: [tx.pure.u32(Number(asUintN(BigInt(start_bin_id))))],
253
- typeArguments: ['u32'],
254
- })
255
- } else {
256
- start_bin = tx.moveCall({
257
- target: `0x1::option::none`,
258
- typeArguments: ['u32'],
259
- })
260
- }
261
-
262
- tx.moveCall({
263
- target: `${dlmm_pool.published_at}::pool::fetch_bins`,
264
- arguments: [tx.object(pool_id), start_bin, tx.pure.u64(limit)],
265
- typeArguments: [coin_type_a, coin_type_b],
266
- })
267
- const res = await this._sdk.FullClient.devInspectTransactionBlock({
268
- transactionBlock: tx,
269
- sender: normalizeSuiAddress('0x0'),
270
- })
271
-
272
- const list = parseBinInfoList(res)
273
- bin_infos.push(...list)
274
- start_bin_id = list.length > 0 ? list[list.length - 1].bin_id + 1 : undefined
275
- hasNext = list.length === limit
276
- }
277
-
278
- return bin_infos.sort((a, b) => a.bin_id - b.bin_id)
279
- }
280
-
281
- async getPoolTransactionList({
282
- pool_id,
283
- pagination_args,
284
- order = 'descending',
285
- full_rpc_url,
286
- }: {
287
- pool_id: string
288
- full_rpc_url?: string
289
- pagination_args: PageQuery
290
- order?: 'ascending' | 'descending' | null | undefined
291
- }): Promise<DataPage<PoolTransactionInfo>> {
292
- const { FullClient: fullClient, sdkOptions } = this._sdk
293
- let client
294
- if (full_rpc_url) {
295
- client = createFullClient(new SuiClient({ url: full_rpc_url }))
296
- } else {
297
- client = fullClient
298
- }
299
- const data: DataPage<PoolTransactionInfo> = {
300
- data: [],
301
- has_next_page: false,
302
- }
303
-
304
- const limit = 50
305
- const query = pagination_args
306
- const user_limit = pagination_args.limit || 10
307
- do {
308
- const res = await client.queryTransactionBlocksByPage({ ChangedObject: pool_id }, { ...query, limit: 50 }, order)
309
- res.data.forEach((item, index) => {
310
- const dataList = parsePoolTransactionInfo(item, index, sdkOptions.dlmm_pool.package_id, pool_id)
311
- data.data = [...data.data, ...dataList]
312
- })
313
- data.has_next_page = res.has_next_page
314
- data.next_cursor = res.next_cursor
315
- query.cursor = res.next_cursor
316
- } while (data.data.length < user_limit && data.has_next_page)
317
-
318
- if (data.data.length > user_limit) {
319
- data.data = data.data.slice(0, user_limit)
320
- data.has_next_page = true
321
- }
322
- if (data.data.length > 0) {
323
- data.next_cursor = data.data[data.data.length - 1].tx
324
- }
325
-
326
- return data
327
- }
328
-
329
- /**
330
- * Get the bin info by range (TODO: need to optimize this method)
331
- * @param bin_manager_handle - The bin manager handle
332
- * @param lower_bin_id - The lower bin id
333
- * @param upper_bin_id - The upper bin id
334
- * @param bin_step - The bin step
335
- * @returns The bin info by range
336
- */
337
- async getRangeBinInfo(bin_manager_handle: string, lower_bin_id: number, upper_bin_id: number, bin_step: number): Promise<BinAmount[]> {
338
- const bin_infos: BinAmount[] = []
339
- for (let bin_id = lower_bin_id; bin_id <= upper_bin_id; bin_id++) {
340
- const bin_info = await Promise.all([this.getBinInfo(bin_manager_handle, bin_id, bin_step)])
341
- bin_infos.push(...bin_info)
342
- }
343
- return bin_infos
344
- }
345
-
346
- /**
347
- * Get the list of DLMM pools by assign pool ids
348
- * @param assign_pool_ids - The assign pool ids
349
- * @returns The list of DLMM pools
350
- */
351
- async getAssignPoolList(assign_pool_ids: string[]): Promise<DlmmPool[]> {
352
- if (assign_pool_ids.length === 0) {
353
- return []
354
- }
355
-
356
- const allPool: DlmmPool[] = []
357
-
358
- try {
359
- const res = await this._sdk.FullClient.batchGetObjects(assign_pool_ids, {
360
- showContent: true,
361
- showType: true,
362
- })
363
- for (const suiObj of res) {
364
- const pool = parseDlmmPool(suiObj)
365
- const cacheKey = `${pool.id}_getDlmmPool`
366
- this._sdk.updateCache(cacheKey, pool)
367
- allPool.push(pool)
368
- }
369
- } catch (error) {
370
- return handleError(DlmmErrorCode.FetchError, error as Error, {
371
- [DETAILS_KEYS.METHOD_NAME]: 'getAssignPoolList',
372
- })
373
- }
374
-
375
- return allPool
376
- }
377
-
378
- /**
379
- * Get a DLMM pool by its object ID.
380
- * @param {string} pool_id The object ID of the pool to get.
381
- * @param {true} force_refresh Whether to force a refresh of the cache.
382
- * @returns {Promise<DlmmPool>} A promise that resolves to a DlmmPool object.
383
- */
384
- async getPool(pool_id: string, force_refresh = true): Promise<DlmmPool> {
385
- try {
386
- const cacheKey = `${pool_id}_getDlmmPool`
387
- const cacheData = this._sdk.getCache<DlmmPool>(cacheKey, force_refresh)
388
- if (cacheData !== undefined) {
389
- return cacheData
390
- }
391
- const suiObj = await this._sdk.FullClient.getObject({
392
- id: pool_id,
393
- options: {
394
- showType: true,
395
- showContent: true,
396
- },
397
- })
398
- const pool = parseDlmmPool(suiObj)
399
- this._sdk.updateCache(cacheKey, pool)
400
- return pool
401
- } catch (error) {
402
- return handleError(DlmmErrorCode.FetchError, error as Error, {
403
- [DETAILS_KEYS.METHOD_NAME]: 'getPool',
404
- [DETAILS_KEYS.REQUEST_PARAMS]: pool_id,
405
- })
406
- }
407
- }
408
-
409
- /**
410
- * Create a pool and add liquidity with a given price
411
- * @param option - The option for creating a pool and adding liquidity with a given price
412
- * @returns The transaction for creating a pool and adding liquidity with a given price
413
- */
414
- async createPoolAndAddWithPricePayload(option: CreatePoolAndAddWithPriceOption): Promise<Transaction> {
415
- const {
416
- bin_step,
417
- url,
418
- coin_type_a,
419
- coin_type_b,
420
- bin_infos,
421
- price_base_coin,
422
- price,
423
- lower_price,
424
- upper_price,
425
- decimals_a,
426
- decimals_b,
427
- strategy_type,
428
- use_bin_infos,
429
- base_factor,
430
- pool_id,
431
- } = option
432
-
433
- let lower_bin_id
434
- let upper_bin_id
435
- let active_id
436
- let new_bin_infos: BinLiquidityInfo = bin_infos
437
-
438
- const is_coin_a_base = price_base_coin === 'coin_a'
439
-
440
- if (is_coin_a_base) {
441
- lower_bin_id = BinUtils.getBinIdFromPrice(lower_price, bin_step, false, decimals_a, decimals_b)
442
- upper_bin_id = BinUtils.getBinIdFromPrice(upper_price, bin_step, true, decimals_a, decimals_b)
443
- active_id = BinUtils.getBinIdFromPrice(price, bin_step, true, decimals_a, decimals_b)
444
- } else {
445
- lower_bin_id = BinUtils.getBinIdFromPrice(d(1).div(upper_price).toString(), bin_step, false, decimals_a, decimals_b)
446
- upper_bin_id = BinUtils.getBinIdFromPrice(d(1).div(lower_price).toString(), bin_step, true, decimals_a, decimals_b)
447
- active_id = BinUtils.getBinIdFromPrice(d(1).div(price).toString(), bin_step, false, decimals_a, decimals_b)
448
-
449
- const calculateOption: CalculateAddLiquidityOption = {
450
- pool_id,
451
- amount_a: bin_infos.amount_b,
452
- amount_b: bin_infos.amount_a,
453
- active_id,
454
- bin_step,
455
- lower_bin_id,
456
- upper_bin_id,
457
- active_bin_of_pool: undefined,
458
- strategy_type: option.strategy_type,
459
- }
460
-
461
- new_bin_infos = await this.sdk.Position.calculateAddLiquidityInfo(calculateOption)
462
- }
463
-
464
- const createPoolAndAddOption: CreatePoolAndAddOption = {
465
- bin_step,
466
- url,
467
- coin_type_a,
468
- coin_type_b,
469
- bin_infos: new_bin_infos,
470
- lower_bin_id,
471
- upper_bin_id,
472
- active_id,
473
- strategy_type,
474
- use_bin_infos,
475
- base_factor,
476
- }
477
-
478
- return this.createPoolAndAddLiquidityPayload(createPoolAndAddOption)
479
- }
480
-
481
- /**
482
- * Create a pool
483
- * @param option - The option for creating a pool
484
- * @param tx - The transaction object
485
- * @returns The transaction object
486
- */
487
- async createPoolPayload(option: CreatePoolOption, tx: Transaction): Promise<TransactionObjectArgument> {
488
- const { dlmm_pool } = this._sdk.sdkOptions
489
- const { bin_step, base_factor, url, coin_type_a, coin_type_b, active_id } = option
490
- tx = tx || new Transaction()
491
-
492
- const { registry_id, global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
493
-
494
- const coin_a_metadata = await this._sdk.FullClient.fetchCoinMetadata(coin_type_a)
495
- if (!coin_a_metadata?.id) {
496
- handleError(DlmmErrorCode.FetchError, new Error(`coin_a_metadata not found: ${coin_type_a}`), {
497
- [DETAILS_KEYS.METHOD_NAME]: 'createPoolAndAddLiquidityPayload',
498
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
499
- })
500
- }
501
- const coin_b_metadata = await this._sdk.FullClient.fetchCoinMetadata(coin_type_b)
502
- if (!coin_b_metadata?.id) {
503
- handleError(DlmmErrorCode.FetchError, new Error(`coin_b_metadata not found: ${coin_type_b}`), {
504
- [DETAILS_KEYS.METHOD_NAME]: 'createPoolAndAddLiquidityPayload',
505
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
506
- })
507
- }
508
-
509
- const [cert, pool_id] = tx.moveCall({
510
- target: `${dlmm_pool.published_at}::registry::create_pool`,
511
- arguments: [
512
- tx.object(registry_id),
513
- tx.object(coin_a_metadata!.id!),
514
- tx.object(coin_b_metadata!.id!),
515
- tx.pure.u16(bin_step),
516
- tx.pure.u16(base_factor),
517
- tx.pure.u32(Number(asUintN(BigInt(active_id)))),
518
- tx.pure.string(url || ''),
519
- tx.object(global_config_id),
520
- tx.object(versioned_id),
521
- tx.object(CLOCK_ADDRESS),
522
- ],
523
- typeArguments: [coin_type_a, coin_type_b],
524
- })
525
-
526
- tx.moveCall({
527
- target: `${dlmm_pool.published_at}::registry::destroy_receipt`,
528
- arguments: [tx.object(cert), pool_id, tx.object(versioned_id)],
529
- typeArguments: [coin_type_a, coin_type_b],
530
- })
531
-
532
- return pool_id
533
- }
534
-
535
- /**
536
- * Create a pool and add liquidity
537
- * @param option - The option for creating a pool and adding liquidity
538
- * @returns The transaction for creating a pool and adding liquidity
539
- */
540
- async createPoolAndAddLiquidityPayload(option: CreatePoolAndAddOption): Promise<Transaction> {
541
- const { dlmm_pool } = this._sdk.sdkOptions
542
- const {
543
- bin_step,
544
- base_factor,
545
- url,
546
- active_id,
547
- coin_type_a,
548
- coin_type_b,
549
- bin_infos,
550
- lower_bin_id,
551
- upper_bin_id,
552
- strategy_type,
553
- use_bin_infos,
554
- } = option
555
-
556
- const { registry_id, global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
557
-
558
- const tx = new Transaction()
559
-
560
- if (isSortedSymbols(fixCoinType(coin_type_a, false), fixCoinType(coin_type_b, false))) {
561
- handleError(DlmmErrorCode.InvalidCoinTypeSequence, new Error('invalid coin type sequence'), {
562
- [DETAILS_KEYS.METHOD_NAME]: 'createPoolAndAddLiquidityPayload',
563
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
564
- })
565
- }
566
-
567
- const width = upper_bin_id - lower_bin_id + 1
568
- if (width > MAX_BIN_PER_POSITION) {
569
- handleError(DlmmErrorCode.InvalidBinWidth, new Error('Width is too large'), {
570
- [DETAILS_KEYS.METHOD_NAME]: 'openPosition',
571
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
572
- })
573
- }
574
-
575
- const coin_a_metadata = await this._sdk.FullClient.fetchCoinMetadata(coin_type_a)
576
- if (!coin_a_metadata?.id) {
577
- handleError(DlmmErrorCode.FetchError, new Error(`coin_a_metadata not found: ${coin_type_a}`), {
578
- [DETAILS_KEYS.METHOD_NAME]: 'createPoolAndAddLiquidityPayload',
579
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
580
- })
581
- }
582
- const coin_b_metadata = await this._sdk.FullClient.fetchCoinMetadata(coin_type_b)
583
- if (!coin_b_metadata?.id) {
584
- handleError(DlmmErrorCode.FetchError, new Error(`coin_b_metadata not found: ${coin_type_b}`), {
585
- [DETAILS_KEYS.METHOD_NAME]: 'createPoolAndAddLiquidityPayload',
586
- [DETAILS_KEYS.REQUEST_PARAMS]: option,
587
- })
588
- }
589
-
590
- console.log('🚀 ~ createPoolAndAddLiquidityPayload ~ option:', {
591
- ...option,
592
- width,
593
- })
594
-
595
- // create pool
596
- const pool_id = await this.createPoolPayload(
597
- {
598
- active_id,
599
- bin_step,
600
- base_factor,
601
- coin_type_a,
602
- coin_type_b,
603
- },
604
- tx
605
- )
606
-
607
- // add liquidity
608
- const addOption: OpenAndAddLiquidityOption = {
609
- pool_id,
610
- bin_infos,
611
- coin_type_a,
612
- coin_type_b,
613
- lower_bin_id,
614
- upper_bin_id,
615
- active_id,
616
- strategy_type,
617
- use_bin_infos,
618
- max_price_slippage: 0,
619
- bin_step,
620
- }
621
- this.sdk.Position.addLiquidityPayload(addOption, tx)
622
-
623
- return tx
624
- }
625
- }