Skip to content

Commit 396d1a0

Browse files
author
cetliquidity
committed
Integrate suilend and fix package config bug
1 parent c0575b8 commit 396d1a0

File tree

7 files changed

+121
-68
lines changed

7 files changed

+121
-68
lines changed

README.md

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,19 @@ By using our aggregator, you can trade more efficiently and securely on the Sui
3131

3232
# Install
3333

34-
The SDK is published to npm registry. To use the SDK in your project, you can run `npm install @cetusprotocol/aggregator-sdk`
34+
The SDK is published to npm registry. To use the SDK in your project, you can
35+
36+
```
37+
npm install @cetusprotocol/aggregator-sdk
38+
```
3539

3640
# Usage
3741

3842
## 1. Init client with rpc and package config
3943

40-
1. Create aggregator client default.
41-
42-
```typescript
43-
const client = new AggregatorClient()
44-
```
45-
46-
2. Customer create aggregator client by your self rpc node.
47-
48-
```typescript
49-
// used to do simulate swap and swap
50-
// https://fullnode.mainnet.sui.io:443
51-
const fullNodeURL = process.env.SUI_RPC!
52-
53-
const suiClient = new SuiClient({
54-
url: fullNodeURL,
55-
})
56-
57-
// provider by cetus
58-
const aggregatorURL = "https://api-sui.cetus.zone/router_v2/find_routes"
59-
60-
// set your wallet address, used to do simulate
61-
const wallet = "0x..."
62-
63-
// import { Env } from "@cetusprotocol/aggregator-sdk"
64-
// Currently, we provide full support for Mainnet,
65-
// while Testnet is only supported for Cetus and DeepBook providers.
66-
const client = new AggregatorClient(
67-
aggregatorURL,
68-
wallet,
69-
suiClient,
70-
Env.Mainnet
71-
)
72-
```
44+
```typescript
45+
const client = new AggregatorClient()
46+
```
7347

7448
## 2. Get best router swap result from aggregator service
7549

@@ -92,35 +66,56 @@ const routerRes = await client.findRouters({
9266
```typescript
9367
const routerTx = new Transaction()
9468

95-
await client.fastRouterSwap({
96-
routers: routerRes.routes,
97-
byAmountIn,
98-
txb: routerTx,
99-
slippage: 0.01,
100-
isMergeTragetCoin: true,
101-
refreshAllCoins: true,
102-
})
69+
if (routerRes != null) {
70+
await client.fastRouterSwap({
71+
routers: routerRes.routes,
72+
byAmountIn,
73+
txb: routerTx,
74+
slippage: 0.01,
75+
isMergeTragetCoin: true,
76+
refreshAllCoins: true,
77+
})
78+
79+
let result = await client.devInspectTransactionBlock(routerTx, keypair)
80+
81+
if (result.effects.status.status === "success") {
82+
console.log("Sim exec transaction success")
83+
const result = await client.signAndExecuteTransaction(routerTx, keypair)
84+
}
85+
console.log("result", result)
86+
}
10387
```
10488

10589
## 4. Build PTB and return target coin
10690

10791
```typescript
10892
const routerTx = new Transaction()
109-
110-
const targetCoin = await client.routerSwap({
111-
routers: routerRes.routes,
112-
byAmountIn: true,
113-
txb: routerTx,
114-
inputCoin,
115-
slippage: 0.01,
116-
})
117-
118-
// you can use this target coin object argument to build your ptb.
119-
const client.transferOrDestoryCoin(
120-
txb,
121-
targetCoinRes.targetCoin,
122-
targetCoinType
123-
)
93+
const byAmountIn = true;
94+
95+
if (routerRes != null) {
96+
const targetCoin = await client.routerSwap({
97+
routers: routerRes.routes,
98+
byAmountIn,
99+
txb: routerTx,
100+
inputCoin,
101+
slippage: 0.01,
102+
})
103+
104+
// you can use this target coin object argument to build your ptb.
105+
const client.transferOrDestoryCoin(
106+
txb,
107+
targetCoin,
108+
targetCoinType
109+
)
110+
111+
let result = await client.devInspectTransactionBlock(routerTx, keypair)
112+
113+
if (result.effects.status.status === "success") {
114+
console.log("Sim exec transaction success")
115+
const result = await client.signAndExecuteTransaction(routerTx, keypair)
116+
}
117+
console.log("result", result)
118+
}
124119
```
125120

126121
# More About Cetus

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cetusprotocol/aggregator-sdk",
3-
"version": "0.3.6",
3+
"version": "0.3.9",
44
"sideEffects": false,
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -38,4 +38,4 @@
3838
"ts-jest": "^29.1.3",
3939
"typescript": "^5.4.5"
4040
}
41-
}
41+
}

src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async function getRouter(endpoint: string, params: FindRouterParams) {
168168
}
169169

170170
// set newest sdk version
171-
url += "&v=1000307"
171+
url += "&v=1000309"
172172

173173
const response = await fetch(url)
174174
return response

src/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import { CoinAsset } from "./types/sui"
3737
import { buildInputCoin } from "./utils/coin"
3838
import { DeepbookV3 } from "./transaction/deepbook_v3"
3939
import { Scallop } from "./transaction/scallop"
40+
import { Suilend } from "./transaction/suilend"
41+
4042
export const CETUS = "CETUS"
4143
export const DEEPBOOKV2 = "DEEPBOOK"
4244
export const KRIYA = "KRIYA"
@@ -51,7 +53,7 @@ export const AFSUI = "AFSUI"
5153
export const BLUEMOVE = "BLUEMOVE"
5254
export const DEEPBOOKV3 = "DEEPBOOKV3"
5355
export const SCALLOP = "SCALLOP"
54-
56+
export const SUILEND = "SUILEND"
5557
export const DEFAULT_ENDPOINT = "https://api-sui.cetus.zone/router_v2"
5658

5759
export type BuildRouterSwapParams = {
@@ -410,7 +412,8 @@ export class AggregatorClient {
410412
publishedAtV2(): string {
411413
if (this.env === Env.Mainnet) {
412414
// return "0x43811be4677f5a5de7bf2dac740c10abddfaa524aee6b18e910eeadda8a2f6ae" // version 1, deepbookv3
413-
return "0x6d70ffa7aa3f924c3f0b573d27d29895a0ee666aaff821073f75cb14af7fd01a" // version 3, deepbookv3 & scallop
415+
// return "0x6d70ffa7aa3f924c3f0b573d27d29895a0ee666aaff821073f75cb14af7fd01a" // version 3, deepbookv3 & scallop
416+
return "0x16d9418726c26d8cb4ce8c9dd75917fa9b1c7bf47d38d7a1a22603135f0f2a56" // version 4 add suilend
414417
} else {
415418
return "0xfd8a73ef0a4b928da9c27fc287dc37c1ca64df71da8e8eac7ca9ece55eb5f448"
416419
}
@@ -491,6 +494,8 @@ export class AggregatorClient {
491494
return new Bluemove(this.env)
492495
case SCALLOP:
493496
return new Scallop(this.env)
497+
case SUILEND:
498+
return new Suilend(this.env)
494499
default:
495500
throw new Error(`Unsupported dex ${provider}`)
496501
}

src/transaction/suilend.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
Transaction,
3+
TransactionObjectArgument,
4+
} from "@mysten/sui/transactions"
5+
import { AggregatorClient, Dex, Env, Path } from ".."
6+
7+
export class Suilend implements Dex {
8+
private liquid_staking_pool: string
9+
private sui_system_state: string
10+
11+
constructor(env: Env) {
12+
if (env !== Env.Mainnet) {
13+
throw new Error("Suilend only supported on mainnet")
14+
}
15+
16+
this.liquid_staking_pool =
17+
env === Env.Mainnet
18+
? "0x15eda7330c8f99c30e430b4d82fd7ab2af3ead4ae17046fcb224aa9bad394f6b"
19+
: "0x0"
20+
21+
this.sui_system_state =
22+
env === Env.Mainnet
23+
? "0x0000000000000000000000000000000000000000000000000000000000000005"
24+
: "0x0"
25+
}
26+
27+
async swap(
28+
client: AggregatorClient,
29+
txb: Transaction,
30+
path: Path,
31+
inputCoin: TransactionObjectArgument
32+
): Promise<TransactionObjectArgument> {
33+
const { direction, from, target } = path
34+
35+
const [func, springCoinType] = direction
36+
? ["swap_a2b", target]
37+
: ["swap_b2a", from]
38+
39+
const args = [
40+
txb.object(this.liquid_staking_pool),
41+
txb.object(this.sui_system_state),
42+
inputCoin,
43+
]
44+
45+
const res = txb.moveCall({
46+
target: `${client.publishedAtV2()}::suilend::${func}`,
47+
typeArguments: [springCoinType],
48+
arguments: args,
49+
}) as TransactionObjectArgument
50+
51+
return res
52+
}
53+
}

tests/router.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ describe("router module", () => {
113113
test("Build router tx", async () => {
114114
const byAmountIn = true
115115
const amount = "320000"
116-
const target = "0x2::sui::SUI"
117-
const from = "0x26b3bc67befc214058ca78ea9a2690298d731a2d4309485ec3d40198063c4abc::eth::ETH"
116+
const from = "0x2::sui::SUI"
117+
const target = "0x83556891f4a0f233ce7b05cfe7f957d4020492a34f5405b2cb9377d060bef4bf::spring_sui::SPRING_SUI"
118118

119119
const res = await client.findRouters({
120120
from,
@@ -123,8 +123,8 @@ describe("router module", () => {
123123
byAmountIn,
124124
depth: 3,
125125
providers: [
126-
"SCALLOP",
127-
"CETUS",
126+
"SUILEND",
127+
// "CETUS",
128128
// "DEEPBOOKV3",
129129
// "DEEPBOOK",
130130
// "AFTERMATH",

0 commit comments

Comments
 (0)