UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

111 lines 4.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEnvironmentsFromArgs = exports.getEnvironmentFromArgs = exports.calculateApy = exports.perDay = exports.DAYS_PER_YEAR = exports.SECONDS_PER_DAY = exports.getBlockNumberAtTimestamp = exports.HttpRequestError = exports.BaseError = exports.Amount = void 0; exports.isStartOfDay = isStartOfDay; exports.applyGranularity = applyGranularity; exports.toApiGranularity = toApiGranularity; exports.calculateTimeRange = calculateTimeRange; const dayjs_1 = __importDefault(require("dayjs")); const utc_js_1 = __importDefault(require("dayjs/plugin/utc.js")); dayjs_1.default.extend(utc_js_1.default); var amount_js_1 = require("./amount.js"); Object.defineProperty(exports, "Amount", { enumerable: true, get: function () { return amount_js_1.Amount; } }); var error_js_1 = require("./error.js"); Object.defineProperty(exports, "BaseError", { enumerable: true, get: function () { return error_js_1.BaseError; } }); Object.defineProperty(exports, "HttpRequestError", { enumerable: true, get: function () { return error_js_1.HttpRequestError; } }); var getBlockNumberAtTimestamp_js_1 = require("./getBlockNumberAtTimestamp.js"); Object.defineProperty(exports, "getBlockNumberAtTimestamp", { enumerable: true, get: function () { return getBlockNumberAtTimestamp_js_1.getBlockNumberAtTimestamp; } }); exports.SECONDS_PER_DAY = 86400; exports.DAYS_PER_YEAR = 365; const perDay = (value) => value * exports.SECONDS_PER_DAY; exports.perDay = perDay; function isStartOfDay(timestamp) { const startOfDay = dayjs_1.default .utc(timestamp * 1000) .startOf("day") .unix(); return startOfDay === timestamp; } const calculateApy = (value) => ((value * exports.SECONDS_PER_DAY + 1) ** exports.DAYS_PER_YEAR - 1) * 100; exports.calculateApy = calculateApy; 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); } function toApiGranularity(granularity) { return granularity === "6h" ? "6h" : "1d"; } function calculateTimeRange(period, startTime, endTime) { const now = dayjs_1.default.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 }; } 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; }; exports.getEnvironmentFromArgs = getEnvironmentFromArgs; 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); }; exports.getEnvironmentsFromArgs = getEnvironmentsFromArgs; //# sourceMappingURL=index.js.map