Skip to content

Commit

Permalink
support classic farm v3 (#1978)
Browse files Browse the repository at this point in the history
* support classic farm v3

* hard code token to avoid crash

* fix: crash when switching from polygon

* fix: reward usd value

* fix: ended farm

* fix: sort

* fix: havest all
  • Loading branch information
viet-nv authored Jun 28, 2023
1 parent 2461cd6 commit e73aba5
Show file tree
Hide file tree
Showing 18 changed files with 1,017 additions and 213 deletions.
41 changes: 20 additions & 21 deletions src/components/YieldPools/FairLaunchPools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import ListItem from './ListItem'
import {
ClassicFarmGridWrapper,
ClassicFarmWrapper,
ClickableText,
ExpandableWrapper,
ListItemWrapper,
TableHeader,
Expand Down Expand Up @@ -191,20 +190,20 @@ const FairLaunchPools = ({ fairLaunchAddress, farms, active }: FarmsListProps) =
{viewMode === VIEW_MODE.LIST && above1200 && (
<TableHeader>
<Row>
<ClickableText>
<Trans>Pools | AMP</Trans>
</ClickableText>
<Text color={theme.subText}>
<Trans>POOLS | AMP</Trans>
</Text>
<InfoHelper text={AMP_HINT} />
</Row>
<Row>
<ClickableText>
<Trans>Staked TVL</Trans>
</ClickableText>
<Text color={theme.subText}>
<Trans>STAKED TVL</Trans>
</Text>
</Row>
<Row>
<ClickableText>
<Text color={theme.subText}>
<Trans>AVG APR</Trans>
</ClickableText>
</Text>
<InfoHelper
text={
active
Expand All @@ -214,33 +213,33 @@ const FairLaunchPools = ({ fairLaunchAddress, farms, active }: FarmsListProps) =
/>
</Row>
<Row>
<ClickableText>
<Trans>Ending in</Trans>
</ClickableText>
<Text color={theme.subText}>
<Trans>ENDING IN</Trans>
</Text>
<InfoHelper
text={t`After harvesting, your rewards will unlock linearly over the indicated time period`}
/>
</Row>
<Row>
<ClickableText>
<Trans>My Deposit | Target Volume</Trans>
</ClickableText>
<Text color={theme.subText}>
<Trans>MY DEPOSIT</Trans>
</Text>
</Row>
<Row justify="flex-end">
<ClickableText>
<Text color={theme.subText}>
<Trans>My Rewards</Trans>
</ClickableText>
</Text>
</Row>
<Row justify="flex-end">
<ClickableText>
<Trans>Actions</Trans>
</ClickableText>
<Text color={theme.subText}>
<Trans>ACTIONS</Trans>
</Text>
</Row>
</TableHeader>
)}

{displayFarms.map(farm => {
return <ListItem key={`${farm.fairLaunchAddress}_${farm.stakeToken}`} farm={farm} />
return <ListItem key={`${farm.fairLaunchAddress}_${farm.stakeToken}_${farm.pid}`} farm={farm} />
})}
</ConditionListWrapper>
</ExpandableWrapper>
Expand Down
26 changes: 15 additions & 11 deletions src/components/YieldPools/HarvestAll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const HarvestAll = ({ totalRewards, onHarvestAll }: { totalRewards: Reward[]; on

const [show, setShow] = useState(false)

const rwLength = totalRewards.filter(rw => rw.amount.toString() !== '0').length

return (
<>
<Modal isOpen={show} onDismiss={() => setShow(false)}>
Expand All @@ -66,17 +68,19 @@ const HarvestAll = ({ totalRewards, onHarvestAll }: { totalRewards: Reward[]; on
</Flex>

<RewardBalanceWrapper>
{totalRewards.map((reward, index) => {
return (
<Fragment key={reward.token.wrapped.address}>
<Flex alignItems="center" fontSize="12px" sx={{ gap: '4px' }}>
{chainId && reward.token.wrapped.address && <CurrencyLogo currency={reward.token} size="16px" />}
{getFullDisplayBalance(reward.amount, reward.token.decimals)}
</Flex>
{index !== totalRewards.length - 1 && <Text color={theme.subText}>|</Text>}
</Fragment>
)
})}
{totalRewards
.filter(rw => rw.amount.toString() !== '0')
.map((reward, index) => {
return (
<Fragment key={reward.token.wrapped.address}>
<Flex alignItems="center" fontSize="12px" sx={{ gap: '4px' }}>
{chainId && reward.token.wrapped.address && <CurrencyLogo currency={reward.token} size="16px" />}
{getFullDisplayBalance(reward.amount, reward.token.decimals)}
</Flex>
{index !== rwLength - 1 && <Text color={theme.subText}>|</Text>}
</Fragment>
)
})}
</RewardBalanceWrapper>

<ButtonPrimary
Expand Down
78 changes: 39 additions & 39 deletions src/components/YieldPools/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { getTradingFeeAPR, useFarmApr, useFarmRewards, useFarmRewardsUSD } from
import { formatTokenBalance, getFullDisplayBalance } from 'utils/formatBalance'
import { getFormattedTimeFromSecond } from 'utils/formatTime'
import { formatDollarAmount } from 'utils/numbers'
import { unwrappedToken } from 'utils/wrappedCurrency'

import { ModalContentWrapper } from './ElasticFarmModals/styled'
import { APRTooltipContent } from './FarmingPoolAPRCell'
Expand Down Expand Up @@ -145,7 +146,13 @@ const ListItem = ({ farm }: ListItemProps) => {

const amp = farm.amp / 10000

const pairSymbol = `${farm.token0.symbol}-${farm.token1.symbol} LP`
const pairSymbol =
currency0 && currency1
? `${unwrappedToken(currency0).symbol}-${unwrappedToken(currency1).symbol} LP`
: `${farm.token0.symbol} - ${farm.token1.symbol} LP`
const symbol0 = currency0 ? unwrappedToken(currency0).symbol : farm.token0.symbol
const symbol1 = currency1 ? unwrappedToken(currency1).symbol : farm.token1.symbol

const [depositValue, setDepositValue] = useState('')
const [withdrawValue, setWithdrawValue] = useState('')
const pairAddressChecksum = isAddressString(chainId, farm.id)
Expand All @@ -155,17 +162,7 @@ const ListItem = ({ farm }: ListItemProps) => {

const isNotStaked = !staked.value.gt(0)

const amountToApprove = useMemo(
() =>
TokenAmount.fromRawAmount(
new Token(chainId, pairAddressChecksum, balance.decimals, pairSymbol, ''),
MaxUint256.toString(),
),
[balance.decimals, chainId, pairAddressChecksum, pairSymbol],
)
const [approvalState, approve] = useApproveCallback(amountToApprove, isEVM ? farm.fairLaunchAddress : undefined)

let isStakeInvalidAmount
let isStakeInvalidAmount = false

try {
isStakeInvalidAmount =
Expand All @@ -176,6 +173,19 @@ const ListItem = ({ farm }: ListItemProps) => {
isStakeInvalidAmount = true
}

const amountToApprove = useMemo(
() =>
TokenAmount.fromRawAmount(
new Token(chainId, pairAddressChecksum, balance.decimals, pairSymbol, ''),
isStakeInvalidAmount
? MaxUint256.toString()
: ethers.utils.parseUnits(depositValue, balance.decimals).toString(), // This causes error if number of decimals > 18
),
[balance.decimals, chainId, depositValue, pairAddressChecksum, pairSymbol, isStakeInvalidAmount],
)

const [approvalState, approve] = useApproveCallback(amountToApprove, isEVM ? farm.fairLaunchAddress : undefined)

let isUnstakeInvalidAmount

try {
Expand Down Expand Up @@ -317,7 +327,7 @@ const ListItem = ({ farm }: ListItemProps) => {
)}/${currencyIdFromAddress(farm.token1?.id, chainId)}/${farm.id}`}
style={{ textDecoration: 'none', marginRight: '6px' }}
>
{farm.token0?.symbol} - {farm.token1?.symbol}
{symbol0} - {symbol1}
</Link>
{/* <MouseoverTooltip text={farm.time} width="fit-content" placement="top">
<Clock size={14} style={{ marginLeft: '6px' }} />
Expand All @@ -330,9 +340,8 @@ const ListItem = ({ farm }: ListItemProps) => {
backgroundColor: theme.blue + '20',
color: theme.blue,
borderRadius: '16px',
padding: '2px 4px',
padding: '2px 8px',
whiteSpace: 'nowrap',
flex: '0 0 71px',
}}
>
AMP = {amp}
Expand Down Expand Up @@ -704,24 +713,7 @@ const ListItem = ({ farm }: ListItemProps) => {
) : (
approvalState === ApprovalState.UNKNOWN && <Dots></Dots>
)}
{modalType !== 'unstake' &&
(approvalState === ApprovalState.NOT_APPROVED || approvalState === ApprovalState.PENDING) && (
<ButtonPrimary
color="blue"
disabled={approvalState === ApprovalState.PENDING}
onClick={approve}
padding="12px"
>
{approvalState === ApprovalState.PENDING ? (
<Dots>
<Trans>Approving </Trans>
</Dots>
) : (
<Trans>Approve</Trans>
)}
</ButtonPrimary>
)}
{approvalState === ApprovalState.APPROVED && chainId && modalType === 'stake' && (
{chainId && modalType === 'stake' && (
<CurrencyInputPanel
value={depositValue}
onUserInput={value => {
Expand All @@ -741,13 +733,23 @@ const ListItem = ({ farm }: ListItemProps) => {
fontSize="14px"
customCurrencySelect={
<ButtonPrimary
disabled={isStakeInvalidAmount}
disabled={isStakeInvalidAmount || approvalState === ApprovalState.PENDING}
padding="8px 12px"
width="max-content"
style={{ minWidth: '80px' }}
onClick={() => handleStake(farm.pid)}
onClick={() => {
if (approvalState === ApprovalState.NOT_APPROVED) {
approve()
} else handleStake(farm.pid)
}}
>
{depositValue && isStakeInvalidAmount ? 'Invalid Amount' : 'Stake'}
{depositValue && isStakeInvalidAmount
? 'Invalid Amount'
: approvalState === ApprovalState.NOT_APPROVED
? 'Approve'
: approvalState === ApprovalState.PENDING
? 'Approving'
: 'Stake'}
</ButtonPrimary>
}
/>
Expand Down Expand Up @@ -798,9 +800,7 @@ const ListItem = ({ farm }: ListItemProps) => {
style={{ textDecoration: 'none' }}
>
<GetLP style={{ textAlign: 'right' }}>
<Trans>
Get {farm.token0?.symbol}-{farm.token1?.symbol} LP ↗
</Trans>
<Trans>Get {pairSymbol}</Trans>
</GetLP>
</Link>
</Flex>
Expand Down
Loading

0 comments on commit e73aba5

Please sign in to comment.