|
1 | 1 | import { Aftermath } from 'aftermath-ts-sdk'; |
2 | 2 | import { handleError } from '../../utils'; |
3 | | -import { RankingMetric, PoolInfo, SDKPool } from './types'; |
| 3 | +import { PoolInfo, SDKPool } from './types'; |
4 | 4 |
|
5 | 5 | class PoolTool { |
6 | 6 | private static sdk: Aftermath | null = null; |
@@ -75,172 +75,265 @@ class PoolTool { |
75 | 75 | } |
76 | 76 |
|
77 | 77 | /** |
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 |
82 | 83 | */ |
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, |
86 | 88 | ): Promise<string> { |
87 | 89 | try { |
88 | 90 | const sdk = PoolTool.initSDK(); |
89 | 91 | await sdk.init(); |
90 | | - const pools = await sdk.Pools().getPools({ objectIds: [] }); |
| 92 | + const pool = await sdk.Pools().getPool({ objectId: poolId }); |
91 | 93 |
|
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 | + }); |
99 | 100 |
|
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, |
102 | 104 | ); |
103 | 105 |
|
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 }); |
132 | 141 |
|
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: [], |
145 | 157 | }, |
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 | + }); |
147 | 182 |
|
148 | 183 | return JSON.stringify([ |
149 | 184 | { |
150 | | - reasoning: `Successfully retrieved top ${numPools} pools ranked by ${metric}`, |
| 185 | + reasoning: 'Successfully retrieved pool statistics', |
151 | 186 | 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 | + }, |
155 | 196 | timestamp: new Date().toISOString(), |
156 | 197 | }, |
157 | 198 | status: 'success', |
158 | | - query: `Get top ${numPools} pools by ${metric}`, |
| 199 | + query: `Get statistics for pool ${poolId}`, |
159 | 200 | errors: [], |
160 | 201 | }, |
161 | 202 | ]); |
162 | 203 | } catch (error) { |
163 | 204 | return JSON.stringify([ |
164 | 205 | 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}`, |
167 | 208 | }), |
168 | 209 | ]); |
169 | 210 | } |
170 | 211 | } |
171 | 212 |
|
172 | 213 | /** |
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 |
178 | 215 | */ |
179 | | - public static async getPoolEvents( |
| 216 | + public static async getDepositTransaction( |
180 | 217 | poolId: string, |
181 | | - eventType: string, |
182 | | - limit: number, |
| 218 | + walletAddress: string, |
| 219 | + amountsIn: Record<string, bigint>, |
| 220 | + slippage: number, |
| 221 | + referrer?: string, |
183 | 222 | ): Promise<string> { |
184 | 223 | try { |
185 | 224 | const sdk = PoolTool.initSDK(); |
186 | 225 | await sdk.init(); |
187 | 226 | 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 | + |
192 | 235 | return JSON.stringify([ |
193 | 236 | { |
194 | | - reasoning: 'Successfully retrieved pool events', |
195 | | - response: events, |
| 237 | + reasoning: 'Successfully created deposit transaction', |
| 238 | + response: { tx, poolId }, |
196 | 239 | status: 'success', |
197 | | - query: `Get ${eventType} events for pool ${poolId}`, |
| 240 | + query: 'Create deposit transaction', |
198 | 241 | errors: [], |
199 | 242 | }, |
200 | 243 | ]); |
201 | 244 | } catch (error) { |
202 | 245 | return JSON.stringify([ |
203 | 246 | 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', |
206 | 249 | }), |
207 | 250 | ]); |
208 | 251 | } |
209 | 252 | } |
210 | 253 |
|
211 | 254 | /** |
212 | | - * Gets pool information |
213 | | - * @param poolId Pool ID to get info for |
214 | | - * @returns Pool information |
| 255 | + * Gets withdraw transaction for a pool |
215 | 256 | */ |
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> { |
217 | 265 | try { |
218 | 266 | const sdk = PoolTool.initSDK(); |
219 | 267 | await sdk.init(); |
220 | 268 | const pool = await sdk.Pools().getPool({ objectId: poolId }); |
221 | 269 |
|
| 270 | + const tx = await pool.getWithdrawTransaction({ |
| 271 | + walletAddress, |
| 272 | + amountsOutDirection, |
| 273 | + lpCoinAmount, |
| 274 | + slippage, |
| 275 | + referrer, |
| 276 | + }); |
| 277 | + |
222 | 278 | return JSON.stringify([ |
223 | 279 | { |
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 }, |
234 | 282 | status: 'success', |
235 | | - query: `Get info for pool ${poolId}`, |
| 283 | + query: 'Create withdraw transaction', |
236 | 284 | errors: [], |
237 | 285 | }, |
238 | 286 | ]); |
239 | 287 | } catch (error) { |
240 | 288 | return JSON.stringify([ |
241 | 289 | 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', |
244 | 337 | }), |
245 | 338 | ]); |
246 | 339 | } |
|
0 commit comments