UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

116 lines 4.15 kB
import dayjs from "dayjs"; import utc from "dayjs/plugin/utc.js"; dayjs.extend(utc); export { Amount } from "./amount.js"; export { BaseError, HttpRequestError } from "./error.js"; export { getBlockNumberAtTimestamp } from "./getBlockNumberAtTimestamp.js"; export const SECONDS_PER_DAY = 86400; export const DAYS_PER_YEAR = 365; export const perDay = (value) => value * SECONDS_PER_DAY; export function isStartOfDay(timestamp) { const startOfDay = dayjs .utc(timestamp * 1000) .startOf("day") .unix(); return startOfDay === timestamp; } export const calculateApy = (value) => ((value * SECONDS_PER_DAY + 1) ** DAYS_PER_YEAR - 1) * 100; /** * Filter snapshots to keep every Nth data point based on granularity. * "6h" and "1d" are handled natively by the API — no client-side filtering applied. * Coarser granularities (7d, 14d, 30d) fetch "1d" from the API and thin here. * * Callers must sort snapshots ascending by timestamp before calling this function. * Always keeps index 0, N, 2N, … so the oldest data point is always retained. */ export function applyGranularity(snapshots, granularity) { const step = { "6h": 1, "1d": 1, "7d": 7, "14d": 14, "30d": 30, }; const n = step[granularity]; if (n <= 1) return snapshots; return snapshots.filter((_, i) => i % n === 0); } /** * Map a SnapshotGranularity to the value passed to the lunar indexer API. * "6h" is natively supported; coarser granularities (7d, 14d, 30d) are not, * so we fetch "1d" and thin client-side via applyGranularity. */ export function toApiGranularity(granularity) { return granularity === "6h" ? "6h" : "1d"; } /** * Calculate start/end times and display granularity based on period or custom timestamps. * Priority: custom timestamps > period > default (365 days) * * Granularity per period: * 1M → 6h (4 data points per day, API-native) * 3M → 1d (one data point per day) * 1Y → 7d (one data point every 7 days, client-side filtered) * ALL → 14d (one data point every 14 days, client-side filtered) */ export function calculateTimeRange(period, startTime, endTime) { const now = dayjs.utc(); const end = endTime ?? now.unix(); if (startTime !== undefined) { return { startTime, endTime: end, granularity: "1d" }; } let start; let granularity; switch (period) { case "1M": start = now.subtract(31, "days").unix(); granularity = "6h"; break; case "3M": start = now.subtract(91, "days").unix(); granularity = "1d"; break; case "1Y": start = now.subtract(366, "days").unix(); granularity = "7d"; break; case "ALL": start = now.subtract(3, "years").unix(); granularity = "14d"; break; case undefined: start = now.subtract(366, "days").unix(); granularity = "1d"; break; } return { startTime: start, endTime: end, granularity }; } export const getEnvironmentFromArgs = (client, args) => { if (args) { const { chainId, network } = args; if (chainId) { return Object.values(client.environments).find((env) => env.chainId === chainId); } if (network) { return client.environments[network]; } } return undefined; }; export const getEnvironmentsFromArgs = (client, args, onlyWithDeployment) => { const onlyEnvironmentsWithDeployment = onlyWithDeployment !== undefined ? onlyWithDeployment : true; if (args) { const { chainId, network } = args; if (Number.isInteger(chainId)) { return [ Object.values(client.environments).find((env) => env.chainId === chainId), ]; } if (network) { return [client.environments[network]]; } } return Object.values(client.environments).filter((r) => onlyEnvironmentsWithDeployment ? r.contracts.views !== undefined : true); }; //# sourceMappingURL=index.js.map