From e600df78ca459c12652b089ac3d2ad5138ba0230 Mon Sep 17 00:00:00 2001 From: viet-nv Date: Fri, 24 Nov 2023 11:26:53 +0700 Subject: [PATCH] support elastic hack --- src/pages/RemoveLiquidityProAmm/index.tsx | 54 ++++++++++++++++------- src/state/farms/elastic/hooks.ts | 9 +++- src/state/farms/elasticv2/updater.tsx | 12 +++-- src/state/user/hooks.tsx | 4 +- 4 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/pages/RemoveLiquidityProAmm/index.tsx b/src/pages/RemoveLiquidityProAmm/index.tsx index aa625764f9..8481d2fb62 100644 --- a/src/pages/RemoveLiquidityProAmm/index.tsx +++ b/src/pages/RemoveLiquidityProAmm/index.tsx @@ -1,7 +1,7 @@ import { BigNumber } from '@ethersproject/bignumber' import { TransactionResponse } from '@ethersproject/providers' import { ZERO } from '@kyberswap/ks-sdk-classic' -import { Currency, CurrencyAmount, Percent, WETH } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount, Percent, WETH } from '@kyberswap/ks-sdk-core' import { FeeAmount, NonfungiblePositionManager } from '@kyberswap/ks-sdk-elastic' import { Trans, t } from '@lingui/macro' import { captureException } from '@sentry/react' @@ -29,6 +29,7 @@ import ProAmmPooledTokens from 'components/ProAmm/ProAmmPooledTokens' import { RowBetween } from 'components/Row' import Slider from 'components/Slider' import { SLIPPAGE_EXPLANATION_URL } from 'components/SlippageWarningNote' +import Toggle from 'components/Toggle' import { MouseoverTooltip, TextDashed } from 'components/Tooltip' import TransactionConfirmationModal, { ConfirmationModalContent, @@ -154,7 +155,15 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { const { position } = useProAmmPositionsFromTokenId(tokenId) const positionManager = useProAmmNFTPositionManagerReadingContract() const theme = useTheme() + const [claimFee, setIsClaimFee] = useState(false) + const { networkInfo, account, chainId, isEVM } = useActiveWeb3React() + useEffect(() => { + if (chainId === ChainId.LINEA || chainId === ChainId.SCROLL) { + setIsClaimFee(true) + } + }, [chainId]) + const { library } = useWeb3React() const toggleWalletModal = useWalletModalToggle() const [removeLiquidityError, setRemoveLiquidityError] = useState('') @@ -317,9 +326,9 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { amount1Min.quotient.toString(), deadline.toString(), buildFlagsForFarmV21({ - isClaimFee: !!feeValue0?.greaterThan('0') && !!feeValue1?.greaterThan('0'), + isClaimFee: claimFee && !!feeValue0?.greaterThan('0') && !!feeValue1?.greaterThan('0'), isSyncFee: !!feeValue0?.greaterThan('0') && !!feeValue1?.greaterThan('0'), - isClaimReward: true, + isClaimReward: claimFee, isReceiveNative: !receiveWETH, }), ] @@ -330,7 +339,7 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { amount0Min.quotient.toString(), amount1Min.quotient.toString(), deadline.toString(), - feeValue0?.greaterThan('0'), + claimFee ? feeValue0?.greaterThan('0') : 0, !receiveWETH, ] : [ @@ -340,7 +349,7 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { amount1Min.quotient.toString(), deadline.toString(), !receiveWETH, - [feeValue0?.greaterThan('0'), true], + [claimFee && feeValue0?.greaterThan('0'), claimFee], ] const gasEstimation = await contract.estimateGas.removeLiquidity(...params) @@ -409,7 +418,7 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { recipient: account, deadline: deadline.toString(), isRemovingLiquid: true, - havingFee: !(feeValue0.equalTo(JSBI.BigInt('0')) && feeValue1.equalTo(JSBI.BigInt('0'))), + havingFee: claimFee && !(feeValue0.equalTo(JSBI.BigInt('0')) && feeValue1.equalTo(JSBI.BigInt('0'))), }, }) const txn = { @@ -434,6 +443,7 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { }) }) .catch((error: any) => { + console.log('error', error) setAttemptingTxn(false) if (!didUserReject(error)) { @@ -467,8 +477,8 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { Removing {liquidityValue0?.toSignificant(6)} {liquidityValue0?.currency?.symbol} and{' '} {liquidityValue1?.toSignificant(6)} {liquidityValue1?.currency?.symbol} - {feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO) ?
: ''} - {feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO) + {claimFee && (feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO)) ?
: ''} + {claimFee && (feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO)) ? `Collecting fee of ${feeValue0?.toSignificant(6)} ${ feeValue0?.currency?.symbol } and ${feeValue1?.toSignificant(6)} ${feeValue1?.currency?.symbol}` @@ -523,13 +533,15 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { title={t`Remove Amount`} /> {positionSDK ? ( - + claimFee ? ( + + ) : null ) : ( )} @@ -780,6 +792,18 @@ function Remove({ tokenId }: { tokenId: BigNumber }) { /> + + + + Claim Your Fees Earned + + { + setIsClaimFee(prev => !prev) + }} + /> + {slippageStatus === SLIPPAGE_STATUS.HIGH && ( diff --git a/src/state/farms/elastic/hooks.ts b/src/state/farms/elastic/hooks.ts index 6ad61e329b..84dacd894b 100644 --- a/src/state/farms/elastic/hooks.ts +++ b/src/state/farms/elastic/hooks.ts @@ -1,5 +1,5 @@ import { defaultAbiCoder } from '@ethersproject/abi' -import { Currency, CurrencyAmount, Token } from '@kyberswap/ks-sdk-core' +import { ChainId, Currency, CurrencyAmount, Token } from '@kyberswap/ks-sdk-core' import { FeeAmount, Position, computePoolAddress } from '@kyberswap/ks-sdk-elastic' import { t } from '@lingui/macro' import { BigNumber } from 'ethers' @@ -677,6 +677,7 @@ const getUserInfoFragment = farmInterface.getFunction('getUserInfo') export function useJoinedPositions() { const positions = useDepositedNfts() const { farms } = useElasticFarms() + const { chainId } = useActiveWeb3React() const params = useMemo(() => { return (farms || []).map(farm => { @@ -762,6 +763,10 @@ export function useJoinedPositions() { } else { rewardPendings[pid][i] = rewardPendings[pid][i].add(amount) } + + // TODO: + if (chainId !== ChainId.LINEA && chainId !== ChainId.SCROLL) + rewardPendings[pid][i] = CurrencyAmount.fromRawAmount(currency, 0) }) } } @@ -774,7 +779,7 @@ export function useJoinedPositions() { } }) return userInfo - }, [result, params, farms, positions]) + }, [result, params, chainId, farms, positions]) } export function useUserInfoByFarm(farmAddress: string): UserInfo { diff --git a/src/state/farms/elasticv2/updater.tsx b/src/state/farms/elasticv2/updater.tsx index 61bacf0727..959bceccb4 100644 --- a/src/state/farms/elasticv2/updater.tsx +++ b/src/state/farms/elasticv2/updater.tsx @@ -2,7 +2,7 @@ import { gql, useLazyQuery } from '@apollo/client' import { defaultAbiCoder } from '@ethersproject/abi' import { getCreate2Address } from '@ethersproject/address' import { keccak256 } from '@ethersproject/solidity' -import { CurrencyAmount, Token, WETH } from '@kyberswap/ks-sdk-core' +import { ChainId, CurrencyAmount, Token, WETH } from '@kyberswap/ks-sdk-core' import { FeeAmount, Pool, Position } from '@kyberswap/ks-sdk-elastic' import { BigNumber } from 'ethers' import { Interface } from 'ethers/lib/utils' @@ -419,9 +419,13 @@ export default function ElasticFarmV2Updater({ interval = true }: { interval?: b +stakedPos.amount0.toExact() * (prices[stakedPos.amount0.currency.wrapped.address] || 0) + +stakedPos.amount1.toExact() * (prices[stakedPos.amount1.currency.wrapped.address] || 0) - const unclaimedRewards = farm.totalRewards.map((rw, i) => - CurrencyAmount.fromRawAmount(rw.currency, item.currentUnclaimedRewards[i].toString()), - ) + // TODO: temporary set 0: item.currentUnclaimedRewards[i].toString() + const unclaimedRewards = + chainId === ChainId.LINEA || chainId === ChainId.SCROLL + ? farm.totalRewards.map((rw, i) => + CurrencyAmount.fromRawAmount(rw.currency, item.currentUnclaimedRewards[i].toString()), + ) + : farm.totalRewards.map((rw, _i) => CurrencyAmount.fromRawAmount(rw.currency, 0)) const unclaimedRewardsUsd = unclaimedRewards.reduce( (total, item) => total + +item.toExact() * (prices[item.currency.wrapped.address] || 0), diff --git a/src/state/user/hooks.tsx b/src/state/user/hooks.tsx index 7bef45192c..78fc322394 100644 --- a/src/state/user/hooks.tsx +++ b/src/state/user/hooks.tsx @@ -4,7 +4,7 @@ import { useDispatch, useSelector } from 'react-redux' import { useGetParticipantInfoQuery, useLazyGetParticipantInfoQuery } from 'services/kyberAISubscription' import { SUGGESTED_BASES } from 'constants/bases' -import { TERM_FILES_PATH } from 'constants/index' +import { INITIAL_ALLOWED_SLIPPAGE, TERM_FILES_PATH } from 'constants/index' import { SupportedLocale } from 'constants/locales' import { PINNED_PAIRS } from 'constants/tokens' import { useActiveWeb3React } from 'hooks' @@ -199,7 +199,7 @@ export function useSwapSlippageTolerance(): [number, (slippage: number) => void] export function usePoolSlippageTolerance(): [number, (slippage: number) => void] { const dispatch = useDispatch() const poolSlippageTolerance = useSelector(state => { - return state.user.poolSlippageTolerance + return state.user.poolSlippageTolerance || INITIAL_ALLOWED_SLIPPAGE }) const setPoolSlippageTolerance = useCallback( (poolSlippageTolerance: number) => {