@cetusprotocol/aggregator-sdk 0.2.0 → 0.2.2
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/bun.lockb +0 -0
- package/dist/index.d.mts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.js +308 -207
- package/dist/index.mjs +307 -208
- package/dist/src/api.d.ts +8 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/msafe.d.ts +2 -0
- package/package.json +1 -1
- package/src/api.ts +124 -35
- package/src/client.ts +7 -2
- package/src/transaction/cetus.ts +3 -2
- package/src/transaction/swap.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/msafe.ts +31 -0
- package/tests/router.test.ts +93 -2
package/dist/src/api.d.ts
CHANGED
|
@@ -10,11 +10,19 @@ export interface FindRouterParams {
|
|
|
10
10
|
splitFactor?: number;
|
|
11
11
|
splitCount?: number;
|
|
12
12
|
providers?: string[];
|
|
13
|
+
liquidityChanges?: PreSwapLpChangeParams[];
|
|
14
|
+
}
|
|
15
|
+
export interface PreSwapLpChangeParams {
|
|
16
|
+
poolID: string;
|
|
17
|
+
ticklower: number;
|
|
18
|
+
tickUpper: number;
|
|
19
|
+
deltaLiquidity: number;
|
|
13
20
|
}
|
|
14
21
|
export type ExtendedDetails = {
|
|
15
22
|
aftermathPoolFlatness?: number;
|
|
16
23
|
aftermathLpSupplyType?: string;
|
|
17
24
|
turbosFeeType?: string;
|
|
25
|
+
afterSqrtPrice?: string;
|
|
18
26
|
};
|
|
19
27
|
export type Path = {
|
|
20
28
|
id: string;
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -18,12 +18,21 @@ export interface FindRouterParams {
|
|
|
18
18
|
splitFactor?: number
|
|
19
19
|
splitCount?: number
|
|
20
20
|
providers?: string[]
|
|
21
|
+
liquidityChanges?: PreSwapLpChangeParams[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PreSwapLpChangeParams {
|
|
25
|
+
poolID: string
|
|
26
|
+
ticklower: number
|
|
27
|
+
tickUpper: number
|
|
28
|
+
deltaLiquidity: number
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
export type ExtendedDetails = {
|
|
24
32
|
aftermathPoolFlatness?: number
|
|
25
33
|
aftermathLpSupplyType?: string
|
|
26
34
|
turbosFeeType?: string
|
|
35
|
+
afterSqrtPrice?: string
|
|
27
36
|
}
|
|
28
37
|
|
|
29
38
|
export type Path = {
|
|
@@ -69,45 +78,17 @@ export async function getRouterResult(
|
|
|
69
78
|
endpoint: string,
|
|
70
79
|
params: FindRouterParams
|
|
71
80
|
): Promise<RouterData | null> {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
depth,
|
|
78
|
-
splitAlgorithm,
|
|
79
|
-
splitFactor,
|
|
80
|
-
splitCount,
|
|
81
|
-
providers,
|
|
82
|
-
} = params
|
|
83
|
-
const fromCoin = completionCoin(from)
|
|
84
|
-
const targetCoin = completionCoin(target)
|
|
85
|
-
|
|
86
|
-
let url = `${endpoint}?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`
|
|
87
|
-
|
|
88
|
-
if (depth) {
|
|
89
|
-
url += `&depth=${depth}`
|
|
81
|
+
let response
|
|
82
|
+
if (params.liquidityChanges && params.liquidityChanges.length > 0) {
|
|
83
|
+
response = await postRouterWithLiquidityChanges(endpoint, params)
|
|
84
|
+
} else {
|
|
85
|
+
response = await getRouter(endpoint, params)
|
|
90
86
|
}
|
|
91
87
|
|
|
92
|
-
if (
|
|
93
|
-
|
|
88
|
+
if (!response) {
|
|
89
|
+
return null
|
|
94
90
|
}
|
|
95
91
|
|
|
96
|
-
if (splitFactor) {
|
|
97
|
-
url += `&split_factor=${splitFactor}`
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (splitCount) {
|
|
101
|
-
url += `&split_count=${splitCount}`
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (providers) {
|
|
105
|
-
if (providers.length > 0) {
|
|
106
|
-
url += `&providers=${providers.join(",")}`
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const response = await fetch(url)
|
|
111
92
|
if (!response.ok) {
|
|
112
93
|
return {
|
|
113
94
|
amountIn: ZERO,
|
|
@@ -142,3 +123,111 @@ export async function getRouterResult(
|
|
|
142
123
|
},
|
|
143
124
|
}
|
|
144
125
|
}
|
|
126
|
+
|
|
127
|
+
async function getRouter(endpoint: string, params: FindRouterParams) {
|
|
128
|
+
try {
|
|
129
|
+
const {
|
|
130
|
+
from,
|
|
131
|
+
target,
|
|
132
|
+
amount,
|
|
133
|
+
byAmountIn,
|
|
134
|
+
depth,
|
|
135
|
+
splitAlgorithm,
|
|
136
|
+
splitFactor,
|
|
137
|
+
splitCount,
|
|
138
|
+
providers,
|
|
139
|
+
} = params
|
|
140
|
+
const fromCoin = completionCoin(from)
|
|
141
|
+
const targetCoin = completionCoin(target)
|
|
142
|
+
|
|
143
|
+
let url = `${endpoint}?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`
|
|
144
|
+
|
|
145
|
+
if (depth) {
|
|
146
|
+
url += `&depth=${depth}`
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (splitAlgorithm) {
|
|
150
|
+
url += `&split_algorithm=${splitAlgorithm}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (splitFactor) {
|
|
154
|
+
url += `&split_factor=${splitFactor}`
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (splitCount) {
|
|
158
|
+
url += `&split_count=${splitCount}`
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (providers) {
|
|
162
|
+
if (providers.length > 0) {
|
|
163
|
+
url += `&providers=${providers.join(",")}`
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const response = await fetch(url)
|
|
168
|
+
return response
|
|
169
|
+
} catch (error) {
|
|
170
|
+
console.error(error)
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function postRouterWithLiquidityChanges(
|
|
176
|
+
endpoint: string,
|
|
177
|
+
params: FindRouterParams
|
|
178
|
+
) {
|
|
179
|
+
const {
|
|
180
|
+
from,
|
|
181
|
+
target,
|
|
182
|
+
amount,
|
|
183
|
+
byAmountIn,
|
|
184
|
+
depth,
|
|
185
|
+
splitAlgorithm,
|
|
186
|
+
splitFactor,
|
|
187
|
+
splitCount,
|
|
188
|
+
providers,
|
|
189
|
+
liquidityChanges,
|
|
190
|
+
} = params
|
|
191
|
+
|
|
192
|
+
const fromCoin = completionCoin(from)
|
|
193
|
+
const targetCoin = completionCoin(target)
|
|
194
|
+
|
|
195
|
+
const url = `${endpoint}`
|
|
196
|
+
|
|
197
|
+
const providersStr = providers?.join(",")
|
|
198
|
+
|
|
199
|
+
const requestData = {
|
|
200
|
+
from: fromCoin,
|
|
201
|
+
target: targetCoin,
|
|
202
|
+
amount: Number(amount.toString()),
|
|
203
|
+
by_amount_in: byAmountIn,
|
|
204
|
+
depth,
|
|
205
|
+
split_algorithm: splitAlgorithm,
|
|
206
|
+
split_factor: splitFactor,
|
|
207
|
+
split_count: splitCount,
|
|
208
|
+
providers: providersStr,
|
|
209
|
+
liquidity_changes: liquidityChanges!.map((change) => ({
|
|
210
|
+
pool: change.poolID,
|
|
211
|
+
tick_lower: change.ticklower,
|
|
212
|
+
tick_upper: change.tickUpper,
|
|
213
|
+
delta_liquidity: change.deltaLiquidity,
|
|
214
|
+
})),
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
console.log("requestData", JSON.stringify(requestData, null, 2))
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
const response = await fetch(url, {
|
|
221
|
+
method: "POST",
|
|
222
|
+
headers: {
|
|
223
|
+
"Content-Type": "application/json",
|
|
224
|
+
},
|
|
225
|
+
body: JSON.stringify(requestData),
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
return response
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error("Error:", error)
|
|
231
|
+
return null
|
|
232
|
+
}
|
|
233
|
+
}
|
package/src/client.ts
CHANGED
|
@@ -337,7 +337,7 @@ export class AggregatorClient {
|
|
|
337
337
|
if (this.env === Env.Mainnet) {
|
|
338
338
|
return "0xeffc8ae61f439bb34c9b905ff8f29ec56873dcedf81c7123ff2f1f67c45ec302"
|
|
339
339
|
} else {
|
|
340
|
-
return "
|
|
340
|
+
return "0x6cbaa3e9fbe902d90191b12f315932bc58079cba422bafbd4b4369f80e61f595"
|
|
341
341
|
}
|
|
342
342
|
}
|
|
343
343
|
|
|
@@ -454,11 +454,16 @@ export function parseRouterResponse(data: any): RouterData {
|
|
|
454
454
|
}
|
|
455
455
|
|
|
456
456
|
let extendedDetails
|
|
457
|
-
if (
|
|
457
|
+
if (
|
|
458
|
+
path.provider === TURBOS ||
|
|
459
|
+
path.provider === AFTERMATH ||
|
|
460
|
+
path.provider === CETUS
|
|
461
|
+
) {
|
|
458
462
|
extendedDetails = {
|
|
459
463
|
aftermathLpSupplyType:
|
|
460
464
|
path.extended_details?.aftermath_lp_supply_type,
|
|
461
465
|
turbosFeeType: path.extended_details?.turbos_fee_type,
|
|
466
|
+
afterSqrtPrice: path.extended_details?.after_sqrt_price,
|
|
462
467
|
}
|
|
463
468
|
}
|
|
464
469
|
|
package/src/transaction/cetus.ts
CHANGED
|
@@ -22,8 +22,9 @@ export class Cetus implements Dex {
|
|
|
22
22
|
: "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a"
|
|
23
23
|
|
|
24
24
|
this.partner =
|
|
25
|
-
partner ??
|
|
26
|
-
|
|
25
|
+
partner ?? env === Env.Mainnet
|
|
26
|
+
? "0x639b5e433da31739e800cd085f356e64cae222966d0f1b11bd9dc76b322ff58b"
|
|
27
|
+
: "0x8e0b7668a79592f70fbfb1ae0aebaf9e2019a7049783b9a4b6fe7c6ae038b528"
|
|
27
28
|
}
|
|
28
29
|
|
|
29
30
|
flash_swap(
|
package/src/transaction/swap.ts
CHANGED
|
@@ -131,6 +131,9 @@ export async function swapInPools(
|
|
|
131
131
|
feeRate: event.fee_rate,
|
|
132
132
|
amountIn: event.amount_in,
|
|
133
133
|
amountOut: event.amount_out,
|
|
134
|
+
extendedDetails: {
|
|
135
|
+
afterSqrtPrice: event.after_sqrt_price,
|
|
136
|
+
},
|
|
134
137
|
},
|
|
135
138
|
],
|
|
136
139
|
amountIn: new BN(event.amount_in ?? 0),
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
import BN from 'bn.js'
|
|
3
|
+
import Decimal from "decimal.js"
|
|
4
|
+
|
|
5
|
+
export const dealWithFastRouterSwapParamsForMsafe = (data: any) => {
|
|
6
|
+
const result =data.map((route: any) => {
|
|
7
|
+
return {
|
|
8
|
+
...route,
|
|
9
|
+
amountIn: route.amountIn.toString(),
|
|
10
|
+
amountOut: route.amountOut.toString(),
|
|
11
|
+
initialPrice: route.initialPrice.toString(),
|
|
12
|
+
path: route.path
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
return result
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const restituteMsafeFastRouterSwapParams = (data: any) => {
|
|
20
|
+
const result =data.map((route: any) => {
|
|
21
|
+
return {
|
|
22
|
+
...route,
|
|
23
|
+
amountIn: new BN(route.amountIn),
|
|
24
|
+
amountOut: new BN(route.amountOut),
|
|
25
|
+
initialPrice: new Decimal(route.initialPrice.toString()),
|
|
26
|
+
path: route.path
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return result
|
|
31
|
+
}
|
package/tests/router.test.ts
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import { describe, test } from "@jest/globals"
|
|
2
2
|
import dotenv from "dotenv"
|
|
3
3
|
import { AggregatorClient } from "~/client"
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
M_CETUS,
|
|
6
|
+
M_HASUI,
|
|
7
|
+
M_NAVI,
|
|
8
|
+
M_SSWP,
|
|
9
|
+
M_SUI,
|
|
10
|
+
M_USDC,
|
|
11
|
+
} from "./test_data.test"
|
|
5
12
|
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
|
|
6
13
|
import { printTransaction } from "~/utils/transaction"
|
|
7
14
|
import BN from "bn.js"
|
|
@@ -116,7 +123,7 @@ describe("router module", () => {
|
|
|
116
123
|
byAmountIn: true,
|
|
117
124
|
depth: 3,
|
|
118
125
|
splitCount: 1,
|
|
119
|
-
|
|
126
|
+
providers: ["CETUS"],
|
|
120
127
|
})
|
|
121
128
|
|
|
122
129
|
if (res != null) {
|
|
@@ -290,4 +297,88 @@ describe("router module", () => {
|
|
|
290
297
|
}
|
|
291
298
|
}
|
|
292
299
|
}, 60000000)
|
|
300
|
+
|
|
301
|
+
test("Build router with liquidity changes", async () => {
|
|
302
|
+
const byAmountIn = true
|
|
303
|
+
const amount = "1000000000"
|
|
304
|
+
|
|
305
|
+
// const from = M_USDC
|
|
306
|
+
// const target = M_SUI
|
|
307
|
+
|
|
308
|
+
const from = M_SUI
|
|
309
|
+
// const target =
|
|
310
|
+
// "0xbde4ba4c2e274a60ce15c1cfff9e5c42e41654ac8b6d906a57efa4bd3c29f47d::hasui::HASUI"
|
|
311
|
+
// const target =
|
|
312
|
+
// "0xf325ce1300e8dac124071d3152c5c5ee6174914f8bc2161e88329cf579246efc::afsui::AFSUI"
|
|
313
|
+
|
|
314
|
+
const target = M_HASUI
|
|
315
|
+
|
|
316
|
+
const res = await client.findRouters({
|
|
317
|
+
from,
|
|
318
|
+
target,
|
|
319
|
+
amount: new BN(amount),
|
|
320
|
+
byAmountIn,
|
|
321
|
+
depth: 2,
|
|
322
|
+
providers: [
|
|
323
|
+
"CETUS",
|
|
324
|
+
// "DEEPBOOK",
|
|
325
|
+
// "AFTERMATH",
|
|
326
|
+
// "FLOWX",
|
|
327
|
+
// "KRIYA",
|
|
328
|
+
// "KRIYAV3",
|
|
329
|
+
// "TURBOS",
|
|
330
|
+
// "FLOWXV3",
|
|
331
|
+
],
|
|
332
|
+
liquidityChanges: [
|
|
333
|
+
{
|
|
334
|
+
poolID:
|
|
335
|
+
"0x871d8a227114f375170f149f7e9d45be822dd003eba225e83c05ac80828596bc",
|
|
336
|
+
ticklower: 100,
|
|
337
|
+
tickUpper: 394,
|
|
338
|
+
deltaLiquidity: -5498684,
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
poolID:
|
|
342
|
+
"0x871d8a227114f375170f149f7e9d45be822dd003eba225e83c05ac80828596bc",
|
|
343
|
+
ticklower: 100,
|
|
344
|
+
tickUpper: 394,
|
|
345
|
+
deltaLiquidity: 986489,
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
})
|
|
349
|
+
|
|
350
|
+
if (res != null) {
|
|
351
|
+
console.log(JSON.stringify(res, null, 2))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
console.log("amount in", res?.amountIn.toString())
|
|
355
|
+
console.log("amount out", res?.amountOut.toString())
|
|
356
|
+
|
|
357
|
+
const txb = new Transaction()
|
|
358
|
+
|
|
359
|
+
if (res != null) {
|
|
360
|
+
console.log(JSON.stringify(res, null, 2))
|
|
361
|
+
await client.fastRouterSwap({
|
|
362
|
+
routers: res.routes,
|
|
363
|
+
byAmountIn,
|
|
364
|
+
txb,
|
|
365
|
+
slippage: 0.01,
|
|
366
|
+
isMergeTragetCoin: false,
|
|
367
|
+
refreshAllCoins: true,
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
printTransaction(txb)
|
|
371
|
+
|
|
372
|
+
let result = await client.devInspectTransactionBlock(txb)
|
|
373
|
+
console.log("🚀 ~ file: router.test.ts:180 ~ test ~ result:", result)
|
|
374
|
+
|
|
375
|
+
// if (result.effects.status.status === "success") {
|
|
376
|
+
// console.log("Sim exec transaction success")
|
|
377
|
+
// const result = await client.signAndExecuteTransaction(txb, keypair)
|
|
378
|
+
// // console.log("result", result)
|
|
379
|
+
// } else {
|
|
380
|
+
// console.log("result", result)
|
|
381
|
+
// }
|
|
382
|
+
}
|
|
383
|
+
}, 600000)
|
|
293
384
|
})
|