Skip to content

Commit

Permalink
feat: integrate my pool/farm apr from BE
Browse files Browse the repository at this point in the history
  • Loading branch information
viet-nv committed Jul 24, 2023
1 parent bedda6a commit 0ae22ea
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { TransactionResponse } from '@ethersproject/providers'
import { ChainId, Currency, CurrencyAmount } from '@kyberswap/ks-sdk-core'
import { NonfungiblePositionManager, Position } from '@kyberswap/ks-sdk-elastic'
import { Trans } from '@lingui/macro'
import { Interface } from 'ethers/lib/utils'
import { useRef } from 'react'
import { useDispatch } from 'react-redux'
import { Flex, Text } from 'rebass'

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 { useActiveWeb3React, useWeb3React } from 'hooks'
import { useProAmmNFTPositionManagerContract, useProMMFarmContract } from 'hooks/useContract'
import { useProAmmNFTPositionManagerContract } from 'hooks/useContract'
import { config } from 'hooks/useElasticLegacy'
import useTheme from 'hooks/useTheme'
import useTransactionDeadline from 'hooks/useTransactionDeadline'
Expand All @@ -39,6 +41,9 @@ type Props = {
position: Position
isLegacy: boolean
}

const FarmInterface = new Interface(PROMM_FARM_ABI)

const CollectFeesPanel: React.FC<Props> = ({
nftId,
chainId,
Expand Down Expand Up @@ -66,7 +71,7 @@ const CollectFeesPanel: React.FC<Props> = ({
libraryRef.current = library

const liquidity = position.liquidity.toString()
const farmContract = useProMMFarmContract(farmAddress || '')

const addTransactionWithType = useTransactionAdder()

const handleBroadcastClaimSuccess = (response: TransactionResponse) => {
Expand Down Expand Up @@ -113,7 +118,6 @@ const CollectFeesPanel: React.FC<Props> = ({
.sendTransaction(newTxn)
.then((response: TransactionResponse) => {
handleBroadcastClaimSuccess(response)

dispatch(setAttemptingTxn(false))
dispatch(setTxnHash(response.hash))
})
Expand All @@ -127,7 +131,7 @@ const CollectFeesPanel: React.FC<Props> = ({
}

const collectFeeFromFarmContract = async () => {
if (!farmContract || !feeValue0 || !feeValue1) {
if (!farmAddress || !feeValue0 || !feeValue1) {
dispatch(setAttemptingTxn(false))
dispatch(setTxError('Something went wrong!'))
return
Expand All @@ -136,31 +140,19 @@ const CollectFeesPanel: React.FC<Props> = ({
const amount0Min = feeValue0.subtract(feeValue0.multiply(basisPointsToPercent(allowedSlippage)))
const amount1Min = feeValue1.subtract(feeValue1.multiply(basisPointsToPercent(allowedSlippage)))
try {
const gasEstimation = await farmContract.estimateGas.claimFee(
const encoded = FarmInterface.encodeFunctionData('claimFee', [
[nftId],
amount0Min.quotient.toString(),
amount1Min.quotient.toString(),
poolAddress,
true,
deadline?.toString(),
)
])

const txResponse = await farmContract.claimFee(
[nftId],
amount0Min.quotient.toString(),
amount1Min.quotient.toString(),
poolAddress,
true,
deadline?.toString(),
{
gasLimit: calculateGasMargin(gasEstimation),
},
)

handleBroadcastClaimSuccess(txResponse)

dispatch(setAttemptingTxn(false))
dispatch(setTxnHash(txResponse.hash))
sendTransaction({
to: farmAddress,
data: encoded,
})
} catch (e) {
dispatch(setShowPendingModal(MODAL_PENDING_TEXTS.COLLECT_FEES))
dispatch(setAttemptingTxn(false))
Expand Down Expand Up @@ -313,7 +305,7 @@ const CollectFeesPanel: React.FC<Props> = ({
}}
onClick={handleClickCollectFees}
>
Collect Fees
<Trans>Collect Fees</Trans>
</ButtonOutlined>
</Flex>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import styled from 'styled-components'

import useTheme from 'hooks/useTheme'
import PositionId from 'pages/MyEarnings/ElasticPools/SinglePosition/PositionId'
import { NFTPosition } from 'state/farms/elastic/types'

export const ViewWrapper = styled.div<{ viewEarning: boolean }>`
display: flex;
Expand All @@ -32,12 +31,8 @@ export type CommonProps = {
chainId: ChainId
pendingFee: [string, string]
tokenPrices: { [id: string]: number }
myPoolAPR: string
myFarmAPR: string
farmAddress: string
currency0: Currency
currency1: Currency
nft: NFTPosition | undefined
}

type Props = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MyEarningsOverTimePanel = styled(OriginalMyEarningsOverTimePanel)`
`

const EarningView: React.FC<CommonProps> = props => {
const { positionEarning, chainId, myFarmAPR, myPoolAPR } = props
const { positionEarning, chainId } = props
const tokensByChainId = useAppSelector(state => state.lists.mapWhitelistTokens)

// format pool value
Expand All @@ -32,6 +32,9 @@ const EarningView: React.FC<CommonProps> = props => {

const earningToday = ticks?.[0]

const myPoolAPR = Number(positionEarning.myPoolApr || '0')
const myFarmAPR = Number(positionEarning.myFarmApr || '0')

return (
<CommonView isEarningView {...props}>
<Flex
Expand Down Expand Up @@ -84,8 +87,8 @@ const EarningView: React.FC<CommonProps> = props => {
</Row>

<Row>
<ValueAPR>{myPoolAPR}</ValueAPR>
<ValueAPR>{myFarmAPR}</ValueAPR>
<ValueAPR>{myPoolAPR ? myPoolAPR.toFixed(2) + '%' : '--'}</ValueAPR>
<ValueAPR>{myFarmAPR ? myFarmAPR.toFixed(2) + '%' : '--'}</ValueAPR>
</Row>
</Column>

Expand Down
Loading

0 comments on commit 0ae22ea

Please sign in to comment.