Skip to content

Commit

Permalink
Merge pull request #629 from curvefi/feat/filter-giga-usd
Browse files Browse the repository at this point in the history
feat: hide USD values > 100T
  • Loading branch information
DanielSchiavini authored Jan 24, 2025
2 parents 852a980 + 0033c7a commit 49dc83f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
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

0 comments on commit 49dc83f

Please sign in to comment.