tensaikit
Version:
An autonomous DeFi AI Agent Kit on Katana enabling AI agents to plan and execute on-chain financial operations.
54 lines (53 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchUserDataByAddress = void 0;
const errors_1 = require("../../../common/errors");
const utils_1 = require("../../../common/utils");
const morphoAPIQuery_1 = require("../query/morphoAPIQuery");
const utils_2 = require("../utils");
/**
* Fetches Morpho Blue user data (positions, vaults, and transactions) by wallet address.
*
* This function queries the Morpho subgraph for a user's detailed activity and positions
* using the connected wallet's address on the supported chain/network.
*
* It retrieves:
* - Market positions (supply, borrow, collateral, PnL)
* - Vault positions (assets, shares, USD value)
* - Transaction history
*
* @param walletProvider - The connected EVM wallet provider instance.
*
* @returns A user object from the subgraph including positions and transactions,
* or `null` if the user has no data or address is not found.
*
* @throws Will throw an error if:
* - Network/chainId is missing or invalid.
* - The network is not supported by the Morpho subgraph.
* - The subgraph query fails or returns invalid data.
*/
const fetchUserDataByAddress = async (walletProvider) => {
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_2.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_1.makeSubgraphQueryCall)(utils_2.MORPHO_GRAPH_API_URL, (0, morphoAPIQuery_1.queryUserDataByAddress)({
chainId: Number(chainId),
address: walletProvider.getAddress(),
}));
if (!response || !response.userByAddress) {
return null;
}
return response.userByAddress;
}
catch (error) {
throw (0, errors_1.handleError)("Failed to fetch user data from Morpho subgraph", error);
}
};
exports.fetchUserDataByAddress = fetchUserDataByAddress;