UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

93 lines 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStakingSnapshots = getStakingSnapshots; 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 getStakingSnapshots(client, args) { const environment = (0, index_js_1.getEnvironmentFromArgs)(client, args); if (!environment) { return []; } const { period, startTime: customStartTime, endTime: customEndTime, } = (args ?? {}); if (!environment.lunarIndexerUrl) { const { startTime } = (0, index_js_1.calculateTimeRange)(period, customStartTime, customEndTime); return environment.indexerUrl ? fetchStakingSnapshotsFromPonder(environment.chainId, environment.indexerUrl, startTime) : []; } try { return await fetchStakingSnapshotsFromLunar(environment.chainId, environment.lunarIndexerUrl, period, customStartTime, customEndTime); } catch (error) { console.warn(`[getStakingSnapshots] Lunar Indexer failed for chain ${environment.chainId}:`, error); environment.onError?.(error, { source: "staking-snapshots", chainId: environment.chainId, }); return []; } } async function fetchStakingSnapshotsFromLunar(chainId, lunarIndexerUrl, period, customStartTime, customEndTime) { const lunarClient = (0, lunar_indexer_client_js_1.createLunarIndexerClient)({ baseUrl: lunarIndexerUrl, timeout: lunar_indexer_client_js_1.DEFAULT_LUNAR_TIMEOUT_MS, }); const { startTime, endTime, granularity } = (0, index_js_1.calculateTimeRange)(period, customStartTime, customEndTime); const allSnapshots = []; let cursor = null; const MAX_PAGES = 100; let page = 0; do { const response = await lunarClient.getStakingSnapshots(chainId, { limit: 1000, granularity: (0, index_js_1.toApiGranularity)(granularity), startTime, endTime, ...(cursor && { cursor }), }); allSnapshots.push(...(0, lunar_indexer_transformers_js_1.transformStakingSnapshots)(response.results)); cursor = response.nextCursor; page++; } while (cursor !== null && page < MAX_PAGES); allSnapshots.sort((a, b) => a.timestamp - b.timestamp); return (0, index_js_1.applyGranularity)(allSnapshots, granularity); } async function fetchStakingSnapshotsFromPonder(chainId, indexerUrl, startTime) { try { const response = await (0, axiosWithRetry_js_1.postWithRetry)(indexerUrl, { query: ` query { stakingDailySnapshots( limit: 365, orderBy: "timestamp" orderDirection: "desc" where: {chainId: ${chainId}} ) { items { chainId totalStaked totalStakedUSD timestamp } } } `, }); if (response.status === 200 && response.data?.data?.stakingDailySnapshots) { const items = response.data.data.stakingDailySnapshots.items; return startTime ? items.filter((item) => item.timestamp >= startTime) : items; } else { return []; } } catch (ex) { console.error("An error occured while fetching getStakingSnapshots...", ex); return []; } } //# sourceMappingURL=getStakingSnapshots.js.map