Skip to content

Commit

Permalink
fix: Unstake farms with vault (#10129)
Browse files Browse the repository at this point in the history
<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to update stake and unstake functions in the
Farms feature to use gas prices specific to the Binance Smart Chain.

### Detailed summary
- Updated stake and unstake functions to use BNB gas prices for non-BSC
vaults
- Added logic to handle BNB gas prices in stake and unstake functions

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Jul 4, 2024
1 parent ccca713 commit 98dad95
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 5 additions & 4 deletions apps/web/src/state/user/hooks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ export function useUserFarmStakedOnly(isActive: boolean): [boolean, (stakedOnly:
[dispatch],
)

const toggleUserFarmStakedOnly = useCallback(
() => setUserFarmStakedOnly(!userFarmStakedOnly),
[setUserFarmStakedOnly, userFarmStakedOnly],
)
const toggleUserFarmStakedOnly = useCallback(() => {
const booleanStakedOnly =
userFarmStakedOnly === FarmStakedOnly.ON_FINISHED ? !isActive : userFarmStakedOnly === FarmStakedOnly.TRUE
setUserFarmStakedOnly(!booleanStakedOnly)
}, [setUserFarmStakedOnly, userFarmStakedOnly, isActive])

return [
userFarmStakedOnly === FarmStakedOnly.ON_FINISHED ? !isActive : userFarmStakedOnly === FarmStakedOnly.TRUE,
Expand Down
10 changes: 9 additions & 1 deletion apps/web/src/views/Farms/hooks/getNonBscVaultFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BIG_ZERO } from '@pancakeswap/utils/bigNumber'
import BigNumber from 'bignumber.js'
import { getCrossFarmingSenderContract, getNonBscVaultContract } from 'utils/contractHelpers'
import { Address } from 'viem'
import { estimateFeesPerGas } from 'viem/actions'
import { publicClient as getPublicClient } from 'utils/viem'

export enum MessageTypes {
Deposit = 0,
Expand Down Expand Up @@ -79,8 +81,14 @@ export const getNonBscVaultContractFee = async ({
userAddress as Address,
messageType,
])
const publicClient = getPublicClient({ chainId })
const { maxFeePerGas } = await estimateFeesPerGas(publicClient)
const evmGasPrice = maxFeePerGas?.toString()
if (!evmGasPrice) {
throw new Error('Missing evm gasPrice')
}
const fee = msgBusFee.times(exchangeRate).div(ORACLE_PRECISION)
const total = new BigNumber(gasPrice).times(estimateEvmGaslimit.toString()).plus(fee)
const total = new BigNumber(evmGasPrice).times(estimateEvmGaslimit.toString()).plus(fee)
return totalFee.plus(total).times(WITHDRAW_BUFFER).toFixed(0)
}

Expand Down
6 changes: 4 additions & 2 deletions apps/web/src/views/Farms/hooks/useStakeFarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { useCallback } from 'react'
import { useFeeDataWithGasPrice } from 'state/user/hooks'
import { bCakeStakeFarm, nonBscStakeFarm, stakeFarm } from 'utils/calls'
import { useOraclePrice } from 'views/Farms/hooks/useFetchOraclePrice'
import { ChainId } from '@pancakeswap/chains'

const useStakeFarms = (pid: number, vaultPid?: number) => {
const { account, chainId } = useAccountActiveChain()
const { gasPrice } = useFeeDataWithGasPrice()
const { gasPrice: bnbGasPrice } = useFeeDataWithGasPrice(ChainId.BSC)

const oraclePrice = useOraclePrice(chainId ?? 0)
const masterChefContract = useMasterchef()
Expand All @@ -23,9 +25,9 @@ const useStakeFarms = (pid: number, vaultPid?: number) => {

const handleStakeNonBsc = useCallback(
async (amount: string) => {
return nonBscStakeFarm(nonBscVaultContract, vaultPid, amount, gasPrice, account, oraclePrice, chainId)
return nonBscStakeFarm(nonBscVaultContract, vaultPid, amount, bnbGasPrice, account, oraclePrice, chainId)
},
[nonBscVaultContract, vaultPid, gasPrice, account, oraclePrice, chainId],
[nonBscVaultContract, vaultPid, bnbGasPrice, account, oraclePrice, chainId],
)

return { onStake: vaultPid ? handleStakeNonBsc : handleStake }
Expand Down
6 changes: 4 additions & 2 deletions apps/web/src/views/Farms/hooks/useUnstakeFarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { useCallback } from 'react'
import { useFeeDataWithGasPrice } from 'state/user/hooks'
import { bCakeUnStakeFarm, nonBscUnstakeFarm, unstakeFarm } from 'utils/calls'
import { useOraclePrice } from 'views/Farms/hooks/useFetchOraclePrice'
import { ChainId } from '@pancakeswap/chains'

const useUnstakeFarms = (pid?: number, vaultPid?: number) => {
const { account, chainId } = useAccountActiveChain()
const { gasPrice } = useFeeDataWithGasPrice()
const { gasPrice: bnbGasPrice } = useFeeDataWithGasPrice(ChainId.BSC)
const oraclePrice = useOraclePrice(chainId ?? 0)
const masterChefContract = useMasterchef()
const nonBscVaultContract = useNonBscVault()
Expand All @@ -22,9 +24,9 @@ const useUnstakeFarms = (pid?: number, vaultPid?: number) => {

const handleUnstakeNonBsc = useCallback(
async (amount: string) => {
return nonBscUnstakeFarm(nonBscVaultContract, vaultPid, amount, gasPrice, account, oraclePrice, chainId)
return nonBscUnstakeFarm(nonBscVaultContract, vaultPid, amount, bnbGasPrice, account, oraclePrice, chainId)
},
[nonBscVaultContract, vaultPid, gasPrice, account, oraclePrice, chainId],
[nonBscVaultContract, vaultPid, bnbGasPrice, account, oraclePrice, chainId],
)

return { onUnstake: vaultPid ? handleUnstakeNonBsc : handleUnstake }
Expand Down

0 comments on commit 98dad95

Please sign in to comment.