@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
65 lines • 2.55 kB
JavaScript
import { fetchAccountMarketPortfolioFromIndexer } from "./lunarIndexerTransform.js";
export async function getAccountMarketPortfolio(client, args) {
// Determine which environment to use
let environment;
// Get all environments as an array
const environments = Object.values(client.environments);
if (args.chainId) {
environment = environments.find((env) => env.chainId === args.chainId);
}
else if (args.network && typeof args.network === "string") {
const envValue = client.environments[args.network];
if (envValue) {
environment = envValue;
}
}
else {
// Default to first environment with lunar indexer
environment = environments.find((env) => env.lunarIndexerUrl);
}
if (!environment) {
throw new Error("Environment not found");
}
const lunarIndexerUrl = environment.lunarIndexerUrl;
if (!lunarIndexerUrl) {
throw new Error("Lunar Indexer URL not configured for this environment. Account market portfolio requires lunar-indexer.");
}
// Build options object with only defined values
const options = {
startTime: args.startTime,
endTime: args.endTime,
};
if (args.chainId !== undefined) {
options.chainId = args.chainId;
}
if (args.marketId !== undefined) {
options.marketId = args.marketId;
}
if (args.granularity !== undefined) {
options.granularity = args.granularity;
}
const response = await fetchAccountMarketPortfolioFromIndexer(lunarIndexerUrl, args.accountAddress, options);
// Transform positions to include marketKey
const { morphoMarkets } = environment.config;
const positions = response.positions.map((position) => ({
timestamp: position.timestamp,
markets: position.markets.map((market) => {
// Find the market key from environment config
const marketKey = Object.keys(morphoMarkets).find((key) => morphoMarkets[key].id.toLowerCase() ===
market.marketId.toLowerCase());
return {
chainId: market.chainId,
marketId: market.marketId,
marketKey: marketKey || market.marketId,
supplyShares: market.supplyShares,
borrowShares: market.borrowShares,
collateral: market.collateral,
};
}),
}));
return {
account: response.account,
positions,
};
}
//# sourceMappingURL=getAccountMarketPortfolio.js.map