UNPKG

kiban-agent-kit

Version:

Open-source framework connecting AI agents to Katana ecosystem protocols

118 lines (117 loc) 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwapTokensTool = exports.GetSwapQuoteTool = void 0; const tools_1 = require("@langchain/core/tools"); const zod_1 = require("zod"); const swap_1 = require("../tools/token/swap"); /** * LangChain tool for getting a swap quote */ class GetSwapQuoteTool extends tools_1.StructuredTool { constructor(agent) { super(); this.name = "get_swap_quote"; this.description = "Get a quote for swapping tokens, including expected output amount and price impact"; this.schema = zod_1.z.object({ tokenIn: zod_1.z .string() .describe("The input token address or symbol (e.g., 'ETH', 'USDC')"), tokenOut: zod_1.z .string() .describe("The output token address or symbol (e.g., 'ETH', 'USDC')"), amount: zod_1.z.string().describe("The amount of input token to swap"), slippagePercentage: zod_1.z .number() .optional() .describe("Optional slippage tolerance percentage (default: 0.5)"), }); this.service = new swap_1.SwapService(agent); } async _call(input) { try { const quote = await this.service.getSwapQuote({ tokenIn: input.tokenIn, tokenOut: input.tokenOut, amount: input.amount, slippagePercentage: input.slippagePercentage, }); return JSON.stringify({ tokenIn: { symbol: quote.tokenIn.symbol, amount: quote.tokenIn.amount, }, tokenOut: { symbol: quote.tokenOut.symbol, amount: quote.tokenOut.amount, }, executionPrice: quote.executionPrice, minimumAmountOut: quote.minimumAmountOut, priceImpact: quote.priceImpact, message: `You can swap ${quote.tokenIn.amount} ${quote.tokenIn.symbol} for approximately ${quote.tokenOut.amount} ${quote.tokenOut.symbol} (minimum: ${quote.minimumAmountOut} ${quote.tokenOut.symbol}).`, }, null, 2); } catch (error) { return `Error getting swap quote: ${error.message}`; } } } exports.GetSwapQuoteTool = GetSwapQuoteTool; /** * LangChain tool for executing a token swap */ class SwapTokensTool extends tools_1.StructuredTool { constructor(agent) { super(); this.name = "swap_tokens"; this.description = "Execute a token swap using Uniswap V3"; this.schema = zod_1.z.object({ tokenIn: zod_1.z .string() .describe("The input token address or symbol (e.g., 'ETH', 'USDC')"), tokenOut: zod_1.z .string() .describe("The output token address or symbol (e.g., 'ETH', 'USDC')"), amount: zod_1.z.string().describe("The amount of input token to swap"), slippagePercentage: zod_1.z .number() .optional() .describe("Optional slippage tolerance percentage (default: 0.5)"), recipient: zod_1.z .string() .optional() .describe("Optional recipient address (default: sender's address)"), }); this.service = new swap_1.SwapService(agent); } async _call(input) { try { // First get a quote to show the user what to expect const quote = await this.service.getSwapQuote({ tokenIn: input.tokenIn, tokenOut: input.tokenOut, amount: input.amount, slippagePercentage: input.slippagePercentage, }); // Execute the swap const result = await this.service.swapTokens({ tokenIn: input.tokenIn, tokenOut: input.tokenOut, amount: input.amount, slippagePercentage: input.slippagePercentage, recipient: input.recipient, }); return JSON.stringify({ transactionHash: result.hash, tokenIn: result.tokenIn, tokenOut: result.tokenOut, amountIn: result.amountIn, expectedAmountOut: result.expectedAmountOut, message: `Successfully swapped ${result.amountIn} ${result.tokenIn} for approximately ${result.expectedAmountOut} ${result.tokenOut}. Transaction hash: ${result.hash}`, }, null, 2); } catch (error) { return `Error executing swap: ${error.message}`; } } } exports.SwapTokensTool = SwapTokensTool;