Skip to content

Commit

Permalink
feat: validateCapacity
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed Nov 27, 2023
1 parent 2a548f6 commit 3e81350
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/components/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const Send = () => {
!send.generatedTx ||
outputs.some(v => !v.address)

const outputErrors = useOutputErrors(outputs, isMainnet)
const outputErrors = useOutputErrors(outputs, isMainnet, isSendMax)

const isMaxBtnDisabled = (() => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export const useSendInfo = ({
},
[setSendInfoList]
)
const outputErrors = useOutputErrors(sendInfoList, isMainnet)
const [isSendMax, setIsSendMax] = useState(false)
const outputErrors = useOutputErrors(sendInfoList, isMainnet, isSendMax)
const totalAmount = useMemo(
() => outputsToTotalAmount(sendInfoList.filter((v, idx) => !!v.amount && !outputErrors[idx].amountError)),
[sendInfoList, outputErrors]
Expand Down Expand Up @@ -172,7 +173,6 @@ export const useSendInfo = ({
}
}, 300)
}, [sendInfoList, setErrorMessage, multisigConfig, dispatch, t, isMainnet, outputErrors])
const [isSendMax, setIsSendMax] = useState(false)
const onSendMaxClick = useCallback(() => {
if (!isSendMax) {
setIsSendMax(true)
Expand Down
12 changes: 12 additions & 0 deletions packages/neuron-ui/src/exceptions/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ export class AmountNegativeException extends RangeError {
this.i18n.fieldValue = value
}
}

export class CapacityTooSmallException extends Error {
public code = ErrorCode.CapacityTooSmall
public i18n: {
bytes: string
}

constructor(bytes: string) {
super(`${I18N_PATH}${ErrorCode.CapacityTooSmall}`)
this.i18n = { bytes }
}
}
9 changes: 6 additions & 3 deletions packages/neuron-ui/src/utils/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
validateAmount,
validateAddress,
validateAmountRange,
validateCapacity,
} from 'utils/validators'
import { MenuItemConstructorOptions, shell } from 'electron'
import { ErrorWithI18n, isErrorWithI18n } from 'exceptions'
Expand Down Expand Up @@ -499,18 +500,20 @@ export const useForceUpdate = <T extends (...args: any[]) => void>(cb: T) => {
}

export const useOutputErrors = (
outputs: Partial<Record<'address' | 'amount' | 'date', string>>[],
isMainnet: boolean
outputs: Partial<Record<'address' | 'amount' | 'date' | 'unit', string>>[],
isMainnet: boolean,
isSendMax?: boolean
) => {
return useMemo(
() =>
outputs.map(({ address, amount, date }) => {
outputs.map(({ address, amount, date, unit }, index) => {
let amountError: ErrorWithI18n | undefined
if (amount !== undefined) {
try {
const extraSize = date ? CONSTANTS.SINCE_FIELD_SIZE : 0
validateAmount(amount)
validateAmountRange(amount, extraSize)
validateCapacity({ address, amount, unit }, isSendMax, index === outputs.length - 1)
} catch (err) {
if (isErrorWithI18n(err)) {
amountError = err
Expand Down
22 changes: 22 additions & 0 deletions packages/neuron-ui/src/utils/validators/capacity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CKBToShannonFormatter } from 'utils/formatters'
import { CapacityTooSmallException } from 'exceptions'
import { bytes as byteUtils } from '@ckb-lumos/codec'
import { ckbCore } from 'services/chain'

export const validateCapacity = (item: State.Output, isSendMax?: boolean, isLast?: boolean) => {
const { amount, unit, address } = item
const capacity = CKBToShannonFormatter(amount, unit)
const script = ckbCore.utils.addressToScript(address as string)

const size = 1 + byteUtils.concat(script.args, script.codeHash).byteLength
const outputSize = 8 + byteUtils.bytify('0x').byteLength + size

const judgement = isSendMax ? !isLast : true

if (BigInt(capacity) < BigInt(outputSize) * BigInt(10 ** 8) && judgement) {
throw new CapacityTooSmallException(outputSize.toString())
}

return true
}
export default validateCapacity
1 change: 1 addition & 0 deletions packages/neuron-ui/src/utils/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './tokenId'
export * from './tokenName'
export * from './symbol'
export * from './decimal'
export * from './capacity'

1 comment on commit 3e81350

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 7000538827

Please sign in to comment.