diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 227602a1e..e1d5fa7b3 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,5 +1,5 @@ import assert from "assert"; -import { ethers, providers } from "ethers"; +import { BigNumber, ethers, providers } from "ethers"; import { utils } from "@across-protocol/sdk-v2"; import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "@across-protocol/constants-v2"; import * as superstruct from "superstruct"; @@ -697,5 +697,5 @@ export const walletBlacklist = (process.env.REACT_APP_WALLET_BLACKLIST || "") .split(",") .map((address) => address.toLowerCase()); -// Fallback gas costs for when the gas estimation fails -export const fallbackEstimatedGasCosts = ethers.utils.parseEther("0.01"); +// Pre-computed gas expenditure for deposits used for estimations +export const gasExpenditureDeposit = BigNumber.from(90_000); diff --git a/src/views/Bridge/hooks/useMaxBalance.ts b/src/views/Bridge/hooks/useMaxBalance.ts index fefe08913..9a1b9eb2c 100644 --- a/src/views/Bridge/hooks/useMaxBalance.ts +++ b/src/views/Bridge/hooks/useMaxBalance.ts @@ -1,17 +1,14 @@ import { useQuery } from "react-query"; -import { BigNumber, providers, constants, utils } from "ethers"; +import { BigNumber, constants } from "ethers"; import { useBalanceBySymbol, useConnection } from "hooks"; import { - getConfig, Route, max, getProvider, - fallbackEstimatedGasCosts, + gasExpenditureDeposit, + gasMultiplierPerChain, } from "utils"; -import { getPaddedGasEstimation } from "utils/transactions"; - -const config = getConfig(); export function useMaxBalance(selectedRoute: Route) { const { balance } = useBalanceBySymbol( @@ -35,14 +32,9 @@ export function useMaxBalance(selectedRoute: Route) { selectedRoute.fromTokenSymbol !== "ETH" ? balance : // For ETH, we need to take the gas costs into account before setting the max. bridgable amount - await estimateGasCostsForDeposit(selectedRoute, signer) - .then((estimatedGasCosts) => - max(balance.sub(estimatedGasCosts), 0) - ) - .catch((err) => { - console.error(err); - return max(balance.sub(fallbackEstimatedGasCosts), 0); - }); + await estimateGasCostsForDeposit(selectedRoute).then( + (estimatedGasCosts) => max(balance.sub(estimatedGasCosts), 0) + ); } else { maxBridgeAmount = constants.Zero; } @@ -51,38 +43,18 @@ export function useMaxBalance(selectedRoute: Route) { }, { enabled: Boolean(account && balance && signer), + retry: true, } ); } -async function estimateGasCostsForDeposit( - selectedRoute: Route, - signer: providers.JsonRpcSigner -) { +/** + * Estimated gas costs for a deposit with an empty message. + * This is used to calculate the maximum amount of ETH that can be bridged. + */ +async function estimateGasCostsForDeposit(selectedRoute: Route) { const provider = getProvider(selectedRoute.fromChain); - const spokePool = config.getSpokePool(selectedRoute.fromChain, signer); - const tokenInfo = config.getTokenInfoByAddress( - selectedRoute.fromChain, - selectedRoute.fromTokenAddress - ); - const amount = utils.parseUnits("0.000001", tokenInfo.decimals); - const argsForEstimation = { - recipient: await signer.getAddress(), - originToken: tokenInfo.address, - amount, - destinationChain: selectedRoute.toChain, - relayerFeePct: 0, - quoteTimestamp: BigNumber.from(Math.floor(Date.now() / 1000)).sub(60 * 60), - message: "0x", - maxCount: constants.MaxUint256, - }; - const paddedGasEstimation = await getPaddedGasEstimation( - selectedRoute.fromChain, - spokePool, - "deposit", - ...Object.values(argsForEstimation), - { value: selectedRoute.isNative ? amount : 0 } - ); const gasPrice = await provider.getGasPrice(); - return gasPrice.mul(paddedGasEstimation); + const gasMultiplier = gasMultiplierPerChain[selectedRoute.fromChain] || 3; + return gasPrice.mul(gasMultiplier).mul(gasExpenditureDeposit); }