@cetusprotocol/aggregator-sdk 1.4.3 → 1.4.5
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/README.md +46 -302
- package/bun.lock +83 -663
- package/dist/index.js +3 -2
- package/dist/index.mjs +3 -2
- package/package.json +8 -14
- package/tsconfig.json +1 -0
- package/vitest.config.ts +16 -0
- package/examples/mergeSwapExample.ts +0 -142
package/README.md
CHANGED
|
@@ -29,129 +29,7 @@ Multi-Platform Support: Currently, we have integrated multiple mainstream DEXs o
|
|
|
29
29
|
|
|
30
30
|
By using our aggregator, you can trade more efficiently and securely on the Sui blockchain, fully leveraging the various opportunities brought by decentralized finance (DeFi).
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
The Cetus Aggregator SDK supports 25+ decentralized exchanges (DEXs) on the Sui blockchain:
|
|
35
|
-
|
|
36
|
-
- **Cetus**
|
|
37
|
-
- **DeepBook V3**
|
|
38
|
-
- **Kriya V2/V3**
|
|
39
|
-
- **FlowX V2/V3**
|
|
40
|
-
- **Turbos**
|
|
41
|
-
- **Aftermath**
|
|
42
|
-
- **Haedal**
|
|
43
|
-
- **Volo**
|
|
44
|
-
- **AfSui**
|
|
45
|
-
- **Bluemove**
|
|
46
|
-
- **Scallop**
|
|
47
|
-
- **Suilend**
|
|
48
|
-
- **Bluefin**
|
|
49
|
-
- **HaedalHMM**
|
|
50
|
-
- **Alphafi**
|
|
51
|
-
- **SpringSui**
|
|
52
|
-
- **Steamm CPMM**
|
|
53
|
-
- **Steamm OMM**
|
|
54
|
-
- **Metastable**
|
|
55
|
-
- **Obric**
|
|
56
|
-
- **HaWAL**
|
|
57
|
-
- **Momentum**
|
|
58
|
-
- **Magma**
|
|
59
|
-
- **Sevenk**
|
|
60
|
-
|
|
61
|
-
# Aggregator SDK V3
|
|
62
|
-
|
|
63
|
-
## Advantages of Aggregator Client V3
|
|
64
|
-
|
|
65
|
-
The new Aggregator Client V3 offers significant improvements over the previous version:
|
|
66
|
-
|
|
67
|
-
### Transaction Traceability
|
|
68
|
-
|
|
69
|
-
- **Complete Transaction History**: All swap transactions are fully traceable on the blockchain
|
|
70
|
-
- **Detailed Route Information**: Track exactly which DEXs and pools were used in multi-hop swaps
|
|
71
|
-
- **Gas Optimization Transparency**: See how transactions were optimized to reduce gas costs
|
|
72
|
-
|
|
73
|
-
### Transaction Merging for Gas Optimization
|
|
74
|
-
|
|
75
|
-
- **Reduced Gas Costs**: Significantly lower transaction fees through optimized execution paths
|
|
76
|
-
- **Smart Route Aggregation**: Automatically combines similar operations to minimize gas usage
|
|
77
|
-
|
|
78
|
-
### Enhanced Features
|
|
79
|
-
|
|
80
|
-
- **Increased Reliability**: More robust transaction execution with better error handling
|
|
81
|
-
|
|
82
|
-
## Migration from V2 to V3 API
|
|
83
|
-
|
|
84
|
-
### Installation
|
|
85
|
-
|
|
86
|
-
Both V2 and V3 APIs use the same package:
|
|
87
|
-
|
|
88
|
-
```bash
|
|
89
|
-
npm install @cetusprotocol/aggregator-sdk
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Key Changes
|
|
93
|
-
|
|
94
|
-
**Important:** Both V2 and V3 use the same `AggregatorClient` class. The main differences are:
|
|
95
|
-
- **Return types**: V3 uses `RouterDataV3` (with flattened `paths`) instead of V2's `RouterData` (with nested `routes`)
|
|
96
|
-
- **Swap methods**: V3 introduces `fastRouterSwap()` for automatic coin handling
|
|
97
|
-
- **Transaction structure**: V3 optimizes gas through transaction merging
|
|
98
|
-
|
|
99
|
-
1. **Client Initialization (Same for both)**
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
import { AggregatorClient } from "@cetusprotocol/aggregator-sdk"
|
|
103
|
-
|
|
104
|
-
const client = new AggregatorClient({
|
|
105
|
-
// Optional: Add custom configuration
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
2. **Router Finding (API unchanged, but return type differs)**
|
|
110
|
-
|
|
111
|
-
```typescript
|
|
112
|
-
import BN from "bn.js"
|
|
113
|
-
|
|
114
|
-
// Works with both V2 and V3
|
|
115
|
-
const routers = await client.findRouters({
|
|
116
|
-
from: "0x2::sui::SUI",
|
|
117
|
-
target: "0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS",
|
|
118
|
-
amount: new BN(1000000),
|
|
119
|
-
byAmountIn: true,
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
// V2 returns: RouterData with routes[]
|
|
123
|
-
// V3 returns: RouterDataV3 with paths[]
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
3. **Transaction Building**
|
|
127
|
-
|
|
128
|
-
```typescript
|
|
129
|
-
import { Transaction } from "@mysten/sui/transactions"
|
|
130
|
-
|
|
131
|
-
const txb = new Transaction()
|
|
132
|
-
|
|
133
|
-
// V3 Method (Recommended) - Automatic coin handling
|
|
134
|
-
await client.fastRouterSwap({
|
|
135
|
-
router: routers,
|
|
136
|
-
txb,
|
|
137
|
-
slippage: 0.01,
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
// V3 Alternative - Manual coin handling for PTB building
|
|
141
|
-
const targetCoin = await client.routerSwap({
|
|
142
|
-
router: routers,
|
|
143
|
-
txb,
|
|
144
|
-
inputCoin, // TransactionObjectArgument
|
|
145
|
-
slippage: 0.01,
|
|
146
|
-
})
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
4. **Benefits of V3 Migration**
|
|
150
|
-
- Reduced gas costs through transaction merging
|
|
151
|
-
- Better transaction traceability on-chain
|
|
152
|
-
- Improved error handling and reliability
|
|
153
|
-
- Access to latest DEX integrations
|
|
154
|
-
- Automatic coin management with `fastRouterSwap()`
|
|
32
|
+
# Aggregator SDK
|
|
155
33
|
|
|
156
34
|
## Install
|
|
157
35
|
|
|
@@ -163,208 +41,74 @@ npm install @cetusprotocol/aggregator-sdk
|
|
|
163
41
|
|
|
164
42
|
## Usage
|
|
165
43
|
|
|
166
|
-
###
|
|
44
|
+
### 1. Init client with rpc and package config
|
|
167
45
|
|
|
168
46
|
```typescript
|
|
169
|
-
|
|
170
|
-
import { Transaction } from "@mysten/sui/transactions"
|
|
171
|
-
import BN from "bn.js"
|
|
172
|
-
import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519"
|
|
173
|
-
|
|
174
|
-
// Initialize your keypair (example using Ed25519)
|
|
175
|
-
const keypair = Ed25519Keypair.deriveKeypair("your-mnemonic-phrase")
|
|
47
|
+
const client = new AggregatorClient({})
|
|
176
48
|
```
|
|
177
49
|
|
|
178
|
-
###
|
|
50
|
+
### 2. Get best router swap result from aggregator service
|
|
179
51
|
|
|
180
52
|
```typescript
|
|
181
|
-
|
|
182
|
-
import { getFullnodeUrl, SuiClient } from "@mysten/sui/client"
|
|
183
|
-
|
|
184
|
-
const client = new AggregatorClient({
|
|
185
|
-
// Optional configuration parameters:
|
|
186
|
-
|
|
187
|
-
// Network environment (default: Mainnet)
|
|
188
|
-
env: Env.Mainnet, // or Env.Testnet
|
|
189
|
-
|
|
190
|
-
// Custom API endpoint for aggregator service
|
|
191
|
-
// endpoint: "https://api-sui.cetus.zone",
|
|
192
|
-
|
|
193
|
-
// Custom Sui client (default: mainnet RPC)
|
|
194
|
-
// client: new SuiClient({ url: getFullnodeUrl("mainnet") }),
|
|
195
|
-
|
|
196
|
-
// Partner ID for revenue sharing
|
|
197
|
-
// partner: "your-partner-id",
|
|
198
|
-
|
|
199
|
-
// CETUS DLMM specific partner ID
|
|
200
|
-
// cetusDlmmPartner: "your-dlmm-partner-id",
|
|
201
|
-
|
|
202
|
-
// Overlay fee rate (0 to 0.01 for 0-1%)
|
|
203
|
-
// overlayFeeRate: 0.001, // 0.1%
|
|
204
|
-
|
|
205
|
-
// Overlay fee receiver address
|
|
206
|
-
// overlayFeeReceiver: "0x...",
|
|
207
|
-
|
|
208
|
-
// Custom Pyth oracle URLs
|
|
209
|
-
// pythUrls: ["https://hermes.pyth.network"],
|
|
210
|
-
|
|
211
|
-
// API key for rate limiting
|
|
212
|
-
// apiKey: "your-api-key",
|
|
213
|
-
})
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
### 2. Find Best Router
|
|
217
|
-
|
|
218
|
-
Get the optimal swap route from the aggregator service:
|
|
219
|
-
|
|
220
|
-
```typescript
|
|
221
|
-
const amount = new BN(1000000) // 1 SUI (with 6 decimals)
|
|
53
|
+
const amount = new BN(1000000)
|
|
222
54
|
const from = "0x2::sui::SUI"
|
|
223
|
-
const target =
|
|
55
|
+
const target =
|
|
56
|
+
"0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS"
|
|
224
57
|
|
|
225
58
|
const routers = await client.findRouters({
|
|
226
59
|
from,
|
|
227
60
|
target,
|
|
228
61
|
amount,
|
|
229
|
-
byAmountIn: true, // true
|
|
230
|
-
// Optional parameters:
|
|
231
|
-
// depth: 3, // Maximum number of hops
|
|
232
|
-
// providers: ["CETUS", "TURBOS"], // Limit to specific DEXs
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
// Check if a valid route was found
|
|
236
|
-
if (!routers || routers.insufficientLiquidity) {
|
|
237
|
-
console.error("No valid route found or insufficient liquidity")
|
|
238
|
-
process.exit(1)
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
console.log(`Expected output: ${routers.amountOut.toString()}`)
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
### 3. Fast Swap (Recommended - Automatic Coin Handling)
|
|
245
|
-
|
|
246
|
-
The easiest way to execute a swap with automatic coin management:
|
|
247
|
-
|
|
248
|
-
```typescript
|
|
249
|
-
const txb = new Transaction()
|
|
250
|
-
|
|
251
|
-
await client.fastRouterSwap({
|
|
252
|
-
router: routers,
|
|
253
|
-
txb,
|
|
254
|
-
slippage: 0.01, // 1% slippage tolerance
|
|
255
|
-
// Optional:
|
|
256
|
-
// partner: "your-partner-id",
|
|
257
|
-
// payDeepFeeAmount: 1000000, // For DeepBook V3 pools
|
|
62
|
+
byAmountIn: true, // true means fix input amount, false means fix output amount
|
|
258
63
|
})
|
|
259
|
-
|
|
260
|
-
// Simulate the transaction first
|
|
261
|
-
const dryRunResult = await client.devInspectTransactionBlock(txb, keypair)
|
|
262
|
-
|
|
263
|
-
if (dryRunResult.effects.status.status === "success") {
|
|
264
|
-
console.log("Simulation successful, executing transaction...")
|
|
265
|
-
|
|
266
|
-
// Execute the actual transaction
|
|
267
|
-
const result = await client.signAndExecuteTransaction(txb, keypair)
|
|
268
|
-
console.log("Transaction result:", result)
|
|
269
|
-
} else {
|
|
270
|
-
console.error("Simulation failed:", dryRunResult.effects.status)
|
|
271
|
-
}
|
|
272
64
|
```
|
|
273
65
|
|
|
274
|
-
###
|
|
275
|
-
|
|
276
|
-
Use this when you need to build complex programmable transaction blocks:
|
|
66
|
+
### 3. Confirm and do fast swap
|
|
277
67
|
|
|
278
68
|
```typescript
|
|
279
|
-
import { coinWithBalance } from "@mysten/sui/transactions"
|
|
280
|
-
|
|
281
69
|
const txb = new Transaction()
|
|
282
70
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// type: from,
|
|
290
|
-
// balance: amount.toString()
|
|
291
|
-
// })
|
|
292
|
-
|
|
293
|
-
// Execute swap and get output coin
|
|
294
|
-
const targetCoin = await client.routerSwap({
|
|
295
|
-
router: routers,
|
|
296
|
-
txb,
|
|
297
|
-
inputCoin,
|
|
298
|
-
slippage: 0.01, // 1% slippage tolerance
|
|
299
|
-
// Optional:
|
|
300
|
-
// partner: "your-partner-id",
|
|
301
|
-
// deepbookv3DeepFee: deepCoinObject, // For DeepBook V3
|
|
302
|
-
})
|
|
303
|
-
|
|
304
|
-
// Use the target coin in your custom PTB logic
|
|
305
|
-
// For example, transfer to recipient or use in another DeFi protocol
|
|
306
|
-
txb.transferObjects([targetCoin], keypair.toSuiAddress())
|
|
307
|
-
|
|
308
|
-
// Or destroy if sending to current address
|
|
309
|
-
// client.transferOrDestroyCoin(txb, targetCoin, target)
|
|
71
|
+
if (routerRes != null) {
|
|
72
|
+
await client.fastRouterSwap({
|
|
73
|
+
routers,
|
|
74
|
+
txb,
|
|
75
|
+
slippage: 0.01,
|
|
76
|
+
})
|
|
310
77
|
|
|
311
|
-
|
|
312
|
-
const dryRunResult = await client.devInspectTransactionBlock(txb, keypair)
|
|
78
|
+
const result = await client.devInspectTransactionBlock(txb, keypair)
|
|
313
79
|
|
|
314
|
-
if (
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
console.error("Simulation failed:", dryRunResult.effects.status)
|
|
80
|
+
if (result.effects.status.status === "success") {
|
|
81
|
+
console.log("Sim exec transaction success")
|
|
82
|
+
const result = await client.signAndExecuteTransaction(txb, keypair)
|
|
83
|
+
}
|
|
84
|
+
console.log("result", result)
|
|
320
85
|
}
|
|
321
86
|
```
|
|
322
87
|
|
|
323
|
-
###
|
|
324
|
-
|
|
325
|
-
Protect against excessive input amounts:
|
|
88
|
+
### 4. Build PTB and return target coin
|
|
326
89
|
|
|
327
90
|
```typescript
|
|
328
91
|
const txb = new Transaction()
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
// Transaction will abort if input amount exceeds maxAmountIn
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
### Error Handling
|
|
343
|
-
|
|
344
|
-
```typescript
|
|
345
|
-
try {
|
|
346
|
-
const routers = await client.findRouters({
|
|
347
|
-
from,
|
|
348
|
-
target,
|
|
349
|
-
amount,
|
|
350
|
-
byAmountIn: true,
|
|
92
|
+
const byAmountIn = true
|
|
93
|
+
|
|
94
|
+
if (routerRes != null) {
|
|
95
|
+
const targetCoin = await client.routerSwap({
|
|
96
|
+
routers,
|
|
97
|
+
txb,
|
|
98
|
+
inputCoin,
|
|
99
|
+
slippage: 0.01,
|
|
351
100
|
})
|
|
352
101
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
102
|
+
// you can use this target coin object argument to build your ptb.
|
|
103
|
+
client.transferOrDestoryCoin(txb, targetCoin, targetCoinType)
|
|
356
104
|
|
|
357
|
-
|
|
358
|
-
throw new Error("Insufficient liquidity for this swap")
|
|
359
|
-
}
|
|
105
|
+
const result = await client.devInspectTransactionBlock(txb, keypair)
|
|
360
106
|
|
|
361
|
-
if (
|
|
362
|
-
|
|
107
|
+
if (result.effects.status.status === "success") {
|
|
108
|
+
console.log("Sim exec transaction success")
|
|
109
|
+
const result = await client.signAndExecuteTransaction(txb, keypair)
|
|
363
110
|
}
|
|
364
|
-
|
|
365
|
-
// Proceed with swap...
|
|
366
|
-
} catch (error) {
|
|
367
|
-
console.error("Swap failed:", error)
|
|
111
|
+
console.log("result", result)
|
|
368
112
|
}
|
|
369
113
|
```
|
|
370
114
|
|
|
@@ -374,34 +118,34 @@ try {
|
|
|
374
118
|
|
|
375
119
|
| Contract | Tag of Repo | Latest published at address |
|
|
376
120
|
| ------------------------- | ----------- | ------------------------------------------------------------------ |
|
|
377
|
-
| CetusAggregatorV2 | mainnet |
|
|
378
|
-
| CetusAggregatorV2ExtendV1 | mainnet |
|
|
379
|
-
| CetusAggregatorV2ExtendV2 | mainnet |
|
|
121
|
+
| CetusAggregatorV2 | mainnet | 0x40e457bc65a398d2db7026881358fcb7cfa2f1bb052bca41f46c55a1103f2d6f |
|
|
122
|
+
| CetusAggregatorV2ExtendV1 | mainnet | 0x2edc22bf96c85482b2208624fa9339255d5055113c92fd6c33add48ce971b774 |
|
|
123
|
+
| CetusAggregatorV2ExtendV2 | mainnet | 0x2e227a3cbc6715518b18ed339d2f967153674b7b257da114ca62c72b2011258a |
|
|
380
124
|
|
|
381
125
|
## Example
|
|
382
126
|
|
|
383
127
|
```
|
|
384
|
-
CetusAggregatorV2 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2/mainnet", rev = "mainnet", override = true }
|
|
128
|
+
CetusAggregatorV2 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2/mainnet", rev = "mainnet-v1.56.0", override = true }
|
|
385
129
|
|
|
386
|
-
CetusAggregatorV2ExtendV1 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2-extend-v1", rev = "mainnet", override = true }
|
|
130
|
+
CetusAggregatorV2ExtendV1 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2-extend-v1", rev = "mainnet-v1.56.0", override = true }
|
|
387
131
|
|
|
388
|
-
CetusAggregatorV2ExtendV2 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2-extend-v2", rev = "mainnet", override = true }
|
|
132
|
+
CetusAggregatorV2ExtendV2 = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2-extend-v2", rev = "mainnet-v1.56.0", override = true }
|
|
389
133
|
```
|
|
390
134
|
|
|
391
135
|
# Simple Aggregator Contract Interface
|
|
392
136
|
|
|
393
|
-
- include: cetus, flowxv3, turbos, bluefin, haedalhmm, momentum, obric
|
|
137
|
+
- include: cetus, flowxv3, turbos, bluefin, haedalhmm, momentum, obric, deepbookv3
|
|
394
138
|
|
|
395
139
|
## Tags corresponding to different networks
|
|
396
140
|
|
|
397
141
|
| Contract | Tag of Repo | Latest published at address |
|
|
398
142
|
| --------------------- | ----------- | ------------------------------------------------------------------ |
|
|
399
|
-
| CetusAggregatorSimple | mainnet |
|
|
143
|
+
| CetusAggregatorSimple | mainnet | 0x54394cb5bceb8073c60df0964b904a607c9974ef771d9e8b3f9c45b8ce075499 |
|
|
400
144
|
|
|
401
145
|
## Example
|
|
402
146
|
|
|
403
147
|
```
|
|
404
|
-
CetusAggregatorSimple = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2/simple-mainnet", rev = "mainnet-v1.
|
|
148
|
+
CetusAggregatorSimple = { git = "https://github.com/CetusProtocol/aggregator.git", subdir = "packages/cetus-aggregator-v2/simple-mainnet", rev = "mainnet-v1.58.0", override = true }
|
|
405
149
|
```
|
|
406
150
|
|
|
407
151
|
## Usage
|