diff --git a/src/hooks/useZap.ts b/src/hooks/useZap.ts index 5efae3070b..1865b413fe 100644 --- a/src/hooks/useZap.ts +++ b/src/hooks/useZap.ts @@ -87,7 +87,7 @@ export const useZapInAmounts = ( useEffect(() => { async function handleCalculateZapInAmounts() { - if (!userIn) { + if (!userIn || userIn?.eq(0)) { setResult({ amounts: { tokenInAmount: BigNumber.from(0), diff --git a/src/pages/AddLiquidity/ZapIn.tsx b/src/pages/AddLiquidity/ZapIn.tsx index 9c393625f2..5e1f10a9db 100644 --- a/src/pages/AddLiquidity/ZapIn.tsx +++ b/src/pages/AddLiquidity/ZapIn.tsx @@ -1,6 +1,14 @@ import { BigNumber } from '@ethersproject/bignumber' import { TransactionResponse } from '@ethersproject/providers' -import { Currency, CurrencyAmount, Fraction, TokenAmount, WETH, computePriceImpact } from '@kyberswap/ks-sdk-core' +import { + Currency, + CurrencyAmount, + Fraction, + Percent, + TokenAmount, + WETH, + computePriceImpact, +} from '@kyberswap/ks-sdk-core' import { Trans, t } from '@lingui/macro' import { captureException } from '@sentry/react' import { parseUnits } from 'ethers/lib/utils' @@ -111,6 +119,9 @@ const ZapIn = ({ isOldStaticFeeContract, } = useDerivedZapInInfo(currencyA ?? undefined, currencyB ?? undefined, pairAddress) + const independentAmount = parsedAmounts[independentField] + const dependentAmount = parsedAmounts[dependentField] + const nativeA = useCurrencyConvertedToNative(currencies[Field.CURRENCY_A]) const nativeB = useCurrencyConvertedToNative(currencies[Field.CURRENCY_B]) @@ -151,7 +162,7 @@ const ZapIn = ({ // get formatted amounts const formattedAmounts = { [independentField]: typedValue, - [dependentField]: noLiquidity ? otherTypedValue : parsedAmounts[dependentField]?.toSignificant(6) ?? '', + [dependentField]: noLiquidity ? otherTypedValue : dependentAmount?.toSignificant(6) ?? '', } // get the max amounts user can add @@ -364,33 +375,30 @@ const ZapIn = ({ userInCurrencyAmount && marketPrices[0] ? parseFloat(userInCurrencyAmount.toSignificant(6)) * marketPrices[0] : 0 const tokenAPoolAllocUsd = - marketPrices[0] && - parsedAmounts && - parsedAmounts[independentField] && - marketPrices[0] * parseFloat((parsedAmounts[independentField] as CurrencyAmount).toSignificant(6)) + marketPrices[0] && independentAmount && marketPrices[0] * parseFloat(independentAmount.toSignificant(6)) const tokenBPoolAllocUsd = - marketPrices[1] && - parsedAmounts && - parsedAmounts[dependentField] && - marketPrices[1] * parseFloat((parsedAmounts[dependentField] as CurrencyAmount).toSignificant(6)) + marketPrices[1] && dependentAmount && marketPrices[1] * parseFloat(dependentAmount.toSignificant(6)) const estimatedUsdForPair: [number, number] = independentField === Field.CURRENCY_A ? [tokenAPoolAllocUsd || 0, tokenBPoolAllocUsd || 0] : [tokenBPoolAllocUsd || 0, tokenAPoolAllocUsd || 0] - const priceImpact = + const inAmount: CurrencyAmount | undefined = userInCurrencyAmount?.subtract( + independentAmount ?? CurrencyAmount.fromRawAmount(userInCurrencyAmount.currency, 0), + ) + + const priceImpact: Percent | undefined = price && userInCurrencyAmount && - !!parsedAmounts[independentField] && - !!parsedAmounts[dependentField] && - !userInCurrencyAmount.lessThan(parsedAmounts[independentField] as CurrencyAmount) - ? computePriceImpact( - independentField === Field.CURRENCY_A ? price : price.invert(), - userInCurrencyAmount?.subtract(parsedAmounts[independentField] as CurrencyAmount), - parsedAmounts[dependentField] as CurrencyAmount, - ) + independentAmount && + dependentAmount && + inAmount && + !inAmount.equalTo(0) && + !dependentAmount.equalTo(0) && + !userInCurrencyAmount.lessThan(independentAmount) + ? computePriceImpact(independentField === Field.CURRENCY_A ? price : price.invert(), inAmount, dependentAmount) : undefined const priceImpactWithoutFee = pair && priceImpact ? computePriceImpactWithoutFee([pair], priceImpact) : undefined @@ -565,7 +573,7 @@ const ZapIn = ({ - {parsedAmounts[independentField]?.toSignificant(6)} (~ + {independentAmount?.toSignificant(6)} (~ {formattedNum((tokenAPoolAllocUsd || 0).toString(), true)}) @@ -578,7 +586,7 @@ const ZapIn = ({ - {parsedAmounts[dependentField]?.toSignificant(6)} (~ + {dependentAmount?.toSignificant(6)} (~ {formattedNum((tokenBPoolAllocUsd || 0).toString(), true)}) @@ -777,10 +785,7 @@ const ZapIn = ({ !isValid || approval !== ApprovalState.APPROVED || (priceImpactSeverity > 3 && !isDegenMode) } error={ - !!parsedAmounts[independentField] && - !!parsedAmounts[dependentField] && - !!pairAddress && - (!isValid || priceImpactSeverity > 2) + !!independentAmount && !!dependentAmount && !!pairAddress && (!isValid || priceImpactSeverity > 2) } > diff --git a/src/state/mint/hooks.ts b/src/state/mint/hooks.ts index c5af973882..269b435d51 100644 --- a/src/state/mint/hooks.ts +++ b/src/state/mint/hooks.ts @@ -143,7 +143,7 @@ export function useDerivedMintInfo( const { [Field.CURRENCY_A]: currencyAAmount, [Field.CURRENCY_B]: currencyBAmount } = parsedAmounts const [tokenAmountA, tokenAmountB] = [currencyAAmount?.wrapped, currencyBAmount?.wrapped] - if (pair && totalSupply && tokenAmountA && tokenAmountB) { + if (pair && totalSupply && tokenAmountA && tokenAmountB && !tokenAmountA.equalTo(0) && !tokenAmountB.equalTo(0)) { try { return pair.getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) } catch (e) { @@ -362,7 +362,7 @@ export function useDerivedZapInInfo( const { [Field.CURRENCY_A]: currencyAAmount, [Field.CURRENCY_B]: currencyBAmount } = parsedAmounts const [tokenAmountA, tokenAmountB] = [currencyAAmount?.wrapped, currencyBAmount?.wrapped] - if (pair && totalSupply && tokenAmountA && tokenAmountB) { + if (pair && totalSupply && tokenAmountA && tokenAmountB && !tokenAmountA.equalTo(0) && !tokenAmountB.equalTo(0)) { try { return pair.getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) } catch (e) {