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

Reset price ack if price diff in % changed, insufficient gas event, log a page load event once (#19961)

This commit is contained in:
Daniel 2023-08-01 18:18:14 +02:00 committed by GitHub
parent 10ffc1ec84
commit 24ca518315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 15 deletions

View File

@ -624,6 +624,7 @@ export enum MetaMetricsEventName {
SwapCompleted = 'Swap Completed', SwapCompleted = 'Swap Completed',
TransactionFinalized = 'Transaction Finalized', TransactionFinalized = 'Transaction Finalized',
ExitedSwaps = 'Exited Swaps', ExitedSwaps = 'Exited Swaps',
SwapError = 'Swap Error',
} }
export enum MetaMetricsEventAccountType { export enum MetaMetricsEventAccountType {
@ -681,6 +682,10 @@ export enum MetaMetricsEventKeyType {
Srp = 'srp', Srp = 'srp',
} }
export enum MetaMetricsEventErrorType {
InsufficientGas = 'insufficient_gas',
}
export enum MetaMetricsNetworkEventSource { export enum MetaMetricsNetworkEventSource {
CustomNetworkForm = 'custom_network_form', CustomNetworkForm = 'custom_network_form',
PopularNetworkList = 'popular_network_list', PopularNetworkList = 'popular_network_list',

View File

@ -99,6 +99,7 @@ import {
setSwapsErrorKey, setSwapsErrorKey,
setBackgroundSwapRouteState, setBackgroundSwapRouteState,
} from '../../../store/actions'; } from '../../../store/actions';
import { SET_SMART_TRANSACTIONS_ERROR } from '../../../store/actionConstants';
import { import {
countDecimals, countDecimals,
fetchTokenPrice, fetchTokenPrice,
@ -556,7 +557,7 @@ export default function PrepareSwapPage({
dispatch(resetSwapsPostFetchState()); dispatch(resetSwapsPostFetchState());
dispatch(setReviewSwapClickedTimestamp()); dispatch(setReviewSwapClickedTimestamp());
trackPrepareSwapPageLoadedEvent(); trackPrepareSwapPageLoadedEvent();
}, [dispatch, trackPrepareSwapPageLoadedEvent]); }, [dispatch]);
const BlockExplorerLink = () => { const BlockExplorerLink = () => {
return ( return (
@ -630,6 +631,10 @@ export default function PrepareSwapPage({
if (!isReviewSwapButtonDisabled) { if (!isReviewSwapButtonDisabled) {
if (isSmartTransaction) { if (isSmartTransaction) {
clearSmartTransactionFees(); // Clean up STX fees eery time there is a form change. clearSmartTransactionFees(); // Clean up STX fees eery time there is a form change.
dispatch({
type: SET_SMART_TRANSACTIONS_ERROR,
payload: null,
});
} }
// Only do quotes prefetching if the Review swap button is enabled. // Only do quotes prefetching if the Review swap button is enabled.
prefetchQuotesWithoutRedirecting(); prefetchQuotesWithoutRedirecting();

View File

@ -74,7 +74,6 @@ import {
showModal, showModal,
setSwapsQuotesPollingLimitEnabled, setSwapsQuotesPollingLimitEnabled,
} from '../../../store/actions'; } from '../../../store/actions';
import { SET_SMART_TRANSACTIONS_ERROR } from '../../../store/actionConstants';
import { import {
ASSET_ROUTE, ASSET_ROUTE,
DEFAULT_ROUTE, DEFAULT_ROUTE,
@ -120,7 +119,11 @@ import {
ButtonLink, ButtonLink,
Text, Text,
} from '../../../components/component-library'; } from '../../../components/component-library';
import { MetaMetricsEventCategory } from '../../../../shared/constants/metametrics'; import {
MetaMetricsEventCategory,
MetaMetricsEventName,
MetaMetricsEventErrorType,
} from '../../../../shared/constants/metametrics';
import { isEqualCaseInsensitive } from '../../../../shared/modules/string-utils'; import { isEqualCaseInsensitive } from '../../../../shared/modules/string-utils';
import { parseStandardTokenTransactionData } from '../../../../shared/modules/transaction.utils'; import { parseStandardTokenTransactionData } from '../../../../shared/modules/transaction.utils';
import { getTokenValueParam } from '../../../../shared/lib/metamask-controller-utils'; import { getTokenValueParam } from '../../../../shared/lib/metamask-controller-utils';
@ -176,6 +179,7 @@ export default function ReviewQuote({ setReceiveToAmount }) {
}, [history, quotes, routeState]); }, [history, quotes, routeState]);
const quotesLastFetched = useSelector(getQuotesLastFetched); const quotesLastFetched = useSelector(getQuotesLastFetched);
const prevQuotesLastFetched = usePrevious(quotesLastFetched);
// Select necessary data // Select necessary data
const gasPrice = useSelector(getUsedSwapsGasPrice); const gasPrice = useSelector(getUsedSwapsGasPrice);
@ -504,6 +508,7 @@ export default function ReviewQuote({ setReceiveToAmount }) {
smartTransactionsError.currentBalanceWei, smartTransactionsError.currentBalanceWei,
); );
} }
const prevEthBalanceNeededStx = usePrevious(ethBalanceNeededStx);
const destinationToken = useSelector(getDestinationTokenInfo, isEqual); const destinationToken = useSelector(getDestinationTokenInfo, isEqual);
useEffect(() => { useEffect(() => {
@ -518,8 +523,13 @@ export default function ReviewQuote({ setReceiveToAmount }) {
} else if (balanceError && !insufficientTokens && !insufficientEth) { } else if (balanceError && !insufficientTokens && !insufficientEth) {
dispatch(setBalanceError(false)); dispatch(setBalanceError(false));
} }
// eslint-disable-next-line }, [
}, [insufficientTokens, insufficientEth, dispatch, isSmartTransaction]); insufficientTokens,
insufficientEth,
dispatch,
isSmartTransaction,
balanceError,
]);
useEffect(() => { useEffect(() => {
const currentTime = Date.now(); const currentTime = Date.now();
@ -697,6 +707,42 @@ export default function ReviewQuote({ setReceiveToAmount }) {
trackBestQuoteReviewedEvent, trackBestQuoteReviewedEvent,
]); ]);
useEffect(() => {
if (
((isSmartTransaction && prevEthBalanceNeededStx) ||
!isSmartTransaction) &&
quotesLastFetched === prevQuotesLastFetched
) {
return;
}
let additionalBalanceNeeded;
if (isSmartTransaction && ethBalanceNeededStx) {
additionalBalanceNeeded = ethBalanceNeededStx;
} else if (!isSmartTransaction && ethBalanceNeeded) {
additionalBalanceNeeded = ethBalanceNeeded;
} else {
return; // A user has enough balance for a gas fee, so we don't need to track it.
}
trackEvent({
event: MetaMetricsEventName.SwapError,
category: MetaMetricsEventCategory.Swaps,
sensitiveProperties: {
...eventObjectBase,
error_type: MetaMetricsEventErrorType.InsufficientGas,
additional_balance_needed: additionalBalanceNeeded,
},
});
}, [
quotesLastFetched,
prevQuotesLastFetched,
ethBalanceNeededStx,
isSmartTransaction,
trackEvent,
prevEthBalanceNeededStx,
ethBalanceNeeded,
eventObjectBase,
]);
const metaMaskFee = usedQuote.fee; const metaMaskFee = usedQuote.fee;
/* istanbul ignore next */ /* istanbul ignore next */
@ -793,6 +839,7 @@ export default function ReviewQuote({ setReceiveToAmount }) {
10, 10,
); );
} }
const prevPriceDifferencePercentage = usePrevious(priceDifferencePercentage);
const shouldShowPriceDifferenceWarning = const shouldShowPriceDifferenceWarning =
!tokenBalanceUnavailable && !tokenBalanceUnavailable &&
@ -841,6 +888,25 @@ export default function ReviewQuote({ setReceiveToAmount }) {
!smartTransactionFees?.tradeTxFees), !smartTransactionFees?.tradeTxFees),
); );
useEffect(() => {
if (
shouldShowPriceDifferenceWarning &&
acknowledgedPriceDifference &&
quotesLastFetched !== prevQuotesLastFetched &&
priceDifferencePercentage !== prevPriceDifferencePercentage
) {
// Reset price difference acknowledgement if price diff % changed.
setAcknowledgedPriceDifference(false);
}
}, [
acknowledgedPriceDifference,
prevQuotesLastFetched,
quotesLastFetched,
shouldShowPriceDifferenceWarning,
priceDifferencePercentage,
prevPriceDifferencePercentage,
]);
useEffect(() => { useEffect(() => {
if (isSmartTransaction && !insufficientTokens) { if (isSmartTransaction && !insufficientTokens) {
const unsignedTx = { const unsignedTx = {
@ -948,16 +1014,6 @@ export default function ReviewQuote({ setReceiveToAmount }) {
usedQuote, usedQuote,
]); ]);
useEffect(() => {
if (isSmartTransaction) {
// Removes a smart transactions error when the component loads.
dispatch({
type: SET_SMART_TRANSACTIONS_ERROR,
payload: null,
});
}
}, [isSmartTransaction, dispatch]);
const destinationValue = calcTokenValue( const destinationValue = calcTokenValue(
destinationTokenValue, destinationTokenValue,
destinationTokenDecimals, destinationTokenDecimals,