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

Commit 3a3a624

Browse files
committed
Add emergencyWithdraw to old farms
1 parent 3c6f43a commit 3a3a624

File tree

4 files changed

+63
-89
lines changed

4 files changed

+63
-89
lines changed

src/bao/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ export const unstake = async (
176176
return tx.transactionHash
177177
})
178178
}
179+
180+
export const emergencyWithdraw = async (masterChefContract, pid, account) => {
181+
return masterChefContract.methods
182+
.emergencyWithdraw(pid)
183+
.send({ from: account })
184+
.on('transactionHash', (tx) => {
185+
console.log(tx)
186+
return tx.transactionHash
187+
})
188+
}
189+
179190
export const harvest = async (masterChefContract, pid, account) => {
180191
return masterChefContract.methods
181192
.claimReward(pid)

src/hooks/useEmergencyWithdraw.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useCallback } from 'react'
2+
3+
import useBao from './useBao'
4+
import { useWallet } from 'use-wallet'
5+
6+
import { getMasterChefContract, emergencyWithdraw } from '../bao/utils'
7+
8+
const useEmergencyWithdraw = (pid: number) => {
9+
const { account } = useWallet()
10+
const bao = useBao()
11+
const masterChefContract = getMasterChefContract(bao)
12+
13+
const handleUnstake = useCallback(
14+
async (amount: string) => {
15+
const txHash = await emergencyWithdraw(masterChefContract, pid, account)
16+
console.log(txHash)
17+
},
18+
[account, pid, bao],
19+
)
20+
21+
return { onUnstake: handleUnstake }
22+
}
23+
24+
export default useEmergencyWithdraw

src/views/Farm/components/Stake.tsx

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
11
import BigNumber from 'bignumber.js'
2-
import React, { useCallback, useState } from 'react'
2+
import React from 'react'
33
import styled from 'styled-components'
44
import { Contract } from 'web3-eth-contract'
55
import Button from '../../../components/Button'
66
import Card from '../../../components/Card'
77
import CardContent from '../../../components/CardContent'
88
import CardIcon from '../../../components/CardIcon'
9-
import IconButton from '../../../components/IconButton'
10-
import { AddIcon } from '../../../components/icons'
119
import Label from '../../../components/Label'
1210
import Value from '../../../components/Value'
1311
import { PoolType } from '../../../contexts/Farms/types'
14-
import useAllowance from '../../../hooks/useAllowance'
15-
import useApprove from '../../../hooks/useApprove'
12+
import useEmergencyWithdraw from '../../../hooks/useEmergencyWithdraw'
1613
import useModal from '../../../hooks/useModal'
17-
import useStake from '../../../hooks/useStake'
1814
import useStakedBalance from '../../../hooks/useStakedBalance'
19-
import useTokenBalance from '../../../hooks/useTokenBalance'
20-
import useUnstake from '../../../hooks/useUnstake'
2115
import { getBalanceNumber } from '../../../utils/formatBalance'
22-
import DepositModal from './DepositModal'
2316
import WithdrawModal from './WithdrawModal'
2417

2518
interface StakeProps {
2619
lpContract: Contract
2720
pid: number
28-
tokenName: string,
21+
tokenName: string
2922
poolType: PoolType
3023
}
3124

32-
const Stake: React.FC<StakeProps> = ({ lpContract, pid, tokenName, poolType }) => {
33-
const [requestedApproval, setRequestedApproval] = useState(false)
34-
35-
const allowance = useAllowance(lpContract)
36-
const { onApprove } = useApprove(lpContract)
37-
38-
const tokenBalance = useTokenBalance(lpContract.options.address)
25+
const Stake: React.FC<StakeProps> = ({ pid, tokenName }) => {
3926
const stakedBalance = useStakedBalance(pid)
4027

41-
const { onStake } = useStake(pid)
42-
const { onUnstake } = useUnstake(pid)
43-
44-
const [onPresentDeposit] = useModal(
45-
<DepositModal
46-
max={tokenBalance}
47-
onConfirm={onStake}
48-
tokenName={tokenName}
49-
/>,
50-
)
28+
const { onUnstake } = useEmergencyWithdraw(pid)
5129

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

60-
const handleApprove = useCallback(async () => {
61-
try {
62-
setRequestedApproval(true)
63-
const txHash = await onApprove()
64-
// user rejected tx or didn't go thru
65-
if (!txHash) {
66-
setRequestedApproval(false)
67-
}
68-
} catch (e) {
69-
console.log(e)
70-
}
71-
}, [onApprove, setRequestedApproval])
72-
7338
return (
7439
<Card>
7540
<CardContent>
@@ -80,27 +45,11 @@ const Stake: React.FC<StakeProps> = ({ lpContract, pid, tokenName, poolType }) =
8045
<Label text={`${tokenName} Tokens Staked`} />
8146
</StyledCardHeader>
8247
<StyledCardActions>
83-
{!allowance.toNumber() ? (
84-
<Button
85-
disabled={requestedApproval}
86-
onClick={handleApprove}
87-
text={`Approve ${tokenName}`}
88-
/>
89-
) : (
90-
<>
91-
<Button
92-
disabled={stakedBalance.eq(new BigNumber(0))}
93-
text="Unstake"
94-
onClick={onPresentWithdraw}
95-
/>
96-
<StyledActionSpacer />
97-
{poolType !== PoolType.ARCHIVED ? (
98-
<IconButton onClick={onPresentDeposit}>
99-
<AddIcon />
100-
</IconButton>
101-
) : ''}
102-
</>
103-
)}
48+
<Button
49+
disabled={stakedBalance.eq(new BigNumber(0))}
50+
text="Unstake"
51+
onClick={onPresentWithdraw}
52+
/>
10453
</StyledCardActions>
10554
</StyledCardContentInner>
10655
</CardContent>

src/views/Farm/components/WithdrawModal.tsx

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import BigNumber from 'bignumber.js'
2-
import React, { useCallback, useMemo, useState } from 'react'
2+
import React, { useMemo, useState } from 'react'
33
import Button from '../../../components/Button'
4+
import Label from '../../../components/Label'
45
import Modal, { ModalProps } from '../../../components/Modal'
56
import ModalActions from '../../../components/ModalActions'
6-
import ModalTitle from '../../../components/ModalTitle'
77
import ModalContent from '../../../components/ModalContent'
8-
import TokenInput from '../../../components/TokenInput'
8+
import ModalTitle from '../../../components/ModalTitle'
99
import { getFullDisplayBalance } from '../../../utils/formatBalance'
1010

1111
interface WithdrawModalProps extends ModalProps {
@@ -20,52 +20,42 @@ const WithdrawModal: React.FC<WithdrawModalProps> = ({
2020
max,
2121
tokenName = '',
2222
}) => {
23-
const [val, setVal] = useState('')
2423
const [pendingTx, setPendingTx] = useState(false)
2524

2625
const fullBalance = useMemo(() => {
2726
return getFullDisplayBalance(max)
2827
}, [max])
2928

30-
const handleChange = useCallback(
31-
(e: React.FormEvent<HTMLInputElement>) => {
32-
setVal(e.currentTarget.value)
33-
},
34-
[setVal],
35-
)
36-
37-
const handleSelectMax = useCallback(() => {
38-
setVal(fullBalance)
39-
}, [fullBalance, setVal])
40-
4129
return (
4230
<Modal>
4331
<ModalTitle text={`Withdraw ${tokenName}`} />
44-
<TokenInput
45-
onSelectMax={handleSelectMax}
46-
onChange={handleChange}
47-
value={val}
48-
max={fullBalance}
49-
symbol={tokenName}
50-
/>
32+
<ModalContent>
33+
Due to an issue with the masterFarmer contract, users cannot withdraw
34+
their staked assets as they would normally. Because the withdraw
35+
function is trying to call the harvest function, and rewards have ended,
36+
the transactions are failing. We are now using the emergencyWithdraw
37+
function to remedy this situation, which takes a fee of 25%. Upon
38+
withdrawal, this 25% fee will be sent to the treasury multisig.
39+
Guardians will refund users this fee on a frequent basis. If you have
40+
any questions, please reach out on Discord. We are sorry for the
41+
inconvenience.
42+
<Label>
43+
Staked Balance: {fullBalance} {tokenName}
44+
</Label>
45+
</ModalContent>
5146
<ModalActions>
5247
<Button text="Cancel" variant="secondary" onClick={onDismiss} />
5348
<Button
5449
disabled={pendingTx}
5550
text={pendingTx ? 'Pending Confirmation' : 'Confirm'}
5651
onClick={async () => {
5752
setPendingTx(true)
58-
await onConfirm(val)
53+
await onConfirm(fullBalance)
5954
setPendingTx(false)
6055
onDismiss()
6156
}}
6257
/>
6358
</ModalActions>
64-
<ModalContent>
65-
{
66-
'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.'
67-
}
68-
</ModalContent>
6959
</Modal>
7060
)
7161
}

0 commit comments

Comments
 (0)