Skip to content

Commit

Permalink
support elastic hack
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Nov 24, 2023
1 parent ae3168b commit e600df7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
54 changes: 39 additions & 15 deletions src/pages/RemoveLiquidityProAmm/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string>('')
Expand Down Expand Up @@ -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,
}),
]
Expand All @@ -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,
]
: [
Expand All @@ -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)
Expand Down Expand Up @@ -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 = {
Expand All @@ -434,6 +443,7 @@ function Remove({ tokenId }: { tokenId: BigNumber }) {
})
})
.catch((error: any) => {
console.log('error', error)
setAttemptingTxn(false)

if (!didUserReject(error)) {
Expand Down Expand Up @@ -467,8 +477,8 @@ function Remove({ tokenId }: { tokenId: BigNumber }) {
<Trans>
Removing {liquidityValue0?.toSignificant(6)} {liquidityValue0?.currency?.symbol} and{' '}
{liquidityValue1?.toSignificant(6)} {liquidityValue1?.currency?.symbol}
{feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO) ? <br /> : ''}
{feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO)
{claimFee && (feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO)) ? <br /> : ''}
{claimFee && (feeValue0?.greaterThan(ZERO) || feeValue1?.greaterThan(ZERO))
? `Collecting fee of ${feeValue0?.toSignificant(6)} ${
feeValue0?.currency?.symbol
} and ${feeValue1?.toSignificant(6)} ${feeValue1?.currency?.symbol}`
Expand Down Expand Up @@ -523,13 +533,15 @@ function Remove({ tokenId }: { tokenId: BigNumber }) {
title={t`Remove Amount`}
/>
{positionSDK ? (
<ProAmmFee
totalFeeRewardUSD={totalFeeRewardUSD}
feeValue0={feeValue0}
feeValue1={feeValue1}
position={positionSDK}
tokenId={tokenId}
/>
claimFee ? (
<ProAmmFee
totalFeeRewardUSD={totalFeeRewardUSD}
feeValue0={feeValue0}
feeValue1={feeValue1}
position={positionSDK}
tokenId={tokenId}
/>
) : null
) : (
<Loader />
)}
Expand Down Expand Up @@ -780,6 +792,18 @@ function Remove({ tokenId }: { tokenId: BigNumber }) {
/>
</div>
</TokenInputWrapper>

<Flex alignItems="center" sx={{ gap: '12px' }} marginTop="0.75rem">
<Text fontSize="12px" fontWeight="500">
Claim Your Fees Earned
</Text>
<Toggle
isActive={claimFee}
toggle={() => {
setIsClaimFee(prev => !prev)
}}
/>
</Flex>
</AmoutToRemoveContent>

{slippageStatus === SLIPPAGE_STATUS.HIGH && (
Expand Down
9 changes: 7 additions & 2 deletions src/state/farms/elastic/hooks.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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)
})
}
}
Expand All @@ -774,7 +779,7 @@ export function useJoinedPositions() {
}
})
return userInfo
}, [result, params, farms, positions])
}, [result, params, chainId, farms, positions])
}

export function useUserInfoByFarm(farmAddress: string): UserInfo {
Expand Down
12 changes: 8 additions & 4 deletions src/state/farms/elasticv2/updater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions src/state/user/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -199,7 +199,7 @@ export function useSwapSlippageTolerance(): [number, (slippage: number) => void]
export function usePoolSlippageTolerance(): [number, (slippage: number) => void] {
const dispatch = useDispatch<AppDispatch>()
const poolSlippageTolerance = useSelector<AppState, AppState['user']['poolSlippageTolerance']>(state => {
return state.user.poolSlippageTolerance
return state.user.poolSlippageTolerance || INITIAL_ALLOWED_SLIPPAGE
})
const setPoolSlippageTolerance = useCallback(
(poolSlippageTolerance: number) => {
Expand Down

0 comments on commit e600df7

Please sign in to comment.