Skip to content

Commit

Permalink
fix: NaN market cap (#10511)
Browse files Browse the repository at this point in the history
* fix: nan market cap

* fix: formatSupply
  • Loading branch information
guanbinrui committed Aug 20, 2023
1 parent 198303d commit e5f906f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/web3-shared/base/src/helpers/formatInteger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BigNumber } from 'bignumber.js'

export function formatInteger(value: BigNumber.Value | null | undefined, fallback?: string | number) {
if (value === undefined || value === null) return fallback
const result = new BigNumber(typeof value === 'string' ? value.replaceAll(',', '') : value)
return result.isNaN() ? fallback : result.toFormat(0)
}
4 changes: 2 additions & 2 deletions packages/web3-shared/base/src/helpers/formatSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ const boundaryValues = {
*/
export function formatSupply(value: BigNumber.Value | null | undefined, fallback?: string | number) {
if (value === undefined || value === null) return fallback
const bgValue = new BigNumber(value)
const bgValue = new BigNumber(typeof value === 'string' ? value.replaceAll(',', '') : value)
const isGreaterThanOrEqualToMin = bgValue.isGreaterThanOrEqualTo(boundaryValues.mid)

const integerValue = bgValue.integerValue(1)
const decimalValue = bgValue.plus(integerValue.negated()).toFixed(2)
const finalValue = integerValue.plus(decimalValue)

if (isGreaterThanOrEqualToMin) return formatCurrency(finalValue, '')
return finalValue.toFormat(finalValue.isInteger() ? 0 : 2, 0)
return finalValue.isNaN() ? fallback : finalValue.toFormat(finalValue.isInteger() ? 0 : 2, 0)
}
1 change: 1 addition & 0 deletions packages/web3-shared/base/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './formatElapsedPure.js'
export * from './formatMarketCap.js'
export * from './formatPercentage.js'
export * from './formatSupply.js'
export * from './formatInteger.js'
export * from './formatURL.js'
export * from './getLocale.js'
export * from './getTokenUSDValue.js'
Expand Down
5 changes: 0 additions & 5 deletions packages/web3-shared/base/src/helpers/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ export function toFixed(value: BigNumber.Value = 0, decimalPlaces?: number) {
return !isUndefined(decimalPlaces) ? n.toFixed(decimalPlaces) : n.toFixed()
}

export function formatInteger(value: BigNumber.Value | null | undefined, fallback?: string | number) {
if (value === undefined || value === null) return fallback
return new BigNumber(value).toFormat(0)
}

/** Trim ending zeros of decimals */
export function trimZero(digit: string) {
const result = digit.replaceAll(/\.([1-9]*)?0+$/g, (_, p1) => {
Expand Down

0 comments on commit e5f906f

Please sign in to comment.