From a0f64b2cb09b5c4f53f90d6e8bcfccf9f4a190c5 Mon Sep 17 00:00:00 2001 From: mainnet-pat Date: Sat, 30 Oct 2021 14:30:56 +0000 Subject: [PATCH] feat(ui): harvest all farms --- src/features/onsen/FarmMenu.tsx | 23 +++++++++++++-- src/features/onsen/useMasterChef.ts | 43 ++++++++++++++++++++++++++++- src/pages/farm/index.tsx | 2 +- 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/features/onsen/FarmMenu.tsx b/src/features/onsen/FarmMenu.tsx index d147c3ec55..2ccc63af9e 100644 --- a/src/features/onsen/FarmMenu.tsx +++ b/src/features/onsen/FarmMenu.tsx @@ -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 (
{account ? ( @@ -44,6 +53,14 @@ const Menu = ({ positionsLength }) => { + {/* */} + await harvestAll(userFarms)} + > + {i18n._(t`Harvest All (${total.toFixed(2)} MIST)`)} + + {/* */} + {/*chainId === ChainId.MAINNET && ( <> { + 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 } } diff --git a/src/pages/farm/index.tsx b/src/pages/farm/index.tsx index 5fe9f67116..1703e70335 100644 --- a/src/pages/farm/index.tsx +++ b/src/pages/farm/index.tsx @@ -460,7 +460,7 @@ export default function Farm(): JSX.Element {