-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathliquidStaking.ts
75 lines (67 loc) · 2.27 KB
/
liquidStaking.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { tool } from "ai";
import { z } from "zod";
import { instantiateAccount, suiClient } from "./sui-utils";
import { LstClient } from "@suilend/springsui-sdk";
import { Transaction } from "@mysten/sui/transactions";
export const liquidStakingTool = tool({
description: "Mint or redeem sSUI tokens using Spring protocol",
parameters: z.object({
action: z
.enum(["mint", "redeem"])
.describe("Whether to mint or redeem sSUI"),
amount: z.number().describe("Amount in MIST units"),
}),
execute: async (args: {
action: "mint" | "redeem";
amount: number;
}) => {
const LIQUID_STAKING_INFO = {
id: "0x15eda7330c8f99c30e430b4d82fd7ab2af3ead4ae17046fcb224aa9bad394f6b",
type: "0x83556891f4a0f233ce7b05cfe7f957d4020492a34f5405b2cb9377d060bef4bf::spring_sui::SPRING_SUI",
weightHookId:
"0xbbafcb2d7399c0846f8185da3f273ad5b26b3b35993050affa44cfa890f1f144",
};
const keypair = await instantiateAccount(process.env.SUI_PRIVATE_KEY);
const lstClient = await LstClient.initialize(
suiClient,
LIQUID_STAKING_INFO
);
const tx = new Transaction();
if (args.action === "mint") {
const mistAmount = Math.floor(args.amount);
const [sui] = tx.splitCoins(tx.gas, [mistAmount]);
const sSui = lstClient.mint(tx, sui);
tx.transferObjects([sSui], keypair.toSuiAddress());
} else {
const lstCoins = await suiClient.getCoins({
owner: keypair.toSuiAddress(),
coinType: LIQUID_STAKING_INFO.type,
limit: 1000,
});
if (lstCoins.data.length > 1) {
tx.mergeCoins(
lstCoins.data[0].coinObjectId,
lstCoins.data.slice(1).map((c) => c.coinObjectId)
);
}
const mistAmount = Math.floor(args.amount);
const [lst] = tx.splitCoins(lstCoins.data[0].coinObjectId, [mistAmount]);
const sui = lstClient.redeem(tx, lst);
tx.transferObjects([sui], keypair.toSuiAddress());
}
const result = await suiClient.signAndExecuteTransaction({
transaction: tx,
signer: keypair,
options: {
showEvents: true,
showEffects: true,
showObjectChanges: true,
},
});
return {
success: true,
transactionId: result.digest,
action: args.action,
};
},
});