@moonwell-fi/moonwell-sdk
Version:
TypeScript Interface for Moonwell
197 lines • 9.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchLiquidStakingRewards = exports.getMarketsData = void 0;
const viem_1 = require("viem");
const index_js_1 = require("../../../common/index.js");
const fetch_headers_js_1 = require("../../../common/fetch-headers.js");
const index_js_2 = require("../../../environments/index.js");
const index_js_3 = require("../../../environments/utils/index.js");
const getMarketsData = async (environment) => {
const homeEnvironment = Object.values(index_js_2.publicEnvironments).find((e) => e.custom?.governance?.chainIds?.includes(environment.chainId)) || environment;
const viewsContract = environment.contracts.views;
const homeViewsContract = homeEnvironment.contracts.views;
const marketData = await Promise.all([
viewsContract?.read.getProtocolInfo(),
viewsContract?.read.getAllMarketsInfo(),
homeViewsContract?.read.getNativeTokenPrice(),
homeViewsContract?.read.getGovernanceTokenPrice(),
]);
const { seizePaused, transferPaused } = marketData[0];
const allMarketsInfo = marketData[1];
const nativeTokenPriceRaw = marketData[2];
const governanceTokenPriceRaw = marketData[3];
const governanceTokenPrice = new index_js_1.Amount(governanceTokenPriceRaw, 18);
const nativeTokenPrice = new index_js_1.Amount(nativeTokenPriceRaw, 18);
const markets = [];
const tokenPrices = allMarketsInfo
.map((marketInfo) => {
const marketFound = (0, index_js_3.findMarketByAddress)(environment, marketInfo.market);
if (marketFound) {
return {
token: marketFound.underlyingToken,
tokenPrice: new index_js_1.Amount(marketInfo.underlyingPrice, 36 - marketFound.underlyingToken.decimals),
};
}
else {
return;
}
})
.filter((token) => !!token);
for (const marketInfo of allMarketsInfo) {
const marketFound = (0, index_js_3.findMarketByAddress)(environment, marketInfo.market);
if (marketFound) {
const { marketConfig, marketToken, underlyingToken, marketKey } = marketFound;
let badDebt = new index_js_1.Amount(0n, underlyingToken.decimals);
if (marketConfig.badDebt === true) {
try {
const badDebtResult = await environment.markets[marketKey]?.read.badDebt();
badDebt = new index_js_1.Amount(badDebtResult, underlyingToken.decimals);
}
catch (error) {
}
}
const supplyCaps = new index_js_1.Amount(marketInfo.supplyCap, underlyingToken.decimals);
const borrowCaps = new index_js_1.Amount(marketInfo.borrowCap, underlyingToken.decimals);
const collateralFactor = new index_js_1.Amount(marketInfo.collateralFactor, 18)
.value;
const underlyingPrice = new index_js_1.Amount(marketInfo.underlyingPrice, 36 - underlyingToken.decimals).value;
const marketTotalSupply = new index_js_1.Amount(marketInfo.totalSupply, marketToken.decimals);
const totalBorrows = new index_js_1.Amount(marketInfo.totalBorrows, underlyingToken.decimals);
const totalReserves = new index_js_1.Amount(marketInfo.totalReserves, underlyingToken.decimals);
const cash = new index_js_1.Amount(marketInfo.cash, underlyingToken.decimals);
const exchangeRate = new index_js_1.Amount(marketInfo.exchangeRate, 10 + underlyingToken.decimals).value;
const reserveFactor = new index_js_1.Amount(marketInfo.reserveFactor, 18).value;
const borrowRate = new index_js_1.Amount(marketInfo.borrowRate, 18);
const supplyRate = new index_js_1.Amount(marketInfo.supplyRate, 18);
const totalSupply = new index_js_1.Amount(marketTotalSupply.value * exchangeRate, underlyingToken.decimals);
const badDebtUsd = badDebt.value * underlyingPrice;
const totalSupplyUsd = totalSupply.value * underlyingPrice;
const totalBorrowsUsd = totalBorrows.value * underlyingPrice;
const totalReservesUsd = totalReserves.value * underlyingPrice;
const supplyCapsUsd = supplyCaps.value * underlyingPrice;
const borrowCapsUsd = borrowCaps.value * underlyingPrice;
const baseSupplyApy = (0, index_js_1.calculateApy)(supplyRate.value);
const baseBorrowApy = (0, index_js_1.calculateApy)(borrowRate.value);
const market = {
marketKey,
chainId: environment.chainId,
seizePaused,
transferPaused,
mintPaused: marketInfo.mintPaused,
borrowPaused: marketInfo.borrowPaused,
deprecated: marketConfig.deprecated === true,
borrowCaps,
borrowCapsUsd,
cash,
collateralFactor,
exchangeRate,
marketToken,
reserveFactor,
supplyCaps,
supplyCapsUsd,
badDebt,
badDebtUsd,
totalBorrows,
totalBorrowsUsd,
totalReserves,
totalReservesUsd,
totalSupply,
totalSupplyUsd,
underlyingPrice,
underlyingToken,
baseBorrowApy,
baseSupplyApy,
totalBorrowApr: 0,
totalSupplyApr: 0,
rewards: [],
};
for (const incentive of marketInfo.incentives) {
let { borrowIncentivesPerSec, supplyIncentivesPerSec, token: tokenAddress, } = incentive;
const token = (0, index_js_3.findTokenByAddress)(environment, tokenAddress);
if (token) {
const isGovernanceToken = token.symbol === environment.custom?.governance?.token;
const isNativeToken = token.address === viem_1.zeroAddress;
const tokenPrice = tokenPrices.find((r) => r?.token.address === incentive.token)?.tokenPrice.value;
const price = isNativeToken
? nativeTokenPrice.value
: isGovernanceToken
? governanceTokenPrice.value
: tokenPrice;
if (price) {
if (token.symbol === "USDC" && borrowIncentivesPerSec === 1n) {
borrowIncentivesPerSec = 0n;
}
const supplyRewardsPerDayUsd = (0, index_js_1.perDay)(new index_js_1.Amount(supplyIncentivesPerSec, token.decimals).value) *
price;
const borrowRewardsPerDayUsd = (0, index_js_1.perDay)(new index_js_1.Amount(borrowIncentivesPerSec, token.decimals).value) *
price;
const supplyApr = totalSupplyUsd === 0
? 0
: (supplyRewardsPerDayUsd / totalSupplyUsd) *
index_js_1.DAYS_PER_YEAR *
100;
const borrowApr = totalBorrowsUsd === 0
? 0
: (borrowRewardsPerDayUsd / totalBorrowsUsd) *
index_js_1.DAYS_PER_YEAR *
100 *
-1;
market.rewards.push({
liquidStakingApr: 0,
borrowApr,
supplyApr,
token,
});
}
}
}
market.totalSupplyApr = market.rewards.reduce((prev, curr) => prev + curr.supplyApr, market.baseSupplyApy);
market.totalBorrowApr = market.rewards.reduce((prev, curr) => prev + curr.borrowApr, market.baseBorrowApy);
markets.push(market);
}
}
return markets;
};
exports.getMarketsData = getMarketsData;
const fetchFromGenericCacheApi = async (uri) => {
const response = await fetch("https://generic-api-cache.moonwell.workers.dev/", {
method: "POST",
body: `{"uri":"${uri}","cacheDuration":"300"}`,
headers: {
...fetch_headers_js_1.MOONWELL_FETCH_JSON_HEADERS,
"Content-Type": "text/plain",
},
});
return response.json();
};
const fetchLiquidStakingRewards = async () => {
const result = {
cbETH: 0,
rETH: 0,
wstETH: 0,
};
try {
const cbETH = await fetchFromGenericCacheApi("https://api.exchange.coinbase.com/wrapped-assets/CBETH");
result.cbETH = Number(cbETH.apy) * 100;
}
catch (error) {
result.cbETH = 0;
}
try {
const rETH = await fetchFromGenericCacheApi("https://rocketpool.net/api/mainnet/payload");
result.rETH = Number(rETH.rethAPR);
}
catch (error) {
result.rETH = 0;
}
try {
const stETH = await fetchFromGenericCacheApi("https://eth-api.lido.fi/v1/protocol/steth/apr/last");
result.wstETH = stETH.data.apr;
}
catch (error) {
result.wstETH = 0;
}
return result;
};
exports.fetchLiquidStakingRewards = fetchLiquidStakingRewards;
//# sourceMappingURL=common.js.map