Skip to content

Commit

Permalink
[GSW-1982] sync price impact calculation (#595)
Browse files Browse the repository at this point in the history
* fix: [GSW-1982] Price impact calculating timing

* fix: [GSW-1982] u-units applied to display values (SwapCard, SwapConfirmModal)

* refactor: [SonarQube] Remove redundant return statement in swap state handling

Remove this redundant jump.
Jump statements should not be redundant typescript:S3626
Software qualities impacted:
Maintainability

* refactor: [SonarQube] replace || operator with utility func

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
Nullish coalescing should be preferred typescript:S6606
Software qualities impacted:
Maintainability

* fix: [GSW-1982] Update token price information while running GetRoutes.
  • Loading branch information
tfrg authored Dec 31, 2024
1 parent 7cffe3a commit 99a9396
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
28 changes: 16 additions & 12 deletions packages/web/src/hooks/swap/data/use-swap-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { formatPrice } from "@utils/new-number-utils";
import { matchInputNumber } from "@utils/number-utils";
import { makeDisplayTokenAmount } from "@utils/token-utils";
import { isEmptyObject } from "@utils/validation-utils";
import { nullish } from "@utils/nullish-utils";

import { useTransactionEventStore } from "@hooks/common/use-transaction-event-store";
import { rawBySqrtX96 } from "@utils/swap-utils";
Expand Down Expand Up @@ -144,6 +145,8 @@ export const useSwapHandler = () => {
!defaultTokenAAmount ? (defaultTokenBAmount ? defaultTokenBAmount : undefined) : undefined,
);

const prevPriceImpact = useRef<BigNumber>(BigNumber(0));

const [submitted, setSubmitted] = useState(false);

const [copied, setCopied] = useState(false);
Expand Down Expand Up @@ -256,6 +259,16 @@ export const useSwapHandler = () => {
return BigNumber(0);
}

if (estimatedAmount === null) {
return prevPriceImpact.current || BigNumber(0);
}

if (type === "EXACT_IN") {
setTokenBAmount(estimatedAmount);
} else {
setTokenAAmount(estimatedAmount);
}

const hasUSDPrice =
!!tokenPrices[checkGnotPath(tokenA.path)]?.usd && !!tokenPrices[checkGnotPath(tokenB.path)]?.usd;

Expand Down Expand Up @@ -285,6 +298,7 @@ export const useSwapHandler = () => {
estimatedRoutes,
(swapFee || 0) / 100,
);
prevPriceImpact.current = BigNumber(priceImpactNum.toFixed(2));
return BigNumber(priceImpactNum.toFixed(2));
}, [estimatedRoutes, swapFee, tokenA, tokenAAmount, tokenB, tokenBAmount, tokenPrices]);

Expand Down Expand Up @@ -936,8 +950,8 @@ export const useSwapHandler = () => {
const broadcastMessage = {
tokenASymbol: tokenA.symbol,
tokenBSymbol: tokenB.symbol,
tokenAAmount: isExactIn ? tokenAAmount : makeDisplayTokenAmount(tokenA, estimatedAmount || 0)?.toString() || "0",
tokenBAmount: isExactIn ? makeDisplayTokenAmount(tokenB, estimatedAmount || 0)?.toString() || "0" : tokenBAmount,
tokenAAmount: isExactIn ? tokenAAmount : nullish.handleFalsy(estimatedAmount, "0"),
tokenBAmount: isExactIn ? nullish.handleFalsy(estimatedAmount, "0") : tokenBAmount,
};

// Handle Wrap and Unwrap
Expand Down Expand Up @@ -1027,16 +1041,6 @@ export const useSwapHandler = () => {
}
return;
}

return;
}

if (type === "EXACT_IN") {
const amount = makeDisplayTokenAmount(tokenB, estimatedAmount || 0) || 0;
setTokenBAmount(amount.toString());
} else {
const amount = makeDisplayTokenAmount(tokenA, estimatedAmount || 0) || 0;
setTokenAAmount(amount.toString());
}
}, [swapState, estimatedAmount, type, tokenA, tokenB]);

Expand Down
12 changes: 10 additions & 2 deletions packages/web/src/hooks/swap/data/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
}, [swapState, estimatedSwapResult, swapAmount]);

const estimatedAmount: string | null = useMemo(() => {
if (!tokenA || !tokenB) {
return null;
}

if (!swapAmount || error) {
return null;
}
Expand All @@ -112,7 +116,10 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
return null;
}

return estimatedSwapResult.amount;
const amount = estimatedSwapResult.amount;
return direction === "EXACT_IN"
? makeDisplayTokenAmount(tokenB, amount)?.toString() || null
: makeDisplayTokenAmount(tokenA, amount)?.toString() || null;
}, [swapAmount, error, swapState, estimatedSwapResult]);

const tokenAmountLimit = useMemo(() => {
Expand All @@ -130,7 +137,7 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
return 0;
}

return tokenA ? makeDisplayTokenAmount(tokenA, tokenAmountLimit) || 0 : 0;
return tokenA ? tokenAmountLimit || 0 : 0;
}
return 0;
}, [direction, estimatedAmount, slippage, tokenA]);
Expand Down Expand Up @@ -239,6 +246,7 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
wrap,
unwrap,
updateSwapAmount,
isEstimatedSwapLoading,
resetSwapAmount: () => setSwapAmount(0),
};
};
5 changes: 5 additions & 0 deletions packages/web/src/react-query/router/use-get-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GetRoutesResponse } from "@repositories/swap/response/get-routes-respon
import { wait } from "@utils/common";

import { QUERY_KEY } from "../query-keys";
import { useGetAllTokenPrices } from "@query/token";

const REFETCH_INTERVAL = 10_000;
const STALE_TIME = 10_000;
Expand All @@ -21,6 +22,7 @@ export const useGetRoutes = (
options?: UseQueryOptions<GetRoutesResponse, Error>,
) => {
const { swapRouterRepository } = useGnoswapContext();
const { refetch: refetchAllTokenPrices } = useGetAllTokenPrices();

return useQuery<GetRoutesResponse, Error>({
queryKey: [
Expand Down Expand Up @@ -59,6 +61,9 @@ export const useGetRoutes = (
throw new SwapError("NOT_FOUND_SWAP_POOL");
}

// Updating Swap Route Data while also updating token price information
refetchAllTokenPrices();

const availRoute = result.estimatedRoutes.reduce((accumulated, current) => accumulated + current.quote, 0);

if (availRoute < 100) {
Expand Down

0 comments on commit 99a9396

Please sign in to comment.