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

Automatically pad decimal places when the deposit dialog automatically provides a maximum value with decimals #2722

Merged
merged 1 commit into from
Jun 25, 2023
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
3 changes: 2 additions & 1 deletion packages/neuron-ui/src/components/NervosDAO/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
shannonToCKBFormatter,
isSuccessResponse,
validateAmount,
padFractionDigitsIfDecimal,
} from 'utils'

import {
Expand Down Expand Up @@ -447,7 +448,7 @@ export const useOnSlide = ({
maxDepositAmount - BigInt(CKBToShannonFormatter(`${value}`)) < BigInt(SHANNON_CKB_RATIO * MIN_AMOUNT)
? shannonToCKBFormatter(`${maxDepositAmount}`, false, '')
: `${value}`
updateDepositValue(amount)
updateDepositValue(padFractionDigitsIfDecimal(amount, 8))
},
[updateDepositValue, maxDepositAmount]
)
Expand Down
4 changes: 3 additions & 1 deletion packages/neuron-ui/src/components/NervosDAO/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getSyncStatus,
CKBToShannonFormatter,
clsx,
padFractionDigitsIfDecimal,
} from 'utils'

import { openExternal } from 'services/remote'
Expand Down Expand Up @@ -263,7 +264,8 @@ const NervosDAO = () => {
useEffect(() => {
try {
if (BigInt(CKBToShannonFormatter(depositValue)) > maxDepositAmount) {
setDepositValue(shannonToCKBFormatter(`${maxDepositAmount}`, false, ''))
const amount = shannonToCKBFormatter(`${maxDepositAmount}`, false, '')
setDepositValue(padFractionDigitsIfDecimal(amount, 8))
}
} catch (error) {
// ignore error
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { padFractionDigitsIfDecimal } from 'utils/formatters'

describe('padFractionDigitsIfDecimal', () => {
it('should pad fraction digits if decimal', () => {
expect(padFractionDigitsIfDecimal(1.2, 5)).toBe('1.20000')
expect(padFractionDigitsIfDecimal(1.23456789, 3)).toBe('1.23456789')
})

it('should not pad fraction digits if not decimal', () => {
expect(padFractionDigitsIfDecimal(1, 5)).toBe('1')
expect(padFractionDigitsIfDecimal(123456789, 3)).toBe('123456789')
})

it('should handle string input', () => {
expect(padFractionDigitsIfDecimal('1.2', 5)).toBe('1.20000')
expect(padFractionDigitsIfDecimal('1.23456789', 3)).toBe('1.23456789')
expect(padFractionDigitsIfDecimal('1', 2)).toBe('1')
})

it('should handle negative numbers', () => {
expect(padFractionDigitsIfDecimal(-1.2, 5)).toBe('-1.20000')
expect(padFractionDigitsIfDecimal(-1.23456789, 3)).toBe('-1.23456789')
expect(padFractionDigitsIfDecimal(-1, 2)).toBe('-1')
})

it('should handle zero padding', () => {
expect(padFractionDigitsIfDecimal(1.2, 0)).toBe('1.2')
expect(padFractionDigitsIfDecimal(1.23456789, 0)).toBe('1.23456789')
expect(padFractionDigitsIfDecimal(1, 0)).toBe('1')
})

it('should handle non-numeric input', () => {
expect(padFractionDigitsIfDecimal('hello', 5)).toBe('hello')
})
})
6 changes: 6 additions & 0 deletions packages/neuron-ui/src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,9 @@ export const errorFormatter = (error: string | FailureFromController['message'],

return error.content || unknownError
}

export const padFractionDigitsIfDecimal = (num: string | number, minimumFractionDigits: number): string => {
const numText = num.toString()
const isDecimal = numText.includes('.')
return isDecimal ? numText.padEnd(numText.indexOf('.') + 1 + minimumFractionDigits, '0') : numText
}