Skip to content

Commit

Permalink
OK-7527: fix check fee
Browse files Browse the repository at this point in the history
  • Loading branch information
hzxiao committed Apr 22, 2022
1 parent 99f1a40 commit 6550d8b
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 8 deletions.
15 changes: 13 additions & 2 deletions packages/components/src/NumberInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,20 @@ export const NumberInput: FC<NumberInputProps> = ({
[enableMaxButton, intl, isMax, onMaxChange],
);

const handleChange = (text: string) => {
let result = text;
const handleChange = (t: string) => {
let text = t.replace(/[^\\.0-9]/g, '');
try {
if (text.startsWith('.')) {
const b = new BigNumber(text);
if (!b.isNaN()) {
text = b.toString();
}
}
} catch (error) {
console.log(error);
}

let result = text;
if (text) {
result = text.replace(/^\D*(\d*(?:\.\d*)?).*$/g, '$1');

Expand Down
29 changes: 29 additions & 0 deletions packages/engine/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint max-classes-per-file: "off" */

export enum OneKeyErrorClassNames {
OneKeyError,
OneKeyValidatorError,
OneKeyValidatorTip,
}
export class OneKeyError extends Error {
className = OneKeyErrorClassNames.OneKeyError;

info: Record<string, string>;

key = 'onekey_error';
Expand Down Expand Up @@ -54,6 +61,28 @@ export class OneKeyHardwareError extends OneKeyError {
key = 'onekey_error_hardware';
}

export class OneKeyValidatorError extends OneKeyError {
className = OneKeyErrorClassNames.OneKeyValidatorError;

key = 'onekey_error_validator';

constructor(key: string, info?: Record<string, string>, message?: string) {
super(message, info);
this.key = key;
}
}

export class OneKeyValidatorTip extends OneKeyError {
className = OneKeyErrorClassNames.OneKeyValidatorTip;

key = 'onekey_tip_validator';

constructor(key: string, info?: Record<string, string>, message?: string) {
super(message, info);
this.key = key;
}
}

export class FailedToTransfer extends OneKeyError {
key = 'msg__engine__failed_to_transfer';
}
Expand Down
105 changes: 105 additions & 0 deletions packages/engine/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { backgroundMethod } from '@onekeyhq/kit/src/background/decorators';
import { getSupportedImpls } from './constants';
import { DBAPI } from './dbs/base';
import * as errors from './errors';
import { OneKeyValidatorError, OneKeyValidatorTip } from './errors';
import * as limits from './limits';
import { implToAccountType } from './managers/impl';
import { ProviderController } from './proxy';
Expand Down Expand Up @@ -191,6 +192,110 @@ class Validators {
const addresses = dbAccounts.map((acc) => acc.address?.toLowerCase());
return addresses.includes(address.toLowerCase());
}

@backgroundMethod()
async validateGasLimit(
networkId: string,
limit: string | BigNumber,
max: string | number,
): Promise<void> {
try {
const v = typeof limit === 'string' ? new BigNumber(limit) : limit;
// TODO 21000 may relate to network
if (v.isNaN() || v.isLessThan(new BigNumber(21000))) {
throw new OneKeyValidatorError('form__gas_limit_invalid_min');
}
const maxLimit = new BigNumber(max);
if (v.isGreaterThan(maxLimit.multipliedBy(new BigNumber(20)))) {
throw new OneKeyValidatorTip('form__gas_limit_invalid_too_much');
}
} catch (e) {
if (
e instanceof OneKeyValidatorError ||
e instanceof OneKeyValidatorTip
) {
throw e;
}
throw new OneKeyValidatorError('form__gas_limit_invalid_min');
}
return Promise.resolve();
}

@backgroundMethod()
async validateMaxFee(
networkId: string,
maxFee: string | BigNumber,
maxPriorityFee: string | BigNumber,
networkMaxFee?: string,
): Promise<void> {
try {
const v = typeof maxFee === 'string' ? new BigNumber(maxFee) : maxFee;
// TODO may relate to network
if (v.isNaN() || v.isLessThan(new BigNumber(0))) {
throw new OneKeyValidatorError('form__max_fee_invalid_too_low');
}
const pv =
typeof maxPriorityFee === 'string'
? new BigNumber(maxPriorityFee)
: maxPriorityFee;
if (v.isLessThan(pv)) {
throw new OneKeyValidatorError('form__max_fee_invalid_min');
}

if (networkMaxFee) {
const networkMax = new BigNumber(networkMaxFee);
if (v.isGreaterThan(networkMax.multipliedBy(new BigNumber(4)))) {
throw new OneKeyValidatorTip('form__max_fee_invalid_too_much');
}
if (v.isLessThan(networkMax)) {
throw new OneKeyValidatorTip('form__max_fee_invalid_too_low');
}
}
} catch (e) {
if (
e instanceof OneKeyValidatorError ||
e instanceof OneKeyValidatorTip
) {
throw e;
}
throw new OneKeyValidatorError('form__max_fee_invalid_too_low');
}
return Promise.resolve();
}

@backgroundMethod()
async validateMaxPriortyFee(
networkId: string,
maxPriorityFee: string | BigNumber,
networkMaxPriorityFee?: string,
): Promise<void> {
try {
const v =
typeof maxPriorityFee === 'string'
? new BigNumber(maxPriorityFee)
: maxPriorityFee;
// TODO may relate to network
if (v.isNaN() || v.isLessThan(new BigNumber(0))) {
throw new OneKeyValidatorError('form__max_priority_fee_invalid_min');
}
if (networkMaxPriorityFee) {
if (v.isLessThan(new BigNumber(networkMaxPriorityFee))) {
throw new OneKeyValidatorTip(
'form__max_priority_fee_invalid_too_low',
);
}
}
} catch (e) {
if (
e instanceof OneKeyValidatorError ||
e instanceof OneKeyValidatorTip
) {
throw e;
}
throw new OneKeyValidatorError('form__max_priority_fee_invalid_min');
}
return Promise.resolve();
}
}

export { Validators };
2 changes: 2 additions & 0 deletions packages/kit/src/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export function throwCrossError(msg: string, ...args: any) {
}

export function toPlainErrorObject(error: {
className?: any;
name?: any;
code?: any;
data?: any;
message?: any;
stack?: any;
}) {
return {
className: error.className,
name: error.name,
code: error.code,
data: error.data,
Expand Down
Loading

0 comments on commit 6550d8b

Please sign in to comment.