UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

130 lines 5.89 kB
import { applyGranularity, calculateTimeRange, getEnvironmentFromArgs, isStartOfDay, toApiGranularity, } from "../../../common/index.js"; import { postWithRetry } from "../../axiosWithRetry.js"; import { DEFAULT_LUNAR_TIMEOUT_MS, createLunarIndexerClient, } from "../../lunar-indexer-client.js"; import { transformPortfolioToSnapshots } from "../../lunar-indexer-transformers.js"; /** * Get historical snapshots of a user's positions across all markets * * @param client - Moonwell client instance * @param args - Parameters including user address and optional time range * @param args.userAddress - The user's wallet address * @param args.period - Predefined time period: "1M" (31 days), "3M" (91 days), "1Y" (366 days), or "ALL" (all available history) * @param args.startTime - Custom start time (unix timestamp in seconds). Overrides period if both startTime and endTime are provided. * @param args.endTime - Custom end time (unix timestamp in seconds). Overrides period if both startTime and endTime are provided. * @param args.granularity - Data granularity: "6h" or "1d". Determines snapshot frequency. * * @returns Array of user position snapshots with USD values for supply, borrow, and collateral * * @remarks * - Default behavior (no time parameters): Returns 365 days of history * - Parameter priority: Custom timestamps > period > default (365 days) * - When using Lunar Indexer, custom time ranges are supported * - Snapshots are filtered to start-of-day for "1d" granularity */ export async function getUserPositionSnapshots(client, args) { const environment = getEnvironmentFromArgs(client, args); if (!environment) { return []; } return fetchUserPositionSnapshots(args.userAddress, environment, args.period, args.startTime, args.endTime, args.granularity); } async function fetchUserPositionSnapshots(userAddress, environment, period, startTime, endTime, granularity) { if (!environment.lunarIndexerUrl) { if (!environment.indexerUrl) return []; try { return await fetchUserPositionSnapshotsFromPonder(userAddress, environment); } catch (error) { console.warn(`[getUserPositionSnapshots] Ponder failed for chain ${environment.chainId}:`, error); environment.onError?.(error, { source: "user-position-snapshots-ponder", chainId: environment.chainId, }); return []; } } try { return await fetchUserPositionSnapshotsFromLunar(userAddress, environment, period, startTime, endTime, granularity); } catch (error) { console.warn(`[getUserPositionSnapshots] Lunar Indexer failed for chain ${environment.chainId}:`, error); environment.onError?.(error, { source: "user-position-snapshots", chainId: environment.chainId, }); return []; } } async function fetchUserPositionSnapshotsFromLunar(userAddress, environment, period, customStartTime, customEndTime, granularity) { if (!environment.lunarIndexerUrl) { throw new Error("Lunar Indexer URL not configured"); } const client = createLunarIndexerClient({ baseUrl: environment.lunarIndexerUrl, timeout: DEFAULT_LUNAR_TIMEOUT_MS, }); const { startTime, endTime, granularity: derivedGranularity, } = calculateTimeRange(period, customStartTime, customEndTime); const resolvedGranularity = granularity ?? derivedGranularity; const portfolio = await client.getAccountPortfolio(userAddress.toLowerCase(), { startTime, endTime, granularity: toApiGranularity(resolvedGranularity), chainId: environment.chainId, }); const snapshots = transformPortfolioToSnapshots(portfolio, environment.chainId).sort((a, b) => a.timestamp - b.timestamp); // Find the first snapshot where user has any position const firstNonZeroIndex = snapshots.findIndex((snapshot) => snapshot.totalSupplyUsd > 0 || snapshot.totalBorrowsUsd > 0 || snapshot.totalCollateralUsd > 0); if (firstNonZeroIndex === -1) { return []; } return applyGranularity(snapshots.slice(firstNonZeroIndex), resolvedGranularity); } async function fetchUserPositionSnapshotsFromPonder(userAddress, environment) { if (!environment.indexerUrl) return []; const dailyData = []; let hasNextPage = true; let endCursor; while (hasNextPage) { const result = await postWithRetry(environment.indexerUrl, { query: ` query { accountDailySnapshots( limit: 365, orderDirection: "desc", orderBy: "timestamp", where: { accountAddress: "${userAddress.toLowerCase()}", chainId: ${environment.chainId} } ${endCursor ? `after: "${endCursor}"` : ""} ) { items { timestamp, totalBorrowsUSD, totalSuppliesUSD, totalCollateralUSD, } pageInfo { hasNextPage endCursor } } } `, }); dailyData.push(...result.data.data.accountDailySnapshots.items.filter((f) => isStartOfDay(f.timestamp))); hasNextPage = result.data.data.accountDailySnapshots.pageInfo.hasNextPage; endCursor = result.data.data.accountDailySnapshots.pageInfo.endCursor; } if (dailyData.length === 0) return []; return dailyData.map((point) => ({ chainId: environment.chainId, timestamp: point.timestamp * 1000, totalSupplyUsd: Number(point.totalSuppliesUSD), totalBorrowsUsd: Number(point.totalBorrowsUSD), totalCollateralUsd: Number(point.totalCollateralUSD), })); } //# sourceMappingURL=getUserPositionSnapshots.js.map