@cetusprotocol/aggregator-sdk 0.0.0-experimental-20240719182939
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/.env.example +4 -0
- package/README.md +0 -0
- package/bun.lockb +0 -0
- package/dist/index.d.mts +267 -0
- package/dist/index.d.ts +267 -0
- package/dist/index.js +6824 -0
- package/dist/index.mjs +6745 -0
- package/dist/src/client.d.ts +88 -0
- package/dist/src/config.d.ts +26 -0
- package/dist/src/const.d.ts +75 -0
- package/dist/src/errors.d.ts +31 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/math.d.ts +5 -0
- package/dist/src/test_data.test.d.ts +8 -0
- package/dist/src/transaction/aftermath.d.ts +24 -0
- package/dist/src/transaction/cetus.d.ts +39 -0
- package/dist/src/transaction/common.d.ts +12 -0
- package/dist/src/transaction/deepbook.d.ts +21 -0
- package/dist/src/transaction/flowx.d.ts +20 -0
- package/dist/src/transaction/index.d.ts +1 -0
- package/dist/src/transaction/kriya.d.ts +21 -0
- package/dist/src/transaction/router.d.ts +6 -0
- package/dist/src/transaction/swap.d.ts +5 -0
- package/dist/src/transaction/turbos.d.ts +22 -0
- package/dist/src/types/CoinAssist.d.ts +122 -0
- package/dist/src/types/sui.d.ts +112 -0
- package/dist/src/utils/account_cap.d.ts +7 -0
- package/dist/src/utils/coin.d.ts +4 -0
- package/dist/src/utils/coin.spec.d.ts +1 -0
- package/dist/src/utils/contracts.d.ts +16 -0
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/transaction.d.ts +3 -0
- package/dist/tests/router.test.d.ts +2 -0
- package/dist/tests/wallet.test.d.ts +1 -0
- package/jest.config.mjs +13 -0
- package/package.json +41 -0
- package/src/client.ts +393 -0
- package/src/config.ts +65 -0
- package/src/const.ts +126 -0
- package/src/errors.ts +44 -0
- package/src/index.ts +5 -0
- package/src/math.ts +37 -0
- package/src/test_data.test.ts +17 -0
- package/src/transaction/aftermath.ts +142 -0
- package/src/transaction/cetus.ts +281 -0
- package/src/transaction/common.ts +169 -0
- package/src/transaction/deepbook.ts +126 -0
- package/src/transaction/flowx.ts +97 -0
- package/src/transaction/index.ts +1 -0
- package/src/transaction/kriya.ts +77 -0
- package/src/transaction/router.ts +341 -0
- package/src/transaction/swap.ts +164 -0
- package/src/transaction/turbos.ts +114 -0
- package/src/types/CoinAssist.ts +217 -0
- package/src/types/sui.ts +148 -0
- package/src/utils/account_cap.ts +62 -0
- package/src/utils/coin.spec.ts +10 -0
- package/src/utils/coin.ts +61 -0
- package/src/utils/contracts.ts +136 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/transaction.ts +20 -0
- package/tests/router.test.ts +255 -0
- package/tests/wallet.test.ts +21 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +23 -0
- package/version.mjs +28 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SuiClient } from "@mysten/sui/client";
|
|
2
|
+
import { Transaction, TransactionObjectArgument } from "@mysten/sui/transactions";
|
|
3
|
+
export type GetOrCreateAccountCapResult = {
|
|
4
|
+
accountCap: TransactionObjectArgument;
|
|
5
|
+
isCreate: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare function getOrCreateAccountCap(txb: Transaction, client: SuiClient, owner: string): Promise<GetOrCreateAccountCapResult>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function completionCoin(s: string): string;
|
|
2
|
+
export declare function compareCoins(coinA: string, coinB: string): boolean;
|
|
3
|
+
export declare function parseTurbosPoolFeeType(typeData: string): string | null;
|
|
4
|
+
export declare function parseAftermathFeeType(typeData: string): string | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SuiAddress, SuiStructTag } from '../types/sui';
|
|
2
|
+
export declare function isSortedSymbols(symbolX: string, symbolY: string): boolean;
|
|
3
|
+
export declare function composeType(address: string, generics: SuiAddress[]): SuiAddress;
|
|
4
|
+
export declare function composeType(address: string, struct: string, generics?: SuiAddress[]): SuiAddress;
|
|
5
|
+
export declare function composeType(address: string, module: string, struct: string, generics?: SuiAddress[]): SuiAddress;
|
|
6
|
+
export declare function extractAddressFromType(type: string): string;
|
|
7
|
+
export declare function extractStructTagFromType(type: string): SuiStructTag;
|
|
8
|
+
export declare function normalizeCoinType(coinType: string): string;
|
|
9
|
+
export declare function fixSuiObjectId(value: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Recursively traverses the given data object and patches any string values that represent Sui object IDs.
|
|
12
|
+
*
|
|
13
|
+
* @param {any} data - The data object to be patched.
|
|
14
|
+
*/
|
|
15
|
+
export declare function patchFixSuiObjectId(data: any): void;
|
|
16
|
+
export declare function createTarget(packageName: string, moduleName: string, functionName: string): `${string}::${string}::${string}`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './contracts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/jest.config.mjs
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cetusprotocol/aggregator-sdk",
|
|
3
|
+
"version": "0.0.0-experimental-20240719182939",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup --format cjs,esm --dts",
|
|
10
|
+
"dev": "tsup --watch",
|
|
11
|
+
"test": "node -r esm node_modules/.bin/jest",
|
|
12
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
|
|
13
|
+
"publish:test": "node version.mjs && npm publish --tag experimental"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bn.js": "^5.1.5",
|
|
17
|
+
"@types/bun": "latest",
|
|
18
|
+
"bn.js": "^5.2.1",
|
|
19
|
+
"decimal.js": "^10.4.3",
|
|
20
|
+
"tsup": "^8.0.2"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@babel/core": "^7.24.5",
|
|
27
|
+
"@babel/preset-env": "^7.24.5",
|
|
28
|
+
"@babel/preset-typescript": "^7.24.1",
|
|
29
|
+
"@jest/globals": "^29.7.0",
|
|
30
|
+
"@mysten/sui": "^1.0.5",
|
|
31
|
+
"@types/jest": "^29.5.12",
|
|
32
|
+
"@types/node": "^20.12.12",
|
|
33
|
+
"babel-jest": "^29.7.0",
|
|
34
|
+
"bip39": "^3.1.0",
|
|
35
|
+
"dotenv": "^16.4.5",
|
|
36
|
+
"jest": "^29.7.0",
|
|
37
|
+
"node-fetch": "^3.3.2",
|
|
38
|
+
"ts-jest": "^29.1.3",
|
|
39
|
+
"typescript": "^5.4.5"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import type { AggregatorConfig } from "./config"
|
|
2
|
+
import Decimal from "decimal.js"
|
|
3
|
+
import { SuiClient } from "@mysten/sui/client"
|
|
4
|
+
import { CoinAsset } from "./types/sui"
|
|
5
|
+
import { createTarget, extractStructTagFromType } from "./utils"
|
|
6
|
+
import { Transaction } from "@mysten/sui/transactions"
|
|
7
|
+
import {
|
|
8
|
+
buildInputCoin,
|
|
9
|
+
checkCoinThresholdAndMergeCoin,
|
|
10
|
+
transferOrDestoryCoin,
|
|
11
|
+
} from "./transaction/common"
|
|
12
|
+
import { expectInputRouterSwap, expectOutputRouterSwap } from "./transaction"
|
|
13
|
+
import { CalculateAmountLimit } from "./math"
|
|
14
|
+
import { Signer } from "@mysten/sui/dist/cjs/cryptography"
|
|
15
|
+
import BN from "bn.js"
|
|
16
|
+
import { swapInPools } from "./transaction/swap"
|
|
17
|
+
import { completionCoin } from "./utils/coin"
|
|
18
|
+
import { SUI_FRAMEWORK_ADDRESS } from "@mysten/sui/utils"
|
|
19
|
+
import { AFTERMATH_AMM, JOIN_FUNC, PAY_MODULE, TURBOS_DEX, ZERO } from "./const"
|
|
20
|
+
|
|
21
|
+
export type ExtendedDetails = {
|
|
22
|
+
aftermathPoolFlatness?: number
|
|
23
|
+
aftermathLpSupplyType?: string
|
|
24
|
+
turbosFeeType?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type Path = {
|
|
28
|
+
id: string
|
|
29
|
+
a2b: boolean
|
|
30
|
+
provider: string
|
|
31
|
+
from: string
|
|
32
|
+
target: string
|
|
33
|
+
feeRate: number
|
|
34
|
+
amountIn: number
|
|
35
|
+
amountOut: number
|
|
36
|
+
extendedDetails?: ExtendedDetails
|
|
37
|
+
version?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type Router = {
|
|
41
|
+
path: Path[]
|
|
42
|
+
amountIn: BN
|
|
43
|
+
amountOut: BN
|
|
44
|
+
initialPrice: Decimal
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type RouterData = {
|
|
48
|
+
amountIn: BN
|
|
49
|
+
amountOut: BN
|
|
50
|
+
routes: Router[]
|
|
51
|
+
insufficientLiquidity: boolean
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type AggregatorResponse = {
|
|
55
|
+
code: number
|
|
56
|
+
msg: string
|
|
57
|
+
data: RouterData
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type BuildRouterSwapParams = {
|
|
61
|
+
routers: Router[]
|
|
62
|
+
amountIn: BN
|
|
63
|
+
amountOut: BN
|
|
64
|
+
byAmountIn: boolean
|
|
65
|
+
slippage: number
|
|
66
|
+
fromCoinType: string
|
|
67
|
+
targetCoinType: string
|
|
68
|
+
partner?: string
|
|
69
|
+
isMergeTragetCoin?: boolean
|
|
70
|
+
refreshAllCoins?: boolean
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface FindRouterParams {
|
|
74
|
+
from: string
|
|
75
|
+
target: string
|
|
76
|
+
amount: BN
|
|
77
|
+
byAmountIn: boolean
|
|
78
|
+
depth: number | null
|
|
79
|
+
splitAlgorithm: string | null
|
|
80
|
+
splitFactor: number | null
|
|
81
|
+
splitCount: number | null
|
|
82
|
+
providers: string[] | null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface SwapInPoolsParams {
|
|
86
|
+
from: string
|
|
87
|
+
target: string
|
|
88
|
+
amount: BN
|
|
89
|
+
byAmountIn: boolean
|
|
90
|
+
pools: string[]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface SwapInPoolsResult {
|
|
94
|
+
isExceed: boolean
|
|
95
|
+
routeData?: RouterData
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export class AggregatorClient {
|
|
99
|
+
private config: AggregatorConfig
|
|
100
|
+
|
|
101
|
+
private wallet: string
|
|
102
|
+
|
|
103
|
+
private client: SuiClient
|
|
104
|
+
|
|
105
|
+
private allCoins: CoinAsset[]
|
|
106
|
+
|
|
107
|
+
constructor(config: AggregatorConfig) {
|
|
108
|
+
this.config = config
|
|
109
|
+
this.client = new SuiClient({ url: config.getFullNodeUrl() })
|
|
110
|
+
this.wallet = config.getWallet()
|
|
111
|
+
this.allCoins = []
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async getAllCoins(): Promise<CoinAsset[]> {
|
|
115
|
+
let cursor = null
|
|
116
|
+
let limit = 50
|
|
117
|
+
|
|
118
|
+
const allCoins: CoinAsset[] = []
|
|
119
|
+
|
|
120
|
+
while (true) {
|
|
121
|
+
const gotAllCoins = await this.client.getAllCoins({
|
|
122
|
+
owner: this.wallet,
|
|
123
|
+
cursor,
|
|
124
|
+
limit,
|
|
125
|
+
})
|
|
126
|
+
for (const coin of gotAllCoins.data) {
|
|
127
|
+
allCoins.push({
|
|
128
|
+
coinAddress: extractStructTagFromType(coin.coinType).source_address,
|
|
129
|
+
coinObjectId: coin.coinObjectId,
|
|
130
|
+
balance: BigInt(coin.balance),
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (gotAllCoins.data.length < limit) {
|
|
135
|
+
break
|
|
136
|
+
}
|
|
137
|
+
cursor = gotAllCoins.data[limit - 1].coinObjectId
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return allCoins
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async findRouter(
|
|
144
|
+
fromRouterParams: FindRouterParams
|
|
145
|
+
): Promise<RouterData | null> {
|
|
146
|
+
const {
|
|
147
|
+
from,
|
|
148
|
+
target,
|
|
149
|
+
amount,
|
|
150
|
+
byAmountIn,
|
|
151
|
+
depth,
|
|
152
|
+
splitAlgorithm,
|
|
153
|
+
splitFactor,
|
|
154
|
+
splitCount,
|
|
155
|
+
providers,
|
|
156
|
+
} = fromRouterParams
|
|
157
|
+
const fromCoin = completionCoin(from)
|
|
158
|
+
const targetCoin = completionCoin(target)
|
|
159
|
+
|
|
160
|
+
let url =
|
|
161
|
+
this.config.getAggregatorUrl() +
|
|
162
|
+
`/find_routes?from=${fromCoin}&target=${targetCoin}&amount=${amount.toString()}&by_amount_in=${byAmountIn}`
|
|
163
|
+
|
|
164
|
+
if (depth) {
|
|
165
|
+
url += `&depth=${depth}`
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (splitAlgorithm) {
|
|
169
|
+
url += `&split_algorithm=${splitAlgorithm}`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (splitFactor) {
|
|
173
|
+
url += `&split_factor=${splitFactor}`
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (splitCount) {
|
|
177
|
+
url += `&split_count=${splitCount}`
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (providers) {
|
|
181
|
+
if (providers.length > 0) {
|
|
182
|
+
url += `&providers=${providers.join(",")}`
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const response = await fetch(url)
|
|
187
|
+
const data = await response.json()
|
|
188
|
+
|
|
189
|
+
if (data.data != null) {
|
|
190
|
+
const res = parseRouterResponse(data.data)
|
|
191
|
+
return res
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (data.msg === "liquidity is not enough") {
|
|
195
|
+
return {
|
|
196
|
+
amountIn: ZERO,
|
|
197
|
+
amountOut: ZERO,
|
|
198
|
+
routes: [],
|
|
199
|
+
insufficientLiquidity: true,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async swapInPools(
|
|
206
|
+
params: SwapInPoolsParams
|
|
207
|
+
): Promise<SwapInPoolsResult | null> {
|
|
208
|
+
let result
|
|
209
|
+
try {
|
|
210
|
+
result = await swapInPools(this.client, params, this.config)
|
|
211
|
+
} catch (e) {
|
|
212
|
+
return null
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return result
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async routerSwap(params: BuildRouterSwapParams): Promise<Transaction> {
|
|
219
|
+
const {
|
|
220
|
+
routers: _,
|
|
221
|
+
amountIn,
|
|
222
|
+
amountOut,
|
|
223
|
+
byAmountIn,
|
|
224
|
+
slippage,
|
|
225
|
+
fromCoinType,
|
|
226
|
+
targetCoinType,
|
|
227
|
+
partner,
|
|
228
|
+
isMergeTragetCoin,
|
|
229
|
+
refreshAllCoins,
|
|
230
|
+
} = params
|
|
231
|
+
const amountLimit = CalculateAmountLimit(
|
|
232
|
+
byAmountIn ? amountOut : amountIn,
|
|
233
|
+
byAmountIn,
|
|
234
|
+
slippage
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
const txb = new Transaction()
|
|
238
|
+
|
|
239
|
+
if (refreshAllCoins || this.allCoins.length === 0) {
|
|
240
|
+
this.allCoins = await this.getAllCoins()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const allCoins = this.allCoins
|
|
244
|
+
let targetCoins = []
|
|
245
|
+
|
|
246
|
+
if (byAmountIn) {
|
|
247
|
+
const buildFromCoinRes = buildInputCoin(
|
|
248
|
+
txb,
|
|
249
|
+
allCoins,
|
|
250
|
+
BigInt(amountIn.toString()),
|
|
251
|
+
fromCoinType
|
|
252
|
+
)
|
|
253
|
+
const fromCoin = buildFromCoinRes.targetCoin
|
|
254
|
+
const swapedCoins = await expectInputRouterSwap(
|
|
255
|
+
this.client,
|
|
256
|
+
params,
|
|
257
|
+
txb,
|
|
258
|
+
fromCoin,
|
|
259
|
+
this.config,
|
|
260
|
+
partner
|
|
261
|
+
)
|
|
262
|
+
const mergedCoin = checkCoinThresholdAndMergeCoin(
|
|
263
|
+
txb,
|
|
264
|
+
swapedCoins,
|
|
265
|
+
targetCoinType,
|
|
266
|
+
amountLimit,
|
|
267
|
+
this.config
|
|
268
|
+
)
|
|
269
|
+
targetCoins.push(mergedCoin)
|
|
270
|
+
} else {
|
|
271
|
+
const fromCoin = buildInputCoin(
|
|
272
|
+
txb,
|
|
273
|
+
allCoins,
|
|
274
|
+
BigInt(amountLimit),
|
|
275
|
+
fromCoinType
|
|
276
|
+
).targetCoin
|
|
277
|
+
const swapedCoins = await expectOutputRouterSwap(
|
|
278
|
+
params,
|
|
279
|
+
txb,
|
|
280
|
+
fromCoin,
|
|
281
|
+
this.config,
|
|
282
|
+
partner
|
|
283
|
+
)
|
|
284
|
+
targetCoins.push(...swapedCoins)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (isMergeTragetCoin) {
|
|
288
|
+
const targetCoinRes = buildInputCoin(
|
|
289
|
+
txb,
|
|
290
|
+
allCoins,
|
|
291
|
+
BigInt(0),
|
|
292
|
+
targetCoinType
|
|
293
|
+
)
|
|
294
|
+
txb.mergeCoins(targetCoinRes.targetCoin, targetCoins)
|
|
295
|
+
if (targetCoinRes.isMintZeroCoin) {
|
|
296
|
+
transferOrDestoryCoin(
|
|
297
|
+
txb,
|
|
298
|
+
targetCoinRes.targetCoin,
|
|
299
|
+
targetCoinType,
|
|
300
|
+
this.config
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
if (targetCoins.length > 1) {
|
|
305
|
+
const vec = txb.makeMoveVec({ elements: targetCoins.slice(1) })
|
|
306
|
+
txb.moveCall({
|
|
307
|
+
target: createTarget(SUI_FRAMEWORK_ADDRESS, PAY_MODULE, JOIN_FUNC),
|
|
308
|
+
typeArguments: [targetCoinType],
|
|
309
|
+
arguments: [targetCoins[0], vec],
|
|
310
|
+
})
|
|
311
|
+
}
|
|
312
|
+
transferOrDestoryCoin(txb, targetCoins[0], targetCoinType, this.config)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return txb
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async signAndExecuteTransaction(txb: Transaction, signer: Signer) {
|
|
319
|
+
const res = await this.client.signAndExecuteTransaction({
|
|
320
|
+
transaction: txb,
|
|
321
|
+
signer,
|
|
322
|
+
options: {
|
|
323
|
+
showEffects: true,
|
|
324
|
+
showEvents: true,
|
|
325
|
+
showInput: true,
|
|
326
|
+
showBalanceChanges: true,
|
|
327
|
+
},
|
|
328
|
+
})
|
|
329
|
+
return res
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async devInspectTransactionBlock(txb: Transaction, signer: Signer) {
|
|
333
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
334
|
+
transactionBlock: txb,
|
|
335
|
+
sender: signer.getPublicKey().toSuiAddress(),
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
return res
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async sendTransaction(txb: Transaction, signer: Signer) {
|
|
342
|
+
const res = await this.client.signAndExecuteTransaction({
|
|
343
|
+
transaction: txb,
|
|
344
|
+
signer,
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
return res
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function parseRouterResponse(data: any): RouterData {
|
|
352
|
+
return {
|
|
353
|
+
amountIn: new BN(data.amount_in.toString()),
|
|
354
|
+
amountOut: new BN(data.amount_out.toString()),
|
|
355
|
+
insufficientLiquidity: false,
|
|
356
|
+
routes: data.routes.map((route: any) => {
|
|
357
|
+
return {
|
|
358
|
+
path: route.path.map((path: any) => {
|
|
359
|
+
let version
|
|
360
|
+
if (path.provider === AFTERMATH_AMM) {
|
|
361
|
+
version =
|
|
362
|
+
path.extended_details.aftermath_pool_flatness === 0 ? "v2" : "v3"
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
let extendedDetails
|
|
366
|
+
if (path.provider === TURBOS_DEX || path.provider === AFTERMATH_AMM) {
|
|
367
|
+
extendedDetails = {
|
|
368
|
+
aftermathLpSupplyType:
|
|
369
|
+
path.extended_details?.aftermath_lp_supply_type,
|
|
370
|
+
turbosFeeType: path.extended_details?.turbos_fee_type,
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
id: path.id,
|
|
376
|
+
a2b: path.direction,
|
|
377
|
+
provider: path.provider,
|
|
378
|
+
from: path.from,
|
|
379
|
+
target: path.target,
|
|
380
|
+
feeRate: path.fee_rate,
|
|
381
|
+
amountIn: path.amount_in,
|
|
382
|
+
amountOut: path.amount_out,
|
|
383
|
+
extendedDetails,
|
|
384
|
+
version,
|
|
385
|
+
}
|
|
386
|
+
}),
|
|
387
|
+
amountIn: new BN(route.amount_in.toString()),
|
|
388
|
+
amountOut: new BN(route.amount_out.toString()),
|
|
389
|
+
initialPrice: new Decimal(route.initial_price.toString()),
|
|
390
|
+
}
|
|
391
|
+
}),
|
|
392
|
+
}
|
|
393
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type Package = {
|
|
2
|
+
packageName: string
|
|
3
|
+
packageId: string
|
|
4
|
+
publishedAt: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export enum ENV {
|
|
8
|
+
MAINNET,
|
|
9
|
+
TESTNET,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class AggregatorConfig {
|
|
13
|
+
private aggregatorUrl: string;
|
|
14
|
+
private fullNodeUrl: string;
|
|
15
|
+
private wallet: string;
|
|
16
|
+
private packages: Package[];
|
|
17
|
+
private env: ENV;
|
|
18
|
+
|
|
19
|
+
constructor(aggregatorUrl: string, fullNodeUrl: string, wallet: string, packages: Package[], env: ENV) {
|
|
20
|
+
this.aggregatorUrl = aggregatorUrl;
|
|
21
|
+
this.fullNodeUrl = fullNodeUrl;
|
|
22
|
+
this.wallet = wallet;
|
|
23
|
+
this.packages = packages;
|
|
24
|
+
this.env = env;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
getAggregatorUrl(): string {
|
|
28
|
+
return this.aggregatorUrl;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getFullNodeUrl(): string {
|
|
32
|
+
return this.fullNodeUrl;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getWallet(): string {
|
|
36
|
+
return this.wallet;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getENV(): ENV {
|
|
40
|
+
return this.env
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getPackages(): Package[] {
|
|
44
|
+
return this.packages;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getPackage(packageName: string) {
|
|
48
|
+
return this.packages.find(pkg => pkg.packageName === packageName);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
addPackage(newPackage: Package): void {
|
|
52
|
+
if (!newPackage.packageName || !newPackage.packageId || !newPackage.publishedAt) {
|
|
53
|
+
throw new Error("Invalid package data");
|
|
54
|
+
}
|
|
55
|
+
this.packages.push(newPackage);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
removePackageById(packageId: string): void {
|
|
59
|
+
this.packages = this.packages.filter(pkg => pkg.packageId !== packageId);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
findPackageById(packageId: string): Package | undefined {
|
|
63
|
+
return this.packages.find(pkg => pkg.packageId === packageId);
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/const.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import BN from "bn.js"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The address representing the clock in the system.
|
|
5
|
+
*/
|
|
6
|
+
export const CLOCK_ADDRESS =
|
|
7
|
+
"0x0000000000000000000000000000000000000000000000000000000000000006"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The address for CoinInfo module.
|
|
11
|
+
*/
|
|
12
|
+
export const CoinInfoAddress = "0x1::coin::CoinInfo"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The address for CoinStore module.
|
|
16
|
+
*/
|
|
17
|
+
export const CoinStoreAddress = "0x1::coin::CoinStore"
|
|
18
|
+
|
|
19
|
+
export const SuiZeroCoinFn = "0x2::coin::zero"
|
|
20
|
+
|
|
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
|
+
// Module names
|
|
33
|
+
export const CETUS_MODULE = "cetus"
|
|
34
|
+
export const DEEPBOOK_MODULE = "deepbook"
|
|
35
|
+
export const KRIYA_MODULE = "kriya"
|
|
36
|
+
export const UTILS_MODULE = "utils"
|
|
37
|
+
export const POOL_MODULT = "pool"
|
|
38
|
+
export const PAY_MODULE = "pay"
|
|
39
|
+
export const FLOWX_AMM_MODULE = "flowx_amm"
|
|
40
|
+
export const TURBOS_MODULE = "turbos"
|
|
41
|
+
export const AFTERMATH_MODULE = "aftermath"
|
|
42
|
+
|
|
43
|
+
export const DEEPBOOK_CUSTODIAN_V2_MODULE = "custodian_v2"
|
|
44
|
+
export const DEEPBOOK_CLOB_V2_MODULE = "clob_v2"
|
|
45
|
+
|
|
46
|
+
// Function names
|
|
47
|
+
export const FlashSwapFunc = "flash_swap"
|
|
48
|
+
export const FlashSwapWithPartnerFunc = "flash_swap_with_partner"
|
|
49
|
+
export const RepayFalshSwapFunc = "repay_flash_swap"
|
|
50
|
+
export const RepayFlashSwapWithPartnerFunc = "repay_flash_swap_with_partner"
|
|
51
|
+
|
|
52
|
+
export const FlashSwapA2BFunc = "flash_swap_a2b"
|
|
53
|
+
export const FlashSwapB2AFunc = "flash_swap_b2a"
|
|
54
|
+
|
|
55
|
+
export const FlashSwapWithPartnerA2BFunc = "flash_swap_with_partner_a2b"
|
|
56
|
+
export const FlashSwapWithPartnerB2AFunc = "flash_swap_with_partner_b2a"
|
|
57
|
+
|
|
58
|
+
export const REPAY_FLASH_SWAP_A2B_FUNC = "repay_flash_swap_a2b"
|
|
59
|
+
export const REPAY_FLASH_SWAP_B2A_FUNC = "repay_flash_swap_b2a"
|
|
60
|
+
|
|
61
|
+
export const REPAY_FLASH_SWAP_WITH_PARTNER_A2B_FUNC =
|
|
62
|
+
"repay_flash_swap_with_partner_a2b"
|
|
63
|
+
export const REPAY_FLASH_SWAP_WITH_PARTNER_B2A_FUNC =
|
|
64
|
+
"repay_flash_swap_with_partner_b2a"
|
|
65
|
+
|
|
66
|
+
export const SWAP_A2B_FUNC = "swap_a2b"
|
|
67
|
+
export const SWAP_B2A_FUNC = "swap_b2a"
|
|
68
|
+
|
|
69
|
+
export const TRANSFER_OR_DESTORY_COIN_FUNC = "transfer_or_destroy_coin"
|
|
70
|
+
export const CHECK_COINS_THRESHOLD_FUNC = "check_coins_threshold"
|
|
71
|
+
|
|
72
|
+
export const JOIN_FUNC = "join_vec"
|
|
73
|
+
|
|
74
|
+
export const TRANSFER_ACCOUNT_CAP = "transfer_account_cap"
|
|
75
|
+
|
|
76
|
+
// Package IDs
|
|
77
|
+
export const DEEPBOOK_PACKAGE_ID =
|
|
78
|
+
"0x000000000000000000000000000000000000000000000000000000000000dee9"
|
|
79
|
+
export const DEEPBOOK_PUBLISHED_AT =
|
|
80
|
+
"0x000000000000000000000000000000000000000000000000000000000000dee9"
|
|
81
|
+
|
|
82
|
+
export const CETUS_PUBLISHED_AT =
|
|
83
|
+
"0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a"
|
|
84
|
+
|
|
85
|
+
// Mainnet Cetus objects IDs
|
|
86
|
+
export const MAINNET_CETUS_GLOBAL_CONFIG_ID =
|
|
87
|
+
"0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f"
|
|
88
|
+
|
|
89
|
+
// Testnet Cetus objects IDs
|
|
90
|
+
export const TESTNET_CETUS_GLOBAL_CONFIG_ID =
|
|
91
|
+
"0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a"
|
|
92
|
+
|
|
93
|
+
export const ZERO = new BN(0)
|
|
94
|
+
|
|
95
|
+
export const ONE = new BN(1)
|
|
96
|
+
|
|
97
|
+
export const TWO = new BN(2)
|
|
98
|
+
|
|
99
|
+
export const U128 = TWO.pow(new BN(128))
|
|
100
|
+
|
|
101
|
+
export const U64_MAX_BN = new BN("18446744073709551615")
|
|
102
|
+
|
|
103
|
+
export const U64_MAX = "18446744073709551615"
|
|
104
|
+
|
|
105
|
+
export const MAINNET_FLOWX_AMM_CONTAINER_ID =
|
|
106
|
+
"0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511"
|
|
107
|
+
export const TESTNET_FLOWX_AMM_CONTAINER_ID = ""
|
|
108
|
+
|
|
109
|
+
export const TURBOS_VERSIONED =
|
|
110
|
+
"0xf1cf0e81048df168ebeb1b8030fad24b3e0b53ae827c25053fff0779c1445b6f"
|
|
111
|
+
|
|
112
|
+
export const MAINNET_AFTERMATH_REGISTRY_ID =
|
|
113
|
+
"0xfcc774493db2c45c79f688f88d28023a3e7d98e4ee9f48bbf5c7990f651577ae"
|
|
114
|
+
export const TESTNET_AFTERMATH_REGISTRY_ID = ""
|
|
115
|
+
export const MAINNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID =
|
|
116
|
+
"0xf194d9b1bcad972e45a7dd67dd49b3ee1e3357a00a50850c52cd51bb450e13b4"
|
|
117
|
+
export const TESTNET_AFTERMATH_PROTOCOL_FEE_VAULT_ID = ""
|
|
118
|
+
export const MAINNET_AFTERMATH_TREASURY_ID =
|
|
119
|
+
"0x28e499dff5e864a2eafe476269a4f5035f1c16f338da7be18b103499abf271ce"
|
|
120
|
+
export const TESTNET_AFTERMATH_TREASURY_ID = ""
|
|
121
|
+
export const MAINNET_AFTERMATH_INSURANCE_FUND_ID =
|
|
122
|
+
"0xf0c40d67b078000e18032334c3325c47b9ec9f3d9ae4128be820d54663d14e3b"
|
|
123
|
+
export const TESTNET_AFTERMATH_INSURANCE_FUND_ID = ""
|
|
124
|
+
export const MAINNET_AFTERMATH_REFERRAL_VAULT_ID =
|
|
125
|
+
"0x35d35b0e5b177593d8c3a801462485572fc30861e6ce96a55af6dc4730709278"
|
|
126
|
+
export const TESTNET_AFTERMATH_REFERRAL_VAULT_ID = ""
|