Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Add emergencyWithdraw to old farms
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalsine85 committed Jan 9, 2023
1 parent 3c6f43a commit 3a3a624
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 89 deletions.
11 changes: 11 additions & 0 deletions src/bao/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ export const unstake = async (
return tx.transactionHash
})
}

export const emergencyWithdraw = async (masterChefContract, pid, account) => {
return masterChefContract.methods
.emergencyWithdraw(pid)
.send({ from: account })
.on('transactionHash', (tx) => {
console.log(tx)
return tx.transactionHash
})
}

export const harvest = async (masterChefContract, pid, account) => {
return masterChefContract.methods
.claimReward(pid)
Expand Down
24 changes: 24 additions & 0 deletions src/hooks/useEmergencyWithdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useCallback } from 'react'

import useBao from './useBao'
import { useWallet } from 'use-wallet'

import { getMasterChefContract, emergencyWithdraw } from '../bao/utils'

const useEmergencyWithdraw = (pid: number) => {
const { account } = useWallet()
const bao = useBao()
const masterChefContract = getMasterChefContract(bao)

const handleUnstake = useCallback(
async (amount: string) => {
const txHash = await emergencyWithdraw(masterChefContract, pid, account)
console.log(txHash)
},
[account, pid, bao],
)

return { onUnstake: handleUnstake }
}

export default useEmergencyWithdraw
71 changes: 10 additions & 61 deletions src/views/Farm/components/Stake.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
import BigNumber from 'bignumber.js'
import React, { useCallback, useState } from 'react'
import React from 'react'
import styled from 'styled-components'
import { Contract } from 'web3-eth-contract'
import Button from '../../../components/Button'
import Card from '../../../components/Card'
import CardContent from '../../../components/CardContent'
import CardIcon from '../../../components/CardIcon'
import IconButton from '../../../components/IconButton'
import { AddIcon } from '../../../components/icons'
import Label from '../../../components/Label'
import Value from '../../../components/Value'
import { PoolType } from '../../../contexts/Farms/types'
import useAllowance from '../../../hooks/useAllowance'
import useApprove from '../../../hooks/useApprove'
import useEmergencyWithdraw from '../../../hooks/useEmergencyWithdraw'
import useModal from '../../../hooks/useModal'
import useStake from '../../../hooks/useStake'
import useStakedBalance from '../../../hooks/useStakedBalance'
import useTokenBalance from '../../../hooks/useTokenBalance'
import useUnstake from '../../../hooks/useUnstake'
import { getBalanceNumber } from '../../../utils/formatBalance'
import DepositModal from './DepositModal'
import WithdrawModal from './WithdrawModal'

interface StakeProps {
lpContract: Contract
pid: number
tokenName: string,
tokenName: string
poolType: PoolType
}

const Stake: React.FC<StakeProps> = ({ lpContract, pid, tokenName, poolType }) => {
const [requestedApproval, setRequestedApproval] = useState(false)

const allowance = useAllowance(lpContract)
const { onApprove } = useApprove(lpContract)

const tokenBalance = useTokenBalance(lpContract.options.address)
const Stake: React.FC<StakeProps> = ({ pid, tokenName }) => {
const stakedBalance = useStakedBalance(pid)

const { onStake } = useStake(pid)
const { onUnstake } = useUnstake(pid)

const [onPresentDeposit] = useModal(
<DepositModal
max={tokenBalance}
onConfirm={onStake}
tokenName={tokenName}
/>,
)
const { onUnstake } = useEmergencyWithdraw(pid)

const [onPresentWithdraw] = useModal(
<WithdrawModal
Expand All @@ -57,19 +35,6 @@ const Stake: React.FC<StakeProps> = ({ lpContract, pid, tokenName, poolType }) =
/>,
)

const handleApprove = useCallback(async () => {
try {
setRequestedApproval(true)
const txHash = await onApprove()
// user rejected tx or didn't go thru
if (!txHash) {
setRequestedApproval(false)
}
} catch (e) {
console.log(e)
}
}, [onApprove, setRequestedApproval])

return (
<Card>
<CardContent>
Expand All @@ -80,27 +45,11 @@ const Stake: React.FC<StakeProps> = ({ lpContract, pid, tokenName, poolType }) =
<Label text={`${tokenName} Tokens Staked`} />
</StyledCardHeader>
<StyledCardActions>
{!allowance.toNumber() ? (
<Button
disabled={requestedApproval}
onClick={handleApprove}
text={`Approve ${tokenName}`}
/>
) : (
<>
<Button
disabled={stakedBalance.eq(new BigNumber(0))}
text="Unstake"
onClick={onPresentWithdraw}
/>
<StyledActionSpacer />
{poolType !== PoolType.ARCHIVED ? (
<IconButton onClick={onPresentDeposit}>
<AddIcon />
</IconButton>
) : ''}
</>
)}
<Button
disabled={stakedBalance.eq(new BigNumber(0))}
text="Unstake"
onClick={onPresentWithdraw}
/>
</StyledCardActions>
</StyledCardContentInner>
</CardContent>
Expand Down
46 changes: 18 additions & 28 deletions src/views/Farm/components/WithdrawModal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import BigNumber from 'bignumber.js'
import React, { useCallback, useMemo, useState } from 'react'
import React, { useMemo, useState } from 'react'
import Button from '../../../components/Button'
import Label from '../../../components/Label'
import Modal, { ModalProps } from '../../../components/Modal'
import ModalActions from '../../../components/ModalActions'
import ModalTitle from '../../../components/ModalTitle'
import ModalContent from '../../../components/ModalContent'
import TokenInput from '../../../components/TokenInput'
import ModalTitle from '../../../components/ModalTitle'
import { getFullDisplayBalance } from '../../../utils/formatBalance'

interface WithdrawModalProps extends ModalProps {
Expand All @@ -20,52 +20,42 @@ const WithdrawModal: React.FC<WithdrawModalProps> = ({
max,
tokenName = '',
}) => {
const [val, setVal] = useState('')
const [pendingTx, setPendingTx] = useState(false)

const fullBalance = useMemo(() => {
return getFullDisplayBalance(max)
}, [max])

const handleChange = useCallback(
(e: React.FormEvent<HTMLInputElement>) => {
setVal(e.currentTarget.value)
},
[setVal],
)

const handleSelectMax = useCallback(() => {
setVal(fullBalance)
}, [fullBalance, setVal])

return (
<Modal>
<ModalTitle text={`Withdraw ${tokenName}`} />
<TokenInput
onSelectMax={handleSelectMax}
onChange={handleChange}
value={val}
max={fullBalance}
symbol={tokenName}
/>
<ModalContent>
Due to an issue with the masterFarmer contract, users cannot withdraw
their staked assets as they would normally. Because the withdraw
function is trying to call the harvest function, and rewards have ended,
the transactions are failing. We are now using the emergencyWithdraw
function to remedy this situation, which takes a fee of 25%. Upon
withdrawal, this 25% fee will be sent to the treasury multisig.
Guardians will refund users this fee on a frequent basis. If you have
any questions, please reach out on Discord. We are sorry for the
inconvenience.
<Label>
Staked Balance: {fullBalance} {tokenName}
</Label>
</ModalContent>
<ModalActions>
<Button text="Cancel" variant="secondary" onClick={onDismiss} />
<Button
disabled={pendingTx}
text={pendingTx ? 'Pending Confirmation' : 'Confirm'}
onClick={async () => {
setPendingTx(true)
await onConfirm(val)
await onConfirm(fullBalance)
setPendingTx(false)
onDismiss()
}}
/>
</ModalActions>
<ModalContent>
{
'Remember the longer you stay in a pool the lower your fee. Read the docs for details, but most users will want to stay in a pool 5 days or longer.'
}
</ModalContent>
</Modal>
)
}
Expand Down

0 comments on commit 3a3a624

Please sign in to comment.