2023-04-13 19:24:33 +02:00
|
|
|
import React, { useState, useContext, useEffect, useRef } from 'react';
|
2023-07-11 16:57:59 +02:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-09-28 21:46:29 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2023-01-16 18:41:46 +01:00
|
|
|
import classnames from 'classnames';
|
2023-01-05 15:58:16 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
2023-04-19 01:23:45 +02:00
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
|
|
|
|
2022-09-28 21:46:29 +02:00
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
|
|
|
import Box from '../../ui/box';
|
|
|
|
import FormField from '../../ui/form-field';
|
2023-07-19 19:55:38 +02:00
|
|
|
import { ButtonLink, Icon, IconName, Text } from '../../component-library';
|
|
|
|
|
2022-09-28 21:46:29 +02:00
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2023-05-23 21:29:14 +02:00
|
|
|
TextAlign,
|
2023-04-06 13:10:14 +02:00
|
|
|
TextVariant,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
|
|
|
Size,
|
2023-01-04 18:01:52 +01:00
|
|
|
BLOCK_SIZES,
|
2023-02-02 21:15:26 +01:00
|
|
|
BackgroundColor,
|
|
|
|
TextColor,
|
2023-07-18 07:03:46 +02:00
|
|
|
Display,
|
|
|
|
FlexDirection,
|
2022-09-28 21:46:29 +02:00
|
|
|
} from '../../../helpers/constants/design-system';
|
2022-11-07 19:21:38 +01:00
|
|
|
import { setCustomTokenAmount } from '../../../ducks/app/app';
|
2023-01-05 15:58:16 +01:00
|
|
|
import { calcTokenAmount } from '../../../../shared/lib/transactions-controller-utils';
|
2023-04-19 01:23:45 +02:00
|
|
|
import { hexToDecimal } from '../../../../shared/modules/conversion.utils';
|
2023-01-05 15:58:16 +01:00
|
|
|
import {
|
|
|
|
MAX_TOKEN_ALLOWANCE_AMOUNT,
|
2023-01-18 10:53:10 +01:00
|
|
|
NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX,
|
2023-01-16 18:41:46 +01:00
|
|
|
DECIMAL_REGEX,
|
2023-01-05 15:58:16 +01:00
|
|
|
} from '../../../../shared/constants/tokens';
|
2023-01-23 20:30:43 +01:00
|
|
|
import { Numeric } from '../../../../shared/modules/Numeric';
|
2023-04-19 01:23:45 +02:00
|
|
|
import { estimateGas } from '../../../store/actions';
|
|
|
|
import { getCustomTxParamsData } from '../../../pages/confirm-approve/confirm-approve.util';
|
|
|
|
import { useGasFeeContext } from '../../../contexts/gasFee';
|
2023-07-11 16:57:59 +02:00
|
|
|
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
|
2022-09-28 21:46:29 +02:00
|
|
|
import { CustomSpendingCapTooltip } from './custom-spending-cap-tooltip';
|
|
|
|
|
|
|
|
export default function CustomSpendingCap({
|
2023-04-19 01:23:45 +02:00
|
|
|
txParams,
|
2022-09-28 21:46:29 +02:00
|
|
|
tokenName,
|
|
|
|
currentTokenBalance,
|
|
|
|
dappProposedValue,
|
|
|
|
siteOrigin,
|
2022-11-07 19:21:38 +01:00
|
|
|
passTheErrorText,
|
2023-01-05 15:58:16 +01:00
|
|
|
decimals,
|
2023-04-19 01:23:45 +02:00
|
|
|
setInputChangeInProgress,
|
2023-07-11 16:57:59 +02:00
|
|
|
customSpendingCap,
|
|
|
|
setCustomSpendingCap,
|
2022-09-28 21:46:29 +02:00
|
|
|
}) {
|
|
|
|
const t = useContext(I18nContext);
|
2022-11-07 19:21:38 +01:00
|
|
|
const dispatch = useDispatch();
|
2023-04-19 01:23:45 +02:00
|
|
|
const { updateTransaction } = useGasFeeContext();
|
2023-04-13 19:24:33 +02:00
|
|
|
const inputRef = useRef(null);
|
2022-11-07 19:21:38 +01:00
|
|
|
|
2022-09-28 21:46:29 +02:00
|
|
|
const [error, setError] = useState('');
|
2023-07-11 16:57:59 +02:00
|
|
|
const [showUseSiteSuggestionButton, setShowUseSiteSuggestionButton] =
|
|
|
|
useState(customSpendingCap !== String(dappProposedValue) && true);
|
2022-09-28 21:46:29 +02:00
|
|
|
const inputLogicEmptyStateText = t('inputLogicEmptyState');
|
|
|
|
|
2023-01-05 15:58:16 +01:00
|
|
|
const replaceCommaToDot = (inputValue) => {
|
|
|
|
return inputValue.replace(/,/gu, '.');
|
|
|
|
};
|
|
|
|
|
|
|
|
const decConversionGreaterThan = (tokenValue, tokenBalance) => {
|
2023-01-23 20:30:43 +01:00
|
|
|
return new Numeric(Number(replaceCommaToDot(tokenValue)), 10).greaterThan(
|
|
|
|
Number(tokenBalance),
|
|
|
|
10,
|
2023-01-05 15:58:16 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-28 21:46:29 +02:00
|
|
|
const getInputTextLogic = (inputNumber) => {
|
2023-01-05 15:58:16 +01:00
|
|
|
if (
|
2023-01-23 20:30:43 +01:00
|
|
|
new Numeric(Number(replaceCommaToDot(inputNumber)), 10).lessThanOrEqualTo(
|
|
|
|
new Numeric(Number(currentTokenBalance), 10),
|
2023-01-05 15:58:16 +01:00
|
|
|
)
|
|
|
|
) {
|
2022-09-28 21:46:29 +02:00
|
|
|
return {
|
|
|
|
className: 'custom-spending-cap__lowerValue',
|
|
|
|
description: t('inputLogicEqualOrSmallerNumber', [
|
2023-04-06 13:10:14 +02:00
|
|
|
<Text
|
2022-09-28 21:46:29 +02:00
|
|
|
key="custom-spending-cap"
|
2023-04-06 13:10:14 +02:00
|
|
|
variant={TextVariant.bodySmBold}
|
2023-07-19 19:55:38 +02:00
|
|
|
as="span"
|
2022-09-28 21:46:29 +02:00
|
|
|
className="custom-spending-cap__input-value-and-token-name"
|
|
|
|
>
|
2023-01-05 15:58:16 +01:00
|
|
|
{replaceCommaToDot(inputNumber)} {tokenName}
|
2023-04-06 13:10:14 +02:00
|
|
|
</Text>,
|
2022-09-28 21:46:29 +02:00
|
|
|
]),
|
|
|
|
};
|
2023-01-05 15:58:16 +01:00
|
|
|
} else if (decConversionGreaterThan(inputNumber, currentTokenBalance)) {
|
2022-09-28 21:46:29 +02:00
|
|
|
return {
|
|
|
|
className: 'custom-spending-cap__higherValue',
|
|
|
|
description: t('inputLogicHigherNumber'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
className: 'custom-spending-cap__emptyState',
|
|
|
|
description: t('inputLogicEmptyState'),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-11-07 19:21:38 +01:00
|
|
|
const [customSpendingCapText, setCustomSpendingCapText] = useState(
|
2023-07-11 16:57:59 +02:00
|
|
|
getInputTextLogic(customSpendingCap).description,
|
2022-11-07 19:21:38 +01:00
|
|
|
);
|
|
|
|
|
2023-04-19 01:23:45 +02:00
|
|
|
const handleChange = async (valueInput) => {
|
|
|
|
if (!txParams) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setInputChangeInProgress(true);
|
2022-09-28 21:46:29 +02:00
|
|
|
let spendingCapError = '';
|
|
|
|
const inputTextLogic = getInputTextLogic(valueInput);
|
|
|
|
const inputTextLogicDescription = inputTextLogic.description;
|
2023-01-16 18:41:46 +01:00
|
|
|
const match = DECIMAL_REGEX.exec(replaceCommaToDot(valueInput));
|
|
|
|
if (match?.[1]?.length > decimals) {
|
2023-04-19 01:23:45 +02:00
|
|
|
setInputChangeInProgress(false);
|
2023-01-16 18:41:46 +01:00
|
|
|
return;
|
|
|
|
}
|
2022-09-28 21:46:29 +02:00
|
|
|
|
2023-01-18 10:53:10 +01:00
|
|
|
if (valueInput && !NUM_W_OPT_DECIMAL_COMMA_OR_DOT_REGEX.test(valueInput)) {
|
2022-09-28 21:46:29 +02:00
|
|
|
spendingCapError = t('spendingCapError');
|
|
|
|
setCustomSpendingCapText(t('spendingCapErrorDescription', [siteOrigin]));
|
|
|
|
setError(spendingCapError);
|
|
|
|
} else {
|
|
|
|
setCustomSpendingCapText(inputTextLogicDescription);
|
|
|
|
setError('');
|
|
|
|
}
|
|
|
|
|
2023-01-05 15:58:16 +01:00
|
|
|
const maxTokenAmount = calcTokenAmount(
|
|
|
|
MAX_TOKEN_ALLOWANCE_AMOUNT,
|
|
|
|
decimals,
|
|
|
|
);
|
|
|
|
if (Number(valueInput.length) > 1 && Number(valueInput)) {
|
|
|
|
const customSpendLimitNumber = new BigNumber(valueInput);
|
|
|
|
if (customSpendLimitNumber.greaterThan(maxTokenAmount)) {
|
|
|
|
spendingCapError = t('spendLimitTooLarge');
|
|
|
|
setError(spendingCapError);
|
|
|
|
}
|
|
|
|
}
|
2023-07-11 16:57:59 +02:00
|
|
|
setCustomSpendingCap(String(valueInput));
|
2022-11-07 19:21:38 +01:00
|
|
|
dispatch(setCustomTokenAmount(String(valueInput)));
|
2023-04-19 01:23:45 +02:00
|
|
|
|
2023-07-11 16:57:59 +02:00
|
|
|
if (String(valueInput) !== '') {
|
|
|
|
try {
|
|
|
|
const newData = getCustomTxParamsData(txParams.data, {
|
|
|
|
customPermissionAmount: valueInput,
|
|
|
|
decimals,
|
|
|
|
});
|
|
|
|
const { from, to, value: txValue } = txParams;
|
|
|
|
const estimatedGasLimit = await estimateGas({
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
value: txValue,
|
|
|
|
data: newData,
|
2023-04-19 01:23:45 +02:00
|
|
|
});
|
2023-07-11 16:57:59 +02:00
|
|
|
if (estimatedGasLimit) {
|
|
|
|
await updateTransaction({
|
|
|
|
gasLimit: hexToDecimal(addHexPrefix(estimatedGasLimit)),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (exp) {
|
|
|
|
console.error('Error in trying to update gas limit', exp);
|
2023-04-19 01:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-11 16:57:59 +02:00
|
|
|
|
2023-04-19 01:23:45 +02:00
|
|
|
setInputChangeInProgress(false);
|
2022-09-28 21:46:29 +02:00
|
|
|
};
|
|
|
|
|
2022-11-07 19:21:38 +01:00
|
|
|
useEffect(() => {
|
2023-07-11 16:57:59 +02:00
|
|
|
if (customSpendingCap === String(dappProposedValue)) {
|
|
|
|
setShowUseSiteSuggestionButton(false);
|
|
|
|
} else {
|
|
|
|
setShowUseSiteSuggestionButton(true);
|
2022-11-07 19:21:38 +01:00
|
|
|
}
|
2023-07-11 16:57:59 +02:00
|
|
|
}, [customSpendingCap, dappProposedValue]);
|
2022-11-07 19:21:38 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
passTheErrorText(error);
|
|
|
|
}, [error, passTheErrorText]);
|
|
|
|
|
2023-04-13 19:24:33 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (inputRef.current) {
|
|
|
|
inputRef.current.focus({
|
|
|
|
preventScroll: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [inputRef.current]);
|
|
|
|
|
2023-01-05 15:58:16 +01:00
|
|
|
const chooseTooltipContentText = decConversionGreaterThan(
|
2023-07-11 16:57:59 +02:00
|
|
|
customSpendingCap,
|
2023-01-05 15:58:16 +01:00
|
|
|
currentTokenBalance,
|
|
|
|
)
|
|
|
|
? t('warningTooltipText', [
|
2023-04-06 13:10:14 +02:00
|
|
|
<Text
|
2023-01-05 15:58:16 +01:00
|
|
|
key="tooltip-text"
|
2023-04-06 13:10:14 +02:00
|
|
|
variant={TextVariant.bodySmBold}
|
2023-07-19 19:55:38 +02:00
|
|
|
as="span"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.errorDefault}
|
2023-01-05 15:58:16 +01:00
|
|
|
>
|
2023-04-19 23:16:49 +02:00
|
|
|
<Icon name={IconName.Warning} /> {t('beCareful')}
|
2023-04-06 13:10:14 +02:00
|
|
|
</Text>,
|
2023-01-05 15:58:16 +01:00
|
|
|
])
|
|
|
|
: t('inputLogicEmptyState');
|
2022-09-28 21:46:29 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Box
|
|
|
|
className="custom-spending-cap"
|
2023-02-02 21:15:26 +01:00
|
|
|
borderRadius={Size.SM}
|
2022-09-28 21:46:29 +02:00
|
|
|
paddingTop={2}
|
|
|
|
paddingRight={6}
|
|
|
|
paddingLeft={6}
|
2023-07-18 07:03:46 +02:00
|
|
|
display={Display.Flex}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.flexStart}
|
2023-07-18 07:03:46 +02:00
|
|
|
flexDirection={FlexDirection.Column}
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.backgroundAlternative}
|
2022-09-28 21:46:29 +02:00
|
|
|
gap={2}
|
|
|
|
>
|
|
|
|
<Box
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent={JustifyContent.center}
|
2023-07-18 07:03:46 +02:00
|
|
|
display={Display.Block}
|
2022-09-28 21:46:29 +02:00
|
|
|
className="custom-spending-cap__input"
|
|
|
|
>
|
|
|
|
<label
|
|
|
|
htmlFor={
|
2023-07-11 16:57:59 +02:00
|
|
|
decConversionGreaterThan(customSpendingCap, currentTokenBalance)
|
2022-09-28 21:46:29 +02:00
|
|
|
? 'custom-spending-cap-input-value'
|
|
|
|
: 'custom-spending-cap'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<FormField
|
2023-04-13 19:24:33 +02:00
|
|
|
inputRef={inputRef}
|
2022-09-28 21:46:29 +02:00
|
|
|
dataTestId="custom-spending-cap-input"
|
|
|
|
wrappingLabelProps={{ as: 'div' }}
|
|
|
|
id={
|
2023-07-11 16:57:59 +02:00
|
|
|
decConversionGreaterThan(customSpendingCap, currentTokenBalance)
|
2022-09-28 21:46:29 +02:00
|
|
|
? 'custom-spending-cap-input-value'
|
|
|
|
: 'custom-spending-cap'
|
|
|
|
}
|
|
|
|
TooltipCustomComponent={
|
|
|
|
<CustomSpendingCapTooltip
|
2023-01-05 15:58:16 +01:00
|
|
|
tooltipContentText={
|
2023-07-11 16:57:59 +02:00
|
|
|
replaceCommaToDot(customSpendingCap)
|
|
|
|
? chooseTooltipContentText
|
|
|
|
: ''
|
2023-01-05 15:58:16 +01:00
|
|
|
}
|
|
|
|
tooltipIcon={
|
2023-07-11 16:57:59 +02:00
|
|
|
replaceCommaToDot(customSpendingCap)
|
|
|
|
? decConversionGreaterThan(
|
|
|
|
customSpendingCap,
|
|
|
|
currentTokenBalance,
|
|
|
|
)
|
2023-01-05 15:58:16 +01:00
|
|
|
: ''
|
|
|
|
}
|
2022-09-28 21:46:29 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
onChange={handleChange}
|
|
|
|
titleText={t('customSpendingCap')}
|
|
|
|
placeholder={t('enterANumber')}
|
|
|
|
error={error}
|
2023-07-11 16:57:59 +02:00
|
|
|
value={customSpendingCap}
|
2022-09-28 21:46:29 +02:00
|
|
|
titleDetail={
|
2023-07-11 16:57:59 +02:00
|
|
|
showUseSiteSuggestionButton && (
|
2023-01-04 18:01:52 +01:00
|
|
|
<ButtonLink
|
2023-02-02 21:15:26 +01:00
|
|
|
size={Size.auto}
|
2022-11-07 19:21:38 +01:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
2023-07-11 16:57:59 +02:00
|
|
|
setShowUseSiteSuggestionButton(false);
|
2022-09-28 21:46:29 +02:00
|
|
|
handleChange(dappProposedValue);
|
2022-11-07 19:21:38 +01:00
|
|
|
}}
|
|
|
|
>
|
2023-07-11 16:57:59 +02:00
|
|
|
{t('useSiteSuggestion')}
|
2023-01-04 18:01:52 +01:00
|
|
|
</ButtonLink>
|
2022-11-07 19:21:38 +01:00
|
|
|
)
|
2022-09-28 21:46:29 +02:00
|
|
|
}
|
|
|
|
titleDetailWrapperProps={{ marginBottom: 2, marginRight: 0 }}
|
|
|
|
/>
|
2023-01-04 18:01:52 +01:00
|
|
|
<Box
|
|
|
|
width={BLOCK_SIZES.MAX}
|
|
|
|
marginLeft="auto"
|
|
|
|
paddingRight={4}
|
|
|
|
paddingBottom={2}
|
2023-05-23 21:29:14 +02:00
|
|
|
textAlign={TextAlign.End}
|
2023-01-16 18:41:46 +01:00
|
|
|
className={classnames('custom-spending-cap__max', {
|
|
|
|
'custom-spending-cap__max--with-error-message': error,
|
|
|
|
})}
|
2023-01-04 18:01:52 +01:00
|
|
|
>
|
|
|
|
<ButtonLink
|
2023-02-02 21:15:26 +01:00
|
|
|
size={Size.auto}
|
2023-01-04 18:01:52 +01:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
handleChange(currentTokenBalance);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('max')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
2023-01-16 18:41:46 +01:00
|
|
|
<Box
|
|
|
|
className={classnames('custom-spending-cap__description', {
|
|
|
|
'custom-spending-cap__description--with-error-message': error,
|
|
|
|
})}
|
2022-09-28 21:46:29 +02:00
|
|
|
>
|
2023-04-06 13:10:14 +02:00
|
|
|
<Text
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
2023-04-06 13:10:14 +02:00
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
|
|
|
paddingTop={2}
|
2023-01-16 18:41:46 +01:00
|
|
|
>
|
2023-07-11 16:57:59 +02:00
|
|
|
{replaceCommaToDot(customSpendingCap)
|
2023-01-16 18:41:46 +01:00
|
|
|
? customSpendingCapText
|
|
|
|
: inputLogicEmptyStateText}
|
2023-04-06 13:10:14 +02:00
|
|
|
</Text>
|
2023-07-11 16:57:59 +02:00
|
|
|
<ButtonLink
|
|
|
|
size={Size.SM}
|
|
|
|
href={ZENDESK_URLS.TOKEN_ALLOWANCE_WITH_SPENDING_CAP}
|
|
|
|
target="_blank"
|
|
|
|
marginBottom={2}
|
|
|
|
>
|
|
|
|
{t('learnMoreUpperCase')}
|
|
|
|
</ButtonLink>
|
2023-01-16 18:41:46 +01:00
|
|
|
</Box>
|
2022-09-28 21:46:29 +02:00
|
|
|
</label>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomSpendingCap.propTypes = {
|
2023-04-19 01:23:45 +02:00
|
|
|
/**
|
|
|
|
* Transaction params
|
|
|
|
*/
|
|
|
|
txParams: PropTypes.object.isRequired,
|
2022-09-28 21:46:29 +02:00
|
|
|
/**
|
|
|
|
* Displayed the token name currently tracked in description related to the input state
|
|
|
|
*/
|
|
|
|
tokenName: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* The current token balance of the token
|
|
|
|
*/
|
2023-01-05 15:58:16 +01:00
|
|
|
currentTokenBalance: PropTypes.string,
|
2022-09-28 21:46:29 +02:00
|
|
|
/**
|
|
|
|
* The dapp suggested amount
|
|
|
|
*/
|
2023-01-05 15:58:16 +01:00
|
|
|
dappProposedValue: PropTypes.string,
|
2022-09-28 21:46:29 +02:00
|
|
|
/**
|
|
|
|
* The origin of the site generally the URL
|
|
|
|
*/
|
|
|
|
siteOrigin: PropTypes.string,
|
|
|
|
/**
|
2022-11-07 19:21:38 +01:00
|
|
|
* Parent component's callback function passed in order to get the error text
|
2022-09-28 21:46:29 +02:00
|
|
|
*/
|
2022-11-07 19:21:38 +01:00
|
|
|
passTheErrorText: PropTypes.func,
|
2023-01-05 15:58:16 +01:00
|
|
|
/**
|
|
|
|
* Number of decimals
|
|
|
|
*/
|
|
|
|
decimals: PropTypes.string,
|
2023-04-19 01:23:45 +02:00
|
|
|
/**
|
|
|
|
* Updating input state to changing
|
|
|
|
*/
|
|
|
|
setInputChangeInProgress: PropTypes.func.isRequired,
|
2023-07-11 16:57:59 +02:00
|
|
|
/**
|
|
|
|
* Custom token amount or The dapp suggested amount
|
|
|
|
*/
|
|
|
|
customSpendingCap: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* State method to update the custom token value
|
|
|
|
*/
|
|
|
|
setCustomSpendingCap: PropTypes.func.isRequired,
|
2022-09-28 21:46:29 +02:00
|
|
|
};
|