UNPKG

tensaikit

Version:

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

262 lines (251 loc) 11.8 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.morphoSubgraphActionProvider = exports.MorphoSubgraphActionProvider = void 0; const zod_1 = require("zod"); const actionDecorator_1 = require("../actionDecorator"); const actionProvider_1 = require("../actionProvider"); const walletProviders_1 = require("../../walletProviders"); const utils_1 = require("./utils"); const schemas_1 = require("./schemas"); const errors_1 = require("../../common/errors"); const utils_2 = require("../../common/utils"); const fetchWhitelistedMarkets_1 = require("./logic/fetchWhitelistedMarkets"); const fetchWhitelistedVaults_1 = require("./logic/fetchWhitelistedVaults"); const fetchMarketStateByUniqueKey_1 = require("./logic/fetchMarketStateByUniqueKey"); const fetchUserDataByAddress_1 = require("./logic/fetchUserDataByAddress"); const fetchCurators_1 = require("./logic/fetchCurators"); /** * Provides subgraph-based read-only actions for Morpho Blue protocol. * Includes interest rates, active markets, and account-level data queries. */ class MorphoSubgraphActionProvider extends actionProvider_1.ActionProvider { /** * Initializes the Morpho Subgraph Action Provider */ constructor() { super("morpho.subgraph", []); this.supportsNetwork = (network) => network.protocolFamily === "evm" && utils_1.MORPHO_SUPPORTED_SUB_GRAPH.includes(network.networkId); } /** * Retrieves a list of active (whitelisted) Morpho Blue markets with configuration and state metrics. * * This function queries the Morpho subgraph to return market entries that are whitelisted, * including supply/borrow APYs, token details, oracle and interest rate model configuration, * utilization, collateral stats, and overall TVL. * * Useful for building dashboards or letting users explore markets available for lending/borrowing. * * @param walletProvider - Instance of EVM-compatible wallet provider. * @param args - Pagination input including: * - `skip`: Number of items to skip. * - `first`: Number of items to fetch (max 100). * * @returns A Promise resolving to a JSON string of active Morpho Blue markets. * * @throws Will throw an error if: * - The network is not supported or is invalid. * - The subgraph query fails or returns an invalid result. */ async getActiveMarkets(walletProvider, args) { try { const response = await (0, fetchWhitelistedMarkets_1.fetchWhitelistedMarkets)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.subgraph.get_active_markets", response); } catch (error) { throw (0, errors_1.handleError)("Error fetching Morpho markets", error); } } /** * Retrieves all whitelisted vaults available on the Morpho Blue protocol for the current network. * * Each vault contains metadata (name, symbol, creator), performance data (APY, total assets), and reward info. * Useful for users selecting yield strategies or analyzing vaults curated by the protocol. * * @param walletProvider - Instance of EVM-compatible wallet provider. * @param args - Pagination input including: * - `skip`: Number of items to skip. * - `first`: Number of items to fetch (max 100). * * @returns A Promise resolving to a JSON string array of whitelisted vaults. * * @throws Will throw if the network is unsupported or the query fails. */ async getWhitelistedVaults(walletProvider, args) { try { const response = await (0, fetchWhitelistedVaults_1.fetchWhitelistedVaults)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.subgraph.get_whitelisted_vaults", response); } catch (error) { throw (0, errors_1.handleError)("Error fetching Morpho vaults", error); } } /** * Fetches the current state of a specific Morpho Blue market using its unique key. * * This action returns on-chain metrics such as TVL, supply/borrow amounts, liquidity, * and performance rates (APY, APR). Useful for tracking market health and performance. * * @param walletProvider - Connected EVM-compatible wallet provider. * @param args - Includes a valid `uniqueKey` and `chainId` to identify the market. * * @returns A Promise resolving to a JSON string of market state details. * * @throws If the network is not supported or subgraph query fails. */ async getMarketStateByUniqueKey(walletProvider, args) { try { const response = await (0, fetchMarketStateByUniqueKey_1.fetchMarketStateByUniqueKey)(walletProvider, args); return (0, utils_2.wrapAndStringify)("morpho.subgraph.get_market_state_by_unique_key", response); } catch (error) { throw (0, errors_1.handleError)("Error fetching market state by unique key", error); } } /** * Retrieves complete portfolio data for the connected wallet in Morpho Blue protocol. * * This includes vault and market positions (with PnL, balances, and USD values), and recent transactions. * Ideal for building user dashboards or financial summaries. * * @param walletProvider - Connected EVM-compatible wallet provider. * * @returns A Promise resolving to a JSON string of the user's positions and activity. * * @throws Will throw if the network is unsupported or query fails. */ async getUserPortfolioData(walletProvider) { try { const response = await (0, fetchUserDataByAddress_1.fetchUserDataByAddress)(walletProvider); return (0, utils_2.wrapAndStringify)("morpho.subgraph.get_user_portfolio_data", response); } catch (error) { throw (0, errors_1.handleError)("Error fetching user portfolio data", error); } } /** * Fetches all curators registered on the Morpho Blue protocol for the connected network. * * Each curator includes metadata (name, image), AUM, and verification status. * Useful for filtering vaults or recommending trusted curators to users. * * @param walletProvider - Instance of EVM-compatible wallet provider. * * @returns A Promise resolving to a JSON string of curators. * * @throws If the network is unsupported or the subgraph call fails. */ async getCurators(walletProvider) { try { const response = await (0, fetchCurators_1.fetchCurators)(walletProvider); return (0, utils_2.wrapAndStringify)("morpho.subgraph.get_curators", response); } catch (error) { throw (0, errors_1.handleError)("Error fetching curators", error); } } } exports.MorphoSubgraphActionProvider = MorphoSubgraphActionProvider; __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_active_markets", description: ` This action fetches active (whitelisted) Morpho Blue markets along with configuration and performance metrics. Inputs: - skip: Number of entries to skip for pagination. - first: Number of entries to fetch (max 100). Each market entry includes: - Token metadata (loan/collateral) - Oracle and interest rate model configuration - Max LTV, supply and borrow rates - Utilization metrics, TVL, and APR rewards `, schema: schemas_1.ActiveMarketsQuerySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoSubgraphActionProvider.prototype, "getActiveMarkets", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_whitelisted_vaults", description: ` This action fetches whitelisted vaults from the Morpho Blue subgraph for the current chain. Inputs: - skip: Number of entries to skip for pagination. - first: Number of entries to fetch (max 100). Each vault includes: - Vault metadata (name, creator, image) - Total assets, APY, rewards info - Whitelist and verification status `, schema: schemas_1.WhitelistedVaultsQuerySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoSubgraphActionProvider.prototype, "getWhitelistedVaults", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_market_state_by_unique_key", description: ` Fetches detailed state information of a Morpho Blue market using its unique key. Inputs: - uniqueKey: Unique identifier of the market (hex string) - chainId: Chain ID of the network (e.g., 137, 8453) The response includes: - Collateral, borrow, supply, and liquidity values (raw + USD) - Historical and daily APYs - Reward rates (APR) `, schema: schemas_1.MarketStateByUniqueKeySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider, void 0]), __metadata("design:returntype", Promise) ], MorphoSubgraphActionProvider.prototype, "getMarketStateByUniqueKey", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_user_portfolio_data", description: ` Fetches a user's full portfolio data on Morpho Blue, including vault and market positions. Inputs: (No manual input, wallet address is derived from provider) The response includes: - Market positions: borrow/supply/collateral + PnL - Vault holdings: assets, shares, USD value - Recent transaction history (hash, type, timestamp) `, schema: schemas_1.UserDataQuerySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider]), __metadata("design:returntype", Promise) ], MorphoSubgraphActionProvider.prototype, "getUserPortfolioData", null); __decorate([ (0, actionDecorator_1.CreateAction)({ name: "get_curators", description: ` This action fetches all verified curators on Morpho Blue for the current chain. Inputs: None Each curator includes: - Name and image - Total AUM (Assets Under Management) - Verification status `, schema: schemas_1.CuratorsQuerySchema, }), __metadata("design:type", Function), __metadata("design:paramtypes", [walletProviders_1.EvmWalletProvider]), __metadata("design:returntype", Promise) ], MorphoSubgraphActionProvider.prototype, "getCurators", null); const morphoSubgraphActionProvider = () => new MorphoSubgraphActionProvider(); exports.morphoSubgraphActionProvider = morphoSubgraphActionProvider;