UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

105 lines 3.71 kB
import { getEnvironmentFromArgs } from "../../common/index.js"; import { getWithRetry, postWithRetry } from "../axiosWithRetry.js"; async function fetchCirculatingSupplyFromLunar(lunarIndexerUrl, chainId) { const all = []; let cursor = null; const MAX_PAGES = 10; let page = 0; do { const params = { limit: "1000" }; if (cursor) { params.cursor = cursor; } const response = await getWithRetry(`${lunarIndexerUrl}/api/v1/staking/circulating-supply/${chainId}`, { params, }); all.push(...response.data.results); cursor = response.data.nextCursor; page++; } while (cursor !== null && page < MAX_PAGES); return all; } export async function getCirculatingSupplySnapshots(client, args) { const environment = getEnvironmentFromArgs(client, args); if (!environment) { return []; } if (!environment.lunarIndexerUrl) { return environment.indexerUrl ? fetchCirculatingSupplyFromPonder(environment) : []; } try { const items = await fetchCirculatingSupplyFromLunar(environment.lunarIndexerUrl, environment.chainId); return items.flatMap((item) => { const token = Object.values(environment.config.tokens).find((t) => t.address.toLowerCase() === item.tokenAddress.toLowerCase()); if (!token) return []; return [ { chainId: item.chainId, token, circulatingSupply: Number.parseFloat(item.circulatingSupply), totalSupply: item.totalSupply, excludedBalance: item.excludedBalance, timestamp: item.timestamp, }, ]; }); } catch (error) { console.warn(`[getCirculatingSupplySnapshots] Lunar Indexer failed for chain ${environment.chainId}:`, error); environment.onError?.(error, { source: "circulating-supply", chainId: environment.chainId, }); return []; } } async function fetchCirculatingSupplyFromPonder(environment) { if (!environment.indexerUrl) return []; try { const response = await postWithRetry(environment.indexerUrl, { query: ` { circulatingSupplyDailySnapshots( where: { chainId: ${environment.chainId} } orderBy: "timestamp" orderDirection: "desc" limit: 1000 ) { items { chainId tokenAddress circulatingSupply timestamp } } } `, }); if (response.status === 200 && response.data?.data?.circulatingSupplyDailySnapshots) { return response.data.data.circulatingSupplyDailySnapshots.items.flatMap((item) => { const token = Object.values(environment.config.tokens).find((t) => t.address.toLowerCase() === item.tokenAddress.toLowerCase()); if (!token) return []; return [ { chainId: item.chainId, token, circulatingSupply: item.circulatingSupply, timestamp: item.timestamp, }, ]; }); } return []; } catch (ex) { console.error("An error occurred while fetching getCirculatingSupplySnapshots...", ex); return []; } } //# sourceMappingURL=getCirculatingSupplySnapshots.js.map