@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
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cetusprotocol/dlmm-sdk",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "SDK for cetus dlmm",
|
|
5
|
+
"typings": "dist/index.d.ts",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.mjs",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"lint": "eslint src/*.ts src/**/*.ts",
|
|
11
|
+
"lint:fix": "eslint src/*.ts src/**/*.ts --fix",
|
|
12
|
+
"build": "pnpm run build:tsup",
|
|
13
|
+
"build:tsup": "npm run build:clean && npm run _build:node",
|
|
14
|
+
"build:clean": "rm -rf dist",
|
|
15
|
+
"_build:node": "tsup --format cjs,esm --dts",
|
|
16
|
+
"build:doc": "npx typedoc",
|
|
17
|
+
"publish_batch:test": "npm publish --tag experimental",
|
|
18
|
+
"publish:test": "node ../../scripts/version.js burn && npm publish --tag experimental"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": ""
|
|
23
|
+
},
|
|
24
|
+
"keywords": [],
|
|
25
|
+
"author": "test",
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@cetusprotocol/common-sdk": "workspace:*",
|
|
29
|
+
"@cetusprotocol/test-utils": "workspace:*"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@cetusprotocol/common-sdk": "*",
|
|
33
|
+
"@mysten/sui": "*"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SdkOptions } from '../sdk'
|
|
2
|
+
import { FullRpcUrlMainnet } from '@cetusprotocol/common-sdk'
|
|
3
|
+
// mainnet
|
|
4
|
+
export const dlmmMainnet: SdkOptions = {
|
|
5
|
+
env: 'mainnet',
|
|
6
|
+
full_rpc_url: FullRpcUrlMainnet,
|
|
7
|
+
dlmm_pool: {
|
|
8
|
+
package_id: '0x8eb878ff59ecd9326697dc80f9df55ccfb050a711bb712981b1dc509f393cbdf',
|
|
9
|
+
published_at: '0xa0f34a9aa8cd17e92c87bd93957c8daeba7fd45c2dbbba73f45ff47a93526c6b',
|
|
10
|
+
version: 1,
|
|
11
|
+
config: {
|
|
12
|
+
registry_id: '0xd436e82a6ef89b0ad4a1b8bd19b0e8e1c662e4a358a8dfe8221b1833abf3685b',
|
|
13
|
+
pools_id: '0x09bb9bde6875ba06551617aec1ea7a653cda0f526ae7ce42bdc6ef4a713a3f19',
|
|
14
|
+
global_config_id: '0xbf90603a8a7488a1440ed88e1d4c8d7b1f0c4cfd827dbb1c70655acc90258cc2',
|
|
15
|
+
versioned_id: '0x8bdb4c81a2d507dbbf4a5762e64a3c39cae1fd201b47aae474eff8b31e1f384a',
|
|
16
|
+
admin_cap_id: '0xec28f827eb125c80f316a0356b84eb52b98c6157bd443095843b1a3788518655',
|
|
17
|
+
partners_id: '0x19c6643ead4b13f4066db107a8606694cff3a2d81b7f11c759da460b5ea23491',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
dlmm_router: {
|
|
21
|
+
package_id: '0xb269f062e8f7fd2bd653a79405dea7c48cb0bf62b11e33c939637d52b1560a65',
|
|
22
|
+
published_at: '0xb269f062e8f7fd2bd653a79405dea7c48cb0bf62b11e33c939637d52b1560a65',
|
|
23
|
+
version: 1,
|
|
24
|
+
},
|
|
25
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { FullRpcUrlTestnet } from '@cetusprotocol/common-sdk'
|
|
2
|
+
import type { SdkOptions } from '../sdk'
|
|
3
|
+
|
|
4
|
+
export const dlmmTestnet: SdkOptions = {
|
|
5
|
+
env: 'testnet',
|
|
6
|
+
full_rpc_url: FullRpcUrlTestnet,
|
|
7
|
+
dlmm_pool: {
|
|
8
|
+
package_id: '0x05a79a247dc4c0daf0c03912cc2f14ebb3d0ca925482c3c09f516b16825bfc45',
|
|
9
|
+
published_at: '0x534b3ca6b921257b33db4d0d0ac5b4f37c1eb15c22c5a55b38faba8d9f91b1c3',
|
|
10
|
+
version: 1,
|
|
11
|
+
config: {
|
|
12
|
+
registry_id: '0xcb49fbeebeb778fcae261c3a9fad8abe7e329ac0f86ef458e0bd5b587b0ad858',
|
|
13
|
+
pools_id: '0x7fc11580586ea9f0941be9973345ebf82cf5c479353b865a22e59cd720b8bf04',
|
|
14
|
+
global_config_id: '0xac39b660d54aadaaef08b17605d0c91c102797be18b00b590647321f11948c35',
|
|
15
|
+
versioned_id: '0xc1d8eae3c5e6b5322136bbe71c3d348b1061695cf199e9c4d5efd1139b294edb',
|
|
16
|
+
admin_cap_id: '0x570ce0b84182892b8af58fd74307a144aa36b330141bfa01d0f0c1978d2325a8',
|
|
17
|
+
partners_id: '0xd7833b08e9637f4431f6a69dad8f6d0f9f3b09c16387b33ac527411df0f99d12',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
dlmm_router: {
|
|
21
|
+
package_id: '0xe2c57dabc1ab2c48d17b50e418d77312c29d1f98594bdf5bffb643b07231ac09',
|
|
22
|
+
published_at: '0x3249405dc05e570e642bc8029f8ce0800ee1e7589e8988af8acc6730fd83dbf5',
|
|
23
|
+
version: 1,
|
|
24
|
+
},
|
|
25
|
+
faucet: {
|
|
26
|
+
package_id: '0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48',
|
|
27
|
+
published_at: '0x14a71d857b34677a7d57e0feb303df1adb515a37780645ab763d42ce8d1a5e48',
|
|
28
|
+
version: 1,
|
|
29
|
+
},
|
|
30
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BaseError } from '@cetusprotocol/common-sdk'
|
|
2
|
+
|
|
3
|
+
export enum DlmmErrorCode {
|
|
4
|
+
FetchError = 'FetchError',
|
|
5
|
+
ParseError = 'ParseError',
|
|
6
|
+
InvalidStrategyParams = 'InvalidStrategyParams',
|
|
7
|
+
InvalidBinWidth = 'InvalidBinWidth',
|
|
8
|
+
GetObjectError = 'GetObjectError',
|
|
9
|
+
InvalidBinId = 'InvalidBinId',
|
|
10
|
+
InsufficientLiquidity = 'InsufficientLiquidity',
|
|
11
|
+
InvalidParams = 'InvalidParams',
|
|
12
|
+
InvalidCoinTypeSequence = 'InvalidCoinTypeSequence',
|
|
13
|
+
NotFound = 'NotFound',
|
|
14
|
+
AmountTooSmall = 'AmountTooSmall',
|
|
15
|
+
LiquiditySupplyIsZero = 'LiquiditySupplyIsZero',
|
|
16
|
+
InvalidDeltaLiquidity = 'InvalidDeltaLiquidity',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class DlmmError extends BaseError {
|
|
20
|
+
constructor(message: string, error_code?: DlmmErrorCode, details?: Record<string, any>) {
|
|
21
|
+
super(message, error_code || 'UnknownError', details)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static isDlmmErrorCode(e: any, code: DlmmErrorCode): boolean {
|
|
25
|
+
return this.isErrorCode<DlmmError>(e, code)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const handleError = (code: DlmmErrorCode, error: Error | string, details?: Record<string, any>) => {
|
|
30
|
+
const errorDetails = {
|
|
31
|
+
...details,
|
|
32
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
throw new DlmmError(error.message, code, errorDetails)
|
|
37
|
+
} else {
|
|
38
|
+
throw new DlmmError(error, code, errorDetails)
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Transaction } from '@mysten/sui/transactions'
|
|
2
|
+
import { CLOCK_ADDRESS, DETAILS_KEYS, fixCoinType, getObjectFields, getPackagerConfigs, IModule } from '@cetusprotocol/common-sdk'
|
|
3
|
+
import { DlmmErrorCode, handleError } from '../errors/errors'
|
|
4
|
+
import { CetusDlmmSDK } from '../sdk'
|
|
5
|
+
import { BinStepConfig, DlmmConfigs, DlmmGlobalConfig, RewardWhiteListOption } from '../types/dlmm'
|
|
6
|
+
import { log } from 'console'
|
|
7
|
+
|
|
8
|
+
export class ConfigModule implements IModule<CetusDlmmSDK> {
|
|
9
|
+
protected _sdk: CetusDlmmSDK
|
|
10
|
+
|
|
11
|
+
constructor(sdk: CetusDlmmSDK) {
|
|
12
|
+
this._sdk = sdk
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
get sdk() {
|
|
16
|
+
return this._sdk
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Build the payload for adding or removing reward white list
|
|
21
|
+
* @param option - The option for adding or removing reward white list
|
|
22
|
+
* @param tx - The transaction to add the reward white list to
|
|
23
|
+
* @returns The transaction for adding or removing reward white list
|
|
24
|
+
*/
|
|
25
|
+
buildRewardWhiteListPayload(option: RewardWhiteListOption, tx?: Transaction): Transaction {
|
|
26
|
+
tx = tx || new Transaction()
|
|
27
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
28
|
+
const { global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
|
|
29
|
+
|
|
30
|
+
const { reward_coin_types, type } = option
|
|
31
|
+
|
|
32
|
+
reward_coin_types.forEach((reward_coin_type) => {
|
|
33
|
+
tx.moveCall({
|
|
34
|
+
target: `${dlmm_pool.published_at}::config::${type === 'add' ? 'add_reward_whitelist' : 'remove_reward_whitelist'}`,
|
|
35
|
+
arguments: [tx.object(global_config_id), tx.object(versioned_id)],
|
|
36
|
+
typeArguments: [reward_coin_type],
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
return tx
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async getBinStepConfigList(bin_steps_handle: string): Promise<BinStepConfig[]> {
|
|
44
|
+
const res = await this._sdk.FullClient.getDynamicFields({
|
|
45
|
+
parentId: bin_steps_handle,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const list: BinStepConfig[] = []
|
|
49
|
+
|
|
50
|
+
const bin_step_ids = res.data.map((item) => item.objectId)
|
|
51
|
+
|
|
52
|
+
if (bin_step_ids.length > 0) {
|
|
53
|
+
const bin_step_configs = await this._sdk.FullClient.batchGetObjects(bin_step_ids, {
|
|
54
|
+
showContent: true,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
bin_step_configs.forEach((item) => {
|
|
58
|
+
const fields = getObjectFields(item)
|
|
59
|
+
const bin_step_config: BinStepConfig = {
|
|
60
|
+
...fields.value.fields,
|
|
61
|
+
}
|
|
62
|
+
list.push(bin_step_config)
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return list
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get the list of bin step configs
|
|
71
|
+
* @returns The list of bin step configs
|
|
72
|
+
*/
|
|
73
|
+
async getDlmmGlobalConfig(): Promise<DlmmGlobalConfig> {
|
|
74
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
75
|
+
const { global_config_id } = getPackagerConfigs(dlmm_pool)
|
|
76
|
+
try {
|
|
77
|
+
const res = await this._sdk.FullClient.getObject({
|
|
78
|
+
id: global_config_id,
|
|
79
|
+
options: { showContent: true },
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const fields = getObjectFields(res)
|
|
83
|
+
|
|
84
|
+
const reward_config = fields.reward_config.fields
|
|
85
|
+
|
|
86
|
+
const white_list: string[] =
|
|
87
|
+
reward_config.reward_white_list?.fields?.contents?.map((item: any) => {
|
|
88
|
+
return fixCoinType(item.fields.key.fields.name, false) as string
|
|
89
|
+
}) || []
|
|
90
|
+
|
|
91
|
+
const globalConfig: DlmmGlobalConfig = {
|
|
92
|
+
id: fields.id.id,
|
|
93
|
+
acl: {
|
|
94
|
+
id: fields.acl.fields.permissions.fields.id.id,
|
|
95
|
+
size: fields.acl.fields.permissions.fields.size,
|
|
96
|
+
},
|
|
97
|
+
allowed_list: {
|
|
98
|
+
id: fields.allowed_list.fields.id.id,
|
|
99
|
+
size: fields.allowed_list.fields.size,
|
|
100
|
+
},
|
|
101
|
+
denied_list: {
|
|
102
|
+
id: fields.denied_list.fields.id.id,
|
|
103
|
+
size: fields.denied_list.fields.size,
|
|
104
|
+
},
|
|
105
|
+
bin_steps: {
|
|
106
|
+
id: fields.bin_steps.fields.id.id,
|
|
107
|
+
size: fields.bin_steps.fields.size,
|
|
108
|
+
},
|
|
109
|
+
reward_white_list: white_list,
|
|
110
|
+
blocked_position: {
|
|
111
|
+
id: fields.restriction.fields.blocked_position.fields.permissions.fields.id.id,
|
|
112
|
+
size: fields.restriction.fields.blocked_position.fields.permissions.fields.size,
|
|
113
|
+
},
|
|
114
|
+
blocked_user: {
|
|
115
|
+
id: fields.restriction.fields.blocked_user.fields.permissions.fields.id.id,
|
|
116
|
+
size: fields.restriction.fields.blocked_user.fields.permissions.fields.size,
|
|
117
|
+
},
|
|
118
|
+
min_reward_duration: Number(reward_config.min_reward_duration),
|
|
119
|
+
non_manager_initialize_reward_cap: Number(reward_config.non_manager_initialize_reward_cap),
|
|
120
|
+
reward_public: reward_config.reward_public,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return globalConfig
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.log('fetchGlobalConfig error: ', error)
|
|
126
|
+
return handleError(DlmmErrorCode.FetchError, error as Error, {
|
|
127
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getBinStepConfigs',
|
|
128
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: global_config_id,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Fetch the configs of the dlmm SDK
|
|
135
|
+
* @returns The configs of the dlmm
|
|
136
|
+
*/
|
|
137
|
+
async fetchDlmmSdkConfigs(): Promise<DlmmConfigs> {
|
|
138
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
139
|
+
const configs: DlmmConfigs = {
|
|
140
|
+
registry_id: '',
|
|
141
|
+
pools_id: '',
|
|
142
|
+
global_config_id: '',
|
|
143
|
+
versioned_id: '',
|
|
144
|
+
admin_cap_id: '',
|
|
145
|
+
partners_id: '',
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const res = await this._sdk.FullClient.getObject({
|
|
149
|
+
id: dlmm_pool.package_id,
|
|
150
|
+
options: { showContent: true, showPreviousTransaction: true },
|
|
151
|
+
})
|
|
152
|
+
const tx_digest = res.data?.previousTransaction!
|
|
153
|
+
const txRes = await this._sdk.FullClient.getTransactionBlock({ digest: tx_digest, options: { showEvents: true } })
|
|
154
|
+
txRes.events?.forEach((event) => {
|
|
155
|
+
const type = event.type
|
|
156
|
+
const parsedJson = event.parsedJson as any
|
|
157
|
+
|
|
158
|
+
if (type.includes('versioned::InitEvent')) {
|
|
159
|
+
configs.versioned_id = parsedJson.versioned
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (type.includes('partner::InitPartnerEvent')) {
|
|
163
|
+
configs.partners_id = parsedJson.partners_id
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (type.includes('config::InitEvent')) {
|
|
167
|
+
configs.global_config_id = parsedJson.config_id
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (type.includes('admin_cap::InitEvent')) {
|
|
171
|
+
configs.admin_cap_id = parsedJson.admin_cap_id
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (type.includes('registry::RegistryEvent')) {
|
|
175
|
+
configs.registry_id = parsedJson.pools_id
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
const registerRes = await this._sdk.FullClient.getObject({ id: configs.registry_id, options: { showContent: true } })
|
|
179
|
+
const registerFields = getObjectFields(registerRes)
|
|
180
|
+
configs.pools_id = registerFields.pools.fields.id.id
|
|
181
|
+
|
|
182
|
+
return configs
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './poolModule'
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { Transaction } from '@mysten/sui/transactions'
|
|
2
|
+
import { CLOCK_ADDRESS, d, DETAILS_KEYS, fixCoinType, getObjectFields, getPackagerConfigs, IModule } from '@cetusprotocol/common-sdk'
|
|
3
|
+
import { DlmmErrorCode, handleError } from '../errors/errors'
|
|
4
|
+
import { parsePartner } from '../utils'
|
|
5
|
+
import { CetusDlmmSDK } from '../sdk'
|
|
6
|
+
import { ClaimRefFeeOption, CreatePartnerOption, Partner, UpdateRefFeeRateOption, UpdateTimeRangeOption } from '../types/dlmm'
|
|
7
|
+
import { BASIS_POINT } from '../types/constants'
|
|
8
|
+
|
|
9
|
+
export class PartnerModule implements IModule<CetusDlmmSDK> {
|
|
10
|
+
protected _sdk: CetusDlmmSDK
|
|
11
|
+
|
|
12
|
+
constructor(sdk: CetusDlmmSDK) {
|
|
13
|
+
this._sdk = sdk
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get sdk() {
|
|
17
|
+
return this._sdk
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get a list of partners.
|
|
22
|
+
* @returns {Promise<Partner[]>} A promise that resolves to an array of Partner objects.
|
|
23
|
+
*/
|
|
24
|
+
async getPartnerList(): Promise<Partner[]> {
|
|
25
|
+
const allPartner: Partner[] = []
|
|
26
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
27
|
+
const { partners_id } = getPackagerConfigs(dlmm_pool)
|
|
28
|
+
try {
|
|
29
|
+
const res = await this._sdk.FullClient.getObject({ id: partners_id, options: { showContent: true } })
|
|
30
|
+
const fields = getObjectFields(res)
|
|
31
|
+
const warpIds = fields.partners.fields.contents.map((item: any) => {
|
|
32
|
+
return item.fields.value
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
if (warpIds.length > 0) {
|
|
36
|
+
const res = await this._sdk.FullClient.batchGetObjects(warpIds, {
|
|
37
|
+
showContent: true,
|
|
38
|
+
showType: true,
|
|
39
|
+
})
|
|
40
|
+
res.forEach((item) => {
|
|
41
|
+
const partner = parsePartner(item)
|
|
42
|
+
allPartner.push(partner)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return handleError(DlmmErrorCode.FetchError, error as Error, {
|
|
47
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getPartnerList',
|
|
48
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: partners_id,
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return allPartner
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get the partner cap ID for a given owner and partner ID.
|
|
57
|
+
* @param owner - The owner of the partner.
|
|
58
|
+
* @param partner_id - The ID of the partner.
|
|
59
|
+
* @returns A promise that resolves to the partner cap ID or undefined if not found.
|
|
60
|
+
*/
|
|
61
|
+
async getPartnerCapId(owner: string, partner_id: string): Promise<string> {
|
|
62
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
63
|
+
try {
|
|
64
|
+
const cacheKey = `partner_cap_id_${owner}_${partner_id}`
|
|
65
|
+
const cached = this._sdk.getCache<string>(cacheKey)
|
|
66
|
+
if (cached) {
|
|
67
|
+
return cached
|
|
68
|
+
}
|
|
69
|
+
const res = await this._sdk.FullClient.getOwnedObjects({
|
|
70
|
+
owner,
|
|
71
|
+
options: {
|
|
72
|
+
showContent: true,
|
|
73
|
+
showType: true,
|
|
74
|
+
},
|
|
75
|
+
filter: {
|
|
76
|
+
StructType: `${dlmm_pool.package_id}::partner::PartnerCap`,
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
let partnerCapId = undefined
|
|
80
|
+
res.data.forEach((item) => {
|
|
81
|
+
const fields = getObjectFields(item)
|
|
82
|
+
if (fields.partner_id === partner_id) {
|
|
83
|
+
partnerCapId = fields.id.id
|
|
84
|
+
this._sdk.updateCache(cacheKey, partnerCapId)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
if (!partnerCapId) {
|
|
88
|
+
return handleError(DlmmErrorCode.NotFound, new Error('Partner cap not found'), {
|
|
89
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getPartnerCapId',
|
|
90
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: {
|
|
91
|
+
owner,
|
|
92
|
+
partner_id,
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
return partnerCapId
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return handleError(DlmmErrorCode.FetchError, error as Error, {
|
|
99
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getPartnerCapId',
|
|
100
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: {
|
|
101
|
+
owner,
|
|
102
|
+
partner_id,
|
|
103
|
+
},
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the balance of a partner
|
|
110
|
+
* @param partner_balance_handle - The handle of the partner balance
|
|
111
|
+
* @returns A promise that resolves to an array of { coin_type: string; balance: string } objects.
|
|
112
|
+
*/
|
|
113
|
+
async getPartnerBalance(partner_balance_handle: string) {
|
|
114
|
+
try {
|
|
115
|
+
const res = await this._sdk.FullClient.getDynamicFieldsByPage(partner_balance_handle)
|
|
116
|
+
|
|
117
|
+
const balanceList: { coin_type: string; balance: string }[] = []
|
|
118
|
+
|
|
119
|
+
const warpIds = res.data.map((item) => item.objectId)
|
|
120
|
+
|
|
121
|
+
if (warpIds.length > 0) {
|
|
122
|
+
const res = await this._sdk.FullClient.batchGetObjects(warpIds, {
|
|
123
|
+
showContent: true,
|
|
124
|
+
showType: true,
|
|
125
|
+
})
|
|
126
|
+
res.forEach((item) => {
|
|
127
|
+
const fields = getObjectFields(item)
|
|
128
|
+
console.log(fields)
|
|
129
|
+
balanceList.push({
|
|
130
|
+
coin_type: fixCoinType(fields.name, false),
|
|
131
|
+
balance: fields.value,
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
return balanceList
|
|
136
|
+
} catch (error) {
|
|
137
|
+
return handleError(DlmmErrorCode.FetchError, error as Error, {
|
|
138
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getPartnerBalance',
|
|
139
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: partner_balance_handle,
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Get a partner by its object ID.
|
|
146
|
+
* @param {string} partner_id The object ID of the partner to get.
|
|
147
|
+
* @returns {Promise<Partner>} A promise that resolves to a Partner object.
|
|
148
|
+
*/
|
|
149
|
+
async getPartner(partner_id: string): Promise<Partner> {
|
|
150
|
+
try {
|
|
151
|
+
const res = await this._sdk.FullClient.getObject({ id: partner_id, options: { showContent: true } })
|
|
152
|
+
const partner = parsePartner(res)
|
|
153
|
+
return partner
|
|
154
|
+
} catch (error) {
|
|
155
|
+
return handleError(DlmmErrorCode.FetchError, error as Error, {
|
|
156
|
+
[DETAILS_KEYS.METHOD_NAME]: 'getPartner',
|
|
157
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: partner_id,
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Update the ref fee rate of a partner
|
|
164
|
+
* @param option - The option for updating the ref fee rate
|
|
165
|
+
* @returns The transaction for updating the ref fee rate
|
|
166
|
+
*/
|
|
167
|
+
updateRefFeeRatePayload(option: UpdateRefFeeRateOption, tx?: Transaction): Transaction {
|
|
168
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
169
|
+
const { partner_id, ref_fee_rate } = option
|
|
170
|
+
|
|
171
|
+
const { global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
|
|
172
|
+
|
|
173
|
+
tx = tx || new Transaction()
|
|
174
|
+
|
|
175
|
+
const ref_fee_rate_raw = d(ref_fee_rate).mul(BASIS_POINT)
|
|
176
|
+
if (ref_fee_rate_raw.gt(BASIS_POINT)) {
|
|
177
|
+
return handleError(DlmmErrorCode.InvalidParams, new Error('ref_fee_rate is cannot be greater than 1'), {
|
|
178
|
+
[DETAILS_KEYS.METHOD_NAME]: 'updateRefFeeRatePayload',
|
|
179
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: option,
|
|
180
|
+
})
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
tx.moveCall({
|
|
184
|
+
target: `${dlmm_pool.published_at}::partner::update_ref_fee_rate`,
|
|
185
|
+
arguments: [tx.object(partner_id), tx.pure.u64(ref_fee_rate_raw.toNumber()), tx.object(global_config_id), tx.object(versioned_id)],
|
|
186
|
+
typeArguments: [],
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
return tx
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Create a claim ref fee payload
|
|
194
|
+
* @param option - The option for claiming ref fee
|
|
195
|
+
* @returns The transaction for claiming ref fee
|
|
196
|
+
*/
|
|
197
|
+
async claimRefFeePayload(option: ClaimRefFeeOption) {
|
|
198
|
+
const { partner_id, partner_cap_id, fee_coin_types } = option
|
|
199
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
200
|
+
const { versioned_id } = getPackagerConfigs(dlmm_pool)
|
|
201
|
+
|
|
202
|
+
const tx = new Transaction()
|
|
203
|
+
|
|
204
|
+
let partnerCapId = partner_cap_id
|
|
205
|
+
if (!partnerCapId) {
|
|
206
|
+
partnerCapId = await this.getPartnerCapId(this._sdk.getSenderAddress(), partner_id)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fee_coin_types.forEach((coin_type) => {
|
|
210
|
+
tx.moveCall({
|
|
211
|
+
target: `${dlmm_pool.published_at}::partner::claim_ref_fee`,
|
|
212
|
+
arguments: [tx.object(partner_id), tx.object(partnerCapId), tx.object(versioned_id)],
|
|
213
|
+
typeArguments: [coin_type],
|
|
214
|
+
})
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
return tx
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
updateTimeRangePayload(option: UpdateTimeRangeOption, tx?: Transaction): Transaction {
|
|
221
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
222
|
+
const { partner_id, start_time, end_time } = option
|
|
223
|
+
|
|
224
|
+
const { global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
|
|
225
|
+
|
|
226
|
+
tx = tx || new Transaction()
|
|
227
|
+
|
|
228
|
+
const startTimeInSeconds = start_time > 1e12 ? Math.floor(start_time / 1000) : start_time
|
|
229
|
+
const endTimeInSeconds = end_time > 1e12 ? Math.floor(end_time / 1000) : end_time
|
|
230
|
+
|
|
231
|
+
if (endTimeInSeconds <= startTimeInSeconds) {
|
|
232
|
+
return handleError(DlmmErrorCode.InvalidParams, new Error('end_time must be greater than start_time'), {
|
|
233
|
+
[DETAILS_KEYS.METHOD_NAME]: 'createPartnerPayload',
|
|
234
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: option,
|
|
235
|
+
})
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
tx.moveCall({
|
|
239
|
+
target: `${dlmm_pool.published_at}::partner::update_time_range`,
|
|
240
|
+
arguments: [
|
|
241
|
+
tx.object(partner_id),
|
|
242
|
+
tx.pure.u64(startTimeInSeconds),
|
|
243
|
+
tx.pure.u64(endTimeInSeconds),
|
|
244
|
+
tx.object(global_config_id),
|
|
245
|
+
tx.object(versioned_id),
|
|
246
|
+
tx.object(CLOCK_ADDRESS),
|
|
247
|
+
],
|
|
248
|
+
typeArguments: [],
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
return tx
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Create a partner
|
|
256
|
+
* @param option - The option for creating a partner
|
|
257
|
+
* @returns The transaction for creating a partner
|
|
258
|
+
*/
|
|
259
|
+
createPartnerPayload(option: CreatePartnerOption): Transaction {
|
|
260
|
+
const { dlmm_pool } = this._sdk.sdkOptions
|
|
261
|
+
const { name, ref_fee_rate, start_time, end_time, recipient } = option
|
|
262
|
+
|
|
263
|
+
const { partners_id, global_config_id, versioned_id } = getPackagerConfigs(dlmm_pool)
|
|
264
|
+
|
|
265
|
+
const tx = new Transaction()
|
|
266
|
+
|
|
267
|
+
const ref_fee_rate_raw = d(ref_fee_rate).mul(BASIS_POINT)
|
|
268
|
+
if (ref_fee_rate_raw.gt(BASIS_POINT)) {
|
|
269
|
+
return handleError(DlmmErrorCode.InvalidParams, new Error('ref_fee_rate is cannot be greater than 1'), {
|
|
270
|
+
[DETAILS_KEYS.METHOD_NAME]: 'createPartnerPayload',
|
|
271
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: option,
|
|
272
|
+
})
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const startTimeInSeconds = start_time > 1e12 ? Math.floor(start_time / 1000) : start_time
|
|
276
|
+
const endTimeInSeconds = end_time > 1e12 ? Math.floor(end_time / 1000) : end_time
|
|
277
|
+
|
|
278
|
+
if (endTimeInSeconds <= startTimeInSeconds) {
|
|
279
|
+
return handleError(DlmmErrorCode.InvalidParams, new Error('end_time must be greater than start_time'), {
|
|
280
|
+
[DETAILS_KEYS.METHOD_NAME]: 'createPartnerPayload',
|
|
281
|
+
[DETAILS_KEYS.REQUEST_PARAMS]: option,
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
tx.moveCall({
|
|
286
|
+
target: `${dlmm_pool.published_at}::partner::create_partner`,
|
|
287
|
+
arguments: [
|
|
288
|
+
tx.object(partners_id),
|
|
289
|
+
tx.pure.string(name),
|
|
290
|
+
tx.pure.u64(ref_fee_rate_raw.toNumber()),
|
|
291
|
+
tx.pure.u64(startTimeInSeconds),
|
|
292
|
+
tx.pure.u64(endTimeInSeconds),
|
|
293
|
+
tx.pure.address(recipient),
|
|
294
|
+
tx.object(global_config_id),
|
|
295
|
+
tx.object(versioned_id),
|
|
296
|
+
],
|
|
297
|
+
typeArguments: [],
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
return tx
|
|
301
|
+
}
|
|
302
|
+
}
|