UNPKG

kiban-agent-kit

Version:

Open-source framework connecting AI agents to Katana ecosystem protocols

100 lines (99 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WalletService = void 0; /** * Core wallet service for wallet-related operations */ class WalletService { constructor(agent) { this.agent = agent; } /** * Get information about the connected wallet */ async getWalletInfo() { const address = this.agent.getAddress(); const balance = await this.agent.getNativeBalance(); const chainInfo = await this.agent.getChainInfo(); return { address, balance: `${balance} ETH`, chain: { name: chainInfo.name, id: chainInfo.id, nativeCurrency: chainInfo.nativeCurrency, }, message: `This wallet (${address}) is connected to ${chainInfo.name} with a balance of ${balance} ETH.`, }; } /** * Get transaction history for the connected wallet */ async getTransactionHistory(limit = 5) { const address = this.agent.getAddress(); // Get the public client from the agent const publicClient = this.agent["clients"].public; // Get transactions from the connected wallet const sentTxs = await publicClient.getTransactionCount({ address: address, blockTag: "latest", }); if (sentTxs === 0) { return { message: `No transactions found for wallet ${address}`, transactions: [], }; } // Since viem doesn't have a direct "get transactions" method, // we'll return a message explaining the limitation return { message: `This wallet (${address}) has sent ${sentTxs} transactions. To view detailed transaction history, please use a block explorer like Etherscan.`, transactionCount: sentTxs, viewOnEtherscan: `https://etherscan.io/address/${address}`, }; } /** * Estimate gas prices and transaction costs */ async estimateGas(to, value) { // Get the public client from the agent const publicClient = this.agent["clients"].public; // Get current gas price const gasPrice = await publicClient.getGasPrice(); // Format gas price in gwei const gasPriceGwei = gasPrice.toString(); let result = { currentGasPrice: `${gasPriceGwei} gwei`, estimatedBaseFee: `${gasPriceGwei} gwei`, }; // If to and value are provided, estimate transaction cost if (to && value) { try { const valueInWei = BigInt(value); const gasEstimate = await publicClient.estimateGas({ account: this.agent.getAddress(), to: to, value: valueInWei, }); const estimatedCost = gasEstimate * gasPrice; result.transactionDetails = { gasUnits: gasEstimate.toString(), estimatedCostWei: estimatedCost.toString(), estimatedCostEth: (Number(estimatedCost) / 1e18).toString(), message: `Estimated cost for this transaction: ${Number(estimatedCost) / 1e18} ETH (${gasEstimate} gas units at ${gasPriceGwei} gwei)`, }; } catch (error) { result.transactionDetails = { gasUnits: "0", estimatedCostWei: "0", estimatedCostEth: "0", message: "Failed to estimate gas", error: error.message, }; } } return result; } } exports.WalletService = WalletService;