Skip to content

Commit

Permalink
feat: Add function to estimate gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
RabbitDoge committed Jun 29, 2021
1 parent f3e1633 commit eaca7fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
File renamed without changes.
40 changes: 39 additions & 1 deletion src/utils/callHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
import BigNumber from 'bignumber.js'
import { DEFAULT_GAS_LIMIT, DEFAULT_TOKEN_DECIMAL } from 'config'
import { ethers } from 'ethers'
import { ethers, Contract } from 'ethers'
import { BIG_TEN, BIG_ZERO } from './bigNumber'

const options = {
gasLimit: DEFAULT_GAS_LIMIT,
}

/**
* Estimate the gas needed to call a function, and add a 10% margin
* @param contract Used to perform the call
* @param methodName The name of the methode called
* @param args An array of arguments to pass to the method
* @returns https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt
*/
export const estimageGas = async (contract: Contract, methodName: string, methodArgs: any[]) => {
if (!contract[methodName]) {
throw new Error(`Method ${methodName} doesn't exist on ${contract.address}`)
}
const rawGasEstimation = await contract.estimateGas[methodName](...methodArgs)
// By convention, ethers.BigNumber values are multiplied by 1000 to avoid dealing with real numbers
const gasEstimation = rawGasEstimation
.mul(ethers.BigNumber.from(10000).add(ethers.BigNumber.from(1000)))
.div(ethers.BigNumber.from(10000))
return gasEstimation
}

/**
* Perform a contract call with a gas value returned from estimateGas
* @param contract Used to perform the call
* @param methodName The name of the methode called
* @param args An array of arguments to pass to the method
* @returns https://docs.ethers.io/v5/api/providers/types/#providers-TransactionReceipt
*/
export const callWithEstimateGas = async (
contract: Contract,
methodName: string,
methodArgs: any[] = [],
): Promise<ethers.providers.TransactionResponse> => {
const gasEstimation = estimageGas(contract, methodName, methodArgs)
const tx = await contract[methodName](...methodArgs, {
gasLimit: gasEstimation,
})
return tx
}

export const approve = async (lpContract, masterChefContract) => {
const tx = await lpContract.approve(masterChefContract.address, ethers.constants.MaxUint256)
return tx.wait()
Expand Down
9 changes: 6 additions & 3 deletions src/views/Pools/components/CakeVaultCard/VaultStakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import useToast from 'hooks/useToast'
import { fetchCakeVaultUserData } from 'state/pools'
import { Pool } from 'state/types'
import { getAddress } from 'utils/addressHelpers'
import { callWithEstimateGas } from 'utils/callHelpers'
import { convertCakeToShares } from '../../helpers'
import FeeSummary from './FeeSummary'

Expand Down Expand Up @@ -81,7 +82,7 @@ const VaultStakeModal: React.FC<VaultStakeModalProps> = ({ pool, stakingMax, isR
const isWithdrawingAll = sharesRemaining.lte(triggerWithdrawAllThreshold)

if (isWithdrawingAll) {
const tx = await cakeVaultContract.withdrawAll()
const tx = await callWithEstimateGas(cakeVaultContract, 'withdrawAll')
setPendingTx(true)
const receipt = await tx.wait()
if (receipt.status) {
Expand All @@ -97,7 +98,9 @@ const VaultStakeModal: React.FC<VaultStakeModalProps> = ({ pool, stakingMax, isR
} else {
// .toString() being called to fix a BigNumber error in prod
// as suggested here https://github.com/ChainSafe/web3.js/issues/2077
const tx = await cakeVaultContract.withdraw(shareStakeToWithdraw.sharesAsBigNumber.toString())
const tx = await callWithEstimateGas(cakeVaultContract, 'withdraw', [
shareStakeToWithdraw.sharesAsBigNumber.toString(),
])
setPendingTx(true)
const receipt = await tx.wait()
if (receipt.status) {
Expand All @@ -115,7 +118,7 @@ const VaultStakeModal: React.FC<VaultStakeModalProps> = ({ pool, stakingMax, isR
const handleDeposit = async (convertedStakeAmount: BigNumber) => {
// .toString() being called to fix a BigNumber error in prod
// as suggested here https://github.com/ChainSafe/web3.js/issues/2077
const tx = await cakeVaultContract.deposit(convertedStakeAmount.toString())
const tx = await callWithEstimateGas(cakeVaultContract, 'deposit', [convertedStakeAmount.toString()])
setPendingTx(true)
const receipt = await tx.wait()
if (receipt.status) {
Expand Down

0 comments on commit eaca7fc

Please sign in to comment.