From 6e11e5e40d40ba12c99043a33013817f5abcfce6 Mon Sep 17 00:00:00 2001 From: viet-nv Date: Mon, 6 Nov 2023 14:08:42 +0700 Subject: [PATCH] fix: price impact --- src/components/ElasticZap/ZapDetail.tsx | 21 ++++++++++++++++--- .../SwapForm/hooks/useBuildRoute.tsx | 2 +- src/constants/index.ts | 2 +- src/pages/AddLiquidityV2/index.tsx | 8 ++++++- src/services/route/index.ts | 4 ++-- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/components/ElasticZap/ZapDetail.tsx b/src/components/ElasticZap/ZapDetail.tsx index 8c50a502e5..af17ee0b78 100644 --- a/src/components/ElasticZap/ZapDetail.tsx +++ b/src/components/ElasticZap/ZapDetail.tsx @@ -123,6 +123,15 @@ export const useZapDetail = ({ newPooledAmount1 = newPooledAmount1.add(position.amount1) } + const remainAmount0 = + pool?.token0 && + result?.remainingAmount0 && + CurrencyAmount.fromRawAmount(pool.token0, result.remainingAmount0.toString()) + const remainAmount1 = + pool?.token1 && + result?.remainingAmount1 && + CurrencyAmount.fromRawAmount(pool.token1, result.remainingAmount1.toString()) + const prices = useTokenPrices( [WETH[chainId].address, currency0?.wrapped.address, currency1?.wrapped.address].filter(Boolean) as string[], ) @@ -137,16 +146,22 @@ export const useZapDetail = ({ const amountInUsd = +(amountIn?.toExact() || '0') * (prices[amountIn?.currency?.wrapped.address || ''] || 0) - const amountUSDAfterSwap = + const remainAmountUsd = currency0 && currency1 + ? +(remainAmount0?.toExact() || 0) * (prices[currency0.wrapped.address] || 0) + + +(remainAmount1?.toExact() || 0) * (prices[currency1.wrapped.address] || 0) + : 0 + + const amountUsdAfterSwap = + (currency0 && currency1 ? +(newPooledAmount0?.toExact() || 0) * (prices[currency0.wrapped.address] || 0) + +(newPooledAmount1?.toExact() || 0) * (prices[currency1.wrapped.address] || 0) - : 0 + : 0) + remainAmountUsd const priceImpact = !prices[currency0?.wrapped?.address || ''] || !prices[currency1?.wrapped?.address || ''] ? NaN - : ((amountInUsd - amountUSDAfterSwap) * 100) / amountInUsd + : ((amountInUsd - amountUsdAfterSwap) * 100) / amountInUsd const priceImpactRes = checkPriceImpact(priceImpact) const [gas, setGas] = useState('') // GWei diff --git a/src/components/SwapForm/hooks/useBuildRoute.tsx b/src/components/SwapForm/hooks/useBuildRoute.tsx index 4dcf21405e..5bb65c84f3 100644 --- a/src/components/SwapForm/hooks/useBuildRoute.tsx +++ b/src/components/SwapForm/hooks/useBuildRoute.tsx @@ -61,7 +61,7 @@ const useBuildRoute = (args: Args) => { slippageTolerance: slippage, sender: account, recipient: to || account, - source: clientId || 'kyberswap-ui', + source: clientId || 'kyberswap', skipSimulateTx: false, permit, } diff --git a/src/constants/index.ts b/src/constants/index.ts index de1867f189..fa340712f7 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -131,7 +131,7 @@ export const CAMPAIGN_LEADERBOARD_ITEM_PER_PAGE = 10 export const CAMPAIGN_YOUR_TRANSACTIONS_ITEM_PER_PAGE = 10000 export const ELASTIC_BASE_FEE_UNIT = 100_000 -export const KYBERSWAP_SOURCE = '{"source":"kyberswap-ui"}' +export const KYBERSWAP_SOURCE = '{"source":"kyberswap"}' export const SWR_KEYS = { getGrantProgramLeaderBoard: (id: number | string) => `${CAMPAIGN_BASE_URL}/api/v1/competitions/${id}/leaderboard`, diff --git a/src/pages/AddLiquidityV2/index.tsx b/src/pages/AddLiquidityV2/index.tsx index c0d94c5238..b26eaf62d3 100644 --- a/src/pages/AddLiquidityV2/index.tsx +++ b/src/pages/AddLiquidityV2/index.tsx @@ -1247,7 +1247,13 @@ export default function AddLiquidity() { setShowZapConfirmation(true) }} - color={zapDetail.priceImpact?.isVeryHigh ? theme.text : undefined} + color={ + zapApprovalState !== ApprovalState.APPROVED + ? theme.textReverse + : zapDetail.priceImpact?.isVeryHigh + ? theme.text + : undefined + } backgroundColor={ zapApprovalState !== ApprovalState.APPROVED ? undefined diff --git a/src/services/route/index.ts b/src/services/route/index.ts index cece92496c..8984d75c5b 100644 --- a/src/services/route/index.ts +++ b/src/services/route/index.ts @@ -24,7 +24,7 @@ const routeApi = createApi({ params, authentication, headers: { - 'x-client-id': clientId || 'kyberswap-ui', + 'x-client-id': clientId || 'kyberswap', }, }), }), @@ -39,7 +39,7 @@ const routeApi = createApi({ signal, authentication, headers: { - 'x-client-id': payload.source || 'kyberswap-ui', + 'x-client-id': payload.source || 'kyberswap', }, }), }),