Skip to content

Commit 20c5e74

Browse files
committed
update methods
1 parent b2e53df commit 20c5e74

File tree

4 files changed

+359
-137
lines changed

4 files changed

+359
-137
lines changed

packages/sui-agent/src/protocols/aftermath/PoolTool.ts

Lines changed: 193 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Aftermath } from 'aftermath-ts-sdk';
22
import { handleError } from '../../utils';
3-
import { RankingMetric, PoolInfo, SDKPool } from './types';
3+
import { PoolInfo, SDKPool } from './types';
44

55
class PoolTool {
66
private static sdk: Aftermath | null = null;
@@ -75,172 +75,265 @@ class PoolTool {
7575
}
7676

7777
/**
78-
* Gets ranked pools by metric
79-
* @param metric Metric to rank by (apr, tvl, fees, volume)
80-
* @param numPools Number of top pools to return
81-
* @returns Ranked pool information
78+
* Gets pool events
79+
* @param poolId Pool ID to get events for
80+
* @param eventType Type of events to get (deposit or withdraw)
81+
* @param limit Maximum number of events to return
82+
* @returns Pool events
8283
*/
83-
public static async getRankedPools(
84-
metric: RankingMetric,
85-
numPools = 5,
84+
public static async getPoolEvents(
85+
poolId: string,
86+
eventType: string,
87+
limit = 10,
8688
): Promise<string> {
8789
try {
8890
const sdk = PoolTool.initSDK();
8991
await sdk.init();
90-
const pools = await sdk.Pools().getPools({ objectIds: [] });
92+
const pool = await sdk.Pools().getPool({ objectId: poolId });
9193

92-
// Process all pools
93-
const processedPools = await Promise.all(
94-
pools.map(async (poolInstance) => {
95-
if (!poolInstance.pool?.objectId) return null;
96-
return this.processPool(poolInstance, poolInstance.pool.objectId);
97-
}),
98-
);
94+
// Use the pools instance to get events
95+
const events = await pool.getInteractionEvents({
96+
walletAddress: pool.pool.creator, // Use the pool creator's address
97+
limit,
98+
cursor: undefined, // Add cursor for pagination
99+
});
99100

100-
const validPools = processedPools.filter(
101-
(pool): pool is PoolInfo => pool !== null && pool.tokens.length > 0,
101+
// Filter events by type
102+
const filteredEvents = events.events?.filter((event) =>
103+
eventType === 'deposit' ? 'deposits' in event : 'withdrawn' in event,
102104
);
103105

104-
// Sort pools based on the specified metric
105-
const sortedPools = validPools.sort((a, b) => {
106-
let valueA: number, valueB: number;
107-
108-
switch (metric) {
109-
case 'apr':
110-
valueA = a.apr;
111-
valueB = b.apr;
112-
break;
113-
case 'tvl':
114-
valueA = a.tvl;
115-
valueB = b.tvl;
116-
break;
117-
case 'fees':
118-
valueA = a.fee;
119-
valueB = b.fee;
120-
break;
121-
case 'volume':
122-
valueA = a.tvl * a.fee; // Using TVL * fee as a proxy for volume
123-
valueB = b.tvl * b.fee;
124-
break;
125-
default:
126-
valueA = a.tvl;
127-
valueB = b.tvl;
128-
}
129-
130-
return valueB - valueA; // Default to descending order
131-
});
106+
return JSON.stringify([
107+
{
108+
reasoning: 'Successfully retrieved pool events',
109+
response: {
110+
events: filteredEvents,
111+
pagination: {
112+
limit,
113+
hasMore: events.events?.length === limit,
114+
},
115+
},
116+
status: 'success',
117+
query: `Get ${eventType} events for pool ${poolId}`,
118+
errors: [],
119+
},
120+
]);
121+
} catch (error) {
122+
return JSON.stringify([
123+
handleError(error, {
124+
reasoning: 'Failed to retrieve pool events',
125+
query: `Attempted to get ${eventType} events for pool ${poolId}`,
126+
}),
127+
]);
128+
}
129+
}
130+
131+
/**
132+
* Gets pool information
133+
* @param poolId Pool ID to get info for
134+
* @returns Pool information
135+
*/
136+
public static async getPoolInfo(poolId: string): Promise<string> {
137+
try {
138+
const sdk = PoolTool.initSDK();
139+
await sdk.init();
140+
const pool = await sdk.Pools().getPool({ objectId: poolId });
132141

133-
// Take only the requested number of pools
134-
const topPools = sortedPools.slice(0, numPools);
135-
136-
// Format the response with ranking information
137-
const rankedPools = topPools.map((pool, index) => ({
138-
rank: index + 1,
139-
id: pool.id,
140-
metrics: {
141-
apr: `${pool.apr.toFixed(2)}%`,
142-
tvl: `$${pool.tvl.toLocaleString()}`,
143-
fee: `${(pool.fee * 100).toFixed(2)}%`,
144-
volume: `$${(pool.tvl * pool.fee).toLocaleString()}`, // Estimated volume
142+
return JSON.stringify([
143+
{
144+
reasoning: 'Successfully retrieved pool information',
145+
response: {
146+
poolId,
147+
pool: {
148+
id: pool.pool.objectId,
149+
metrics: pool.stats || {},
150+
coins: pool.pool.coins,
151+
},
152+
timestamp: new Date().toISOString(),
153+
},
154+
status: 'success',
155+
query: `Get info for pool ${poolId}`,
156+
errors: [],
145157
},
146-
}));
158+
]);
159+
} catch (error) {
160+
return JSON.stringify([
161+
handleError(error, {
162+
reasoning: 'Failed to retrieve pool information',
163+
query: `Attempted to get info for pool ${poolId}`,
164+
}),
165+
]);
166+
}
167+
}
168+
169+
/**
170+
* Gets pool statistics
171+
* @param poolId Pool ID to get stats for
172+
* @returns Pool statistics including TVL, volume, APR etc.
173+
*/
174+
public static async getPoolStats(poolId: string): Promise<string> {
175+
try {
176+
const sdk = PoolTool.initSDK();
177+
await sdk.init();
178+
179+
const stats = await sdk.Pools().getPoolsStats({
180+
poolIds: [poolId],
181+
});
147182

148183
return JSON.stringify([
149184
{
150-
reasoning: `Successfully retrieved top ${numPools} pools ranked by ${metric}`,
185+
reasoning: 'Successfully retrieved pool statistics',
151186
response: {
152-
metric,
153-
numPools,
154-
pools: rankedPools,
187+
poolId,
188+
stats: {
189+
volume: stats[0].volume,
190+
tvl: stats[0].tvl,
191+
supplyPerLps: stats[0].supplyPerLps,
192+
lpPrice: stats[0].lpPrice,
193+
fees: stats[0].fees,
194+
apr: stats[0].apr,
195+
},
155196
timestamp: new Date().toISOString(),
156197
},
157198
status: 'success',
158-
query: `Get top ${numPools} pools by ${metric}`,
199+
query: `Get statistics for pool ${poolId}`,
159200
errors: [],
160201
},
161202
]);
162203
} catch (error) {
163204
return JSON.stringify([
164205
handleError(error, {
165-
reasoning: 'Failed to retrieve ranked pools',
166-
query: `Attempted to get top ${numPools} pools by ${metric}`,
206+
reasoning: 'Failed to retrieve pool statistics',
207+
query: `Attempted to get statistics for pool ${poolId}`,
167208
}),
168209
]);
169210
}
170211
}
171212

172213
/**
173-
* Gets pool events
174-
* @param poolId Pool ID to get events for
175-
* @param eventType Type of events to get (deposit or withdraw)
176-
* @param limit Maximum number of events to return
177-
* @returns Pool events
214+
* Gets deposit transaction for a pool
178215
*/
179-
public static async getPoolEvents(
216+
public static async getDepositTransaction(
180217
poolId: string,
181-
eventType: string,
182-
limit: number,
218+
walletAddress: string,
219+
amountsIn: Record<string, bigint>,
220+
slippage: number,
221+
referrer?: string,
183222
): Promise<string> {
184223
try {
185224
const sdk = PoolTool.initSDK();
186225
await sdk.init();
187226
const pool = await sdk.Pools().getPool({ objectId: poolId });
188-
const events =
189-
eventType === 'deposit'
190-
? await pool.getDepositEvents({ limit })
191-
: await pool.getWithdrawEvents({ limit });
227+
228+
const tx = await pool.getDepositTransaction({
229+
walletAddress,
230+
amountsIn,
231+
slippage,
232+
referrer,
233+
});
234+
192235
return JSON.stringify([
193236
{
194-
reasoning: 'Successfully retrieved pool events',
195-
response: events,
237+
reasoning: 'Successfully created deposit transaction',
238+
response: { tx, poolId },
196239
status: 'success',
197-
query: `Get ${eventType} events for pool ${poolId}`,
240+
query: 'Create deposit transaction',
198241
errors: [],
199242
},
200243
]);
201244
} catch (error) {
202245
return JSON.stringify([
203246
handleError(error, {
204-
reasoning: 'Failed to retrieve pool events',
205-
query: `Attempted to get ${eventType} events for pool ${poolId}`,
247+
reasoning: 'Failed to create deposit transaction',
248+
query: 'Create deposit transaction',
206249
}),
207250
]);
208251
}
209252
}
210253

211254
/**
212-
* Gets pool information
213-
* @param poolId Pool ID to get info for
214-
* @returns Pool information
255+
* Gets withdraw transaction for a pool
215256
*/
216-
public static async getPoolInfo(poolId: string): Promise<string> {
257+
public static async getWithdrawTransaction(
258+
poolId: string,
259+
walletAddress: string,
260+
amountsOutDirection: Record<string, bigint>,
261+
lpCoinAmount: bigint,
262+
slippage: number,
263+
referrer?: string,
264+
): Promise<string> {
217265
try {
218266
const sdk = PoolTool.initSDK();
219267
await sdk.init();
220268
const pool = await sdk.Pools().getPool({ objectId: poolId });
221269

270+
const tx = await pool.getWithdrawTransaction({
271+
walletAddress,
272+
amountsOutDirection,
273+
lpCoinAmount,
274+
slippage,
275+
referrer,
276+
});
277+
222278
return JSON.stringify([
223279
{
224-
reasoning: 'Successfully retrieved pool information',
225-
response: {
226-
poolId,
227-
pool: {
228-
id: pool.pool.objectId,
229-
metrics: pool.stats || {},
230-
coins: pool.pool.coins,
231-
},
232-
timestamp: new Date().toISOString(),
233-
},
280+
reasoning: 'Successfully created withdraw transaction',
281+
response: { tx, poolId },
234282
status: 'success',
235-
query: `Get info for pool ${poolId}`,
283+
query: 'Create withdraw transaction',
236284
errors: [],
237285
},
238286
]);
239287
} catch (error) {
240288
return JSON.stringify([
241289
handleError(error, {
242-
reasoning: 'Failed to retrieve pool information',
243-
query: `Attempted to get info for pool ${poolId}`,
290+
reasoning: 'Failed to create withdraw transaction',
291+
query: 'Create withdraw transaction',
292+
}),
293+
]);
294+
}
295+
}
296+
297+
/**
298+
* Gets trade transaction for a pool
299+
*/
300+
public static async getTradeTransaction(
301+
poolId: string,
302+
walletAddress: string,
303+
coinInType: string,
304+
coinInAmount: bigint,
305+
coinOutType: string,
306+
slippage: number,
307+
referrer?: string,
308+
): Promise<string> {
309+
try {
310+
const sdk = PoolTool.initSDK();
311+
await sdk.init();
312+
const pool = await sdk.Pools().getPool({ objectId: poolId });
313+
314+
const tx = await pool.getTradeTransaction({
315+
walletAddress,
316+
coinInType,
317+
coinInAmount,
318+
coinOutType,
319+
slippage,
320+
referrer,
321+
});
322+
323+
return JSON.stringify([
324+
{
325+
reasoning: 'Successfully created trade transaction',
326+
response: { tx, poolId },
327+
status: 'success',
328+
query: 'Create trade transaction',
329+
errors: [],
330+
},
331+
]);
332+
} catch (error) {
333+
return JSON.stringify([
334+
handleError(error, {
335+
reasoning: 'Failed to create trade transaction',
336+
query: 'Create trade transaction',
244337
}),
245338
]);
246339
}

0 commit comments

Comments
 (0)