Skip to content

Commit

Permalink
add get specific asset tier token info List
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCMars committed Dec 21, 2023
1 parent 805a69a commit 3645e65
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import express, { Express, NextFunction, Request, Response } from 'express';
import { getTokenContract } from './helpers/contract-getter';
import { PROVIDER } from './constant';
import { Mainnet } from './scripts/markets/mainnet';
import { dashboardGetTokenDeposit, dashboardGetTLV, getDashTokenInfo } from './helpers/Dashboard/getData';
import { dashboardGetTokenDeposit, dashboardGetTLV, getDashTokenInfo, getTierTokenListInfo } from './helpers/Dashboard/getData';
import { getTotalCRT } from './helpers/Dashboard/getTotalCRT';
import { getAssetInfo } from './helpers/Asset/getAssetInfo';
import { getUserInfo } from './helpers/User/getUserInfo';
Expand Down Expand Up @@ -56,6 +56,8 @@ app.get("/dashboard/tvlAmount", (req: Request, res: Response) => dashboardGetTLV

app.get("/dashboard/crtAmount", (req: Request, res: Response) => getTotalCRT(req, res));

app.get("/market/:assetTier", (req: Request, res: Response) => getTierTokenListInfo(req, res));

app.get("/dashboard/tokenInfo/:tokenSymbol/:assetTier", (req: Request, res: Response) => getDashTokenInfo(req, res));

app.get("/assetPage/assetInfo/:tokenSymbol/:assetTier", (req: Request, res: Response) => getAssetInfo(req, res));
Expand Down
55 changes: 47 additions & 8 deletions helpers/Dashboard/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { BigNumber, ethers, Contract } from "ethers";
import { constructTokenRiskName } from "../utils";
import { Mainnet } from '../../scripts/markets/mainnet';
import express, { Express, Request, Response } from 'express';

import exp from "constants";

import { getAssetTierInfo } from "./getTierAsset";
export async function calAssetTotalDeposit(asset: string, highrisk: number) {
let token = await getTokenContract(asset);
let tokenTotalDeposit = BigNumber.from(0);
Expand All @@ -19,6 +17,22 @@ export async function calAssetTotalDeposit(asset: string, highrisk: number) {
return tokenTotalDeposit
}

export const calDebtTotalBorrow = async (symbol: string, highrisk: number) => {
let token = await getTokenContract(symbol);
let tokenTotalDebt = BigNumber.from(0);
let counter = await getCounter();

for (let risk = 2; risk >= highrisk; risk--) {
let token_risk = constructTokenRiskName(symbol, risk);
let reserveInfo = await counter.getReserveData(token.address, risk);
let pToken_Tier_debt = await getVariableDebtToken(reserveInfo.variableDebtTokenAddress);
let pToken_Tier_debtSupply = await pToken_Tier_debt.totalSupply();

tokenTotalDebt = tokenTotalDebt.add(pToken_Tier_debtSupply);
}
return tokenTotalDebt;
}

async function calculateTotalAssetUSD(asset: Contract, assetAmount: BigNumber) {
let priceOrcale = await getPrestareOracle();
let oracle_dicimals = ethers.utils.parseUnits("1", 8);
Expand All @@ -41,6 +55,13 @@ export const getTokenTotalAssetUSD = async function (asset: string, highrisk: nu
return totalAmount;
}

export const getdebtTotalAssetUSD = async (asset: string, highrisk: number) => {
let tokenContract = await getTokenContract(asset);
let tokenBorrow = await calDebtTotalBorrow(asset, highrisk);
let totalBorrowAmount = await calculateTotalAssetUSD(tokenContract, tokenBorrow);
return totalBorrowAmount;
}

export const dashboardGetTokenDeposit = async (req: Request, res: Response) => {
let params = req.params;
let symbol: string = params.tokenSymbol;
Expand All @@ -52,9 +73,13 @@ export const dashboardGetTokenDeposit = async (req: Request, res: Response) => {
for (let [asset, highrisk] of Object.entries(Mainnet.AssetTier)) {
// console.log(asset);
if (asset == symbol) {
let totalAmount = await getTokenTotalAssetUSD(asset, highrisk);
console.log("total:", totalAmount);
res.json({ "totalAmount": totalAmount.toNumber()});
let totalSupplyAmount = await getTokenTotalAssetUSD(asset, highrisk);
console.log("total:", totalSupplyAmount);
let totalBorrowAmount = await getdebtTotalAssetUSD(asset, highrisk);
res.json({
"totalSupplyAmount": totalSupplyAmount.toNumber(),
"totalBorrowAmount": totalBorrowAmount.toNumber()
});
}
}
}
Expand All @@ -76,7 +101,7 @@ export const dashboardGetTLV = async (req: Request, res: Response) => {
res.json({"totalTVL": totalTVL});
}

export const getDashTokenInfo =async (req: Request, res: Response) => {
export const getDashTokenInfo = async (req: Request, res: Response) => {
let params = req.params;
let symbol: string = params.tokenSymbol;
let assetTier: string = params.assetTier;
Expand All @@ -94,7 +119,7 @@ export const getDashTokenInfo =async (req: Request, res: Response) => {

let pToken_assetTier = await getPToken(reserveInfo.pTokenAddress);
let pToken_Supply = await pToken_assetTier.totalSupply();
console.log("pUSDC_C total Supply is", pToken_Supply);
console.log("pToken_Supply total Supply is", pToken_Supply);
let pToken_Tier_debt = await getVariableDebtToken(reserveInfo.variableDebtTokenAddress);
let pToken_Tier_debtSupply = await pToken_Tier_debt.totalSupply();

Expand All @@ -107,3 +132,17 @@ export const getDashTokenInfo =async (req: Request, res: Response) => {
res.json(info);
}

export const getTierTokenListInfo = async (req: Request, res: Response) => {
let params = req.params;
let assetTier: number = Number(params.assetTier);
let assetTierInfoList: { [key: string]: any }[] = [];
for (let [asset, highrisk] of Object.entries(Mainnet.AssetTier)) {
console.log(highrisk);
if (assetTier >= highrisk) {
console.log(asset);
let assetInfo = await getAssetTierInfo(asset, assetTier);
assetTierInfoList.push(assetInfo);
}
}
res.json({assetTierInfoList});
}
39 changes: 39 additions & 0 deletions helpers/Dashboard/getTierAsset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getPTokenContract, getTokenContract, getPrestareOracle, getCounter, getPToken, getVariableDebtToken } from "../contract-getter";
import { oneRay } from "../constants";
import { constructTokenRiskName } from "../utils";

export const getAssetTierInfo = async (symbol: string, assetTier: number) => {
console.log("get %{symbol} Info");
let token = await getTokenContract(symbol);
let tokenAssetSym = constructTokenRiskName(symbol ,assetTier)
let counter = await getCounter();
let reserveInfo = await counter.getReserveData(token.address, assetTier);
console.log(reserveInfo);
let supplyIR = reserveInfo.currentLiquidityRate.mul(10000).div(oneRay);
let borrowIR = reserveInfo.currentVariableBorrowRate.mul(10000).div(oneRay);

let pToken_assetTier = await getPToken(reserveInfo.pTokenAddress);
let pTokenName = await pToken_assetTier.symbol();
let pToken_Supply = await pToken_assetTier.totalSupply();
let pTokenDeci = await pToken_assetTier.decimals();
let supply = {
"Amount": pToken_Supply,
"Decimal": pTokenDeci
}
console.log("pToken_Supply total Supply is", pToken_Supply);
let pToken_Tier_debt = await getVariableDebtToken(reserveInfo.variableDebtTokenAddress);
let pToken_Tier_debtSupply = await pToken_Tier_debt.totalSupply();
let pToken_Tier_debtDeci = await pToken_Tier_debt.decimals();
let debt = {
"Amount": pToken_Tier_debtSupply,
"Decimal": pToken_Tier_debtDeci
}
let assetInfo = {
"Name": tokenAssetSym,
"SupplyAPY": supplyIR,
"BorrowAPY": borrowIR,
"TotalSupply": supply,
"TotalBorrow": debt
}
return assetInfo
}

0 comments on commit 3645e65

Please sign in to comment.