tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
61 lines (60 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchPositionInfoFromContract = void 0;
const errors_1 = require("../../../common/errors");
const utils_1 = require("../utils");
const morphoBlueABI_1 = require("../abi/morphoBlueABI");
/**
* Fetches the position information for the connected wallet in a specific Morpho Blue market.
*
* This function uses the provided EVM wallet provider to query the Morpho Blue contract
* for the current user's position in the specified market. It retrieves supply shares,
* borrow shares, and collateral balances associated with the wallet address.
*
* @param walletProvider - An instance of {@link EvmWalletProvider} connected to the user's wallet.
* @param args - An object validated by {@link GetMarketPositionInfoSchema}, 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 {bigint} supplyShares - The number of supply shares the wallet holds in the market.
* @property {bigint} borrowShares - The number of borrow shares the wallet has taken from the market.
* @property {bigint} collateral - The amount of collateral the wallet has deposited in the market.
*
* @throws Will throw an error if:
* - The wallet provider's network or chainId is invalid or not available.
* - The market ID is invalid or the contract returns no data.
* - Any unexpected error occurs while interacting with the contract.
*/
const fetchPositionInfoFromContract = 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: "position",
args: [args.marketId, walletProvider.getAddress()],
});
if (!marketResponse) {
throw (0, errors_1.createError)("Invalid market id or missing market information", errors_1.ErrorCode.INVALID_INPUT);
}
const supplyShares = marketResponse[0];
const borrowShares = marketResponse[1];
const collateral = marketResponse[2];
return {
marketId: args.marketId,
supplyShares: supplyShares,
borrowShares: borrowShares,
collateral: collateral,
};
}
catch (error) {
throw (0, errors_1.handleError)("Error fetching Morpho Vault Positions", error);
}
};
exports.fetchPositionInfoFromContract = fetchPositionInfoFromContract;