UNPKG

@moonwell-fi/moonwell-sdk

Version:

TypeScript Interface for Moonwell

71 lines 2.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserPositionSnapshots = getUserPositionSnapshots; const axios_1 = __importDefault(require("axios")); const dayjs_1 = __importDefault(require("dayjs")); const utc_js_1 = __importDefault(require("dayjs/plugin/utc.js")); const index_js_1 = require("../../../common/index.js"); dayjs_1.default.extend(utc_js_1.default); async function getUserPositionSnapshots(client, args) { const environment = (0, index_js_1.getEnvironmentFromArgs)(client, args); if (!environment) { return []; } return fetchUserPositionSnapshots(args.userAddress, environment); } async function fetchUserPositionSnapshots(userAddress, environment) { const dailyData = []; let hasNextPage = true; let endCursor; while (hasNextPage) { const result = await axios_1.default.post(environment.indexerUrl, { query: ` query { accountDailySnapshots( limit: 365, orderDirection: "desc", orderBy: "timestamp", where: { accountAddress: "${userAddress.toLowerCase()}", chainId: ${environment.chainId} } ${endCursor ? `after: "${endCursor}"` : ""} ) { items { timestamp, totalBorrowsUSD, totalSuppliesUSD, totalCollateralUSD, } pageInfo { hasNextPage endCursor } } } `, }); dailyData.push(...result.data.data.accountDailySnapshots.items.filter((f) => (0, index_js_1.isStartOfDay)(f.timestamp))); hasNextPage = result.data.data.accountDailySnapshots.pageInfo.hasNextPage; endCursor = result.data.data.accountDailySnapshots.pageInfo.endCursor; } if (dailyData.length > 0) { return dailyData.map((point) => { const borrowUsd = Number(point.totalBorrowsUSD); const suppliedUsd = Number(point.totalSuppliesUSD); const collateralUsd = Number(point.totalCollateralUSD); const result = { chainId: environment.chainId, timestamp: point.timestamp * 1000, totalSupplyUsd: suppliedUsd, totalBorrowsUsd: borrowUsd, totalCollateralUsd: collateralUsd, }; return result; }); } else { return []; } } //# sourceMappingURL=getUserPositionSnapshots.js.map