From f8858d482f6aac5b1f0c0977feb4a043bd25100f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Leszczyk?= Date: Wed, 18 Dec 2024 15:32:21 +0100 Subject: [PATCH] fix: make swap reverse work --- src/components/common/BNInput.tsx | 5 ++++- src/pages/Swap/hooks/useSwap.tsx | 1 + src/pages/Swap/hooks/useSwapStateFunctions.ts | 22 ++++++++++++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/components/common/BNInput.tsx b/src/components/common/BNInput.tsx index 2ad44ef7..5c671109 100644 --- a/src/components/common/BNInput.tsx +++ b/src/components/common/BNInput.tsx @@ -60,7 +60,10 @@ export function BNInput({ return; } - if (value === 0n && Number(valStr) === 0) { + if (value === 0n) { + if (Number(valStr) !== 0) { + setValStr(''); + } return; } diff --git a/src/pages/Swap/hooks/useSwap.tsx b/src/pages/Swap/hooks/useSwap.tsx index 8faee9ea..0a85ca30 100644 --- a/src/pages/Swap/hooks/useSwap.tsx +++ b/src/pages/Swap/hooks/useSwap.tsx @@ -163,6 +163,7 @@ export function useSwap() { return { setValuesDebouncedSubject, swapError, + setSwapError, isSwapLoading, optimalRate, swapGasLimit, diff --git a/src/pages/Swap/hooks/useSwapStateFunctions.ts b/src/pages/Swap/hooks/useSwapStateFunctions.ts index 1d3c983d..870dde1d 100644 --- a/src/pages/Swap/hooks/useSwapStateFunctions.ts +++ b/src/pages/Swap/hooks/useSwapStateFunctions.ts @@ -48,6 +48,7 @@ export function useSwapStateFunctions() { swapGasLimit, destAmount, setDestAmount, + setSwapError, } = useSwap(); const calculateTokenValueToInput = useCallback( @@ -186,15 +187,24 @@ export function useSwapStateFunctions() { ); return; } + setSwapError({ message: '' }); setSelectedFromToken(toToken); setSelectedToToken(fromToken); setIsReversed((reversed) => reversed); - calculateSwapValue({ - fromToken: toToken, - toToken: fromToken, - fromValue: undefined, - toValue: undefined, - }); + + if (!toToken || !fromToken || !destAmount) { + return; + } + + const fromValue = { + amount: bigIntToString(BigInt(destAmount), toToken.decimals), + bigint: BigInt(destAmount), + }; + setDestAmount(''); + setFromTokenValue(fromValue); + setFromDefaultValue(fromValue.bigint); + setToTokenValue(undefined); + calculateTokenValueToInput(BigInt(destAmount), 'to', toToken, fromToken); }; const onTokenChange = ({