Skip to content

Commit

Permalink
Fixed really small token formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Sep 5, 2023
1 parent 4398092 commit 1969b15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
13 changes: 3 additions & 10 deletions packages/state/recoil/selectors/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
} from '@dao-dao/types'
import {
MAINNET,
convertDenomToMicroDenomWithDecimals,
cosmWasmClientRouter,
cosmosSdkVersionIs47OrHigher,
cosmosValidatorToValidator,
Expand Down Expand Up @@ -934,15 +933,9 @@ export const communityPoolBalancesSelector = selectorFamily<
(token, i): GenericTokenBalance => ({
token,
balance: BigInt(
// If this is a token with 18 decimals, its balance is still
// returned with 6 decimals.
// TODO: Figure out why ? Just happens with DAI.
convertDenomToMicroDenomWithDecimals(
decodeCosmosSdkDecFromProto(pool[i].amount)
.floor()
.toFloatApproximation(),
token.decimals === 18 ? 18 - 6 : 0
)
decodeCosmosSdkDecFromProto(pool[i].amount)
.floor()
.toFloatApproximation()
).toString(),
})
)
Expand Down
6 changes: 5 additions & 1 deletion packages/stateless/components/token/TokenAmountDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export const TokenAmountDisplay = ({
}

// Extract amount from loaded value.
const amount = typeof _amount === 'number' ? _amount : _amount.data
let amount = typeof _amount === 'number' ? _amount : _amount.data
// If USD amount too small, set to 0.
if (estimatedUsdValue && amount < 0.01) {
amount = 0
}

const options: Intl.NumberFormatOptions & { roundingPriority: string } = {
maximumFractionDigits: decimals,
Expand Down
12 changes: 9 additions & 3 deletions packages/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ export const isoStringForLocalDateString = (dateString: string) =>
new Date(dateString).toISOString()

// Select number of decimal digits, rounding down / truncating.
export const toFixedDown = (value: Number, digits: Number) => {
export const toFixedDown = (value: number, digits: number) => {
// If contains scientific notation, truncate and use BigInt to get rid of it.
let stringifiedValue = value.toString()
if (stringifiedValue.includes('e')) {
stringifiedValue = BigInt(Math.floor(value)).toString()
}

const re = new RegExp('(\\d+\\.\\d{' + digits + '})(\\d)')
const matches = value.toString().match(re)
return matches ? parseFloat(matches[1]) : value.valueOf()
const matches = stringifiedValue.match(re)
return matches ? parseFloat(matches[1]) : value
}

0 comments on commit 1969b15

Please sign in to comment.