From 698ca68829f973effa53f42abbaeb17840b4680b Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Wed, 5 Jun 2024 14:25:18 +0500 Subject: [PATCH] chore: use decimal places with token values --- src/pages/InterProtocol.tsx | 69 ++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/src/pages/InterProtocol.tsx b/src/pages/InterProtocol.tsx index b6f8928..92970e6 100644 --- a/src/pages/InterProtocol.tsx +++ b/src/pages/InterProtocol.tsx @@ -7,16 +7,21 @@ import { ValueCardGrid } from '@/components/ValueCardGrid'; import { PageHeader } from '@/components/PageHeader'; import { PageContent } from '@/components/PageContent'; import { colors } from '@/components/palette'; -import { formatPercent, roundPrice, formatPrice, formatIST, subQueryFetcher, fetchDataFromUrl } from '@/utils'; -import { INTER_DASHBOARD_QUERY } from '@/queries'; import { - GET_INTERCHAIN_BALANCES_URL, -} from '@/constants'; + formatPercent, + roundPrice, + formatPrice, + formatIST, + subQueryFetcher, + fetchDataFromUrl, + createNumberWithLeadingZeroes, +} from '@/utils'; +import { INTER_DASHBOARD_QUERY } from '@/queries'; +import { GET_INTERCHAIN_BALANCES_URL } from '@/constants'; import InterProtocolSkeleton from '@/components/InterProtocolSkeleton'; import { AccountData, InterProtocolResponse } from '@/types/interprotocol-types'; import { OraclePriceNode } from '@/types/vault-types'; - const RADIAN = Math.PI / 180; const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent, payload }: any) => { @@ -36,7 +41,6 @@ const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, per ); }; - function getInterchainBalance(data: AccountData[]): number { const balanceList = data .map((account) => ({ @@ -81,19 +85,36 @@ export function InterProtocol() { {}, ); - const psmMinted = - dashboardResponse.psmMetrics.nodes.reduce((agg, node) => agg + Number(node.mintedPoolBalance), 0) / 1_000_000; - const psmAnchor = dashboardResponse.psmMetrics.nodes.reduce( - (agg, node) => agg + Number(node.anchorPoolBalance) / 10 ** (boardAuxes[node.denom] || 6), - 0, - ); - const vaultMinted = - dashboardResponse.vaultManagerMetrics.nodes.reduce((agg, node) => agg + Number(node.totalDebt), 0) / 1_000_000; - const totalMinted = psmMinted + vaultMinted; + const psmMinted = dashboardResponse.psmMetrics.nodes.reduce((agg, node) => { + const decimalPlaces = (boardAuxes && boardAuxes[node.denom]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + return (agg + Number(node.mintedPoolBalance)) / tokenDivisor; + }, 0); + const psmAnchor = dashboardResponse.psmMetrics.nodes.reduce((agg, node) => { + const decimalPlaces = (boardAuxes && boardAuxes[node.denom]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + return (agg + Number(node.anchorPoolBalance)) / 10 ** tokenDivisor; + }, 0); + + const vaultMinted = dashboardResponse.vaultManagerMetrics.nodes.reduce((agg, node) => { + const decimalPlaces = (boardAuxes && boardAuxes[node.liquidatingCollateralBrand]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + return agg + Number(node.totalDebt) / tokenDivisor; + }, 0); + + const totalMinted = psmMinted + vaultMinted; + const ISTDecimalPlaces = createNumberWithLeadingZeroes((boardAuxes && boardAuxes['IST']) || 6); const vaultMintLimit = - dashboardResponse.vaultManagerGovernances.nodes.reduce((agg, node) => agg + Number(node.debtLimit), 0) / 1_000_000; - const psmMintLimit = dashboardResponse.psmGovernances.nodes.reduce((agg, node) => agg + Number(node.mintLimit), 0) / 1_000_000; + dashboardResponse.vaultManagerGovernances.nodes.reduce((agg, node) => agg + Number(node.debtLimit), 0) / + ISTDecimalPlaces; + + const psmMintLimit = dashboardResponse.psmGovernances.nodes.reduce((agg, node) => { + const decimalPlaces = (boardAuxes && boardAuxes[node.denom]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + return (agg + Number(node.mintLimit)) / tokenDivisor; + }, 0); + const totalMintLimit = vaultMintLimit + psmMintLimit; // bottom cards @@ -101,18 +122,24 @@ export function InterProtocol() { (agg, node) => agg + node.allocations.nodes.reduce((agg_, node_) => { + const decimalPlaces = (boardAuxes && boardAuxes[node_.denom]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); const allocationInUsd = - ((Number(node_.value) / 1_000_000) * Number(oraclePrices[node_.denom]?.typeOutAmount || 1_000_000)) / 1_000_000; + ((Number(node_.value) / tokenDivisor) * Number(oraclePrices[node_.denom]?.typeOutAmount || 0)) / tokenDivisor; return agg_ + allocationInUsd; }, 0), 0, ); const reserveShortfall = - dashboardResponse.reserveMetrics.nodes.reduce((agg, node) => agg + Number(node.shortfallBalance), 0) / 1_000_000; + dashboardResponse.reserveMetrics.nodes.reduce((agg, node) => agg + Number(node.shortfallBalance), 0) / + ISTDecimalPlaces; const totalLockedCollateral = dashboardResponse.vaultManagerMetrics.nodes.reduce((agg, node) => { + const decimalPlaces = (boardAuxes && boardAuxes[node.liquidatingCollateralBrand]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + const collateralInUsd = - ((Number(node.totalCollateral) / 1_000_000) * - Number(oraclePrices[node.liquidatingCollateralBrand]?.typeOutAmount) || 0) / 1_000_000; + ((Number(node.totalCollateral) / tokenDivisor) * + Number(oraclePrices[node.liquidatingCollateralBrand]?.typeOutAmount) || 0) / tokenDivisor; return agg + collateralInUsd; }, 0);