Skip to content

Commit 4f2d4d7

Browse files
committed
feat: add suilend tools and Navi
1 parent 1d8fe84 commit 4f2d4d7

File tree

6 files changed

+944
-92
lines changed

6 files changed

+944
-92
lines changed

packages/sui-agent/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"dotenv": "^16.4.7",
3030
"mongoose": "^8.10.0",
3131
"navi-sdk": "^1.4.24",
32-
"typescript": "^5.7.3"
32+
"typescript": "^5.7.3",
33+
"@suilend/sdk": "^1.1.36"
3334
},
3435
"devDependencies": {
3536
"@types/bn.js": "^5.1.6",

packages/sui-agent/src/protocols/navi/index.ts

Lines changed: 217 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NAVISDKClient, Sui } from 'navi-sdk';
22
import { handleError } from '../../utils';
33
import sentioApi from '../../config/sentio';
4-
import { Liquidation } from './types';
4+
import { Liquidation, NaviPool, NaviPoolsResponse } from './types';
55

66
// Initialize NAVI SDK client
77
let naviClient: NAVISDKClient | null = null;
@@ -138,12 +138,10 @@ async function checkUserLiquidations(address: string) {
138138
),
139139
totalLiquidations: liquidations.length,
140140
};
141-
} catch (error) {
142-
handleError(error, {
143-
reasoning: 'Failed to fetch liquidation status for user ',
144-
query: `Attempted to fetch liquidation status for user`,
145-
});
146-
console.error('Error checking user liquidations:', error);
141+
} catch (error: unknown) {
142+
const errorMessage =
143+
error instanceof Error ? error.message : 'Unknown error occurred';
144+
console.error('Error checking user liquidations:', errorMessage);
147145
return {
148146
asUser: [],
149147
asLiquidator: [],
@@ -156,16 +154,33 @@ export async function checkUserLiquidationStatusTool(
156154
...args: (string | number | bigint | boolean)[]
157155
): Promise<string> {
158156
const [walletAddress] = args as [string];
159-
const result = await checkUserLiquidations(walletAddress);
160-
return JSON.stringify([
161-
{
162-
reasoning: 'Successfully retrieved liquidation status',
163-
response: JSON.stringify(result),
164-
status: 'success',
165-
query: `Check liquidation status for ${walletAddress}`,
166-
errors: [],
167-
},
168-
]);
157+
try {
158+
const result = await checkUserLiquidations(walletAddress);
159+
return JSON.stringify([
160+
{
161+
reasoning: 'Successfully retrieved liquidation status',
162+
response: {
163+
liquidations: result,
164+
address: walletAddress,
165+
},
166+
status: 'success',
167+
query: `Check liquidation status for ${walletAddress}`,
168+
errors: [],
169+
},
170+
]);
171+
} catch (error: unknown) {
172+
const errorMessage =
173+
error instanceof Error ? error.message : 'Unknown error occurred';
174+
return JSON.stringify([
175+
{
176+
reasoning: 'Failed to retrieve liquidation status',
177+
response: null,
178+
status: 'failure',
179+
query: `Check liquidation status for ${walletAddress}`,
180+
errors: [errorMessage],
181+
},
182+
]);
183+
}
169184
}
170185

171186
/**
@@ -174,21 +189,45 @@ export async function checkUserLiquidationStatusTool(
174189
* @returns JSON string with NAVI portfolio
175190
*/
176191

177-
export async function getNaviPortfolio(address: string) {
178-
const account = naviClient?.accounts[0];
179-
if (!account) {
180-
throw new Error('NAVI SDK client not initialized');
192+
export async function getNaviPortfolio(
193+
...args: (string | number | bigint | boolean)[]
194+
): Promise<string> {
195+
const [address] = args as [string];
196+
await initializeNaviClient();
197+
try {
198+
if (!naviClient) {
199+
throw new Error('NAVI SDK client not initialized');
200+
}
201+
const account = naviClient.accounts[0];
202+
if (!account) {
203+
throw new Error('No account found');
204+
}
205+
const result = await account.getNAVIPortfolio(address, true);
206+
return JSON.stringify([
207+
{
208+
reasoning: 'Successfully retrieved NAVI portfolio',
209+
response: {
210+
portfolio: result,
211+
address: address,
212+
},
213+
status: 'success',
214+
query: `Get NAVI portfolio for ${address}`,
215+
errors: [],
216+
},
217+
]);
218+
} catch (error: unknown) {
219+
const errorMessage =
220+
error instanceof Error ? error.message : 'Unknown error occurred';
221+
return JSON.stringify([
222+
{
223+
reasoning: 'Failed to retrieve NAVI portfolio',
224+
response: null,
225+
status: 'failure',
226+
query: `Get NAVI portfolio for ${address}`,
227+
errors: [errorMessage],
228+
},
229+
]);
181230
}
182-
const result = account.getNAVIPortfolio(address, true);
183-
return JSON.stringify([
184-
{
185-
reasoning: 'Successfully retrieved NAVI portfolio',
186-
response: JSON.stringify(result),
187-
status: 'success',
188-
query: `Get NAVI portfolio for ${address}`,
189-
errors: [],
190-
},
191-
]);
192231
}
193232

194233
/**
@@ -197,21 +236,41 @@ export async function getNaviPortfolio(address: string) {
197236
* @returns JSON string with available rewards
198237
*/
199238

200-
export async function getNaviAvailableRewards(address: string) {
201-
const account = naviClient?.accounts[0];
202-
if (!account) {
203-
throw new Error('NAVI SDK client not initialized');
239+
export async function getNaviAvailableRewards(
240+
...args: (string | number | bigint | boolean)[]
241+
): Promise<string> {
242+
const [address] = args as [string];
243+
await initializeNaviClient();
244+
try {
245+
if (!naviClient) {
246+
throw new Error('NAVI SDK client not initialized');
247+
}
248+
const result = await naviClient.getAddressAvailableRewards(address, 1);
249+
return JSON.stringify([
250+
{
251+
reasoning: 'Successfully retrieved NAVI available rewards',
252+
response: {
253+
rewards: result,
254+
address: address,
255+
},
256+
status: 'success',
257+
query: `Get NAVI available rewards for ${address}`,
258+
errors: [],
259+
},
260+
]);
261+
} catch (error: unknown) {
262+
const errorMessage =
263+
error instanceof Error ? error.message : 'Unknown error occurred';
264+
return JSON.stringify([
265+
{
266+
reasoning: 'Failed to retrieve NAVI rewards',
267+
response: null,
268+
status: 'failure',
269+
query: `Get NAVI available rewards for ${address}`,
270+
errors: [errorMessage],
271+
},
272+
]);
204273
}
205-
const result = naviClient?.getAddressAvailableRewards(address, 1);
206-
return JSON.stringify([
207-
{
208-
reasoning: 'Successfully retrieved NAVI available rewards',
209-
response: JSON.stringify(result),
210-
status: 'success',
211-
query: `Get NAVI available rewards for ${address}`,
212-
errors: [],
213-
},
214-
]);
215274
}
216275

217276
/**
@@ -220,17 +279,41 @@ export async function getNaviAvailableRewards(address: string) {
220279
* @returns JSON string with NAVI pools
221280
*/
222281

223-
export async function getNaviPools(coin: string) {
224-
const result = naviClient?.getPoolInfo();
225-
return JSON.stringify([
226-
{
227-
reasoning: 'Successfully retrieved NAVI pools',
228-
response: JSON.stringify(result),
229-
status: 'success',
230-
query: `Get NAVI pools for ${coin}`,
231-
errors: [],
232-
},
233-
]);
282+
export async function getNaviPools(
283+
...args: (string | number | bigint | boolean)[]
284+
): Promise<string> {
285+
const [coin] = args as [string];
286+
await initializeNaviClient();
287+
try {
288+
if (!naviClient) {
289+
throw new Error('NAVI SDK client not initialized');
290+
}
291+
const result = (await naviClient.getPoolInfo()) as NaviPool[];
292+
return JSON.stringify([
293+
{
294+
reasoning: 'Successfully retrieved NAVI pools',
295+
response: {
296+
pools: result,
297+
coin: coin,
298+
} as NaviPoolsResponse & { coin: string },
299+
status: 'success',
300+
query: `Get NAVI pools for ${coin}`,
301+
errors: [],
302+
},
303+
]);
304+
} catch (error: unknown) {
305+
const errorMessage =
306+
error instanceof Error ? error.message : 'Unknown error occurred';
307+
return JSON.stringify([
308+
{
309+
reasoning: 'Failed to retrieve NAVI pools',
310+
response: null,
311+
status: 'failure',
312+
query: `Get NAVI pools for ${coin}`,
313+
errors: [errorMessage],
314+
},
315+
]);
316+
}
234317
}
235318

236319
/**
@@ -240,21 +323,46 @@ export async function getNaviPools(coin: string) {
240323
* @returns JSON string with deposit result
241324
*/
242325

243-
export async function depositNavi(coin: string, amount: number) {
244-
const account = naviClient?.accounts[0];
245-
if (!account) {
246-
throw new Error('NAVI SDK client not initialized');
326+
export async function depositNavi(
327+
...args: (string | number | bigint | boolean)[]
328+
): Promise<string> {
329+
const [coin, amount] = args as [string, number];
330+
await initializeNaviClient();
331+
try {
332+
if (!naviClient) {
333+
throw new Error('NAVI SDK client not initialized');
334+
}
335+
const account = naviClient.accounts[0];
336+
if (!account) {
337+
throw new Error('No account found');
338+
}
339+
const result = await account.depositToNavi(Sui, amount);
340+
return JSON.stringify([
341+
{
342+
reasoning: 'Successfully deposited NAVI',
343+
response: {
344+
transaction: result,
345+
coin: coin,
346+
amount: amount,
347+
},
348+
status: 'success',
349+
query: `Deposit NAVI ${coin} ${amount}`,
350+
errors: [],
351+
},
352+
]);
353+
} catch (error: unknown) {
354+
const errorMessage =
355+
error instanceof Error ? error.message : 'Unknown error occurred';
356+
return JSON.stringify([
357+
{
358+
reasoning: 'Failed to deposit NAVI',
359+
response: null,
360+
status: 'failure',
361+
query: `Deposit NAVI ${coin} ${amount}`,
362+
errors: [errorMessage],
363+
},
364+
]);
247365
}
248-
const result = account.depositToNavi(Sui, amount);
249-
return JSON.stringify([
250-
{
251-
reasoning: 'Successfully deposited NAVI',
252-
response: JSON.stringify(result),
253-
status: 'success',
254-
query: `Deposit NAVI ${coin} ${amount}`,
255-
errors: [],
256-
},
257-
]);
258366
}
259367

260368
/**
@@ -264,19 +372,44 @@ export async function depositNavi(coin: string, amount: number) {
264372
* @returns JSON string with withdrawal result
265373
*/
266374

267-
export async function withrawFromNavi(coin: string, amount: number) {
268-
const account = naviClient?.accounts[0];
269-
if (!account) {
270-
throw new Error('NAVI SDK client not initialized');
375+
export async function withrawFromNavi(
376+
...args: (string | number | bigint | boolean)[]
377+
): Promise<string> {
378+
const [coin, amount] = args as [string, number];
379+
await initializeNaviClient();
380+
try {
381+
if (!naviClient) {
382+
throw new Error('NAVI SDK client not initialized');
383+
}
384+
const account = naviClient.accounts[0];
385+
if (!account) {
386+
throw new Error('No account found');
387+
}
388+
const result = await account.withdraw(Sui, amount);
389+
return JSON.stringify([
390+
{
391+
reasoning: 'Successfully withdrew from NAVI',
392+
response: {
393+
transaction: result,
394+
coin: coin,
395+
amount: amount,
396+
},
397+
status: 'success',
398+
query: `Withdraw NAVI ${coin} ${amount}`,
399+
errors: [],
400+
},
401+
]);
402+
} catch (error: unknown) {
403+
const errorMessage =
404+
error instanceof Error ? error.message : 'Unknown error occurred';
405+
return JSON.stringify([
406+
{
407+
reasoning: 'Failed to withdraw from NAVI',
408+
response: null,
409+
status: 'failure',
410+
query: `Withdraw NAVI ${coin} ${amount}`,
411+
errors: [errorMessage],
412+
},
413+
]);
271414
}
272-
const result = account.withdraw(Sui, amount);
273-
return JSON.stringify([
274-
{
275-
reasoning: 'Successfully withdrew from NAVI',
276-
response: JSON.stringify(result),
277-
status: 'success',
278-
query: `Withdraw NAVI ${coin} ${amount}`,
279-
errors: [],
280-
},
281-
]);
282415
}

0 commit comments

Comments
 (0)