UNPKG

kiban-agent-kit

Version:

Open-source framework connecting AI agents to Katana ecosystem protocols

150 lines (149 loc) 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KibanAgentKit = void 0; const viem_1 = require("viem"); const accounts_1 = require("viem/accounts"); const chains_1 = require("viem/chains"); const wallet_1 = require("../tools/wallet"); const token_1 = require("../tools/token"); const chains_2 = require("../constants/chains"); const dexscreener_1 = require("../tools/dexscreener"); const wallet_2 = require("../tools/wallet"); const token_2 = require("../tools/token"); class KibanAgentKit { constructor(config) { // Validate private key if (!config.privateKey) { throw new Error("Private key is required. Please provide a valid private key in your configuration."); } if (typeof config.privateKey !== "string" || !config.privateKey.startsWith("0x")) { throw new Error("Invalid private key format. Private key must be a hex string starting with '0x'."); } // Set up chain configuration this.chain = config.chain || chains_1.mainnet; // Validate chain support if (!chains_2.SUPPORTED_CHAINS.find((c) => c.id === this.chain.id)) { throw new Error(`Chain ${this.chain.id} is not supported. Supported chains are: ${chains_2.SUPPORTED_CHAINS.map((c) => c.name).join(", ")}`); } // Set up RPC URL const rpcUrl = config.rpcUrl || chains_2.DEFAULT_RPC_URLS[this.chain.id]; if (!rpcUrl) { throw new Error(`No RPC URL provided for chain ${this.chain.name} (ID: ${this.chain.id}). Please provide a valid RPC URL in your configuration.`); } try { // Create account from private key this.account = (0, accounts_1.privateKeyToAccount)(config.privateKey); // Create public and wallet clients this.clients = { public: (0, viem_1.createPublicClient)({ chain: this.chain, transport: (0, viem_1.http)(rpcUrl), }), wallet: (0, viem_1.createWalletClient)({ chain: this.chain, transport: (0, viem_1.http)(rpcUrl), account: this.account, }), }; // Initialize tools this.walletTools = (0, wallet_1.createWalletTools)(this.clients.public, this.clients.wallet, this.chain); this.tokenTools = (0, token_1.createTokenTools)(this.clients.public, this.clients.wallet, this.chain); // Initialize services this.dexScreenerService = new dexscreener_1.DexScreenerService(); this.walletService = new wallet_2.WalletService(this); this.tokenService = new token_2.TokenService(this); console.log(`🔗 Connected to ${this.chain.name} (Chain ID: ${this.chain.id})`); } catch (err) { const error = err; if (error.message.includes("invalid private key")) { throw new Error(`Invalid private key: The provided private key is not in the correct format. Please ensure it's a valid 32-byte hex string starting with '0x'.`); } else if (error.message.includes("network")) { throw new Error(`Network error: Failed to connect to RPC URL ${rpcUrl}. Please check your internet connection and RPC URL validity.`); } else { throw new Error(`Failed to initialize Kiban Agent: ${error.message}. Please check your configuration and try again.`); } } } // Wallet operations getAddress() { return this.walletTools.getAddress(); } getChainId() { return this.chain.id; } async getChainInfo() { return this.walletTools.getChainInfo(); } async getNativeBalance() { return this.walletTools.getBalance(); } // Token operations async checkToken(tokenAddressOrSymbol) { return this.tokenTools.checkToken(tokenAddressOrSymbol); } async sendTokens(params) { return this.tokenTools.sendTokens(params); } async approveSpending(params) { return this.tokenTools.approveSpending(params); } async getTokenMetadata(tokenAddress) { return this.tokenTools.getTokenMetadata(tokenAddress); } async getAllowance(params) { return this.tokenTools.getAllowance(params); } async waitForTransaction(hash) { return this.tokenTools.waitForTransaction(hash); } // Add a method to get gas price async getGasPrice() { return this.clients.public.getGasPrice(); } // Add a method to estimate gas async estimateGas(params) { if (!(0, viem_1.isAddress)(params.to)) { throw new Error(`Invalid address: ${params.to}`); } return this.clients.public.estimateGas({ account: this.account.address, to: params.to, value: params.value, }); } // DexScreener service methods async getTokenData(tokenAddress) { return this.dexScreenerService.getTokenData(tokenAddress); } async searchTokenByTicker(ticker) { return this.dexScreenerService.searchTokenByTicker(ticker); } // Wallet service methods async getWalletInfo() { return this.walletService.getWalletInfo(); } async getTransactionHistory(limit = 5) { return this.walletService.getTransactionHistory(limit); } async estimateGasForTransaction(to, value) { return this.walletService.estimateGas(to, value); } // Token service methods async getTokenInfo(tokenAddress) { return this.tokenService.getTokenInfo(tokenAddress); } async sendTokensWithService(params) { return this.tokenService.sendTokens(params); } async approveTokenSpending(params) { return this.tokenService.approveSpending(params); } async getTokenAllowance(params) { return this.tokenService.getAllowance(params); } } exports.KibanAgentKit = KibanAgentKit;