Skip to content

Commit b8ad032

Browse files
authored
fix: swaps misc (#2768)
1 parent ee54f88 commit b8ad032

File tree

14 files changed

+58
-25
lines changed

14 files changed

+58
-25
lines changed

src/components/transactions/Swap/actions/CollateralSwap/CollateralSwapActionsViaParaswapAdapters.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export const CollateralSwapActionsViaParaswapAdapters = ({
228228
],
229229
error: undefined, // Clear any existing errors
230230
});
231+
trackingHandlers.trackGasEstimationError(error);
231232
} else {
232233
// For other errors, handle normally
233234
setTxError(parsedError);
@@ -239,14 +240,13 @@ export const CollateralSwapActionsViaParaswapAdapters = ({
239240
actionBlocked: parsedError.actionBlocked,
240241
},
241242
});
243+
const reason = error instanceof Error ? error.message : 'Swap failed';
244+
trackingHandlers.trackSwapFailed(reason);
242245
}
243246

244247
setMainTxState({
245248
loading: false,
246249
});
247-
248-
const reason = error instanceof Error ? error.message : 'Swap failed';
249-
trackingHandlers.trackSwapFailed(reason);
250250
}
251251
};
252252

src/components/transactions/Swap/actions/DebtSwap/DebtSwapActionsViaParaswap.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,22 @@ export const DebtSwapActionsViaParaswap = ({
212212
],
213213
error: undefined, // Clear any existing errors
214214
});
215+
trackingHandlers.trackGasEstimationError(error);
215216
} else {
216217
// For other errors, handle normally
217218
setTxError(parsedError);
218219
setState({
219220
actionsLoading: false,
220221
});
222+
223+
const reason = error instanceof Error ? error.message : 'Swap failed';
224+
trackingHandlers.trackSwapFailed(reason);
221225
}
222226

223227
setMainTxState({
224228
txHash: undefined,
225229
loading: false,
226230
});
227-
228-
const reason = error instanceof Error ? error.message : 'Swap failed';
229-
trackingHandlers.trackSwapFailed(reason);
230231
}
231232
};
232233

src/components/transactions/Swap/actions/RepayWithCollateral/RepayWithCollateralActionsViaParaswap.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,20 +249,20 @@ export const RepayWithCollateralActionsViaParaswap = ({
249249
],
250250
error: undefined, // Clear any existing errors
251251
});
252+
trackingHandlers.trackGasEstimationError(error);
252253
} else {
253254
// For other errors, handle normally
254255
setTxError(parsedError);
255256
setState({
256257
actionsLoading: false,
257258
});
259+
const reason = error instanceof Error ? error.message : undefined;
260+
trackingHandlers.trackSwapFailed(reason);
258261
}
259262

260263
setMainTxState({
261264
loading: false,
262265
});
263-
264-
const reason = error instanceof Error ? error.message : undefined;
265-
trackingHandlers.trackSwapFailed(reason);
266266
}
267267
};
268268

src/components/transactions/Swap/actions/SwapActions/SwapActionsViaParaswap.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,21 @@ export const SwapActionsViaParaswap = ({
187187
],
188188
error: undefined, // Clear any existing errors
189189
});
190+
trackingHandlers.trackGasEstimationError(error);
190191
} else {
191192
// For other errors, handle normally
192193
setTxError(parsedError);
193194
setState({
194195
actionsLoading: false,
195196
});
197+
const reason = error instanceof Error ? error.message : 'Swap failed';
198+
trackingHandlers.trackSwapFailed(reason);
196199
}
197200

198201
setMainTxState({
199202
txHash: undefined,
200203
loading: false,
201204
});
202-
203-
const reason = error instanceof Error ? error.message : 'Swap failed';
204-
trackingHandlers.trackSwapFailed(reason);
205205
}
206206
} else {
207207
setTxError(

src/components/transactions/Swap/actions/WithdrawAndSwap/WithdrawAndSwapActionsViaParaswap.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,21 @@ export const WithdrawAndSwapActionsViaParaswap = ({
169169
],
170170
error: undefined, // Clear any existing errors
171171
});
172+
trackingHandlers.trackGasEstimationError(error);
172173
} else {
173174
// For other errors, handle normally
174175
setTxError(parsedError);
175176
setState({
176177
actionsLoading: false,
177178
});
179+
const reason = error instanceof Error ? error.message : undefined;
180+
trackingHandlers.trackSwapFailed(reason);
178181
}
179182

180183
setMainTxState({
181184
txHash: undefined,
182185
loading: false,
183186
});
184-
const reason = error instanceof Error ? error.message : undefined;
185-
trackingHandlers.trackSwapFailed(reason);
186187
}
187188
};
188189

src/components/transactions/Swap/analytics/state.helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ export const swapUserDeniedToAnalyticsEventParams = (state: SwapState): TrackEve
135135
...swapStateToAnalyticsEventParams(state),
136136
};
137137
};
138+
139+
export const swapTrackGasEstimationErrorToAnalyticsEventParams = (
140+
state: SwapState
141+
): TrackEventProperties => {
142+
return {
143+
...swapStateToAnalyticsEventParams(state),
144+
errorMessage: 'Gas estimation error',
145+
};
146+
};

src/components/transactions/Swap/analytics/useTrackAnalytics.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
swapInputChangeToAnalyticsEventParams,
88
swapQuoteToAnalyticsEventParams,
99
swapTrackApprovalToAnalyticsEventParams,
10+
swapTrackGasEstimationErrorToAnalyticsEventParams,
1011
swapTrackSwapFailedToAnalyticsEventParams,
1112
swapTrackSwapFilledToAnalyticsEventParams,
1213
swapTrackSwapToAnalyticsEventParams,
@@ -22,6 +23,7 @@ export type TrackAnalyticsHandlers = {
2223
trackSwap: () => void;
2324
trackSwapFilled: (executedSellAmount: string, executedBuyAmount: string) => void;
2425
trackSwapFailed: (reason?: string) => void;
26+
trackGasEstimationError: (error: SwapError) => void;
2527
};
2628

2729
/*
@@ -59,5 +61,10 @@ export const useHandleAnalytics = ({ state }: { state: SwapState }) => {
5961
),
6062
trackSwapFailed: (reason?: string) =>
6163
trackEvent(SWAP.SWAP_FAILED, swapTrackSwapFailedToAnalyticsEventParams(state, reason)),
64+
trackGasEstimationError: () =>
65+
trackEvent(
66+
SWAP.GAS_ESTIMATION_ERROR,
67+
swapTrackGasEstimationErrorToAnalyticsEventParams(state)
68+
),
6269
};
6370
};

src/components/transactions/Swap/details/CowCostsDetails.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const CowCostsDetails = ({ state }: { state: SwapState }) => {
3737
.toNumber();
3838
const flashloanFeeUsd = Number(flashloanFeeFormatted) * flashLoanFeeTokenPriceUnitUsd;
3939
const flashloanFeeToken = state.sellAmountToken;
40+
const isFlashloanUsed = state.useFlashloan;
4041

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

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

77-
const totalCostsInUsd = networkFeeUsd + partnerFeeUsd + (flashloanFeeUsd ?? 0); // + costs.slippageInUsd;
78+
const totalCostsInUsd =
79+
networkFeeUsd + partnerFeeUsd + (isFlashloanUsed ? flashloanFeeUsd ?? 0 : 0); // + costs.slippageInUsd;
7880

7981
return (
8082
<Accordion
@@ -183,7 +185,7 @@ export const CowCostsDetails = ({ state }: { state: SwapState }) => {
183185
/>
184186
</Box>
185187
</Row>
186-
{!!(flashloanFeeFormatted && flashloanFeeToken && flashloanFeeUsd) && (
188+
{!!(flashloanFeeFormatted && flashloanFeeToken && flashloanFeeUsd && isFlashloanUsed) && (
187189
<Row
188190
mx={2}
189191
mb={2}

src/components/transactions/Swap/errors/SwapErrors.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { Dispatch, useEffect } from 'react';
33
import { useModalContext } from '../../../../hooks/useModal';
44
import { TrackAnalyticsHandlers } from '../analytics/useTrackAnalytics';
55
import { SwapError, SwapParams, SwapState } from '../types';
6-
import { isProtocolSwapState } from '../types/state.types';
6+
import { ActionsBlockedReason, isProtocolSwapState } from '../types/state.types';
77
import { errorToConsole } from './shared/console.helpers';
88
import {
99
FlashLoanDisabledBlockingGuard,
@@ -66,7 +66,9 @@ export const SwapErrors = ({
6666
}
6767
}, [state.error]);
6868

69-
if (hasInsufficientBalance(state)) {
69+
const insufficientBalance = hasInsufficientBalance(state);
70+
71+
if (insufficientBalance || state.actionsBlocked?.[ActionsBlockedReason.INSUFFICIENT_BALANCE]) {
7072
return (
7173
<InsufficientBalanceGuard
7274
state={state}

src/components/transactions/Swap/errors/shared/InsufficientBalanceGuard.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,38 @@ export const InsufficientBalanceGuard = ({
5353
});
5454
}
5555
} else {
56+
// Clear error and actionsBlocked if balance is sufficient
57+
// Check both error and actionsBlocked to handle cases where error was cleared elsewhere
5658
const isBalanceError =
5759
state.error?.rawError instanceof Error &&
5860
state.error.rawError.message === 'BalanceLowerThanInput';
61+
const isBalanceBlocked = state.actionsBlocked?.[ActionsBlockedReason.INSUFFICIENT_BALANCE];
5962

60-
if (isBalanceError) {
63+
if (isBalanceError || isBalanceBlocked) {
6164
setState({
62-
error: undefined,
65+
...(isBalanceError ? { error: undefined } : {}),
6366
actionsBlocked: {
6467
[ActionsBlockedReason.INSUFFICIENT_BALANCE]: undefined,
6568
},
6669
});
6770
}
6871
}
6972
}, [
73+
state.inputAmount,
74+
state.outputAmount,
7075
state.debouncedInputAmount,
7176
state.debouncedOutputAmount,
7277
state.sourceToken.balance,
7378
state.destinationToken.balance,
7479
state.sellAmountFormatted,
80+
state.buyAmountFormatted,
81+
state.sellAmountToken,
82+
state.buyAmountToken,
7583
state.isInvertedSwap,
7684
state.side,
7785
state.swapType,
78-
state.buyAmountFormatted,
79-
state.sellAmountFormatted,
86+
state.error,
87+
state.actionsBlocked,
8088
]);
8189

8290
if (hasInsufficientBalance(state)) {

0 commit comments

Comments
 (0)