@cetusprotocol/aggregator-sdk 0.0.7 → 0.1.0
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/dist/index.d.mts +98 -102
- package/dist/index.d.ts +98 -102
- package/dist/index.js +1228 -1556
- package/dist/index.mjs +1215 -1543
- package/dist/src/api.d.ts +53 -0
- package/dist/src/client.d.ts +38 -65
- package/dist/src/const.d.ts +0 -9
- package/dist/src/index.d.ts +9 -5
- package/dist/src/transaction/afsui.d.ts +10 -0
- package/dist/src/transaction/aftermath.d.ts +13 -24
- package/dist/src/transaction/cetus.d.ts +9 -33
- package/dist/src/transaction/deepbook_v2.d.ts +14 -0
- package/dist/src/transaction/flowx_v2.d.ts +7 -0
- package/dist/src/transaction/haedal.d.ts +6 -0
- package/dist/src/transaction/index.d.ts +6 -1
- package/dist/src/transaction/kriya_v2.d.ts +6 -0
- package/dist/src/transaction/kriya_v3.d.ts +7 -0
- package/dist/src/transaction/swap.d.ts +1 -2
- package/dist/src/transaction/turbos.d.ts +7 -22
- package/dist/src/transaction/volo.d.ts +8 -0
- package/dist/src/utils/coin.d.ts +9 -0
- package/dist/{src → tests}/test_data.test.d.ts +1 -0
- package/package.json +2 -2
- package/src/api.ts +144 -0
- package/src/client.ts +295 -239
- package/src/const.ts +0 -13
- package/src/index.ts +10 -5
- package/src/transaction/afsui.ts +60 -0
- package/src/transaction/aftermath.ts +71 -124
- package/src/transaction/cetus.ts +87 -258
- package/src/transaction/deepbook_v2.ts +122 -0
- package/src/transaction/flowx_v2.ts +42 -0
- package/src/transaction/haedal.ts +41 -0
- package/src/transaction/index.ts +17 -1
- package/src/transaction/kriya_v2.ts +38 -0
- package/src/transaction/kriya_v3.ts +46 -0
- package/src/transaction/swap.ts +17 -31
- package/src/transaction/turbos.ts +40 -99
- package/src/transaction/volo.ts +52 -0
- package/src/utils/coin.ts +91 -6
- package/src/utils/transaction.ts +1 -1
- package/tests/router.test.ts +127 -81
- package/{src → tests}/test_data.test.ts +2 -0
- package/dist/src/config.d.ts +0 -26
- package/dist/src/transaction/common.d.ts +0 -12
- package/dist/src/transaction/deepbook.d.ts +0 -21
- package/dist/src/transaction/flowx.d.ts +0 -20
- package/dist/src/transaction/kriya.d.ts +0 -21
- package/dist/src/transaction/router.d.ts +0 -6
- package/dist/src/utils/account_cap.d.ts +0 -7
- package/src/config.ts +0 -65
- package/src/transaction/common.ts +0 -174
- package/src/transaction/deepbook.ts +0 -126
- package/src/transaction/flowx.ts +0 -97
- package/src/transaction/kriya.ts +0 -77
- package/src/transaction/router.ts +0 -341
- package/src/utils/account_cap.ts +0 -62
package/src/client.ts
CHANGED
|
@@ -1,95 +1,64 @@
|
|
|
1
|
-
import type { AggregatorConfig } from "./config"
|
|
2
1
|
import Decimal from "decimal.js"
|
|
3
2
|
import { SuiClient } from "@mysten/sui/client"
|
|
4
|
-
import { CoinAsset, SuiAddress } from "./types/sui"
|
|
5
|
-
import { createTarget, extractStructTagFromType } from "./utils"
|
|
6
|
-
import { Transaction } from "@mysten/sui/transactions"
|
|
7
3
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "
|
|
12
|
-
import { expectInputRouterSwap, expectOutputRouterSwap } from "./transaction"
|
|
13
|
-
import { CalculateAmountLimit } from "./math"
|
|
14
|
-
import { Signer } from "@mysten/sui/dist/cjs/cryptography"
|
|
4
|
+
Transaction,
|
|
5
|
+
TransactionObjectArgument,
|
|
6
|
+
} from "@mysten/sui/transactions"
|
|
7
|
+
import { Signer } from "@mysten/sui/cryptography"
|
|
15
8
|
import BN from "bn.js"
|
|
16
|
-
import { swapInPools } from "./transaction/swap"
|
|
17
|
-
import { completionCoin } from "./utils/coin"
|
|
18
9
|
import { SUI_FRAMEWORK_ADDRESS } from "@mysten/sui/utils"
|
|
19
|
-
import { AFTERMATH_AMM, JOIN_FUNC, PAY_MODULE, TURBOS_DEX, ZERO } from "./const"
|
|
20
10
|
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
export
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type RouterData = {
|
|
57
|
-
amountIn: BN
|
|
58
|
-
amountOut: BN
|
|
59
|
-
routes: Router[]
|
|
60
|
-
insufficientLiquidity: boolean
|
|
61
|
-
error?: RouterError
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type AggregatorResponse = {
|
|
65
|
-
code: number
|
|
66
|
-
msg: string
|
|
67
|
-
data: RouterData
|
|
68
|
-
}
|
|
11
|
+
Dex,
|
|
12
|
+
Env,
|
|
13
|
+
extractStructTagFromType,
|
|
14
|
+
FindRouterParams,
|
|
15
|
+
getRouterResult,
|
|
16
|
+
Router,
|
|
17
|
+
RouterData,
|
|
18
|
+
} from "."
|
|
19
|
+
import { Aftermath } from "./transaction/aftermath"
|
|
20
|
+
import { DeepbookV2 } from "./transaction/deepbook_v2"
|
|
21
|
+
import { KriyaV2 } from "./transaction/kriya_v2"
|
|
22
|
+
import { FlowxV2 } from "./transaction/flowx_v2"
|
|
23
|
+
import { Turbos } from "./transaction/turbos"
|
|
24
|
+
import { Cetus } from "./transaction/cetus"
|
|
25
|
+
import { swapInPools } from "./transaction/swap"
|
|
26
|
+
import { CalculateAmountLimit } from "./math"
|
|
27
|
+
import { buildInputCoin } from "./utils/coin"
|
|
28
|
+
import { CoinAsset } from "./types/sui"
|
|
29
|
+
import { KriyaV3 } from "./transaction/kriya_v3"
|
|
30
|
+
import { Haedal } from "./transaction/haedal"
|
|
31
|
+
import { Afsui } from "./transaction/afsui"
|
|
32
|
+
import { Volo } from "./transaction/volo"
|
|
33
|
+
|
|
34
|
+
export const CETUS = "CETUS"
|
|
35
|
+
export const DEEPBOOKV2 = "DEEPBOOK"
|
|
36
|
+
export const KRIYA = "KRIYA"
|
|
37
|
+
export const FLOWXV2 = "FLOWX"
|
|
38
|
+
export const KRIYAV3 = "KRIYAV3"
|
|
39
|
+
export const TURBOS = "TURBOS"
|
|
40
|
+
export const AFTERMATH = "AFTERMATH"
|
|
41
|
+
export const HAEDAL = "HAEDAL"
|
|
42
|
+
export const VOLO = "VOLO"
|
|
43
|
+
export const AFSUI = "AFSUI"
|
|
69
44
|
|
|
70
45
|
export type BuildRouterSwapParams = {
|
|
71
46
|
routers: Router[]
|
|
72
|
-
amountIn: BN
|
|
73
|
-
amountOut: BN
|
|
74
47
|
byAmountIn: boolean
|
|
48
|
+
inputCoin: TransactionObjectArgument
|
|
75
49
|
slippage: number
|
|
76
|
-
|
|
77
|
-
targetCoinType: string
|
|
50
|
+
txb: Transaction
|
|
78
51
|
partner?: string
|
|
79
|
-
isMergeTragetCoin?: boolean
|
|
80
|
-
refreshAllCoins?: boolean
|
|
81
52
|
}
|
|
82
53
|
|
|
83
|
-
export
|
|
84
|
-
|
|
85
|
-
target: string
|
|
86
|
-
amount: BN
|
|
54
|
+
export type BuildFastRouterSwapParams = {
|
|
55
|
+
routers: Router[]
|
|
87
56
|
byAmountIn: boolean
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
57
|
+
slippage: number
|
|
58
|
+
txb: Transaction
|
|
59
|
+
partner?: string
|
|
60
|
+
isMergeTragetCoin?: boolean
|
|
61
|
+
refreshAllCoins?: boolean
|
|
93
62
|
}
|
|
94
63
|
|
|
95
64
|
export interface SwapInPoolsParams {
|
|
@@ -106,30 +75,27 @@ export interface SwapInPoolsResult {
|
|
|
106
75
|
}
|
|
107
76
|
|
|
108
77
|
export class AggregatorClient {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
private client: SuiClient
|
|
114
|
-
|
|
78
|
+
public endpoint: string
|
|
79
|
+
public signer: string
|
|
80
|
+
public client: SuiClient
|
|
81
|
+
public env: Env
|
|
115
82
|
private allCoins: CoinAsset[]
|
|
116
83
|
|
|
117
|
-
constructor(
|
|
118
|
-
this.
|
|
119
|
-
this.client =
|
|
120
|
-
this.
|
|
84
|
+
constructor(endpoint: string, signer: string, client: SuiClient, env: Env) {
|
|
85
|
+
this.endpoint = endpoint
|
|
86
|
+
this.client = client
|
|
87
|
+
this.signer = signer
|
|
88
|
+
this.env = env
|
|
121
89
|
this.allCoins = []
|
|
122
90
|
}
|
|
123
91
|
|
|
124
92
|
async getAllCoins(): Promise<CoinAsset[]> {
|
|
125
93
|
let cursor = null
|
|
126
94
|
let limit = 50
|
|
127
|
-
|
|
128
95
|
const allCoins: CoinAsset[] = []
|
|
129
|
-
|
|
130
96
|
while (true) {
|
|
131
97
|
const gotAllCoins = await this.client.getAllCoins({
|
|
132
|
-
owner: this.
|
|
98
|
+
owner: this.signer,
|
|
133
99
|
cursor,
|
|
134
100
|
limit,
|
|
135
101
|
})
|
|
@@ -140,96 +106,107 @@ export class AggregatorClient {
|
|
|
140
106
|
balance: BigInt(coin.balance),
|
|
141
107
|
})
|
|
142
108
|
}
|
|
143
|
-
|
|
144
109
|
if (gotAllCoins.data.length < limit) {
|
|
145
110
|
break
|
|
146
111
|
}
|
|
147
112
|
cursor = gotAllCoins.data[limit - 1].coinObjectId
|
|
148
113
|
}
|
|
149
|
-
|
|
150
114
|
return allCoins
|
|
151
115
|
}
|
|
152
116
|
|
|
153
|
-
async
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const {
|
|
157
|
-
from,
|
|
158
|
-
target,
|
|
159
|
-
amount,
|
|
160
|
-
byAmountIn,
|
|
161
|
-
depth,
|
|
162
|
-
splitAlgorithm,
|
|
163
|
-
splitFactor,
|
|
164
|
-
splitCount,
|
|
165
|
-
providers,
|
|
166
|
-
} = fromRouterParams
|
|
167
|
-
const fromCoin = completionCoin(from)
|
|
168
|
-
const targetCoin = completionCoin(target)
|
|
169
|
-
|
|
170
|
-
let url =
|
|
171
|
-
this.config.getAggregatorUrl() +
|
|
172
|
-
`/find_routes?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`
|
|
173
|
-
|
|
174
|
-
if (depth) {
|
|
175
|
-
url += `&depth=${depth}`
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (splitAlgorithm) {
|
|
179
|
-
url += `&split_algorithm=${splitAlgorithm}`
|
|
180
|
-
}
|
|
117
|
+
async findRouters(params: FindRouterParams): Promise<RouterData | null> {
|
|
118
|
+
return getRouterResult(this.endpoint, params)
|
|
119
|
+
}
|
|
181
120
|
|
|
182
|
-
|
|
183
|
-
|
|
121
|
+
async expectInputSwap(
|
|
122
|
+
txb: Transaction,
|
|
123
|
+
inputCoin: TransactionObjectArgument,
|
|
124
|
+
routers: Router[],
|
|
125
|
+
amountOutLimit: BN,
|
|
126
|
+
partner?: string
|
|
127
|
+
) {
|
|
128
|
+
if (routers.length === 0) {
|
|
129
|
+
throw new Error("No router found")
|
|
184
130
|
}
|
|
131
|
+
const splitAmounts = routers.map((router) => router.amountIn.toString())
|
|
132
|
+
const inputCoinType = routers[0].path[0].from
|
|
133
|
+
const outputCoinType = routers[0].path[routers[0].path.length - 1].target
|
|
134
|
+
const inputCoins = txb.splitCoins(inputCoin, splitAmounts)
|
|
135
|
+
const outputCoins = []
|
|
136
|
+
for (let i = 0; i < routers.length; i++) {
|
|
137
|
+
if (routers[i].path.length === 0) {
|
|
138
|
+
throw new Error("Empty path")
|
|
139
|
+
}
|
|
140
|
+
let nextCoin = inputCoins[i] as TransactionObjectArgument
|
|
141
|
+
for (const path of routers[i].path) {
|
|
142
|
+
const dex = this.newDex(path.provider, partner)
|
|
143
|
+
nextCoin = await dex.swap(this, txb, path, nextCoin)
|
|
144
|
+
}
|
|
185
145
|
|
|
186
|
-
|
|
187
|
-
url += `&split_count=${splitCount}`
|
|
146
|
+
outputCoins.push(nextCoin)
|
|
188
147
|
}
|
|
148
|
+
this.transferOrDestoryCoin(txb, inputCoin, inputCoinType)
|
|
149
|
+
const mergedTargetCointhis = this.checkCoinThresholdAndMergeCoin(
|
|
150
|
+
txb,
|
|
151
|
+
outputCoins,
|
|
152
|
+
outputCoinType,
|
|
153
|
+
amountOutLimit
|
|
154
|
+
)
|
|
155
|
+
return mergedTargetCointhis
|
|
156
|
+
}
|
|
189
157
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
158
|
+
async expectOutputSwap(
|
|
159
|
+
txb: Transaction,
|
|
160
|
+
inputCoin: TransactionObjectArgument,
|
|
161
|
+
routers: Router[],
|
|
162
|
+
partner?: string
|
|
163
|
+
): Promise<TransactionObjectArgument> {
|
|
164
|
+
const returnCoins: TransactionObjectArgument[] = []
|
|
165
|
+
const receipts: TransactionObjectArgument[] = []
|
|
166
|
+
const targetCoins = []
|
|
167
|
+
const dex = new Cetus(this.env, partner)
|
|
168
|
+
for (let i = 0; i < routers.length; i++) {
|
|
169
|
+
const router = routers[i]
|
|
170
|
+
for (let j = router.path.length - 1; j >= 0; j--) {
|
|
171
|
+
const path = router.path[j]
|
|
172
|
+
const flashSwapResult = dex.flash_swap(this, txb, path, false)
|
|
173
|
+
returnCoins.unshift(flashSwapResult.targetCoin)
|
|
174
|
+
receipts.unshift(flashSwapResult.flashReceipt)
|
|
193
175
|
}
|
|
194
|
-
}
|
|
195
176
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
177
|
+
let nextRepayCoin = inputCoin
|
|
178
|
+
for (let j = 0; j < router.path.length; j++) {
|
|
179
|
+
const path = router.path[j]
|
|
180
|
+
const repayResult = dex.repay_flash_swap(
|
|
181
|
+
this,
|
|
182
|
+
txb,
|
|
183
|
+
path,
|
|
184
|
+
nextRepayCoin,
|
|
185
|
+
receipts[j]
|
|
186
|
+
)
|
|
187
|
+
nextRepayCoin = returnCoins[j]
|
|
188
|
+
if (j === 0) {
|
|
189
|
+
inputCoin = repayResult
|
|
190
|
+
} else {
|
|
191
|
+
this.transferOrDestoryCoin(txb, repayResult, path.from)
|
|
192
|
+
}
|
|
193
|
+
if (j === router.path.length - 1) {
|
|
194
|
+
targetCoins.push(nextRepayCoin)
|
|
195
|
+
}
|
|
209
196
|
}
|
|
210
197
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
198
|
+
const inputCoinType = routers[0].path[0].from
|
|
199
|
+
this.transferOrDestoryCoin(txb, inputCoin, inputCoinType)
|
|
200
|
+
if (targetCoins.length > 1) {
|
|
201
|
+
const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) })
|
|
202
|
+
txb.moveCall({
|
|
203
|
+
target: `${SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
|
|
204
|
+
typeArguments: [routers[0].path[routers[0].path.length - 1].target],
|
|
205
|
+
arguments: [targetCoins[0], vec],
|
|
206
|
+
})
|
|
217
207
|
}
|
|
218
208
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return {
|
|
222
|
-
amountIn: ZERO,
|
|
223
|
-
amountOut: ZERO,
|
|
224
|
-
routes: [],
|
|
225
|
-
insufficientLiquidity,
|
|
226
|
-
error: {
|
|
227
|
-
code: AggregatorServerErrorCode.InsufficientLiquidity,
|
|
228
|
-
msg: getAggregatorServerErrorMessage(
|
|
229
|
-
AggregatorServerErrorCode.InsufficientLiquidity
|
|
230
|
-
),
|
|
231
|
-
},
|
|
232
|
-
}
|
|
209
|
+
return targetCoins[0]
|
|
233
210
|
}
|
|
234
211
|
|
|
235
212
|
async swapInPools(
|
|
@@ -237,113 +214,193 @@ export class AggregatorClient {
|
|
|
237
214
|
): Promise<SwapInPoolsResult | null> {
|
|
238
215
|
let result
|
|
239
216
|
try {
|
|
240
|
-
result = await swapInPools(this.client, params, this.
|
|
217
|
+
result = await swapInPools(this.client, params, this.signer)
|
|
241
218
|
} catch (e) {
|
|
242
219
|
console.error("swapInPools error:", e)
|
|
243
220
|
return null
|
|
244
221
|
}
|
|
245
|
-
|
|
246
222
|
return result
|
|
247
223
|
}
|
|
248
224
|
|
|
249
|
-
async routerSwap(
|
|
225
|
+
async routerSwap(
|
|
226
|
+
params: BuildRouterSwapParams
|
|
227
|
+
): Promise<TransactionObjectArgument> {
|
|
228
|
+
const { routers, inputCoin, slippage, byAmountIn, txb, partner } = params
|
|
229
|
+
const amountIn = routers.reduce(
|
|
230
|
+
(acc, router) => acc.add(router.amountIn),
|
|
231
|
+
new BN(0)
|
|
232
|
+
)
|
|
233
|
+
const amountOut = routers.reduce(
|
|
234
|
+
(acc, router) => acc.add(router.amountOut),
|
|
235
|
+
new BN(0)
|
|
236
|
+
)
|
|
237
|
+
const amountLimit = CalculateAmountLimit(
|
|
238
|
+
byAmountIn ? amountOut : amountIn,
|
|
239
|
+
byAmountIn,
|
|
240
|
+
slippage
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
if (byAmountIn) {
|
|
244
|
+
const targetCoin = await this.expectInputSwap(
|
|
245
|
+
txb,
|
|
246
|
+
inputCoin,
|
|
247
|
+
routers,
|
|
248
|
+
new BN(amountLimit),
|
|
249
|
+
partner
|
|
250
|
+
)
|
|
251
|
+
return targetCoin
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// When exact output, we will set slippage limit in split coin.
|
|
255
|
+
const splitedInputCoins = txb.splitCoins(inputCoin, [
|
|
256
|
+
amountLimit.toString(),
|
|
257
|
+
])
|
|
258
|
+
this.transferOrDestoryCoin(txb, inputCoin, routers[0].path[0].from)
|
|
259
|
+
const targetCoin = await this.expectOutputSwap(
|
|
260
|
+
txb,
|
|
261
|
+
splitedInputCoins[0],
|
|
262
|
+
routers,
|
|
263
|
+
partner
|
|
264
|
+
)
|
|
265
|
+
return targetCoin
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// auto build input coin
|
|
269
|
+
// auto merge, transfer or destory target coin.
|
|
270
|
+
async fastRouterSwap(params: BuildFastRouterSwapParams) {
|
|
250
271
|
const {
|
|
251
|
-
routers
|
|
252
|
-
amountIn,
|
|
253
|
-
amountOut,
|
|
272
|
+
routers,
|
|
254
273
|
byAmountIn,
|
|
255
274
|
slippage,
|
|
256
|
-
|
|
257
|
-
targetCoinType,
|
|
275
|
+
txb,
|
|
258
276
|
partner,
|
|
259
277
|
isMergeTragetCoin,
|
|
260
278
|
refreshAllCoins,
|
|
261
279
|
} = params
|
|
280
|
+
if (refreshAllCoins || this.allCoins.length === 0) {
|
|
281
|
+
this.allCoins = await this.getAllCoins()
|
|
282
|
+
}
|
|
283
|
+
const fromCoinType = routers[0].path[0].from
|
|
284
|
+
const targetCoinType = routers[0].path[routers[0].path.length - 1].target
|
|
285
|
+
const amountIn = routers.reduce(
|
|
286
|
+
(acc, router) => acc.add(router.amountIn),
|
|
287
|
+
new BN(0)
|
|
288
|
+
)
|
|
289
|
+
const amountOut = routers.reduce(
|
|
290
|
+
(acc, router) => acc.add(router.amountOut),
|
|
291
|
+
new BN(0)
|
|
292
|
+
)
|
|
262
293
|
const amountLimit = CalculateAmountLimit(
|
|
263
294
|
byAmountIn ? amountOut : amountIn,
|
|
264
295
|
byAmountIn,
|
|
265
296
|
slippage
|
|
266
297
|
)
|
|
267
|
-
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
fromCoinType
|
|
283
|
-
)
|
|
284
|
-
const fromCoin = buildFromCoinRes.targetCoin
|
|
285
|
-
const swapedCoins = await expectInputRouterSwap(
|
|
286
|
-
this.client,
|
|
287
|
-
params,
|
|
288
|
-
txb,
|
|
289
|
-
fromCoin,
|
|
290
|
-
this.config,
|
|
291
|
-
partner
|
|
292
|
-
)
|
|
293
|
-
const mergedCoin = checkCoinThresholdAndMergeCoin(
|
|
294
|
-
txb,
|
|
295
|
-
swapedCoins,
|
|
296
|
-
targetCoinType,
|
|
297
|
-
amountLimit,
|
|
298
|
-
this.config
|
|
299
|
-
)
|
|
300
|
-
targetCoins.push(mergedCoin)
|
|
301
|
-
} else {
|
|
302
|
-
const fromCoin = buildInputCoin(
|
|
303
|
-
txb,
|
|
304
|
-
allCoins,
|
|
305
|
-
BigInt(amountLimit),
|
|
306
|
-
fromCoinType
|
|
307
|
-
).targetCoin
|
|
308
|
-
const swapedCoins = await expectOutputRouterSwap(
|
|
309
|
-
params,
|
|
310
|
-
txb,
|
|
311
|
-
fromCoin,
|
|
312
|
-
this.config,
|
|
313
|
-
partner
|
|
314
|
-
)
|
|
315
|
-
targetCoins.push(...swapedCoins)
|
|
316
|
-
}
|
|
298
|
+
const amount = byAmountIn ? amountIn : amountLimit
|
|
299
|
+
const buildFromCoinRes = buildInputCoin(
|
|
300
|
+
txb,
|
|
301
|
+
this.allCoins,
|
|
302
|
+
BigInt(amount.toString()),
|
|
303
|
+
fromCoinType
|
|
304
|
+
)
|
|
305
|
+
const targetCoin = await this.routerSwap({
|
|
306
|
+
routers,
|
|
307
|
+
inputCoin: buildFromCoinRes.targetCoin,
|
|
308
|
+
slippage,
|
|
309
|
+
byAmountIn,
|
|
310
|
+
txb,
|
|
311
|
+
partner,
|
|
312
|
+
})
|
|
317
313
|
|
|
318
314
|
if (isMergeTragetCoin) {
|
|
319
315
|
const targetCoinRes = buildInputCoin(
|
|
320
316
|
txb,
|
|
321
|
-
allCoins,
|
|
317
|
+
this.allCoins,
|
|
322
318
|
BigInt(0),
|
|
323
319
|
targetCoinType
|
|
324
320
|
)
|
|
325
|
-
txb.mergeCoins(targetCoinRes.targetCoin,
|
|
321
|
+
txb.mergeCoins(targetCoinRes.targetCoin, [targetCoin])
|
|
326
322
|
if (targetCoinRes.isMintZeroCoin) {
|
|
327
|
-
transferOrDestoryCoin(
|
|
323
|
+
this.transferOrDestoryCoin(
|
|
328
324
|
txb,
|
|
329
325
|
targetCoinRes.targetCoin,
|
|
330
|
-
targetCoinType
|
|
331
|
-
this.config
|
|
326
|
+
targetCoinType
|
|
332
327
|
)
|
|
333
328
|
}
|
|
334
329
|
} else {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
330
|
+
this.transferOrDestoryCoin(txb, targetCoin, targetCoinType)
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
publishedAt(): string {
|
|
335
|
+
if (this.env === Env.Mainnet) {
|
|
336
|
+
return "0x9fd702cf1a8db5a2e99be20be6750d22ec75c30d57e3d9ccbc37c9b4ad7bef21"
|
|
337
|
+
} else {
|
|
338
|
+
return "0x0"
|
|
344
339
|
}
|
|
340
|
+
}
|
|
345
341
|
|
|
346
|
-
|
|
342
|
+
transferOrDestoryCoin(
|
|
343
|
+
txb: Transaction,
|
|
344
|
+
coin: TransactionObjectArgument,
|
|
345
|
+
coinType: string
|
|
346
|
+
) {
|
|
347
|
+
txb.moveCall({
|
|
348
|
+
target: `${this.publishedAt()}::utils::transfer_or_destroy_coin`,
|
|
349
|
+
typeArguments: [coinType],
|
|
350
|
+
arguments: [coin],
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
checkCoinThresholdAndMergeCoin(
|
|
355
|
+
txb: Transaction,
|
|
356
|
+
coins: TransactionObjectArgument[],
|
|
357
|
+
coinType: string,
|
|
358
|
+
amountLimit: BN
|
|
359
|
+
) {
|
|
360
|
+
let targetCoin = coins[0]
|
|
361
|
+
if (coins.length > 1) {
|
|
362
|
+
let vec = txb.makeMoveVec({ elements: coins.slice(1) })
|
|
363
|
+
txb.moveCall({
|
|
364
|
+
target: `${SUI_FRAMEWORK_ADDRESS}::pay::join_vec`,
|
|
365
|
+
typeArguments: [coinType],
|
|
366
|
+
arguments: [coins[0], vec],
|
|
367
|
+
})
|
|
368
|
+
targetCoin = coins[0]
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const res = txb.moveCall({
|
|
372
|
+
target: `${this.publishedAt()}::utils::check_coin_threshold`,
|
|
373
|
+
typeArguments: [coinType],
|
|
374
|
+
arguments: [targetCoin, txb.pure.u64(amountLimit.toString())],
|
|
375
|
+
})
|
|
376
|
+
return targetCoin
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
newDex(provider: string, partner?: string): Dex {
|
|
380
|
+
switch (provider) {
|
|
381
|
+
case CETUS:
|
|
382
|
+
return new Cetus(this.env, partner)
|
|
383
|
+
case DEEPBOOKV2:
|
|
384
|
+
return new DeepbookV2(this.env)
|
|
385
|
+
case KRIYA:
|
|
386
|
+
return new KriyaV2(this.env)
|
|
387
|
+
case KRIYAV3:
|
|
388
|
+
return new KriyaV3(this.env)
|
|
389
|
+
case FLOWXV2:
|
|
390
|
+
return new FlowxV2(this.env)
|
|
391
|
+
case TURBOS:
|
|
392
|
+
return new Turbos(this.env)
|
|
393
|
+
case AFTERMATH:
|
|
394
|
+
return new Aftermath(this.env)
|
|
395
|
+
case HAEDAL:
|
|
396
|
+
return new Haedal(this.env)
|
|
397
|
+
case AFSUI:
|
|
398
|
+
return new Afsui(this.env)
|
|
399
|
+
case VOLO:
|
|
400
|
+
return new Volo(this.env)
|
|
401
|
+
default:
|
|
402
|
+
throw new Error(`Unsupported dex ${provider}`)
|
|
403
|
+
}
|
|
347
404
|
}
|
|
348
405
|
|
|
349
406
|
async signAndExecuteTransaction(txb: Transaction, signer: Signer) {
|
|
@@ -363,7 +420,7 @@ export class AggregatorClient {
|
|
|
363
420
|
async devInspectTransactionBlock(txb: Transaction) {
|
|
364
421
|
const res = await this.client.devInspectTransactionBlock({
|
|
365
422
|
transactionBlock: txb,
|
|
366
|
-
sender: this.
|
|
423
|
+
sender: this.signer,
|
|
367
424
|
})
|
|
368
425
|
|
|
369
426
|
return res
|
|
@@ -374,7 +431,6 @@ export class AggregatorClient {
|
|
|
374
431
|
transaction: txb,
|
|
375
432
|
signer,
|
|
376
433
|
})
|
|
377
|
-
|
|
378
434
|
return res
|
|
379
435
|
}
|
|
380
436
|
}
|
|
@@ -388,13 +444,13 @@ export function parseRouterResponse(data: any): RouterData {
|
|
|
388
444
|
return {
|
|
389
445
|
path: route.path.map((path: any) => {
|
|
390
446
|
let version
|
|
391
|
-
if (path.provider ===
|
|
447
|
+
if (path.provider === AFTERMATH) {
|
|
392
448
|
version =
|
|
393
449
|
path.extended_details.aftermath_pool_flatness === 0 ? "v2" : "v3"
|
|
394
450
|
}
|
|
395
451
|
|
|
396
452
|
let extendedDetails
|
|
397
|
-
if (path.provider ===
|
|
453
|
+
if (path.provider === TURBOS || path.provider === AFTERMATH) {
|
|
398
454
|
extendedDetails = {
|
|
399
455
|
aftermathLpSupplyType:
|
|
400
456
|
path.extended_details?.aftermath_lp_supply_type,
|
|
@@ -404,7 +460,7 @@ export function parseRouterResponse(data: any): RouterData {
|
|
|
404
460
|
|
|
405
461
|
return {
|
|
406
462
|
id: path.id,
|
|
407
|
-
|
|
463
|
+
direction: path.direction,
|
|
408
464
|
provider: path.provider,
|
|
409
465
|
from: path.from,
|
|
410
466
|
target: path.target,
|
package/src/const.ts
CHANGED
|
@@ -3,8 +3,6 @@ import BN from "bn.js"
|
|
|
3
3
|
/**
|
|
4
4
|
* The address representing the clock in the system.
|
|
5
5
|
*/
|
|
6
|
-
export const CLOCK_ADDRESS =
|
|
7
|
-
"0x0000000000000000000000000000000000000000000000000000000000000006"
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* The address for CoinInfo module.
|
|
@@ -18,17 +16,6 @@ export const CoinStoreAddress = "0x1::coin::CoinStore"
|
|
|
18
16
|
|
|
19
17
|
export const SuiZeroCoinFn = "0x2::coin::zero"
|
|
20
18
|
|
|
21
|
-
// Dex names
|
|
22
|
-
export const AGGREGATOR = "aggregator"
|
|
23
|
-
export const CETUS_DEX = "CETUS"
|
|
24
|
-
export const DEEPBOOK_DEX = "DEEPBOOK"
|
|
25
|
-
export const KRIYA_DEX = "KRIYA"
|
|
26
|
-
export const FLOWX_AMM = "FLOWX"
|
|
27
|
-
export const TURBOS_DEX = "TURBOS"
|
|
28
|
-
export const AFTERMATH_AMM = "AFTERMATH"
|
|
29
|
-
|
|
30
|
-
export const INTEGRATE = "integrate"
|
|
31
|
-
|
|
32
19
|
// Module names
|
|
33
20
|
export const CETUS_MODULE = "cetus"
|
|
34
21
|
export const DEEPBOOK_MODULE = "deepbook"
|