1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-21 17:37:01 +01:00

Fixing error when user input some letter in token allowance flow (#17117)

* Fixing error when user input some letter in token allowance flow

* Rename the regex variable name and added a brief comment above its definition

Co-authored-by: Pedro Figueiredo <pedro.figueiredo@consensys.net>
This commit is contained in:
Vladimir Saric 2023-01-18 10:53:10 +01:00 committed by GitHub
parent 1df1955276
commit c82c967c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 5 deletions

View File

@ -44,5 +44,7 @@ export const MAX_TOKEN_ALLOWANCE_AMOUNT = new BigNumber(2)
.pow(256)
.minus(1)
.toString(10);
export const TOKEN_ALLOWANCE_VALUE_REGEX = /^[0-9]{1,}([,.][0-9]{1,})?$/u;
// number with optional decimal point using a comma or dot
export const NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX =
/^[0-9]{1,}([,.][0-9]{1,})?$/u;
export const DECIMAL_REGEX = /\.(\d*)/u;

View File

@ -29,7 +29,7 @@ import {
} from '../../../../shared/modules/conversion.utils';
import {
MAX_TOKEN_ALLOWANCE_AMOUNT,
TOKEN_ALLOWANCE_VALUE_REGEX,
NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX,
DECIMAL_REGEX,
} from '../../../../shared/constants/tokens';
import { CustomSpendingCapTooltip } from './custom-spending-cap-tooltip';
@ -112,7 +112,7 @@ export default function CustomSpendingCap({
return;
}
if (valueInput && !TOKEN_ALLOWANCE_VALUE_REGEX.test(valueInput)) {
if (valueInput && !NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX.test(valueInput)) {
spendingCapError = t('spendingCapError');
setCustomSpendingCapText(t('spendingCapErrorDescription', [siteOrigin]));
setError(spendingCapError);

View File

@ -52,7 +52,10 @@ import { getCustomTxParamsData } from '../confirm-approve/confirm-approve.util';
import { setCustomTokenAmount } from '../../ducks/app/app';
import { valuesFor } from '../../helpers/utils/util';
import { calcTokenAmount } from '../../../shared/lib/transactions-controller-utils';
import { MAX_TOKEN_ALLOWANCE_AMOUNT } from '../../../shared/constants/tokens';
import {
MAX_TOKEN_ALLOWANCE_AMOUNT,
NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX,
} from '../../../shared/constants/tokens';
import { ConfirmPageContainerNavigation } from '../../components/app/confirm-page-container';
export default function TokenAllowance({
@ -102,7 +105,11 @@ export default function TokenAllowance({
return inputValue.replace(/,/gu, '.');
};
let customPermissionAmount = replaceCommaToDot(customTokenAmount).toString();
let customPermissionAmount = NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX.test(
customTokenAmount,
)
? replaceCommaToDot(customTokenAmount).toString()
: '0';
const maxTokenAmount = calcTokenAmount(MAX_TOKEN_ALLOWANCE_AMOUNT, decimals);
if (customTokenAmount.length > 1 && Number(customTokenAmount)) {