1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 14:15:06 +01:00
metamask-extension/ui/components/app/edit-gas-popover/edit-gas-popover.component.js
Olusegun Akintayo c54f9b3097
update gas fees (#13646)
* Draft methods to brak updateTransaction into smaller more targeted
methods.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* This is a combination of 76 commits.

normalize and validate tx params.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Method to normalize tx and check if it's unapproved.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Move the methods to controllers/transactions/index.js

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Flesh out the methods to update transaction with custom notes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

enforce that only the properties for the specific methid can be updated via the method.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Test update gas fees

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update swap approval transaction

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

use lodash to remove undefined properties
update swap transaction tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Updates transaction user settings.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add more parameters to updateSwapTransaction
    approvalTxId
    estimatedBaseFee

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add Update Transaction Metrics

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update transaction gas fees actions.js

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update EIP 1559 Params.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint Fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Documentations.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove metrics from this PR

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes: Removed unused variables

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add more params to updateTransactionGasFees.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update eip1559 method to editableParams.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Fix Mocha tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

add gasPrice to updateEditableParams

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove duplicated Params in notes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

A few more tests to cover if
    transaction status is not unapproved
    transaction is passed more parameters than it requires.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update Transaction Gas Fees.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update gas fees in edit-gas-popover.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove metrics.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update gas settings and user settings.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Fix unit tests.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Draft methods to brak updateTransaction into smaller more targeted
methods.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

normalize and validate tx params.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Method to normalize tx and check if it's unapproved.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Move the methods to controllers/transactions/index.js

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Flesh out the methods to update transaction with custom notes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Test update gas fees

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update swap approval transaction

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

use lodash to remove undefined properties
update swap transaction tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Updates transaction user settings.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add Update Transaction Metrics

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update transaction gas fees actions.js

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update EIP 1559 Params.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint Fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Documentations.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove metrics from this PR

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes: Removed unused variables

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Add more params to updateTransactionGasFees.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update eip1559 method to editableParams.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Fix Mocha tests

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

add gasPrice to updateEditableParams

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove duplicated Params in notes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

A few more tests to cover if
    transaction status is not unapproved
    transaction is passed more parameters than it requires.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update Transaction Gas Fees.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove metrics.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Update gas settings and user settings.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Fix unit tests.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Remove dup;icated method from rebase.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

unrelated change

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Force re-run workflow

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fix

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Do not hideLoading since we're not showing it.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

UpdateTransaction should be renamed to updateGasFees

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

updateGasFees in gas-modal-page-container.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

fix:
    update previous gas params update method
    add types to the jsdoc comments.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

updateTransactionGasFees should have been updatePreviousGasParams

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Previous gas fees can be updated for confirmed transactions.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

add updatePreviousGasParams to mocked functions.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

Lint fixes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* we need to await the first dispatch before we call the second

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* update values to make tests pass

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* More changes to make e2e pass

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Need to wait a bit after save for changes to take effect.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Remove merge comments.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Await one dispatch before calling another

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* We don't need goHome anymore.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Tests must use async...await syntax too since we have await in the
useTranasctionFunction

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Add delay after button click for values to update

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Wait a moment after clicking save for values to update

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Wait after clicking save...

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Merge update transaction gas fees and transaction user settings
Show loading indicator on edit gas popover
Fix tests.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Lint fixes

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* Fix JSDoc

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>

* updatePreviousGasParams should also return updated transaction meta.

Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-03-25 21:11:04 +04:00

332 lines
10 KiB
JavaScript

import React, { useCallback, useContext, useState } from 'react';
import PropTypes from 'prop-types';
import { useDispatch, useSelector } from 'react-redux';
import { useGasFeeInputs } from '../../../hooks/gasFeeInput/useGasFeeInputs';
import { getGasLoadingAnimationIsShowing } from '../../../ducks/app/app';
import { txParamsAreDappSuggested } from '../../../../shared/modules/transaction.utils';
import {
EDIT_GAS_MODES,
GAS_LIMITS,
GAS_RECOMMENDATIONS,
CUSTOM_GAS_ESTIMATE,
} from '../../../../shared/constants/gas';
import {
decGWEIToHexWEI,
decimalToHex,
hexToDecimal,
} from '../../../helpers/utils/conversions.util';
import Popover from '../../ui/popover';
import Button from '../../ui/button';
import EditGasDisplay from '../edit-gas-display';
import EditGasDisplayEducation from '../edit-gas-display-education';
import { I18nContext } from '../../../contexts/i18n';
import {
createCancelTransaction,
createSpeedUpTransaction,
hideModal,
updateTransactionGasFees,
updateCustomSwapsEIP1559GasParams,
updateSwapsUserFeeLevel,
hideLoadingIndication,
showLoadingIndication,
} from '../../../store/actions';
import LoadingHeartBeat from '../../ui/loading-heartbeat';
import { checkNetworkAndAccountSupports1559 } from '../../../selectors';
import { useIncrementedGasFees } from '../../../hooks/useIncrementedGasFees';
import { isLegacyTransaction } from '../../../helpers/utils/transactions.util';
export default function EditGasPopover({
popoverTitle = '',
confirmButtonText = '',
editGasDisplayProps = {},
defaultEstimateToUse = GAS_RECOMMENDATIONS.MEDIUM,
transaction,
mode,
onClose,
minimumGasLimit = GAS_LIMITS.SIMPLE,
}) {
const t = useContext(I18nContext);
const dispatch = useDispatch();
const supportsEIP1559 =
useSelector(checkNetworkAndAccountSupports1559) &&
!isLegacyTransaction(transaction?.txParams);
const gasLoadingAnimationIsShowing = useSelector(
getGasLoadingAnimationIsShowing,
);
const showEducationButton =
(mode === EDIT_GAS_MODES.MODIFY_IN_PLACE ||
mode === EDIT_GAS_MODES.SWAPS) &&
supportsEIP1559;
const [showEducationContent, setShowEducationContent] = useState(false);
const [
dappSuggestedGasFeeAcknowledged,
setDappSuggestedGasFeeAcknowledged,
] = useState(false);
const minimumGasLimitDec = hexToDecimal(minimumGasLimit);
const updatedCustomGasSettings = useIncrementedGasFees(transaction);
let updatedTransaction = transaction;
if (mode === EDIT_GAS_MODES.SPEED_UP || mode === EDIT_GAS_MODES.CANCEL) {
updatedTransaction = {
...transaction,
userFeeLevel: CUSTOM_GAS_ESTIMATE,
txParams: {
...transaction.txParams,
...updatedCustomGasSettings,
},
};
}
const {
maxPriorityFeePerGas,
setMaxPriorityFeePerGas,
maxPriorityFeePerGasFiat,
maxFeePerGas,
setMaxFeePerGas,
maxFeePerGasFiat,
estimatedMaximumNative,
estimatedMinimumNative,
isGasEstimatesLoading,
gasEstimateType,
gasPrice,
setGasPrice,
gasLimit,
setGasLimit,
properGasLimit,
estimateToUse,
setEstimateToUse,
estimatedMinimumFiat,
estimatedMaximumFiat,
hasGasErrors,
gasErrors,
gasWarnings,
onManualChange,
balanceError,
estimatesUnavailableWarning,
estimatedBaseFee,
isNetworkBusy,
} = useGasFeeInputs(
defaultEstimateToUse,
updatedTransaction,
minimumGasLimit,
mode,
);
const txParamsHaveBeenCustomized =
estimateToUse === CUSTOM_GAS_ESTIMATE ||
txParamsAreDappSuggested(updatedTransaction);
/**
* Temporary placeholder, this should be managed by the parent component but
* we will be extracting this component from the hard to maintain modal
* component. For now this is just to be able to appropriately close
* the modal in testing
*/
const closePopover = useCallback(() => {
if (onClose) {
onClose();
} else {
dispatch(hideModal());
}
}, [onClose, dispatch]);
const onSubmit = useCallback(async () => {
if (!updatedTransaction || !mode) {
closePopover();
}
const newGasSettings = {
gas: decimalToHex(gasLimit),
gasLimit: decimalToHex(gasLimit),
estimateSuggested: defaultEstimateToUse,
estimateUsed: estimateToUse,
};
if (supportsEIP1559) {
newGasSettings.maxFeePerGas = decGWEIToHexWEI(maxFeePerGas ?? gasPrice);
newGasSettings.maxPriorityFeePerGas = decGWEIToHexWEI(
maxPriorityFeePerGas ?? maxFeePerGas ?? gasPrice,
);
} else {
newGasSettings.gasPrice = decGWEIToHexWEI(gasPrice);
}
const cleanTransactionParams = { ...updatedTransaction.txParams };
if (supportsEIP1559) {
delete cleanTransactionParams.gasPrice;
}
const updatedTxMeta = {
...updatedTransaction,
userEditedGasLimit: gasLimit !== Number(transaction.originalGasEstimate),
userFeeLevel: estimateToUse || CUSTOM_GAS_ESTIMATE,
txParams: {
...cleanTransactionParams,
...newGasSettings,
},
};
switch (mode) {
case EDIT_GAS_MODES.CANCEL:
dispatch(
createCancelTransaction(updatedTransaction.id, newGasSettings, {
estimatedBaseFee,
}),
);
break;
case EDIT_GAS_MODES.SPEED_UP:
dispatch(
createSpeedUpTransaction(updatedTransaction.id, newGasSettings, {
estimatedBaseFee,
}),
);
break;
case EDIT_GAS_MODES.MODIFY_IN_PLACE:
newGasSettings.userEditedGasLimit = updatedTxMeta.userEditedGasLimit;
newGasSettings.userFeeLevel = updatedTxMeta.userFeeLevel;
dispatch(showLoadingIndication());
await dispatch(
updateTransactionGasFees(updatedTxMeta.id, newGasSettings),
);
dispatch(hideLoadingIndication());
break;
case EDIT_GAS_MODES.SWAPS:
// This popover component should only be used for the "FEE_MARKET" type in Swaps.
if (supportsEIP1559) {
dispatch(
updateSwapsUserFeeLevel(estimateToUse || CUSTOM_GAS_ESTIMATE),
);
dispatch(updateCustomSwapsEIP1559GasParams(newGasSettings));
}
break;
default:
break;
}
closePopover();
}, [
updatedTransaction,
mode,
dispatch,
closePopover,
gasLimit,
gasPrice,
transaction.originalGasEstimate,
maxFeePerGas,
maxPriorityFeePerGas,
supportsEIP1559,
estimateToUse,
estimatedBaseFee,
defaultEstimateToUse,
]);
let title = t('editGasTitle');
if (popoverTitle) {
title = popoverTitle;
} else if (showEducationContent) {
title = t('editGasEducationModalTitle');
} else if (mode === EDIT_GAS_MODES.SPEED_UP) {
title = t('speedUpPopoverTitle');
} else if (mode === EDIT_GAS_MODES.CANCEL) {
title = t('cancelPopoverTitle');
}
const footerButtonText = confirmButtonText || t('save');
return (
<Popover
title={title}
onClose={closePopover}
className="edit-gas-popover__wrapper"
onBack={
showEducationContent ? () => setShowEducationContent(false) : undefined
}
footer={
showEducationContent ? null : (
<>
<Button
type="primary"
onClick={onSubmit}
disabled={
hasGasErrors ||
balanceError ||
((isGasEstimatesLoading || gasLoadingAnimationIsShowing) &&
!txParamsHaveBeenCustomized)
}
>
{footerButtonText}
</Button>
</>
)
}
>
<div style={{ padding: '0 20px 20px 20px', position: 'relative' }}>
{showEducationContent ? (
<EditGasDisplayEducation />
) : (
<>
{process.env.IN_TEST ? null : <LoadingHeartBeat />}
<EditGasDisplay
showEducationButton={showEducationButton}
dappSuggestedGasFeeAcknowledged={dappSuggestedGasFeeAcknowledged}
setDappSuggestedGasFeeAcknowledged={
setDappSuggestedGasFeeAcknowledged
}
maxPriorityFeePerGas={maxPriorityFeePerGas}
setMaxPriorityFeePerGas={setMaxPriorityFeePerGas}
maxPriorityFeePerGasFiat={maxPriorityFeePerGasFiat}
maxFeePerGas={maxFeePerGas}
setMaxFeePerGas={setMaxFeePerGas}
maxFeePerGasFiat={maxFeePerGasFiat}
estimatedMaximumNative={estimatedMaximumNative}
estimatedMinimumNative={estimatedMinimumNative}
isGasEstimatesLoading={isGasEstimatesLoading}
gasEstimateType={gasEstimateType}
gasPrice={gasPrice}
setGasPrice={setGasPrice}
gasLimit={gasLimit}
setGasLimit={setGasLimit}
properGasLimit={properGasLimit}
estimateToUse={estimateToUse}
setEstimateToUse={setEstimateToUse}
estimatedMinimumFiat={estimatedMinimumFiat}
estimatedMaximumFiat={estimatedMaximumFiat}
onEducationClick={() => setShowEducationContent(true)}
mode={mode}
transaction={updatedTransaction}
gasErrors={gasErrors}
gasWarnings={gasWarnings}
onManualChange={onManualChange}
minimumGasLimit={minimumGasLimitDec}
balanceError={balanceError}
estimatesUnavailableWarning={estimatesUnavailableWarning}
hasGasErrors={hasGasErrors}
txParamsHaveBeenCustomized={txParamsHaveBeenCustomized}
isNetworkBusy={isNetworkBusy}
{...editGasDisplayProps}
/>
</>
)}
</div>
</Popover>
);
}
EditGasPopover.propTypes = {
popoverTitle: PropTypes.string,
editGasDisplayProps: PropTypes.object,
confirmButtonText: PropTypes.string,
onClose: PropTypes.func,
transaction: PropTypes.object,
mode: PropTypes.oneOf(Object.values(EDIT_GAS_MODES)),
defaultEstimateToUse: PropTypes.string,
minimumGasLimit: PropTypes.string,
};