tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
50 lines (49 loc) • 2.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchMarketStateByUniqueKey = void 0;
const errors_1 = require("../../../common/errors");
const utils_1 = require("../utils");
const utils_2 = require("../../../common/utils");
const morphoAPIQuery_1 = require("../query/morphoAPIQuery");
/**
* Fetches the on-chain state of a Morpho Blue market using its unique key.
*
* This function queries the Morpho subgraph to retrieve the current market state
* (liquidity, borrow/supply stats, APYs, etc.) associated with the provided `uniqueKey`.
*
* @param walletProvider - Connected EVM wallet instance.
* @param args - Object containing a valid `uniqueKey` (bytes32) to identify the market.
*
* @returns A Promise resolving to the state of the specified market,
* or `null` if the market is not found or has no recorded state.
*
* @throws Will throw an error if:
* - Network or chainId is missing or invalid.
* - The network is not supported by Morpho subgraph.
* - A subgraph call fails or returns an invalid result.
*/
const fetchMarketStateByUniqueKey = async (walletProvider, args) => {
try {
const network = walletProvider.getNetwork();
const chainId = network.chainId;
const networkId = network.networkId;
if (!chainId || !networkId) {
throw (0, errors_1.createError)("Invalid or missing network", errors_1.ErrorCode.INVALID_NETWORK);
}
if (!utils_1.MORPHO_SUPPORTED_SUB_GRAPH.includes(network.networkId)) {
throw (0, errors_1.createError)("Network not supported!", errors_1.ErrorCode.INVALID_NETWORK);
}
const response = await (0, utils_2.makeSubgraphQueryCall)(utils_1.MORPHO_GRAPH_API_URL, (0, morphoAPIQuery_1.queryMarketStateByUniqueKey)({
chainId: Number(chainId),
uniqueKey: args.uniqueKey,
}));
if (!response || !response.marketByUniqueKey) {
return null;
}
return response.marketByUniqueKey;
}
catch (error) {
throw (0, errors_1.handleError)("Failed to fetch market state by uniqueKey", error);
}
};
exports.fetchMarketStateByUniqueKey = fetchMarketStateByUniqueKey;