Skip to content

Commit

Permalink
fix: make swap reverse work
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Dec 18, 2024
1 parent 3223b37 commit f8858d4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/components/common/BNInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export function BNInput({
return;
}

if (value === 0n && Number(valStr) === 0) {
if (value === 0n) {
if (Number(valStr) !== 0) {
setValStr('');
}
return;
}

Expand Down
1 change: 1 addition & 0 deletions src/pages/Swap/hooks/useSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export function useSwap() {
return {
setValuesDebouncedSubject,
swapError,
setSwapError,
isSwapLoading,
optimalRate,
swapGasLimit,
Expand Down
22 changes: 16 additions & 6 deletions src/pages/Swap/hooks/useSwapStateFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function useSwapStateFunctions() {
swapGasLimit,
destAmount,
setDestAmount,
setSwapError,
} = useSwap();

const calculateTokenValueToInput = useCallback(
Expand Down Expand Up @@ -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 = ({
Expand Down

0 comments on commit f8858d4

Please sign in to comment.