diff --git a/src/components/BarDiagram/BarDiagram.tsx b/src/components/BarDiagram/BarDiagram.tsx index 7a17f525..a5cc45e3 100644 --- a/src/components/BarDiagram/BarDiagram.tsx +++ b/src/components/BarDiagram/BarDiagram.tsx @@ -3,7 +3,7 @@ import { type FC } from 'react' import { BarChart, XAxis, YAxis, Tooltip, Bar, ResponsiveContainer, type TooltipProps } from 'recharts' import { type NameType, type ValueType } from 'recharts/types/component/DefaultTooltipContent' import { format } from 'date-fns' -import { toFixedCutZero } from '@/utils' +import { abbreviateAmount, toFixedCutZero } from '@/utils' import Skeleton from 'react-loading-skeleton' export interface IChartData { @@ -68,16 +68,7 @@ export const BarDiagram: FC = (props) => { interval={'preserveStartEnd'} minTickGap={2} tickFormatter={(value: number) => { - if (value >= 1000000000) { - return `${(value / 1000000000).toFixed(1)}B`// Billions - } - if (value >= 1000000) { - return `${(value / 1000000).toFixed(1)}M` // Millions - } - if (value >= 1000) { - return `${(value / 1000).toFixed(1)}k` // Thousands - } - return String((value).toFixed(1)) + return abbreviateAmount(String(value), 1) }} domain={[0, (dataMax: number) => Math.max((dataMax * 1.15), 10)]} allowDataOverflow={true} diff --git a/src/components/Price/Price.component.tsx b/src/components/Price/Price.component.tsx index 5e2e9efb..879978e1 100644 --- a/src/components/Price/Price.component.tsx +++ b/src/components/Price/Price.component.tsx @@ -1,4 +1,4 @@ -import { convertBigIntValueToNormal, toFixedCutZero, toFixedNumber, weiMultiply } from '@/utils/bigint' +import { convertBigIntValueToNormal, abbreviateAmount, toFixedCutZero, toFixedNumber, weiMultiply } from '@/utils/bigint' import { type tPoolIds } from '@cedro-finance/sdk' import { type FC, useMemo } from 'react' import { parseUnits } from 'viem' @@ -36,15 +36,24 @@ export interface IValueWithPriceProps { displayUnit?: boolean variant?: 'primary' | 'secondary' unit?: string + abbreviateNumber?: boolean } -export const ValueWithPrice: FC = ({ pool, value, displayUnit = false, unit, variant = 'primary' }) => { +export const ValueWithPrice: FC = (props) => { + const { pool, value, displayUnit = false, unit, variant = 'primary', abbreviateNumber = false } = props const { oraclePrices } = useAppSelector(state => state.pool) + const formattedValue = toFixedNumber(value, 4, true) const poolPrice = useMemo(() => { return oraclePrices[pool] ?? 0n }, [oraclePrices, pool]) + const tokenValue = useMemo(() => { + if (+value > 1000 && abbreviateNumber) { + return abbreviateAmount(value, 2) + } else return formattedValue + }, [value]) + const dollarEquivalent = useMemo(() => { if (value) { const _value = value.replace(/,/g, '') @@ -56,9 +65,8 @@ export const ValueWithPrice: FC = ({ pool, value, displayU return (
- {/* TODO should the precision be 4 for tokens? */}
- {toFixedNumber(value, 4, true)}{' '} + {tokenValue} {displayUnit ? unit ?? getTokenSymbol(pool) : ''}
${toFixedCutZero(dollarEquivalent, 2, true)}
diff --git a/src/components/layout/Layout.component.tsx b/src/components/layout/Layout.component.tsx index a9d16cf9..f08edf57 100644 --- a/src/components/layout/Layout.component.tsx +++ b/src/components/layout/Layout.component.tsx @@ -50,7 +50,7 @@ export const Layout: FC = ({
)} - + > = [ ) }, @@ -246,6 +247,7 @@ export const supplyListColumns: Array> = [ ) }, @@ -569,6 +571,7 @@ export const HistoryColumnDefinition: Array> variant="secondary" displayUnit={true} unit={cedroClient.Config.getPoolWrappedTokens(collateralPool).ceToken} + abbreviateNumber={true} /> ) }, diff --git a/src/containers/market.tsx b/src/containers/market.tsx index 204c41a3..f0b322a1 100644 --- a/src/containers/market.tsx +++ b/src/containers/market.tsx @@ -147,7 +147,7 @@ export const MarketColumnDefinition: Array> = [ header: 'Total Supply', cell: (props) => { const pool = (props.row.original).pool - return }, meta: { @@ -187,7 +187,7 @@ export const MarketColumnDefinition: Array> = [ header: 'Total Borrow', cell: (props) => { const pool = (props.row.original).pool - return }, meta: { diff --git a/src/utils/bigint.ts b/src/utils/bigint.ts index a4641c5e..8f0c5be7 100644 --- a/src/utils/bigint.ts +++ b/src/utils/bigint.ts @@ -55,16 +55,30 @@ export const toFormatNumber = ( return num } -export const formatAmount = (str: string, fix = 0): string => { +export const abbreviateAmount = (str: string, fix = 0): string => { const num = +str if (num >= 1000000000) { - return (num / 1000000000).toFixed(fix) + ' B' + return roundDown(num / 1000000000, fix) + ' B' } else if (num >= 1000000) { - return (num / 1000000).toFixed(fix) + ' M' + return roundDown(num / 1000000, fix) + ' M' + } else if (num >= 1000) { + return roundDown(num / 1000, fix) + ' k' } else { return num.toFixed(fix) } } + +export const roundDown = (num: string | number, fix = 2): string => { + if (typeof num === 'number') { + num = String(num) + } + const [integer, decimal] = num.split('.') + if (decimal && decimal.length > fix) { + return integer + '.' + decimal.slice(0, fix) + } + return num +} + export const toCutFrontZero = (num: string): string => { while (num.length > 1 && num.startsWith('0') && num[1] !== '.') { num = num.slice(1)