From 6b2bc811fd372b1c1bb2b90addfb7620098b6f99 Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Wed, 5 Jun 2024 16:34:37 +0500 Subject: [PATCH] chore: use decimal places on reserve page --- src/pages/Reserve.tsx | 7 ++++++- src/queries.ts | 6 ++++++ src/types/reserve-types.ts | 2 ++ src/utils.tsx | 6 ++++++ src/widgets/ReserveCosmosSummary.tsx | 15 ++++++++++++--- src/widgets/ReserveSummary.tsx | 9 ++++++--- 6 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/pages/Reserve.tsx b/src/pages/Reserve.tsx index fdf84c0..232ab15 100644 --- a/src/pages/Reserve.tsx +++ b/src/pages/Reserve.tsx @@ -23,6 +23,10 @@ export const Reserve = () => { const oraclePrices: { [key: string]: OraclePriceNode } = response?.oraclePrices?.nodes?.reduce((agg, node) => ({ ...agg, [node.typeInName]: node }), {}) || {}; + const boardAuxes: { [key: string]: number } = response?.boardAuxes?.nodes?.reduce( + (agg, node) => ({ ...agg, [node.allegedName]: node.decimalPlaces }), + {}, + ); // TODO: make the fallback implementation in this section of code more readable const reserveDashboardQueryData: ReserveDashboardData = response?.reserveMetrics?.nodes?.map((vaultNode) => ({ @@ -66,9 +70,10 @@ export const Reserve = () => {
- + diff --git a/src/queries.ts b/src/queries.ts index faab3ff..b355ff3 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -160,6 +160,12 @@ query { typeOutAmount } } + boardAuxes { + nodes { + allegedName + decimalPlaces + } + } }` diff --git a/src/types/reserve-types.ts b/src/types/reserve-types.ts index 64d5997..8508e93 100644 --- a/src/types/reserve-types.ts +++ b/src/types/reserve-types.ts @@ -25,9 +25,11 @@ export type ReserveMetricsNode = { allocations: { nodes: Array }; shortfallBalance: number; }; +export type BoardAuxesNode = { allegedName: string; decimalPlaces: number }; export type ReserveDashboardResponse = { oraclePrices: { nodes: Array }; reserveMetrics: { nodes: Array }; + boardAuxes: { nodes: Array }; }; export type ReserveDashboardData = Array<{ shortfallBalance: number; diff --git a/src/utils.tsx b/src/utils.tsx index c0e14b5..811d9cd 100644 --- a/src/utils.tsx +++ b/src/utils.tsx @@ -249,3 +249,9 @@ export function createNumberWithLeadingZeroes(numOfZeroes: number) { } export const parseBigInt = (str: string) => Number(str.slice(0, -1)) + +export const getTokenDivisor = (boardAuxes: { [key: string]: number }, tokenName: string) => { + const decimalPlaces = (boardAuxes && boardAuxes[tokenName]) || 6; + const tokenDivisor = createNumberWithLeadingZeroes(decimalPlaces); + return tokenDivisor; +}; \ No newline at end of file diff --git a/src/widgets/ReserveCosmosSummary.tsx b/src/widgets/ReserveCosmosSummary.tsx index 028bbe7..f3d21b4 100644 --- a/src/widgets/ReserveCosmosSummary.tsx +++ b/src/widgets/ReserveCosmosSummary.tsx @@ -1,6 +1,6 @@ import { ValueCard } from '@/components/ValueCard'; import { GET_ACCOUNT_BALANCE_URL, UIST_DENOMINATION } from '@/constants'; -import { fetchDataFromUrl, formatPrice } from '@/utils'; +import { fetchDataFromUrl, formatPrice, getTokenDivisor } from '@/utils'; import { AxiosResponse, AxiosError } from 'axios'; import useSWR from 'swr'; import DataStatus from '@/components/DataStatus'; @@ -10,9 +10,16 @@ type Props = { isLoading: boolean; error: any; reserveAddress: string; + boardAuxes: { [key: string]: number }; }; -export function ReserveCosmosSummary({ title = 'Cosmos Reserve', isLoading, reserveAddress, error }: Props) { +export function ReserveCosmosSummary({ + title = 'Cosmos Reserve', + boardAuxes, + isLoading, + reserveAddress, + error, +}: Props) { if (isLoading || error || !reserveAddress) { const errorMessage = !reserveAddress && 'Oops! Unable to show Cosmos Reserve'; @@ -34,7 +41,9 @@ export function ReserveCosmosSummary({ title = 'Cosmos Reserve', isLoading, rese const istReserve: { amount: number } = reserveBalance?.data?.balances?.find( (balance: { denom: string }) => balance.denom === UIST_DENOMINATION, ); - const istReserveBalance = (istReserve?.amount || 0) / 1_000_000; + + const istDivisor = getTokenDivisor(boardAuxes, 'IST'); + const istReserveBalance = (istReserve?.amount || 0) / istDivisor; return ; } diff --git a/src/widgets/ReserveSummary.tsx b/src/widgets/ReserveSummary.tsx index 05c312f..1cefca2 100644 --- a/src/widgets/ReserveSummary.tsx +++ b/src/widgets/ReserveSummary.tsx @@ -1,15 +1,16 @@ import { Skeleton } from '@/components/ui/skeleton'; import { ValueCard } from '@/components/ValueCard'; import { ReserveDashboardData } from '@/types/reserve-types'; -import { formatPrice } from '@/utils'; +import { formatPrice, getTokenDivisor } from '@/utils'; type Props = { title?: string; data: ReserveDashboardData; + boardAuxes: { [key: string]: number }; isLoading: boolean; }; -export function ReserveSummary({ title = 'Total Reserve Assets', data, isLoading }: Props) { +export function ReserveSummary({ title = 'Total Reserve Assets', data, boardAuxes, isLoading }: Props) { if (isLoading || !data) { return } />; } @@ -17,7 +18,9 @@ export function ReserveSummary({ title = 'Total Reserve Assets', data, isLoading (agg, node) => agg + Object.values(node.allocations).reduce((agg_, node_) => { - const allocationInUsd = ((Number(node_.value) / 1_000_000) * Number(node_.typeOutAmount || 1_000_000)) / 1_000_000; + const tokenDivisor = getTokenDivisor(boardAuxes, node_.denom); + const allocationInUsd = + ((Number(node_.value) / tokenDivisor) * Number(node_.typeOutAmount || tokenDivisor)) / tokenDivisor; return agg_ + allocationInUsd; }, 0), 0,