@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
95 lines • 5.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMarketSnapshots = getMarketSnapshots;
const amount_js_1 = require("../../../common/amount.js");
const index_js_1 = require("../../../common/index.js");
const getMarketSnapshots_js_1 = require("../../core/markets/getMarketSnapshots.js");
const lunarIndexerTransform_js_1 = require("./lunarIndexerTransform.js");
async function getMarketSnapshots(client, args) {
const environment = (0, index_js_1.getEnvironmentFromArgs)(client, args);
if (!environment) {
throw new Error("Environment not found");
}
let { marketId, market } = args;
if (!marketId) {
marketId = environment.config.morphoMarkets[market].id;
}
const marketConfig = Object.values(environment.config.morphoMarkets).find((m) => m.id.toLowerCase() === marketId.toLowerCase());
if (!marketConfig) {
throw new Error(`Market ${marketId} not found in configuration`);
}
const loanToken = environment.config.tokens[marketConfig.loanToken];
const lunarIndexerUrl = environment.lunarIndexerUrl;
if (!lunarIndexerUrl) {
const coreSnapshots = await (0, getMarketSnapshots_js_1.fetchIsolatedMarketSnapshots)(marketId, environment, undefined, args.startTime, args.endTime);
const snapshots = coreSnapshots.map((s) => ({
timestamp: s.timestamp,
blockNumber: 0n,
totalSupplyAssets: new amount_js_1.Amount(BigInt(Math.round(s.totalSupply * 10 ** loanToken.decimals)), loanToken.decimals),
totalBorrowAssets: new amount_js_1.Amount(BigInt(Math.round(s.totalBorrows * 10 ** loanToken.decimals)), loanToken.decimals),
totalLiquidity: new amount_js_1.Amount(BigInt(Math.round(s.totalLiquidity * 10 ** loanToken.decimals)), loanToken.decimals),
totalReallocatableLiquidity: new amount_js_1.Amount(BigInt(Math.round(s.totalReallocatableLiquidity * 10 ** loanToken.decimals)), loanToken.decimals),
totalSupplyAssetsUsd: s.totalSupplyUsd,
totalBorrowAssetsUsd: s.totalBorrowsUsd,
totalLiquidityUsd: s.totalLiquidityUsd,
totalReallocatableLiquidityUsd: s.totalReallocatableLiquidityUsd,
loanTokenPrice: s.loanTokenPrice,
collateralTokenPrice: s.collateralTokenPrice,
supplyApy: s.baseSupplyApy,
borrowApy: s.baseBorrowApy,
lltv: 0,
}));
return { snapshots };
}
const options = {};
if (args.startTime !== undefined) {
options.startTime = args.startTime;
}
if (args.endTime !== undefined) {
options.endTime = args.endTime;
}
if (args.limit !== undefined) {
options.limit = args.limit;
}
if (args.cursor !== undefined) {
options.cursor = args.cursor;
}
if (args.granularity !== undefined) {
options.granularity = args.granularity;
}
const response = await (0, lunarIndexerTransform_js_1.fetchMarketSnapshotsFromIndexer)(lunarIndexerUrl, environment.chainId, marketId, options);
const snapshots = response.results.map((snapshot) => {
const totalSupplyAssets = new amount_js_1.Amount(BigInt(Math.floor(Number.parseFloat(snapshot.totalSupplyAssets) *
10 ** loanToken.decimals)), loanToken.decimals);
const totalBorrowAssets = new amount_js_1.Amount(BigInt(Math.floor(Number.parseFloat(snapshot.totalBorrowAssets) *
10 ** loanToken.decimals)), loanToken.decimals);
const totalLiquidity = new amount_js_1.Amount(BigInt(Math.floor(Number.parseFloat(snapshot.totalLiquidity) * 10 ** loanToken.decimals)), loanToken.decimals);
const totalReallocatableLiquidity = new amount_js_1.Amount(BigInt(Math.floor(Number.parseFloat(snapshot.totalReallocatableLiquidity ?? "0") *
10 ** loanToken.decimals)), loanToken.decimals);
return {
timestamp: snapshot.timestamp,
blockNumber: BigInt(snapshot.blockNumber),
totalSupplyAssets,
totalBorrowAssets,
totalLiquidity,
totalReallocatableLiquidity,
totalSupplyAssetsUsd: Number.parseFloat(snapshot.totalSupplyAssetsUsd),
totalBorrowAssetsUsd: Number.parseFloat(snapshot.totalBorrowAssetsUsd),
totalLiquidityUsd: Number.parseFloat(snapshot.totalLiquidityUsd),
totalReallocatableLiquidityUsd: Number.parseFloat(snapshot.totalReallocatableLiquidityUsd ?? "0"),
loanTokenPrice: Number.parseFloat(snapshot.loanTokenPrice),
collateralTokenPrice: Number.parseFloat(snapshot.collateralTokenPrice),
supplyApy: Number.parseFloat(snapshot.supplyApy),
borrowApy: Number.parseFloat(snapshot.borrowApy),
lltv: Number.parseFloat(snapshot.lltv),
};
});
const result = {
snapshots,
};
if (response.nextCursor !== undefined) {
result.nextCursor = response.nextCursor;
}
return result;
}
//# sourceMappingURL=getMarketSnapshots.js.map