Skip to content

Commit

Permalink
feat(my-earnings): handle collect fee + harvest in case position in f…
Browse files Browse the repository at this point in the history
…arm v2
  • Loading branch information
viet-nv committed Sep 7, 2023
1 parent 49e8617 commit a084800
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ButtonOutlined } from 'components/Button'
import CurrencyLogo from 'components/CurrencyLogo'
import { MouseoverTooltip } from 'components/Tooltip'
import PROMM_FARM_ABI from 'constants/abis/v2/farm.json'
import FarmV2ABI from 'constants/abis/v2/farmv2.json'
import { useActiveWeb3React, useWeb3React } from 'hooks'
import { useProAmmNFTPositionManagerContract } from 'hooks/useContract'
import { config } from 'hooks/useElasticLegacy'
Expand All @@ -31,6 +32,7 @@ import { formatDollarAmount } from 'utils/numbers'

type Props = {
nftId: string
fId: string
feeValue0: CurrencyAmount<Currency>
feeValue1: CurrencyAmount<Currency>
feeUsd: number
Expand All @@ -43,9 +45,11 @@ type Props = {
}

const FarmInterface = new Interface(PROMM_FARM_ABI)
const FarmV2Interface = new Interface(FarmV2ABI)

const CollectFeesPanel: React.FC<Props> = ({
nftId,
fId,
chainId,
feeUsd,
feeValue0,
Expand Down Expand Up @@ -140,14 +144,19 @@ const CollectFeesPanel: React.FC<Props> = ({
const amount0Min = feeValue0.subtract(feeValue0.multiply(basisPointsToPercent(allowedSlippage)))
const amount1Min = feeValue1.subtract(feeValue1.multiply(basisPointsToPercent(allowedSlippage)))
try {
const encoded = FarmInterface.encodeFunctionData('claimFee', [
[nftId],
amount0Min.quotient.toString(),
amount1Min.quotient.toString(),
poolAddress,
true,
deadline?.toString(),
])
const encoded = (fId ? FarmV2Interface : FarmInterface).encodeFunctionData(
'claimFee',
fId
? [fId, [nftId], amount0Min.quotient.toString(), amount1Min.quotient.toString(), deadline?.toString(), true]
: [
[nftId],
amount0Min.quotient.toString(),
amount1Min.quotient.toString(),
poolAddress,
true,
deadline?.toString(),
],
)

sendTransaction({
to: farmAddress,
Expand Down
55 changes: 45 additions & 10 deletions src/pages/MyEarnings/ElasticPools/SinglePosition/PositionView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Divider from 'components/Divider'
import FormattedCurrencyAmount from 'components/FormattedCurrencyAmount'
import { MouseoverTooltip } from 'components/Tooltip'
import PROMM_FARM_ABI from 'constants/abis/v2/farm.json'
import FarmV2ABI from 'constants/abis/v2/farmv2.json'
import { VERSION } from 'constants/v2'
import { useActiveWeb3React, useWeb3React } from 'hooks'
import { useAllTokens } from 'hooks/Tokens'
Expand All @@ -39,6 +40,7 @@ import { formatDollarAmount } from 'utils/numbers'
import ActionButtons from './ActionButtons'

const FarmInterface = new Interface(PROMM_FARM_ABI)
const FarmV2Interface = new Interface(FarmV2ABI)

const PositionView: React.FC<CommonProps> = props => {
const { positionEarning, position, pendingFee, tokenPrices: prices, chainId, currency0, currency1 } = props
Expand Down Expand Up @@ -103,6 +105,7 @@ const PositionView: React.FC<CommonProps> = props => {
const onRemoveLiquidityFromLegacyPosition = async () => {
if (currentChainId !== chainId) {
changeNetwork(chainId, () => {
console.log(chainId)
dispatch(updateChainId(chainId))
removeLiquidity()
})
Expand All @@ -114,35 +117,62 @@ const PositionView: React.FC<CommonProps> = props => {
const theme = useTheme()
const tokens = useAllTokens(true, chainId)

const farmRewards = positionEarning.joinedPositions?.[0]?.farmingPool?.rewardTokensIds?.map((rwId, index) => {
const token = tokens[rwId] || new Token(chainId, rwId, 18, '', '')
let farmRewards =
positionEarning.joinedPositions?.[0]?.farmingPool?.rewardTokensIds?.map((rwId, index) => {
const token = tokens[rwId] || new Token(chainId, rwId, 18, '', '')

return CurrencyAmount.fromRawAmount(token, positionEarning.joinedPositions?.[0]?.pendingRewards?.[index] || '0')
})
return CurrencyAmount.fromRawAmount(token, positionEarning.joinedPositions?.[0]?.pendingRewards?.[index] || '0')
}) || []

const farmV2Rewards =
positionEarning.farmV2DepositedPosition?.pendingRewards.map((amount, index) => {
const tokenId = positionEarning.farmV2DepositedPosition?.farmV2.rewards[index].token || ''
const token = tokens[tokenId] || new Token(chainId, tokenId, 18, '', '')

return CurrencyAmount.fromRawAmount(token, amount)
}) || []

farmRewards = farmRewards.concat(farmV2Rewards)

console.log(farmRewards)

const disabledHarvest =
!positionEarning.joinedPositions?.[0]?.pendingRewards?.some(item => item !== '0') &&
!positionEarning.farmV2DepositedPosition?.pendingRewards?.some(item => item !== '0')

const addTransactionWithType = useTransactionAdder()

const handleHarvest = () => {
const farmContract = positionEarning.joinedPositions?.[0]?.farmId
const farmContract =
positionEarning.joinedPositions?.[0]?.farmId || positionEarning.farmV2DepositedPosition?.farmV2.id.split('_')[0]
const isInFarmV2 = !!positionEarning.farmV2DepositedPosition
const pId = positionEarning.joinedPositions?.[0]?.pid
const fId = positionEarning?.farmV2DepositedPosition?.farmV2.id.split('_')[1]

const library = libraryRef.current

dispatch(setShowPendingModal(MODAL_PENDING_TEXTS.HARVEST))
dispatch(setAttemptingTxn(true))

if (!library || !pId || !farmContract) {
if (!library || (isInFarmV2 ? !fId : !pId) || !farmContract) {
dispatch(setAttemptingTxn(false))
dispatch(setTxError('Something went wrong!'))
return
}

const encodedPid = defaultAbiCoder.encode(['tupple(uint256[] pIds)'], [{ pIds: [pId] }])
const encodedData = FarmInterface.encodeFunctionData('harvestMultiplePools', [[positionEarning.id], [encodedPid]])
let encodedData = ''
if (isInFarmV2) {
encodedData = FarmV2Interface.encodeFunctionData('claimReward', [fId, [positionEarning.id]])
} else {
const encodedPid = defaultAbiCoder.encode(['tupple(uint256[] pIds)'], [{ pIds: [pId] }])
encodedData = FarmInterface.encodeFunctionData('harvestMultiplePools', [[positionEarning.id], [encodedPid]])
}

const txn = {
to: farmContract,
data: encodedData,
}

library
.getSigner()
.estimateGas(txn)
Expand Down Expand Up @@ -251,12 +281,17 @@ const PositionView: React.FC<CommonProps> = props => {

<CollectFeesPanel
nftId={positionEarning.id}
fId={positionEarning?.farmV2DepositedPosition?.farmV2.id.split('_')[1]}
chainId={chainId}
feeUsd={feeUsd}
feeValue0={feeReward0}
feeValue1={feeReward1}
hasUserDepositedInFarm={positionEarning.owner !== positionEarning.ownerOriginal}
farmAddress={positionEarning.depositedPosition?.farm || positionEarning.joinedPositions?.[0]?.farmId}
farmAddress={
positionEarning?.farmV2DepositedPosition?.farmV2.id.split('_')[0] ||
positionEarning.depositedPosition?.farm ||
positionEarning.joinedPositions?.[0]?.farmId
}
poolAddress={positionEarning.pool.id}
position={position}
isLegacy={isLegacyPosition}
Expand Down Expand Up @@ -299,7 +334,7 @@ const PositionView: React.FC<CommonProps> = props => {
</Flex>

<ButtonOutlined
disabled={!positionEarning.joinedPositions?.[0]?.pendingRewards?.some(item => item !== '0')}
disabled={disabledHarvest}
style={{
height: '36px',
width: 'fit-content',
Expand Down
8 changes: 8 additions & 0 deletions src/services/earning/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export type ElasticPositionEarningWithDetails = {
pendingRewardUSD: string[]
liquidity: string
}[]

farmV2DepositedPosition: {
farmV2: {
id: string
rewards: Array<{ amount: string; index: number; token: string }>
}
pendingRewards: Array<string>
}
} & HistoricalEarning

export type ClassicPositionEarningWithDetails = {
Expand Down

0 comments on commit a084800

Please sign in to comment.