Skip to content

Commit 61eab4f

Browse files
authored
fix(neuron-ui): add decimal validation on deposit value (#1093)
* fix(neuron-ui): add decimal validation on deposit value * refactor(neuron-ui): use bigint in comparison
1 parent dc82cbb commit 61eab4f

5 files changed

Lines changed: 48 additions & 30 deletions

File tree

packages/neuron-ui/src/components/NervosDAO/index.tsx

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { updateNervosDaoData, clearNervosDaoData } from 'states/stateProvider/ac
1010
import calculateGlobalAPY from 'utils/calculateGlobalAPY'
1111
import calculateFee from 'utils/calculateFee'
1212
import { shannonToCKBFormatter, CKBToShannonFormatter } from 'utils/formatters'
13-
import { MIN_DEPOSIT_AMOUNT, MEDIUM_FEE_RATE, CapacityUnit } from 'utils/const'
13+
import { MIN_DEPOSIT_AMOUNT, MEDIUM_FEE_RATE, SHANNON_CKB_RATIO, CapacityUnit } from 'utils/const'
14+
import { verifyAmount } from 'utils/validators'
1415

1516
import { generateDepositTx, generateWithdrawTx, generateClaimTx } from 'services/remote'
1617
import { ckbCore } from 'services/chain'
@@ -59,27 +60,34 @@ const NervosDAO = ({
5960
}
6061
clearTimeout(timer)
6162
timer = setTimeout(() => {
62-
if (+value < MIN_DEPOSIT_AMOUNT) {
63+
setErrorMessage('')
64+
clearGeneratedTx()
65+
66+
const verifyRes = verifyAmount(value)
67+
if (verifyRes !== true) {
68+
setErrorMessage(t(`messages.codes.${verifyRes.code}`, { fieldName: 'deposit' }))
69+
return
70+
}
71+
72+
if (BigInt(CKBToShannonFormatter(value)) < BigInt(MIN_DEPOSIT_AMOUNT * SHANNON_CKB_RATIO)) {
6373
setErrorMessage(t('nervos-dao.minimal-fee-required', { minimal: MIN_DEPOSIT_AMOUNT }))
64-
clearGeneratedTx()
65-
} else {
66-
setErrorMessage('')
67-
generateDepositTx({
68-
feeRate: `${MEDIUM_FEE_RATE}`,
69-
capacity: CKBToShannonFormatter(value, CapacityUnit.CKB),
70-
walletID: wallet.id,
71-
}).then(res => {
72-
if (res.status === 1) {
73-
dispatch({
74-
type: AppActions.UpdateGeneratedTx,
75-
payload: res.result,
76-
})
77-
} else {
78-
clearGeneratedTx()
79-
setErrorMessage(`${typeof res.message === 'string' ? res.message : res.message.content}`)
80-
}
81-
})
74+
return
8275
}
76+
77+
generateDepositTx({
78+
feeRate: `${MEDIUM_FEE_RATE}`,
79+
capacity: CKBToShannonFormatter(value, CapacityUnit.CKB),
80+
walletID: wallet.id,
81+
}).then(res => {
82+
if (res.status === 1) {
83+
dispatch({
84+
type: AppActions.UpdateGeneratedTx,
85+
payload: res.result,
86+
})
87+
} else {
88+
setErrorMessage(`${typeof res.message === 'string' ? res.message : res.message.content}`)
89+
}
90+
})
8391
}, 500)
8492
setDepositValue(value)
8593
},

packages/neuron-ui/src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@
270270
"mnemonic": "Mnemonic",
271271
"keystore-path": "Keystore File",
272272
"keystore-name": "Wallet name",
273-
"keystore-password": "Password"
273+
"keystore-password": "Password",
274+
"deposit": "Deposit"
274275
},
275276
"codes": {
276277
"-3": "",

packages/neuron-ui/src/locales/zh.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@
270270
"mnemonic": "助记词",
271271
"keystore-path": "Keystore 文件",
272272
"keystore-name": "钱包名称",
273-
"keystore-password": "密码"
273+
"keystore-password": "密码",
274+
"deposit": "存入金额"
274275
},
275276
"codes": {
276277
"-3": "",

packages/neuron-ui/src/utils/formatters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const currencyFormatter = (
7272
return `${integer.replace(/\B(?=(\d{3})+(?!\d))/g, delimiter)}${dot}${decimal} ${unit}`
7373
}
7474

75-
export const CKBToShannonFormatter = (amount: string = '0', unit: CapacityUnit) => {
75+
export const CKBToShannonFormatter = (amount: string = '0', unit: CapacityUnit = CapacityUnit.CKB) => {
7676
if (Number.isNaN(+amount)) {
7777
console.warn(`Amount is not a valid number`)
7878
return `${amount} ${unit}`

packages/neuron-ui/src/utils/validators.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { MAX_NETWORK_NAME_LENGTH } from 'utils/const'
1+
import {
2+
MAX_NETWORK_NAME_LENGTH,
3+
MIN_PASSWORD_LENGTH,
4+
MAX_PASSWORD_LENGTH,
5+
MIN_AMOUNT,
6+
MAX_DECIMAL_DIGITS,
7+
SHANNON_CKB_RATIO,
8+
ErrorCode,
9+
} from 'utils/const'
10+
import { CKBToShannonFormatter } from 'utils/formatters'
211
import { ckbCore } from 'services/chain'
3-
import { MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH, MIN_AMOUNT, MAX_DECIMAL_DIGITS, ErrorCode } from './const'
412

513
export const verifyAddress = (address: string): boolean => {
614
if (typeof address !== 'string' || address.length !== 46) {
@@ -14,27 +22,27 @@ export const verifyAddress = (address: string): boolean => {
1422
}
1523

1624
export const verifyAmountRange = (amount: string = '') => {
17-
return +amount >= MIN_AMOUNT
25+
return BigInt(CKBToShannonFormatter(amount)) >= BigInt(MIN_AMOUNT * SHANNON_CKB_RATIO)
1826
}
1927

2028
export const verifyAmount = (amount: string = '0') => {
2129
if (Number.isNaN(+amount)) {
2230
return { code: ErrorCode.FieldInvalid }
2331
}
24-
if (+amount < 0) {
25-
return { code: ErrorCode.NotNegative }
26-
}
2732
const [, decimal = ''] = amount.split('.')
2833
if (decimal.length > MAX_DECIMAL_DIGITS) {
2934
return {
3035
code: ErrorCode.DecimalExceed,
3136
}
3237
}
38+
if (BigInt(CKBToShannonFormatter(amount)) < BigInt(0)) {
39+
return { code: ErrorCode.NotNegative }
40+
}
3341
return true
3442
}
3543

3644
export const verifyTotalAmount = (totalAmount: string, fee: string, balance: string) => {
37-
if (+balance < 0) {
45+
if (BigInt(balance) < BigInt(0)) {
3846
return false
3947
}
4048
return BigInt(totalAmount) + BigInt(fee) <= BigInt(balance)

0 commit comments

Comments
 (0)