Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export const CollateralSwapActionsViaParaswapAdapters = ({
],
error: undefined, // Clear any existing errors
});
trackingHandlers.trackGasEstimationError(error);
} else {
// For other errors, handle normally
setTxError(parsedError);
Expand All @@ -239,14 +240,13 @@ export const CollateralSwapActionsViaParaswapAdapters = ({
actionBlocked: parsedError.actionBlocked,
},
});
const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}

setMainTxState({
loading: false,
});

const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,22 @@ export const DebtSwapActionsViaParaswap = ({
],
error: undefined, // Clear any existing errors
});
trackingHandlers.trackGasEstimationError(error);
} else {
// For other errors, handle normally
setTxError(parsedError);
setState({
actionsLoading: false,
});

const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}

setMainTxState({
txHash: undefined,
loading: false,
});

const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,20 @@ export const RepayWithCollateralActionsViaParaswap = ({
],
error: undefined, // Clear any existing errors
});
trackingHandlers.trackGasEstimationError(error);
} else {
// For other errors, handle normally
setTxError(parsedError);
setState({
actionsLoading: false,
});
const reason = error instanceof Error ? error.message : undefined;
trackingHandlers.trackSwapFailed(reason);
}

setMainTxState({
loading: false,
});

const reason = error instanceof Error ? error.message : undefined;
trackingHandlers.trackSwapFailed(reason);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,21 +187,21 @@ export const SwapActionsViaParaswap = ({
],
error: undefined, // Clear any existing errors
});
trackingHandlers.trackGasEstimationError(error);
} else {
// For other errors, handle normally
setTxError(parsedError);
setState({
actionsLoading: false,
});
const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}

setMainTxState({
txHash: undefined,
loading: false,
});

const reason = error instanceof Error ? error.message : 'Swap failed';
trackingHandlers.trackSwapFailed(reason);
}
} else {
setTxError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,21 @@ export const WithdrawAndSwapActionsViaParaswap = ({
],
error: undefined, // Clear any existing errors
});
trackingHandlers.trackGasEstimationError(error);
} else {
// For other errors, handle normally
setTxError(parsedError);
setState({
actionsLoading: false,
});
const reason = error instanceof Error ? error.message : undefined;
trackingHandlers.trackSwapFailed(reason);
}

setMainTxState({
txHash: undefined,
loading: false,
});
const reason = error instanceof Error ? error.message : undefined;
trackingHandlers.trackSwapFailed(reason);
}
};

Expand Down
9 changes: 9 additions & 0 deletions src/components/transactions/Swap/analytics/state.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,12 @@ export const swapUserDeniedToAnalyticsEventParams = (state: SwapState): TrackEve
...swapStateToAnalyticsEventParams(state),
};
};

export const swapTrackGasEstimationErrorToAnalyticsEventParams = (
state: SwapState
): TrackEventProperties => {
return {
...swapStateToAnalyticsEventParams(state),
errorMessage: 'Gas estimation error',
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
swapInputChangeToAnalyticsEventParams,
swapQuoteToAnalyticsEventParams,
swapTrackApprovalToAnalyticsEventParams,
swapTrackGasEstimationErrorToAnalyticsEventParams,
swapTrackSwapFailedToAnalyticsEventParams,
swapTrackSwapFilledToAnalyticsEventParams,
swapTrackSwapToAnalyticsEventParams,
Expand All @@ -22,6 +23,7 @@ export type TrackAnalyticsHandlers = {
trackSwap: () => void;
trackSwapFilled: (executedSellAmount: string, executedBuyAmount: string) => void;
trackSwapFailed: (reason?: string) => void;
trackGasEstimationError: (error: SwapError) => void;
};

/*
Expand Down Expand Up @@ -59,5 +61,10 @@ export const useHandleAnalytics = ({ state }: { state: SwapState }) => {
),
trackSwapFailed: (reason?: string) =>
trackEvent(SWAP.SWAP_FAILED, swapTrackSwapFailedToAnalyticsEventParams(state, reason)),
trackGasEstimationError: () =>
trackEvent(
SWAP.GAS_ESTIMATION_ERROR,
swapTrackGasEstimationErrorToAnalyticsEventParams(state)
),
};
};
6 changes: 4 additions & 2 deletions src/components/transactions/Swap/details/CowCostsDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const CowCostsDetails = ({ state }: { state: SwapState }) => {
.toNumber();
const flashloanFeeUsd = Number(flashloanFeeFormatted) * flashLoanFeeTokenPriceUnitUsd;
const flashloanFeeToken = state.sellAmountToken;
const isFlashloanUsed = state.useFlashloan;

if (!state.buyAmountToken || !state.sellAmountToken) return null;

Expand Down Expand Up @@ -74,7 +75,8 @@ export const CowCostsDetails = ({ state }: { state: SwapState }) => {
partnerFeeToken = state.buyAmountToken;
}

const totalCostsInUsd = networkFeeUsd + partnerFeeUsd + (flashloanFeeUsd ?? 0); // + costs.slippageInUsd;
const totalCostsInUsd =
networkFeeUsd + partnerFeeUsd + (isFlashloanUsed ? flashloanFeeUsd ?? 0 : 0); // + costs.slippageInUsd;

return (
<Accordion
Expand Down Expand Up @@ -183,7 +185,7 @@ export const CowCostsDetails = ({ state }: { state: SwapState }) => {
/>
</Box>
</Row>
{!!(flashloanFeeFormatted && flashloanFeeToken && flashloanFeeUsd) && (
{!!(flashloanFeeFormatted && flashloanFeeToken && flashloanFeeUsd && isFlashloanUsed) && (
<Row
mx={2}
mb={2}
Expand Down
6 changes: 4 additions & 2 deletions src/components/transactions/Swap/errors/SwapErrors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { Dispatch, useEffect } from 'react';
import { useModalContext } from '../../../../hooks/useModal';
import { TrackAnalyticsHandlers } from '../analytics/useTrackAnalytics';
import { SwapError, SwapParams, SwapState } from '../types';
import { isProtocolSwapState } from '../types/state.types';
import { ActionsBlockedReason, isProtocolSwapState } from '../types/state.types';
import { errorToConsole } from './shared/console.helpers';
import {
FlashLoanDisabledBlockingGuard,
Expand Down Expand Up @@ -66,7 +66,9 @@ export const SwapErrors = ({
}
}, [state.error]);

if (hasInsufficientBalance(state)) {
const insufficientBalance = hasInsufficientBalance(state);

if (insufficientBalance || state.actionsBlocked?.[ActionsBlockedReason.INSUFFICIENT_BALANCE]) {
return (
<InsufficientBalanceGuard
state={state}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,38 @@ export const InsufficientBalanceGuard = ({
});
}
} else {
// Clear error and actionsBlocked if balance is sufficient
// Check both error and actionsBlocked to handle cases where error was cleared elsewhere
const isBalanceError =
state.error?.rawError instanceof Error &&
state.error.rawError.message === 'BalanceLowerThanInput';
const isBalanceBlocked = state.actionsBlocked?.[ActionsBlockedReason.INSUFFICIENT_BALANCE];

if (isBalanceError) {
if (isBalanceError || isBalanceBlocked) {
setState({
error: undefined,
...(isBalanceError ? { error: undefined } : {}),
actionsBlocked: {
[ActionsBlockedReason.INSUFFICIENT_BALANCE]: undefined,
},
});
}
}
}, [
state.inputAmount,
state.outputAmount,
state.debouncedInputAmount,
state.debouncedOutputAmount,
state.sourceToken.balance,
state.destinationToken.balance,
state.sellAmountFormatted,
state.buyAmountFormatted,
state.sellAmountToken,
state.buyAmountToken,
state.isInvertedSwap,
state.side,
state.swapType,
state.buyAmountFormatted,
state.sellAmountFormatted,
state.error,
state.actionsBlocked,
]);

if (hasInsufficientBalance(state)) {
Expand Down
2 changes: 0 additions & 2 deletions src/components/transactions/Swap/hooks/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,7 @@ const useMultiProviderSwapQuoteQuery = ({
!state.mainTxState.txHash && // Don't fetch quotes once transaction is sent
!state.mainTxState.loading && // Don't fetch quotes while transaction is processing
!approvalTxState?.loading && // Don't fetch quotes while approval is processing
!approvalTxState?.success && // Don't fetch quotes while approval is successful
provider !== SwapProvider.NONE &&
!state.quoteRefreshPaused &&
!state.isWrongNetwork
);
})(),
Expand Down
2 changes: 2 additions & 0 deletions src/components/transactions/Swap/inputs/SwapInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const SwapInputs = ({
quoteTimerPausedAccumMs: undefined,
}
: {}),
...(state.orderType === OrderType.MARKET ? { quoteRefreshPaused: false } : {}),
inputAmount: maxAmount,
inputAmountUSD: computeUSD(maxAmount),
isMaxSelected: true,
Expand All @@ -103,6 +104,7 @@ export const SwapInputs = ({
quoteTimerPausedAccumMs: undefined,
}
: {}),
...(state.orderType === OrderType.MARKET ? { quoteRefreshPaused: false } : {}),
inputAmount: value,
inputAmountUSD: computeUSD(value),
isMaxSelected: value === state.forcedMaxValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function LowHealthFactorWarning({
Low health factor after swap. Your position will carry a higher risk of liquidation.
</Trans>
</Typography>
{!state.actionsBlocked[ActionsBlockedReason.LOW_HEALTH_FACTOR] && (
{!state.actionsBlocked[ActionsBlockedReason.IS_LIQUIDATABLE] && (
<Box
sx={{
display: 'flex',
Expand Down
3 changes: 3 additions & 0 deletions src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export enum SWAP {

/// The user denied a wallet request
USER_DENIED = 'SWAP_USER_DENIED_WALLET_REQUEST',

/// The user has encountered a gas estimation error
GAS_ESTIMATION_ERROR = 'SWAP_GAS_ESTIMATION_ERROR',
}

export const REWARDS = {
Expand Down
Loading