mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-09 15:40:55 +00:00
throw exception when non-num string is passed to Bignumber()
This commit is contained in:
parent
8da8544316
commit
53c7c20a29
@ -347,7 +347,12 @@ export const calculateFee = (gasPrice: string, gasLimit: string): string => {
|
|||||||
|
|
||||||
export const calculateTotal = (amount: string, gasPrice: string, gasLimit: string): string => {
|
export const calculateTotal = (amount: string, gasPrice: string, gasLimit: string): string => {
|
||||||
try {
|
try {
|
||||||
return new BigNumber(amount).plus(calculateFee(gasPrice, gasLimit)).toFixed();
|
const bAmount = new BigNumber(amount);
|
||||||
|
// BigNumber() returns NaN on non-numeric string
|
||||||
|
if (bAmount.isNaN()) {
|
||||||
|
throw new Error('Amount is not a number');
|
||||||
|
}
|
||||||
|
return bAmount.plus(calculateFee(gasPrice, gasLimit)).toFixed();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
|
@ -300,7 +300,12 @@ export const destinationTagValidation = ($state: State): PayloadAction<State> =>
|
|||||||
|
|
||||||
const calculateTotal = (amount: string, fee: string): string => {
|
const calculateTotal = (amount: string, fee: string): string => {
|
||||||
try {
|
try {
|
||||||
return new BigNumber(amount).plus(fee).toFixed();
|
const bAmount = new BigNumber(amount);
|
||||||
|
// BigNumber() returns NaN on non-numeric string
|
||||||
|
if (bAmount.isNaN()) {
|
||||||
|
throw new Error('Amount is not a number');
|
||||||
|
}
|
||||||
|
return bAmount.plus(fee).toFixed();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ declare module 'bignumber.js' {
|
|||||||
isLessThan(n: $npm$big$number$object): boolean;
|
isLessThan(n: $npm$big$number$object): boolean;
|
||||||
lte(n: $npm$big$number$object): boolean;
|
lte(n: $npm$big$number$object): boolean;
|
||||||
isLessThanOrEqualTo(n: $npm$big$number$object): boolean;
|
isLessThanOrEqualTo(n: $npm$big$number$object): boolean;
|
||||||
|
isNaN(): boolean;
|
||||||
minus(n: $npm$big$number$object): T_BigNumber;
|
minus(n: $npm$big$number$object): T_BigNumber;
|
||||||
mod(n: $npm$big$number$object): T_BigNumber;
|
mod(n: $npm$big$number$object): T_BigNumber;
|
||||||
plus(n: $npm$big$number$object): T_BigNumber;
|
plus(n: $npm$big$number$object): T_BigNumber;
|
||||||
|
@ -56,7 +56,12 @@ export const hexToString = (hex: string): string => {
|
|||||||
|
|
||||||
export const toDecimalAmount = (amount: string | number, decimals: number): string => {
|
export const toDecimalAmount = (amount: string | number, decimals: number): string => {
|
||||||
try {
|
try {
|
||||||
return new BigNumber(amount).div(10 ** decimals).toString(10);
|
const bAmount = new BigNumber(amount);
|
||||||
|
// BigNumber() returns NaN on non-numeric string
|
||||||
|
if (bAmount.isNaN()) {
|
||||||
|
throw new Error('Amount is not a number');
|
||||||
|
}
|
||||||
|
return bAmount.div(10 ** decimals).toString(10);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
@ -64,7 +69,12 @@ export const toDecimalAmount = (amount: string | number, decimals: number): stri
|
|||||||
|
|
||||||
export const fromDecimalAmount = (amount: string | number, decimals: number): string => {
|
export const fromDecimalAmount = (amount: string | number, decimals: number): string => {
|
||||||
try {
|
try {
|
||||||
return new BigNumber(amount).times(10 ** decimals).toString(10);
|
const bAmount = new BigNumber(amount);
|
||||||
|
// BigNumber() returns NaN on non-numeric string
|
||||||
|
if (bAmount.isNaN()) {
|
||||||
|
throw new Error('Amount is not a number');
|
||||||
|
}
|
||||||
|
return bAmount.times(10 ** decimals).toString(10);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return '0';
|
return '0';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user