Skip to content

Commit c427956

Browse files
authored
Merge pull request #53 from aniket866/fix-infinite-loading
Infinite Swap Execution Loader With Missing Failure Handling
2 parents cb43b94 + b2aee89 commit c427956

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

components/swap/swap-interface.tsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ export function SwapInterface() {
311311
return;
312312
}
313313
setLoading(true);
314-
315314
// Calculate minimum tokens out with slippage tolerance
316-
const amountOutNum = parseFloat(swapState.amountOut);
317-
const effectiveSlippage = zeroSlippageMode ? 0 : slippageTolerance;
318-
const slippageMultiplier = (100 - effectiveSlippage) / 100;
319-
const minimumTokenOut = (amountOutNum * slippageMultiplier).toString();
315+
try {
316+
const amountOutNum = parseFloat(swapState.amountOut);
317+
const effectiveSlippage = zeroSlippageMode ? 0 : slippageTolerance;
318+
const slippageMultiplier = (100 - effectiveSlippage) / 100;
319+
const minimumTokenOut = (amountOutNum * slippageMultiplier).toString();
320320

321321
if (
322322
swapState.tokenIn.symbol == "ETH" ||
@@ -328,7 +328,9 @@ export function SwapInterface() {
328328
amountIn: (Math.round(Number(swapState.amountIn) * 1e18 )).toString(),
329329
minimumAmountToBuy: (Math.round(Number(minimumTokenOut) * 1e18)).toString(),
330330
};
331+
331332
const result = await contractClient.buy(buyRequest);
333+
332334
if (result.success) {
333335
toast.success(
334336
<div>
@@ -362,6 +364,7 @@ export function SwapInterface() {
362364
minimumEthAmount: (Math.round(Number(minimumTokenOut) * 1e18)).toString(),
363365
};
364366
const result = await contractClient.sell(sellRequest);
367+
365368
if (result.success) {
366369
toast.success(
367370
<div>
@@ -396,6 +399,7 @@ export function SwapInterface() {
396399
minimumTokenOut: (Math.round(Number(minimumTokenOut) * 1e18)).toString(),
397400
};
398401
const result = await contractClient.swap(swapRequest);
402+
399403
if (result.success) {
400404
toast.success(
401405
<div>
@@ -411,14 +415,17 @@ export function SwapInterface() {
411415
</div>
412416
);
413417
} else {
418+
toast.error(`Swap Failed!: ${result.error || "An error occurred."}`);
419+
}
420+
} catch (error) {
414421
toast.error(
415-
`Swap Failed!: ${
416-
result.error || "An error occurred during the swap process."
417-
}`
422+
error instanceof Error ? error.message : "Transaction failed"
418423
);
424+
}
425+
finally {
426+
setLoading(false);
427+
setShowPreview(false);
419428
}
420-
setLoading(false);
421-
setShowPreview(false);
422429
};
423430

424431
const getPoolLength = async () => {

0 commit comments

Comments
 (0)