diff --git a/src/bao/utils.js b/src/bao/utils.js index d5471aa..6bcd551 100644 --- a/src/bao/utils.js +++ b/src/bao/utils.js @@ -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) diff --git a/src/hooks/useEmergencyWithdraw.ts b/src/hooks/useEmergencyWithdraw.ts new file mode 100644 index 0000000..7ecb00a --- /dev/null +++ b/src/hooks/useEmergencyWithdraw.ts @@ -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 diff --git a/src/views/Farm/components/Stake.tsx b/src/views/Farm/components/Stake.tsx index 39c4fd6..020475c 100644 --- a/src/views/Farm/components/Stake.tsx +++ b/src/views/Farm/components/Stake.tsx @@ -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 = ({ 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 = ({ pid, tokenName }) => { const stakedBalance = useStakedBalance(pid) - const { onStake } = useStake(pid) - const { onUnstake } = useUnstake(pid) - - const [onPresentDeposit] = useModal( - , - ) + const { onUnstake } = useEmergencyWithdraw(pid) const [onPresentWithdraw] = useModal( = ({ 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 ( @@ -80,27 +45,11 @@ const Stake: React.FC = ({ lpContract, pid, tokenName, poolType }) =