Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/features/onsen/FarmMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { ChainId } from '@mistswapdex/sdk'
import { ChainId, CurrencyAmount, MASTERCHEF_V2_ADDRESS } from '@mistswapdex/sdk'
import NavLink from '../../components/NavLink'
import React from 'react'
import React, { useState } from 'react'
import { t } from '@lingui/macro'
import { useLingui } from '@lingui/react'
import { useActiveWeb3React } from '../../hooks'
import { useWalletModalToggle } from '../../state/application/hooks'
import useMasterChef from './useMasterChef'
import { Chef } from './enum'
import { MIST } from '../../config/tokens'

const Menu = ({ positionsLength }) => {
const Menu = ({ positionsLength, farms }) => {
useState()
const { account, chainId } = useActiveWeb3React()
const { i18n } = useLingui()
const toggleWalletModal = useWalletModalToggle()

const { harvestAll } = useMasterChef(Chef.MASTERCHEF_V2)
const zero = CurrencyAmount.fromRawAmount(MIST[chainId], 0);
const userFarms = [...(farms || [])].filter(farm => farm.pending);
const total = userFarms ? userFarms.reduce((sum, farm) => farm.pendingSushi.add(sum), zero) : zero;

return (
<div className="space-y-4">
{account ? (
Expand Down Expand Up @@ -44,6 +53,14 @@ const Menu = ({ positionsLength }) => {
</a>
</NavLink>

{/* <Link href=""> */}
<a className="flex items-center justify-between px-4 py-6 text-base font-bold border border-transparent rounded cursor-pointer bg-dark-900 hover:bg-dark-800"
onClick={async () => await harvestAll(userFarms)}
>
{i18n._(t`Harvest All (${total.toFixed(2)} MIST)`)}
</a>
{/* </Link> */}

{/*chainId === ChainId.MAINNET && (
<>
<NavLink
Expand Down
43 changes: 42 additions & 1 deletion src/features/onsen/useMasterChef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,46 @@ export default function useMasterChef(chef: Chef) {
[account, chef, contract, sushi]
)

return { deposit, withdraw, harvest }
const harvestAll = useCallback(
async (farms: any[]) => {
if (chef === Chef.MASTERCHEF) {
throw Error("MasterChef version 1 is not supported")
}

try {
let tx

const pids = farms.map(farm => farm.id)
const pendingSushis: BigNumber[] = await Promise.all(pids.map(pid => contract?.pendingSushi(pid, account)))
const sum: BigNumber = pendingSushis.reduce((sum: BigNumber, value: BigNumber) => sum.add(value), BigNumber.from(0))
const balanceOf: BigNumber = await sushi?.balanceOf(contract?.address)

const calls = pids.map(pid => contract?.interface?.encodeFunctionData('harvest', [pid, account]));

// If MasterChefV2 doesn't have enough sushi to harvest all farms, batch in a harvest.
if (sum.gt(balanceOf)) {
tx = await contract?.batch(
[
contract?.interface?.encodeFunctionData('harvestFromMasterChef'),
...calls
],
true
)
} else {
tx = await contract?.batch(
calls,
true
)
}

return tx
} catch (e) {
console.error(e)
return e
}
},
[account, chef, contract, sushi]
)

return { deposit, withdraw, harvest, harvestAll }
}
2 changes: 1 addition & 1 deletion src/pages/farm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export default function Farm(): JSX.Element {
<meta key="description" name="description" content="Farm MIST" />
</Head>
<div className={classNames('sticky top-0 hidden lg:block md:col-span-1')} style={{ maxHeight: '40rem' }}>
<Menu positionsLength={positions.length} />
<Menu positionsLength={positions.length} farms={farms} />
</div>
<div className={classNames('space-y-6 col-span-4 lg:col-span-3')}>
<Search
Expand Down