UNPKG

kiban-agent-kit

Version:

Open-source framework connecting AI agents to Katana ecosystem protocols

153 lines (152 loc) 5.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TransactionHistoryTool = exports.GasEstimatorTool = exports.WalletInfoTool = exports.EvmTransferTool = exports.EvmBalanceTool = void 0; const tools_1 = require("@langchain/core/tools"); const zod_1 = require("zod"); const wallet_1 = require("../tools/wallet"); const viem_1 = require("viem"); /** * LangChain tool for checking native token (ETH) balance */ class EvmBalanceTool extends tools_1.StructuredTool { constructor(agent) { super(); this.agent = agent; this.name = "check_eth_balance"; this.description = "Get the native ETH balance of a wallet address"; this.schema = zod_1.z.object({ address: zod_1.z.string().describe("The wallet address to check balance for"), }); } async _call(input) { const balance = await this.agent.getNativeBalance(); const formattedBalance = (0, viem_1.formatEther)(BigInt(balance)); return `${formattedBalance} ETH`; } } exports.EvmBalanceTool = EvmBalanceTool; /** * LangChain tool for transferring ETH or ERC20 tokens */ class EvmTransferTool extends tools_1.StructuredTool { constructor(agent) { super(); this.agent = agent; this.name = "transfer_tokens"; this.description = "Transfer ETH or ERC20 tokens to another address"; this.schema = zod_1.z.object({ to: zod_1.z.string().describe("The recipient wallet address"), amount: zod_1.z.string().describe("The amount to transfer"), tokenAddress: zod_1.z .string() .optional() .describe("Optional ERC20 token address. If not provided, sends ETH"), }); } async _call(input) { try { if (!input.tokenAddress) { // Send ETH const tx = await this.agent.sendTokens({ token: "eth", to: input.to, amount: input.amount, }); return `ETH transfer transaction sent: ${tx}`; } else { // Send ERC20 const tx = await this.agent.sendTokens({ token: input.tokenAddress, to: input.to, amount: input.amount, }); return `Token transfer transaction sent: ${tx}`; } } catch (error) { return `Error: ${error.message}`; } } } exports.EvmTransferTool = EvmTransferTool; /** * LangChain tool for getting wallet information */ class WalletInfoTool extends tools_1.StructuredTool { constructor(agent) { super(); this.name = "get_wallet_info"; this.description = "Get information about the currently connected wallet including address, balance, and chain"; this.schema = zod_1.z.object({}); this.service = new wallet_1.WalletService(agent); } async _call() { try { const walletInfo = await this.service.getWalletInfo(); return JSON.stringify(walletInfo, null, 2); } catch (error) { return `Error retrieving wallet information: ${error.message}`; } } } exports.WalletInfoTool = WalletInfoTool; /** * LangChain tool for estimating gas */ class GasEstimatorTool extends tools_1.StructuredTool { constructor(agent) { super(); this.name = "estimate_gas"; this.description = "Get current gas prices and estimate transaction costs"; this.schema = zod_1.z.object({ to: zod_1.z .string() .optional() .describe("Optional recipient address for transaction cost estimation"), value: zod_1.z .string() .optional() .describe("Optional amount in ETH for transaction cost estimation"), }); this.service = new wallet_1.WalletService(agent); } async _call(input) { try { const gasEstimate = await this.service.estimateGas(input.to, input.value); return JSON.stringify(gasEstimate, null, 2); } catch (error) { return `Error estimating gas: ${error.message}`; } } } exports.GasEstimatorTool = GasEstimatorTool; /** * LangChain tool for retrieving transaction history */ class TransactionHistoryTool extends tools_1.StructuredTool { constructor(agent) { super(); this.name = "get_transaction_history"; this.description = "Get recent transactions for the connected wallet"; this.schema = zod_1.z.object({ limit: zod_1.z .number() .optional() .describe("Maximum number of transactions to return (default: 5)"), }); this.service = new wallet_1.WalletService(agent); } async _call(input) { try { const history = await this.service.getTransactionHistory(input.limit || 5); return JSON.stringify(history, null, 2); } catch (error) { return `Error retrieving transaction history: ${error.message}`; } } } exports.TransactionHistoryTool = TransactionHistoryTool;