2021-12-03 16:59:48 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
import { useGasFeeContext } from '../../../../contexts/gasFee';
|
2021-12-06 19:47:26 +01:00
|
|
|
import { bnGreaterThan, bnLessThan } from '../../../../helpers/utils/util';
|
2021-12-03 16:59:48 +01:00
|
|
|
import { TYPOGRAPHY } from '../../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../../hooks/useI18nContext';
|
2021-12-06 19:47:26 +01:00
|
|
|
import { MAX_GAS_LIMIT_DEC } from '../../../../pages/send/send.constants';
|
2021-12-03 16:59:48 +01:00
|
|
|
import Button from '../../../ui/button';
|
|
|
|
import FormField from '../../../ui/form-field';
|
|
|
|
import I18nValue from '../../../ui/i18n-value';
|
|
|
|
import Typography from '../../../ui/typography';
|
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
import { useAdvancedGasFeePopoverContext } from '../context';
|
|
|
|
|
|
|
|
const validateGasLimit = (gasLimit, minimumGasLimitDec) => {
|
|
|
|
return bnLessThan(gasLimit, minimumGasLimitDec) ||
|
|
|
|
bnGreaterThan(gasLimit, MAX_GAS_LIMIT_DEC)
|
|
|
|
? 'editGasLimitOutOfBoundsV2'
|
|
|
|
: null;
|
|
|
|
};
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
const AdvancedGasFeeGasLimit = () => {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const {
|
|
|
|
setGasLimit: setGasLimitInContext,
|
2021-12-06 19:47:26 +01:00
|
|
|
} = useAdvancedGasFeePopoverContext();
|
|
|
|
const {
|
|
|
|
gasLimit: gasLimitInTransaction,
|
|
|
|
minimumGasLimitDec,
|
|
|
|
} = useGasFeeContext();
|
2021-12-03 16:59:48 +01:00
|
|
|
const [isEditing, setEditing] = useState(false);
|
|
|
|
const [gasLimit, setGasLimit] = useState(gasLimitInTransaction);
|
2021-12-06 19:47:26 +01:00
|
|
|
const [gasLimitError, setGasLimitError] = useState();
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
const updateGasLimit = (value) => {
|
|
|
|
setGasLimit(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setGasLimitInContext(gasLimit);
|
2021-12-06 19:47:26 +01:00
|
|
|
const error = validateGasLimit(gasLimit, minimumGasLimitDec);
|
|
|
|
setGasLimitError(error);
|
2021-12-08 23:56:10 +01:00
|
|
|
}, [gasLimit, minimumGasLimitDec, setGasLimitInContext]);
|
2021-12-03 16:59:48 +01:00
|
|
|
|
|
|
|
if (isEditing) {
|
|
|
|
return (
|
|
|
|
<FormField
|
2021-12-06 19:47:26 +01:00
|
|
|
error={
|
|
|
|
gasLimitError
|
|
|
|
? t(gasLimitError, [minimumGasLimitDec - 1, MAX_GAS_LIMIT_DEC])
|
|
|
|
: ''
|
|
|
|
}
|
2021-12-03 16:59:48 +01:00
|
|
|
onChange={updateGasLimit}
|
|
|
|
titleText={t('gasLimitV2')}
|
|
|
|
value={gasLimit}
|
|
|
|
numeric
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-12-06 19:47:26 +01:00
|
|
|
<Typography
|
|
|
|
tag={TYPOGRAPHY.Paragraph}
|
|
|
|
variant={TYPOGRAPHY.H7}
|
|
|
|
className="advanced-gas-fee-gas-limit"
|
2021-12-21 20:45:28 +01:00
|
|
|
margin={[0, 2]}
|
2021-12-06 19:47:26 +01:00
|
|
|
>
|
|
|
|
<strong>
|
|
|
|
<I18nValue messageKey="gasLimitV2" />
|
|
|
|
</strong>
|
|
|
|
<span>{gasLimit}</span>
|
|
|
|
<Button
|
|
|
|
className="advanced-gas-fee-gas-limit__edit-link"
|
|
|
|
onClick={() => setEditing(true)}
|
|
|
|
type="link"
|
2021-12-03 16:59:48 +01:00
|
|
|
>
|
2021-12-06 19:47:26 +01:00
|
|
|
<I18nValue messageKey="edit" />
|
|
|
|
</Button>
|
2021-12-03 16:59:48 +01:00
|
|
|
</Typography>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AdvancedGasFeeGasLimit;
|