UNPKG

tensaikit

Version:

An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.

120 lines (117 loc) 5.66 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.erc20ActionProvider = exports.ERC20ActionProvider = void 0; const zod_1 = require("zod"); const actionProvider_1 = require("../actionProvider"); const actionDecorator_1 = require("../actionDecorator"); const schemas_1 = require("./schemas"); const constants_1 = require("./constants"); const viem_1 = require("viem"); const walletProviders_1 = require("../../walletProviders"); const utils_1 = require("../../common/utils"); const errors_1 = require("../../common/errors"); /** * ERC20ActionProvider provides actions to interact with ERC20 tokens such as * checking token balances and initiating token transfers. */ class ERC20ActionProvider extends actionProvider_1.ActionProvider { /** * Constructs a new ERC20ActionProvider instance with no predefined actions. */ constructor() { super("erc20", []); this.supportsNetwork = (network) => network.protocolFamily === "evm"; } /** * Fetches the ERC20 token balance for the connected wallet address. * * @param walletProvider - The EVM-compatible wallet provider * @param args - Contains the contract address of the ERC20 token * @returns A formatted message showing the balance in human-readable units */ async getBalance(walletProvider, args) { try { const balance = await walletProvider.readContract({ address: args.contractAddress, abi: constants_1.abi, functionName: "balanceOf", args: [walletProvider.getAddress()], }); const decimals = await walletProvider.readContract({ address: args.contractAddress, abi: constants_1.abi, functionName: "decimals", args: [], }); return (0, utils_1.wrapAndStringify)("erc20.get_balance", `Balance of ${args.contractAddress} is ${(0, viem_1.formatUnits)(balance, decimals)}`); } catch (error) { throw (0, errors_1.handleError)("Error getting balance.", error); } } /** * Transfers ERC20 tokens from the connected wallet to another onchain address. * * @param walletProvider - The EVM-compatible wallet provider * @param args - Contains token contract address, destination, and amount * @returns A success message including transaction hash, or an error message */ async transfer(walletProvider, args) { try { const hash = await walletProvider.sendTransaction({ to: args.contractAddress, data: (0, viem_1.encodeFunctionData)({ abi: constants_1.abi, functionName: "transfer", args: [args.destination, BigInt(args.amount)], }), }); await walletProvider.waitForTransactionReceipt(hash); return (0, utils_1.wrapAndStringify)("erc20.transfer", `Transferred ${args.amount} of ${args.contractAddress} to ${args.destination}.\nTransaction hash for the transfer: ${hash}`); } catch (error) { throw (0, errors_1.handleError)("Error transferring the asset.", error); } } } exports.ERC20ActionProvider = ERC20ActionProvider; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_balance", description: `This tool will get the balance of an ERC20 asset in the wallet. It takes the contract address as input.`, schema: schemas_1.GetBalanceSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], ERC20ActionProvider.prototype, "getBalance", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "transfer", description: ` This tool will transfer an ERC20 token from the wallet to another onchain address. It takes the following inputs: - amount: The amount to transfer - contractAddress: The contract address of the token to transfer - destination: Where to send the funds (can be an onchain address, ENS 'example.eth'') Important notes: - Ensure sufficient balance of the input asset before transferring - When sending native assets (e.g. 'eth' on katana-network), ensure there is sufficient balance for the transfer itself AND the gas cost of this transfer `, schema: schemas_1.TransferSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], ERC20ActionProvider.prototype, "transfer", null); const erc20ActionProvider = () => new ERC20ActionProvider(); exports.erc20ActionProvider = erc20ActionProvider;