UNPKG

tensaikit

Version:

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

73 lines (72 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchMarketStateFromContract = void 0; const errors_1 = require("../../../common/errors"); const utils_1 = require("../utils"); const morphoBlueABI_1 = require("../abi/morphoBlueABI"); /** * Fetches the current state variables of a Morpho Blue market using the given market ID. * * This function: * 1. Detects the current network from the wallet provider. * 2. Resolves the correct Morpho Blue contract address using the chain ID. * 3. Calls the `market` function of the Morpho Blue contract to retrieve market-level statistics. * 4. Extracts and returns state variables such as total supply/borrow assets and shares, last update timestamp, and fee. * * This is useful for computing utilization rates, health factors, and APYs in frontend or analytics dashboards. * * @param walletProvider - An instance of {@link EvmWalletProvider} used to interact with the connected EVM network. * @param args - An object matching {@link GetMarketStatesSchema}, including: * @property {string} marketId - The bytes32 hex string identifying the target Morpho Blue market. * * @returns A Promise resolving to an object with: * @property {string} marketId - The ID of the queried market. * @property {bigint} totalSupplyAssets - Total amount of supplied assets in the market. * @property {bigint} totalSupplyShares - Total shares issued for suppliers. * @property {bigint} totalBorrowAssets - Total borrowed assets in the market. * @property {bigint} totalBorrowShares - Total shares representing borrowed positions. * @property {bigint} lastUpdate - Timestamp of the last interest accrual or update. * @property {bigint} fee - Current fee charged by the protocol (likely in basis points or similar unit). * * @throws Will throw if: * - Network information is missing or invalid. * - The market contract call fails or returns empty. */ const fetchMarketStateFromContract = async (walletProvider, args) => { try { const network = walletProvider.getNetwork(); const chainId = network.chainId; if (!chainId) { throw (0, errors_1.createError)("Invalid or missing network", errors_1.ErrorCode.INVALID_NETWORK); } const morphoBlueContractAddress = (0, utils_1.getMorphoBlueContractAddress)(Number(chainId)); const marketResponse = await walletProvider.readContract({ address: morphoBlueContractAddress, abi: morphoBlueABI_1.MORPHO_BLUE_ABI, functionName: "market", args: [args.marketId], }); if (!marketResponse) { throw (0, errors_1.createError)("Invalid market id or missing market information", errors_1.ErrorCode.INVALID_INPUT); } const totalSupplyAssets = marketResponse[0]; const totalSupplyShares = marketResponse[1]; const totalBorrowAssets = marketResponse[2]; const totalBorrowShares = marketResponse[3]; const lastUpdate = marketResponse[4]; const fee = marketResponse[5]; return { marketId: args.marketId, totalSupplyAssets: totalSupplyAssets, totalSupplyShares: totalSupplyShares, totalBorrowAssets: totalBorrowAssets, totalBorrowShares: totalBorrowShares, lastUpdate: lastUpdate, fee: fee, }; } catch (error) { throw (0, errors_1.handleError)("Error fetching states for Morpho Vault", error); } }; exports.fetchMarketStateFromContract = fetchMarketStateFromContract;