UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

113 lines 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserPositionSnapshots = getUserPositionSnapshots; const index_js_1 = require("../../../common/index.js"); const axiosWithRetry_js_1 = require("../../axiosWithRetry.js"); const lunar_indexer_client_js_1 = require("../../lunar-indexer-client.js"); const lunar_indexer_transformers_js_1 = require("../../lunar-indexer-transformers.js"); async function getUserPositionSnapshots(client, args) { const environment = (0, index_js_1.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 = (0, lunar_indexer_client_js_1.createLunarIndexerClient)({ baseUrl: environment.lunarIndexerUrl, timeout: lunar_indexer_client_js_1.DEFAULT_LUNAR_TIMEOUT_MS, }); const { startTime, endTime, granularity: derivedGranularity, } = (0, index_js_1.calculateTimeRange)(period, customStartTime, customEndTime); const resolvedGranularity = granularity ?? derivedGranularity; const portfolio = await client.getAccountPortfolio(userAddress.toLowerCase(), { startTime, endTime, granularity: (0, index_js_1.toApiGranularity)(resolvedGranularity), chainId: environment.chainId, }); const snapshots = (0, lunar_indexer_transformers_js_1.transformPortfolioToSnapshots)(portfolio, environment.chainId).sort((a, b) => a.timestamp - b.timestamp); const firstNonZeroIndex = snapshots.findIndex((snapshot) => snapshot.totalSupplyUsd > 0 || snapshot.totalBorrowsUsd > 0 || snapshot.totalCollateralUsd > 0); if (firstNonZeroIndex === -1) { return []; } return (0, index_js_1.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 (0, axiosWithRetry_js_1.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) => (0, index_js_1.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