Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hide USD values > 100T #629

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/curve-ui-kit/src/shared/ui/Metric.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useMemo, useState } from 'react'

import Alert from '@mui/material/Alert'
import Stack from '@mui/material/Stack'
Expand All @@ -13,6 +13,7 @@ import { TypographyVariantKey } from '@ui-kit/themes/typography'
import { abbreviateNumber, scaleSuffix } from '@ui-kit/utils'
import { Duration } from '../../themes/design/0_primitives'
import AlertTitle from '@mui/material/AlertTitle'
import { MAX_USD_VALUE } from 'ui'

const { Spacing, IconSize } = SizesAndSpaces

Expand Down Expand Up @@ -94,6 +95,14 @@ type MetricValueProps = Required<Pick<Props, 'value' | 'formatter' | 'abbreviate
copyValue: () => void
}

function runFormatter(value: number, formatter: (value: number) => string, abbreviate: boolean, symbol?: string) {
if (symbol === '$' && value > MAX_USD_VALUE) {
console.warn(`USD value is too large: ${value}`)
return `?`
}
return formatter(abbreviate ? abbreviateNumber(value) : value)
}

const MetricValue = ({
value,
formatter,
Expand All @@ -114,7 +123,10 @@ const MetricValue = ({
)}

<Typography variant={fontVariant} color="textPrimary">
{formatter(abbreviate ? abbreviateNumber(value) : value)}
{useMemo(
() => runFormatter(value, formatter, abbreviate, unit?.symbol),
[formatter, abbreviate, value, unit?.symbol],
)}
</Typography>

{abbreviate && (
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/utils/utilsConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const CURVE_ASSETS_URL = `${CURVE_CDN_URL}/curve-assets`
export const CURVE_LOGO_URL = `${CURVE_ASSETS_URL}/branding/logo.png`
export const NOT_FOUND_IMAGE_URL = `${CURVE_ASSETS_URL}/branding/four-oh-llama.jpg`

// Sometimes API returns overflowed USD values. Don't show them!
export const MAX_USD_VALUE = 100_000_000_000_000 // $ 100T 🤑

export const getImageBaseUrl = (blockchainId: string) =>
`${CURVE_ASSETS_URL}/images/assets${blockchainId == 'ethereum' ? '' : `-${blockchainId}`}/`

Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/utils/utilsFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { detect, fromUrl, fromNavigator } from '@lingui/detect-locale'
import BigNumber from 'bignumber.js'
import isUndefined from 'lodash/isUndefined'
import isNaN from 'lodash/isNaN'
import { MAX_USD_VALUE } from './utilsConstants'

BigNumber.config({ EXPONENTIAL_AT: 20, ROUNDING_MODE: BigNumber.ROUND_HALF_UP })
export const BN = BigNumber
Expand Down Expand Up @@ -140,6 +141,10 @@ export function formatNumber(val: number | string | undefined | null, options?:
}

function _formatNumber(val: string | number, options: NumberFormatOptions) {
if (options.currency === 'USD' && Number(val) > MAX_USD_VALUE) {
console.warn(`USD value is too large: ${val}`)
return `?`
}
return new Intl.NumberFormat(localeDetected, options).format(
options.style === 'percent' ? Number(val) / 100 : Number(val),
)
Expand Down
Loading