Skip to content

Commit

Permalink
fix: price impact
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Nov 6, 2023
1 parent b42cf0c commit 6e11e5e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
21 changes: 18 additions & 3 deletions src/components/ElasticZap/ZapDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/components/SwapForm/hooks/useBuildRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
8 changes: 7 additions & 1 deletion src/pages/AddLiquidityV2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/services/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const routeApi = createApi({
params,
authentication,
headers: {
'x-client-id': clientId || 'kyberswap-ui',
'x-client-id': clientId || 'kyberswap',
},
}),
}),
Expand All @@ -39,7 +39,7 @@ const routeApi = createApi({
signal,
authentication,
headers: {
'x-client-id': payload.source || 'kyberswap-ui',
'x-client-id': payload.source || 'kyberswap',
},
}),
}),
Expand Down

0 comments on commit 6e11e5e

Please sign in to comment.