UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

108 lines 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCirculatingSupplySnapshots = getCirculatingSupplySnapshots; const index_js_1 = require("../../common/index.js"); const axiosWithRetry_js_1 = require("../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 (0, axiosWithRetry_js_1.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; } async function getCirculatingSupplySnapshots(client, args) { const environment = (0, index_js_1.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 (0, axiosWithRetry_js_1.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