Skip to content

Commit

Permalink
fix: [GSW-2048] Cannot call API when selecting token after entering q…
Browse files Browse the repository at this point in the history
…uantity
  • Loading branch information
tfrg committed Jan 9, 2025
1 parent c70899b commit 0690b2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
34 changes: 30 additions & 4 deletions packages/web/src/hooks/swap/data/use-swap-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export const useSwapHandler = () => {
const [swapRateAction, setSwapRateAction] = useState<"ATOB" | "BTOA">("BTOA");
const [tokenAAmount = "", setTokenAAmount] = useState(defaultTokenAAmount ?? undefined);

const estimateFlagRef = useRef(0);

const [tokenBAmount = "", setTokenBAmount] = useState(() =>
!defaultTokenAAmount ? (defaultTokenBAmount ? defaultTokenBAmount : undefined) : undefined,
);
Expand Down Expand Up @@ -621,10 +623,6 @@ export const useSwapHandler = () => {
return;
}

if (tokenA && tokenB) {
updateSwapAmount(result.value);
}

if (isSameToken) {
setTokenAAmount(result.value);
setTokenBAmount(result.value);
Expand Down Expand Up @@ -654,6 +652,7 @@ export const useSwapHandler = () => {
...prev,
type: "EXACT_IN",
}));
updateSwapAmount(result.value);
setTokenAAmount(result.value);
},
[isSameToken, setSwapValue, tokenA, tokenB?.symbol],
Expand Down Expand Up @@ -1095,6 +1094,33 @@ export const useSwapHandler = () => {
});
}, []);

useEffect(() => {
// Do not execute unless both tokens A and B are selected
if (!tokenA?.symbol || !tokenB?.symbol) {
return;
}

// Do not execute this logic if the user entered it directly
const isUserInput = tokenAAmount !== "" || tokenBAmount !== "";
if (isUserInput) {
return;
}

// Set default values only when there is no user input
if (estimateFlagRef.current === 0) {
if (!!defaultTokenAAmount) {
estimateFlagRef.current += 1;
changeTokenAAmount(defaultTokenAAmount);
return;
}
if (!!defaultTokenBAmount) {
estimateFlagRef.current += 1;
changeTokenBAmount(defaultTokenBAmount);
return;
}
}
}, [tokenA, tokenB, defaultTokenAAmount, defaultTokenBAmount, tokenAAmount, tokenBAmount]);

// useEffect to manage loading state when token amount changes
useEffect(() => {
if (!tokenA?.symbol || !tokenB?.symbol) return;
Expand Down
7 changes: 6 additions & 1 deletion packages/web/src/hooks/swap/data/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
setIsTyping(false);
return;
}

const processedAmount = amount.endsWith(".") ? amount.slice(0, -1) : amount;
const newAmount = BigNumber(processedAmount).isZero() ? 0 : BigNumber(processedAmount).toNumber();

if (!tokenA || !tokenB) {
setSwapAmount(newAmount);
setIsTyping(false);
return;
}

setSwapAmount(prevAmount => {
const hasValueChanged = prevAmount !== newAmount;

Expand Down

0 comments on commit 0690b2a

Please sign in to comment.