state/user/hooks#useUserFarmStakedOnly TypeScript Examples
The following examples show how to use
state/user/hooks#useUserFarmStakedOnly.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Farms.tsx From glide-frontend with GNU General Public License v3.0 | 4 votes |
Farms: React.FC = () => {
const { path } = useRouteMatch()
const { pathname } = useLocation()
const { t } = useTranslation()
const { data: farmsLP, userDataLoaded } = useFarms()
const cakePrice = usePriceCakeUsdc()
const [query, setQuery] = useState('')
const [viewMode] = usePersistState(ViewMode.TABLE, { localStorageKey: 'glide_farm_view' })
const { account, chainId, library } = useWeb3React()
const [sortOption, setSortOption] = useState('hot')
const chosenFarmsLength = useRef(0)
const isArchived = pathname.includes('archived')
const isInactive = pathname.includes('history')
const isActive = !isInactive && !isArchived
const { currentBlock } = useBlock()
usePollFarmsData(isArchived)
// Users with no wallet connected should see 0 as Earned amount
// Connected users should see loading indicator until first userData has loaded
const userDataReady = !account || (!!account && userDataLoaded)
const [stakedOnly, setStakedOnly] = useUserFarmStakedOnly(isActive)
const activeFarms = farmsLP.filter(
(farm) => farm.pid !== 0 && farm.multiplier !== 'X' && farm.pid !== 6 && farm.pid !== 9 && !isArchivedPid(farm.pid),
)
const inactiveFarms = farmsLP.filter(
(farm) => farm.pid !== 0 && farm.pid !== 12 && farm.multiplier === '0X' && !isArchivedPid(farm.pid),
)
// const activeFarms = farmsLP.filter((farm) => farm.pid !== 0 && !isArchivedPid(farm.pid))
// const inactiveFarms = farmsLP.filter(
// (farm) => farm.pid !== 0 && farm.multiplier === '0X' && farm.multiplier !== '0X' && !isArchivedPid(farm.pid),
// )
const archivedFarms = farmsLP.filter((farm) => isArchivedPid(farm.pid))
const stakedOnlyFarms = activeFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const stakedInactiveFarms = inactiveFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const stakedArchivedFarms = archivedFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const farmsList = useCallback(
(farmsToDisplay: Farm[]): FarmWithStakedValue[] => {
let farmsToDisplayWithAPR: FarmWithStakedValue[] = farmsToDisplay.map((farm) => {
if (!farm.lpTotalInQuoteToken || !farm.quoteToken.usdcPrice) {
return farm
}
const totalLiquidity = new BigNumber(farm.lpTotalInQuoteToken).times(farm.quoteToken.usdcPrice)
const { glideRewardsApr, lpRewardsApr } = isActive
? getFarmApr(
new BigNumber(farm.poolWeight),
cakePrice,
totalLiquidity,
farm.lpAddresses[ChainId.MAINNET],
currentBlock,
)
: { glideRewardsApr: 0, lpRewardsApr: 0 }
return { ...farm, apr: glideRewardsApr, lpRewardsApr, liquidity: totalLiquidity }
})
if (query) {
const lowercaseQuery = latinise(query.toLowerCase())
farmsToDisplayWithAPR = farmsToDisplayWithAPR.filter((farm: FarmWithStakedValue) => {
return latinise(farm.lpSymbol.toLowerCase()).includes(lowercaseQuery)
})
}
return farmsToDisplayWithAPR
},
[cakePrice, query, isActive, currentBlock],
)
const handleChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value)
}
const loadMoreRef = useRef<HTMLDivElement>(null)
const [numberOfFarmsVisible, setNumberOfFarmsVisible] = useState(NUMBER_OF_FARMS_VISIBLE)
const [observerIsSet, setObserverIsSet] = useState(false)
const chosenFarmsMemoized = useMemo(() => {
let chosenFarms = []
const sortFarms = (farms: FarmWithStakedValue[]): FarmWithStakedValue[] => {
switch (sortOption) {
case 'apr':
return orderBy(farms, (farm: FarmWithStakedValue) => farm.apr + farm.lpRewardsApr, 'desc')
case 'multiplier':
return orderBy(
farms,
(farm: FarmWithStakedValue) => (farm.multiplier ? Number(farm.multiplier.slice(0, -1)) : 0),
'desc',
)
case 'earned':
return orderBy(
farms,
(farm: FarmWithStakedValue) => (farm.userData ? Number(farm.userData.earnings) : 0),
'desc',
)
case 'liquidity':
return orderBy(farms, (farm: FarmWithStakedValue) => Number(farm.liquidity), 'desc')
default:
return farms
}
}
if (isActive) {
chosenFarms = stakedOnly ? farmsList(stakedOnlyFarms) : farmsList(activeFarms)
}
if (isInactive) {
chosenFarms = stakedOnly ? farmsList(stakedInactiveFarms) : farmsList(inactiveFarms)
}
if (isArchived) {
chosenFarms = stakedOnly ? farmsList(stakedArchivedFarms) : farmsList(archivedFarms)
}
return sortFarms(chosenFarms).slice(0, numberOfFarmsVisible)
}, [
sortOption,
activeFarms,
farmsList,
inactiveFarms,
archivedFarms,
isActive,
isInactive,
isArchived,
stakedArchivedFarms,
stakedInactiveFarms,
stakedOnly,
stakedOnlyFarms,
numberOfFarmsVisible,
])
chosenFarmsLength.current = chosenFarmsMemoized.length
useEffect(() => {
const showMoreFarms = (entries) => {
const [entry] = entries
if (entry.isIntersecting) {
setNumberOfFarmsVisible((farmsCurrentlyVisible) => {
if (farmsCurrentlyVisible <= chosenFarmsLength.current) {
return farmsCurrentlyVisible + NUMBER_OF_FARMS_VISIBLE
}
return farmsCurrentlyVisible
})
}
}
if (!observerIsSet) {
const loadMoreObserver = new IntersectionObserver(showMoreFarms, {
rootMargin: '0px',
threshold: 1,
})
loadMoreObserver.observe(loadMoreRef.current)
setObserverIsSet(true)
}
}, [chosenFarmsMemoized, observerIsSet])
const rowData = chosenFarmsMemoized.map((farm) => {
const { token, quoteToken } = farm
const tokenAddress = token.address
const quoteTokenAddress = quoteToken.address
const lpLabel = farm.lpSymbol && farm.lpSymbol.split(' ')[0].toUpperCase().replace('', '')
const row: RowProps = {
apr: {
value: getDisplayApr(farm.apr, farm.lpRewardsApr), // TBD
multiplier: farm.multiplier,
lpLabel,
tokenAddress,
quoteTokenAddress,
cakePrice,
originalValue: farm.apr,
},
farm: {
label: lpLabel,
pid: farm.pid,
token: farm.token,
quoteToken: farm.quoteToken,
},
earned: {
earnings: getBalanceNumber(new BigNumber(farm.userData.earnings)),
pid: farm.pid,
},
liquidity: {
liquidity: farm.liquidity,
},
multiplier: {
multiplier: farm.multiplier,
},
details: farm,
}
return row
})
const renderContent = (): JSX.Element => {
if (viewMode === ViewMode.TABLE && rowData.length) {
const columnSchema = DesktopColumnSchema
const columns = columnSchema.map((column) => ({
id: column.id,
name: column.name,
label: column.label,
sort: (a: RowType<RowProps>, b: RowType<RowProps>) => {
switch (column.name) {
case 'farm':
return b.id - a.id
case 'apr':
if (a.original.apr.value && b.original.apr.value) {
return Number(a.original.apr.value) - Number(b.original.apr.value)
}
return 0
case 'earned':
return a.original.earned.earnings - b.original.earned.earnings
default:
return 1
}
},
sortable: column.sortable,
}))
return <Table data={rowData} columns={columns} userDataReady={userDataReady} />
}
return (
<FlexLayout>
<Route exact path={`${path}`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
cakePrice={cakePrice}
account={account}
removed={false}
/>
))}
</Route>
<Route exact path={`${path}/history`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
cakePrice={cakePrice}
account={account}
removed
/>
))}
</Route>
<Route exact path={`${path}/archived`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
cakePrice={cakePrice}
account={account}
removed
/>
))}
</Route>
</FlexLayout>
)
}
const handleSortOptionChange = (option: OptionProps): void => {
setSortOption(option.value)
}
return (
<>
<Page>
<PageHeader>
<Flex justifyContent="space-between" flexDirection={['column', null, null, 'row']}>
<Flex flex="1" flexDirection="column" mr={['8px', 0]}>
<GradientHeading as="h1" scale="xxl" color="glide" mb="24px">
{t('Farms')}
</GradientHeading>
<Heading scale="lg" color="text">
{t('Deposit LP tokens to earn')}
</Heading>
</Flex>
{chainId === 20 && Number(GLIDE_START_BLOCK.toString()) - currentBlock + 30000 > 0 && (
<Flex flex="1" height="fit-content" justifyContent="center" alignItems="center" mt={['24px', null, '0']}>
<CountdownCard currentBlock={currentBlock} targetBlock={Number(GLIDE_START_BLOCK.toString())} />
</Flex>
)}
</Flex>
{/* <NavLink exact activeClassName="active" to="/farms/auction" id="lottery-pot-banner">
<Button p="0" variant="text">
<Text color="primary" bold fontSize="16px" mr="4px">
{t('Community Auctions')}
</Text>
<ArrowForwardIcon color="primary" />
</Button>
</NavLink> */}
</PageHeader>
<ControlContainer>
<ViewControls>
{/* <ToggleView viewMode={viewMode} onToggle={(mode: ViewMode) => setViewMode(mode)} /> */}
<ToggleWrapper>
<Toggle checked={stakedOnly} onChange={() => setStakedOnly(!stakedOnly)} scale="sm" />
<Text> {t('Staked only')}</Text>
</ToggleWrapper>
<FarmTabButtons hasStakeInFinishedFarms={stakedInactiveFarms.length > 0} />
</ViewControls>
<FilterContainer>
<LabelWrapper>
<Text textTransform="uppercase">{t('Sort by')}</Text>
<Select
options={[
{
label: t('Hot'),
value: 'hot',
},
{
label: t('APR'),
value: 'apr',
},
{
label: t('Multiplier'),
value: 'multiplier',
},
{
label: t('Earned'),
value: 'earned',
},
{
label: t('Liquidity'),
value: 'liquidity',
},
]}
onChange={handleSortOptionChange}
/>
</LabelWrapper>
<LabelWrapper style={{ marginLeft: 16 }}>
<Text textTransform="uppercase">{t('Search')}</Text>
<SearchInput onChange={handleChangeQuery} placeholder={t('Search Farms')} />
</LabelWrapper>
</FilterContainer>
</ControlContainer>
{chainId !== 20 && (
<ConnectContainer justifyContent="center">
<Button
onClick={() => {
setupNetwork(20, library)
}}
>
{t('Connect to the Elastos network to begin')}
</Button>
</ConnectContainer>
)}
{renderContent()}
{account && !userDataLoaded && stakedOnly && (
<Flex justifyContent="center">
<Loading />
</Flex>
)}
<div ref={loadMoreRef} />
{/* <StyledImage src="/images/decorations/3dpan.png" alt="Pancake illustration" width={120} height={103} /> */}
</Page>
</>
)
}
Example #2
Source File: Farms.tsx From vvs-ui with GNU General Public License v3.0 | 4 votes |
Farms: React.FC = () => {
const { path } = useRouteMatch()
const { pathname } = useLocation()
const { t } = useTranslation()
const { data: farmsLP, userDataLoaded } = useFarms()
const vvsPrice = usePriceVvsUsdc()
const [query, setQuery] = useState('')
const [viewMode, setViewMode] = useUserFarmsViewMode()
const { account } = useWeb3React()
const [sortOption, setSortOption] = useState('hot')
const { observerRef, isIntersecting } = useIntersectionObserver()
const chosenFarmsLength = useRef(0)
const isArchived = pathname.includes('archived')
const isInactive = pathname.includes('history')
const isActive = !isInactive && !isArchived
usePollFarmsWithUserData(isArchived)
// Users with no wallet connected should see 0 as Earned amount
// Connected users should see loading indicator until first userData has loaded
const userDataReady = !account || (!!account && userDataLoaded)
const [stakedOnly, setStakedOnly] = useUserFarmStakedOnly(isActive)
// const activeFarms = farmsLP.filter((farm) => farm.pid !== 0 && farm.multiplier !== '0X' && !isArchivedPid(farm.pid))
// const inactiveFarms = farmsLP.filter((farm) => farm.pid !== 0 && farm.multiplier === '0X' && !isArchivedPid(farm.pid))
const activeFarms = farmsLP.filter(farm => farm.pid !== 0 && farm.pid !== 3) // FIXME temp show CRO/VVS and CRO/USDC with 0x
const inactiveFarms = farmsLP.filter(() => false) // FIXME temp show nothing
const archivedFarms = farmsLP.filter((farm) => isArchivedPid(farm.pid))
const stakedOnlyFarms = activeFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const stakedInactiveFarms = inactiveFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const stakedArchivedFarms = archivedFarms.filter(
(farm) => farm.userData && new BigNumber(farm.userData.stakedBalance).isGreaterThan(0),
)
const farmsList = useCallback(
(farmsToDisplay: DeserializedFarm[]): FarmWithStakedValue[] => {
let farmsToDisplayWithAPR: FarmWithStakedValue[] = farmsToDisplay.map((farm) => {
if (!farm.lpTotalInQuoteToken || !farm.quoteTokenPriceUsdc) {
return farm
}
const totalLiquidity = new BigNumber(farm.lpTotalInQuoteToken).times(farm.quoteTokenPriceUsdc)
const { vvsRewardsApr, lpRewardsApr } = isActive
? getFarmApr(new BigNumber(farm.poolWeight), vvsPrice, totalLiquidity, getAddress(farm.lpAddresses))
: { vvsRewardsApr: 0, lpRewardsApr: 0 }
return { ...farm, apr: vvsRewardsApr, lpRewardsApr, liquidity: totalLiquidity }
})
if (query) {
const lowercaseQuery = latinise(query.toLowerCase())
farmsToDisplayWithAPR = farmsToDisplayWithAPR.filter((farm: FarmWithStakedValue) => {
return latinise(farm.lpSymbol.toLowerCase()).includes(lowercaseQuery)
})
}
return farmsToDisplayWithAPR
},
[vvsPrice, query, isActive],
)
const handleChangeQuery = (event: React.ChangeEvent<HTMLInputElement>) => {
setQuery(event.target.value)
}
const [numberOfFarmsVisible, setNumberOfFarmsVisible] = useState(NUMBER_OF_FARMS_VISIBLE)
const chosenFarmsMemoized = useMemo(() => {
let chosenFarms = []
const sortFarms = (farms: FarmWithStakedValue[]): FarmWithStakedValue[] => {
switch (sortOption) {
case 'apr':
return orderBy(farms, (farm: FarmWithStakedValue) => farm.apr + farm.lpRewardsApr, 'desc')
case 'multiplier':
return orderBy(
farms,
(farm: FarmWithStakedValue) => (farm.multiplier ? Number(farm.multiplier.slice(0, -1)) : 0),
'desc',
)
case 'earned':
return orderBy(
farms,
(farm: FarmWithStakedValue) => (farm.userData ? Number(farm.userData.earnings) : 0),
'desc',
)
case 'liquidity':
return orderBy(farms, (farm: FarmWithStakedValue) => Number(farm.liquidity), 'desc')
default:
return farms
}
}
if (isActive) {
chosenFarms = stakedOnly ? farmsList(stakedOnlyFarms) : farmsList(activeFarms)
}
if (isInactive) {
chosenFarms = stakedOnly ? farmsList(stakedInactiveFarms) : farmsList(inactiveFarms)
}
if (isArchived) {
chosenFarms = stakedOnly ? farmsList(stakedArchivedFarms) : farmsList(archivedFarms)
}
return sortFarms(chosenFarms).slice(0, numberOfFarmsVisible)
}, [
sortOption,
activeFarms,
farmsList,
inactiveFarms,
archivedFarms,
isActive,
isInactive,
isArchived,
stakedArchivedFarms,
stakedInactiveFarms,
stakedOnly,
stakedOnlyFarms,
numberOfFarmsVisible,
])
chosenFarmsLength.current = chosenFarmsMemoized.length
useEffect(() => {
if (isIntersecting) {
setNumberOfFarmsVisible((farmsCurrentlyVisible) => {
if (farmsCurrentlyVisible <= chosenFarmsLength.current) {
return farmsCurrentlyVisible + NUMBER_OF_FARMS_VISIBLE
}
return farmsCurrentlyVisible
})
}
}, [isIntersecting])
const rowData = chosenFarmsMemoized.map((farm) => {
const { token, quoteToken } = farm
const tokenAddress = token.address
const quoteTokenAddress = quoteToken.address
// const lpLabel = farm.lpSymbol && farm.lpSymbol.split(' ')[0].toUpperCase().replace('VVS', '') // FIXME replace ??
const lpLabel = farm.lpSymbol && farm.lpSymbol.split(' ')[0].toUpperCase()
const row: RowProps = {
apr: {
value: getDisplayApr(farm.apr, farm.lpRewardsApr),
pid: farm.pid,
multiplier: farm.multiplier,
lpLabel,
lpSymbol: farm.lpSymbol,
tokenAddress,
quoteTokenAddress,
vvsPrice,
originalValue: farm.apr,
},
allocation: {
poolWeight: farm.poolWeight.toNumber(),
},
farm: {
label: lpLabel,
pid: farm.pid,
token: farm.token,
quoteToken: farm.quoteToken,
},
earned: {
earnings: getBalanceNumber(new BigNumber(farm.userData.earnings)),
pid: farm.pid,
},
liquidity: {
liquidity: farm.liquidity,
lpTokenBalanceMC: farm.lpTokenBalanceMC,
},
multiplier: {
multiplier: farm.multiplier,
},
details: farm,
}
return row
})
const renderContent = (): JSX.Element => {
if (viewMode === ViewMode.TABLE && rowData.length) {
const columnSchema = isBlindMode() ? DesktopColumnBlindModeSchema : DesktopColumnSchema
const columns = columnSchema.map((column) => ({
id: column.id,
name: column.name,
label: column.label,
sort: (a: RowType<RowProps>, b: RowType<RowProps>) => {
switch (column.name) {
case 'farm':
return b.id - a.id
case 'apr':
if (a.original.apr.value && b.original.apr.value) {
return Number(a.original.apr.value) - Number(b.original.apr.value)
}
return 0
case 'earned':
return a.original.earned.earnings - b.original.earned.earnings
default:
return 1
}
},
sortable: column.sortable,
}))
return <Table data={rowData} columns={columns} userDataReady={userDataReady} />
}
return (
<FlexLayout>
<Route exact path={`${path}`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
vvsPrice={vvsPrice}
account={account}
removed={false}
/>
))}
</Route>
<Route exact path={`${path}/history`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
vvsPrice={vvsPrice}
account={account}
removed
/>
))}
</Route>
<Route exact path={`${path}/archived`}>
{chosenFarmsMemoized.map((farm) => (
<FarmCard
key={farm.pid}
farm={farm}
displayApr={getDisplayApr(farm.apr, farm.lpRewardsApr)}
vvsPrice={vvsPrice}
account={account}
removed
/>
))}
</Route>
</FlexLayout>
)
}
const handleSortOptionChange = (option: OptionProps): void => {
setSortOption(option.value)
}
return (
<>
<StyledPageHeader>
<FarmBackground />
<StyledHeading as="h1" scale="xxl" mb="20px" weight={500}>
{t('Crystal Farms')}
</StyledHeading>
<StyledHeading scale="md">{t('Our harvests are always bountiful.')}</StyledHeading>
<StyledHeading scale="md" mb="36px" mt="14px">
{t('Pick your farm and earn.')}
</StyledHeading>
<ControlContainer>
<ViewControls>
<ToggleView viewMode={viewMode} onToggle={(mode: ViewMode) => setViewMode(mode)} />
<ToggleWrapper>
<Toggle
id="staked-only-farms"
checked={stakedOnly}
onChange={() => setStakedOnly(!stakedOnly)}
scale="sm"
/>
<Text color="white">{t('Staked only')}</Text>
</ToggleWrapper>
<FarmTabButtons hasStakeInFinishedFarms={stakedInactiveFarms.length > 0} />
</ViewControls>
<FilterContainer>
<LabelWrapper>
{/* <Text textTransform="uppercase">{t('Sort by')}</Text> */}
<Select
options={[
{
label: t('Hot'),
value: 'hot',
},
{
label: t('APR'),
value: 'apr',
},
{
label: t('Multiplier'),
value: 'multiplier',
},
{
label: t('Earned'),
value: 'earned',
},
{
label: t('Staked Liquidity'),
value: 'liquidity',
},
].filter(x => !(isBlindMode() && (['liquidity', 'apr'].includes(x.value))))}
onOptionChange={handleSortOptionChange}
/>
</LabelWrapper>
<LabelWrapper style={{ marginLeft: 16 }}>
{/* <Text textTransform="uppercase">{t('Search')}</Text> */}
<SearchInput onChange={handleChangeQuery} placeholder="Search" />
</LabelWrapper>
</FilterContainer>
</ControlContainer>
</StyledPageHeader>
<Page style={{ marginTop: '-90px', position: 'relative' }}>
{renderContent()}
{account && !userDataLoaded && stakedOnly && (
<Flex justifyContent="center">
<Loading />
</Flex>
)}
<div ref={observerRef} />
</Page>
</>
)
}