UNPKG

tensaikit

Version:

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

116 lines (113 loc) 5.18 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); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SushiSwapPriceActions = void 0; const zod_1 = __importDefault(require("zod")); const actionProvider_1 = require("../../actionProvider"); const actionDecorator_1 = require("../../actionDecorator"); const walletProviders_1 = require("../../../walletProviders"); const errors_1 = require("../../../common/errors"); const schemas_1 = require("../schemas"); const logic_1 = require("../logic"); const utils_1 = require("../../../common/utils"); /** * SushiSwapPriceActions provides methods to fetch token prices from SushiSwap. * * This action group offers price-related utilities using SushiSwap's pricing module, * allowing access to both individual token prices and bulk price data for all tokens * on a connected EVM-compatible network. * * Responsibilities: * - Fetch USD prices for all tokens supported by SushiSwap on a given chain * - Fetch USD price for a specific token using its contract address * */ class SushiSwapPriceActions extends actionProvider_1.ActionProvider { constructor() { super("sushi_swap.price", []); this.supportsNetwork = (network) => network.protocolFamily === "evm"; } /** * Fetches USD prices for all tokens on the connected EVM chain. * * @param walletProvider - Instance of EVM-compatible WalletProvider * @param _ - Unused (empty) arguments validated by schema * @returns A JSON string of all token prices * @throws If chainId is missing or API call fails */ async getAllTokenPrices(walletProvider, _) { try { const chainId = walletProvider.getNetwork().chainId; if (!chainId) { throw (0, errors_1.createError)("Invalid or missing chain ID", errors_1.ErrorCode.INVALID_NETWORK); } const response = await (0, logic_1.fetchAllTokenPrices)(chainId); return (0, utils_1.wrapAndStringify)("sushi_swap.price.get_all_token_prices", response); } catch (error) { throw (0, errors_1.handleError)("Failed to fetch all token prices", error); } } /** * Fetches the USD price of a specific token from SushiSwap. * * @param walletProvider - Instance of EVM-compatible WalletProvider * @param args - Includes `tokenAddress` of the token to fetch * @returns A JSON string containing the token price * @throws If chainId is missing or API call fails */ async getTokenPrice(walletProvider, args) { try { const { tokenAddress } = args; const chainId = walletProvider.getNetwork().chainId; if (!chainId) { throw (0, errors_1.createError)("Invalid or missing chain ID", errors_1.ErrorCode.INVALID_NETWORK); } const response = await (0, logic_1.fetchTokenPrice)(chainId, tokenAddress); return (0, utils_1.wrapAndStringify)("sushi_swap.price.get_token_price", response.toString()); } catch (error) { throw (0, errors_1.handleError)("Failed to fetch token price", error); } } } exports.SushiSwapPriceActions = SushiSwapPriceActions; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_all_token_prices", description: ` Returns USD prices for all tokens on a specific chain. `, schema: schemas_1.GetAllTokenPricesSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], SushiSwapPriceActions.prototype, "getAllTokenPrices", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_token_price", description: ` Returns the USD price of a specific token on a given chain. Inputs: - tokenAddress: Token contract address. Notes: - Ensure the token address is valid and supported by SushiSwap. `, schema: schemas_1.GetTokenPriceSchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], SushiSwapPriceActions.prototype, "getTokenPrice", null);