tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
69 lines (68 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchMarketConfigFromContract = void 0;
const errors_1 = require("../../../common/errors");
const utils_1 = require("../utils");
const morphoBlueABI_1 = require("../abi/morphoBlueABI");
/**
* Fetches detailed market information for a given Morpho Blue market ID.
*
* This function interacts with the Morpho Blue smart contract on the current network,
* using the provided EVM wallet provider to read the market parameters associated with
* the specified `marketId`. These parameters include the loan token address, collateral
* token address, oracle, interest rate model (IRM), and loan-to-value ratio (LLTV).
*
* @param walletProvider - An instance of {@link EvmWalletProvider} connected to the user's wallet.
* @param args - An object validated by {@link GetMarketInfoSchema}, containing:
* @property {string} marketId - The unique bytes32 market identifier (hex string).
*
* @returns A Promise resolving to an object with the following properties:
* @property {string} marketId - The original market ID used in the request.
* @property {string} loanToken - The address of the loan token for the market.
* @property {string} collateralToken - The address of the collateral token.
* @property {string} oracle - The address of the oracle associated with the market.
* @property {string} irm - The address of the Interest Rate Model contract.
* @property {bigint} lltv - The loan-to-value ratio in basis points (1e4 = 100%).
*
* @throws Will throw an error if:
* - The network or chainId is invalid or unavailable.
* - The market ID does not exist or returns no response from the contract.
* - Any unexpected error occurs during contract read.
*/
const fetchMarketConfigFromContract = 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: "idToMarketParams",
args: [args.marketId],
});
if (!marketResponse) {
throw (0, errors_1.createError)("Invalid market id or missing market information", errors_1.ErrorCode.INVALID_INPUT);
}
const loanToken = marketResponse[0];
const collateralToken = marketResponse[1];
const oracle = marketResponse[2];
const irm = marketResponse[3];
const lltv = marketResponse[4];
return {
marketId: args.marketId,
morphoBlueContractAddress: morphoBlueContractAddress,
loanToken: loanToken,
collateralToken: collateralToken,
oracle: oracle,
irm: irm,
lltv: lltv,
};
}
catch (error) {
throw (0, errors_1.handleError)("Error fetching Morpho Vault Information", error);
}
};
exports.fetchMarketConfigFromContract = fetchMarketConfigFromContract;