-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: use pre-computed gas usage for deposit max amounts #873
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,8 @@ | ||
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, | ||
} from "utils"; | ||
import { getPaddedGasEstimation } from "utils/transactions"; | ||
|
||
const config = getConfig(); | ||
import { Route, max, getProvider, gasExpenditureDeposit } from "utils"; | ||
|
||
export function useMaxBalance(selectedRoute: Route) { | ||
const { balance } = useBalanceBySymbol( | ||
|
@@ -35,14 +26,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 +37,13 @@ export function useMaxBalance(selectedRoute: Route) { | |
}, | ||
{ | ||
enabled: Boolean(account && balance && signer), | ||
retry: true, | ||
} | ||
); | ||
} | ||
|
||
async function estimateGasCostsForDeposit( | ||
selectedRoute: Route, | ||
signer: providers.JsonRpcSigner | ||
) { | ||
async function estimateGasCostsForDeposit(selectedRoute: Route) { | ||
pxrl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
return gasPrice.mul(gasExpenditureDeposit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it makes sense to me. Changed here f20b1af |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooc, how regularly does this run? I wonder if we want some smoothing logic so that the estimated gas cost doesn't bounce around and cause the deposit amount to flip-flop between being valid and invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default is 5 minutes... But we can easily customize the refresh interval for this value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow - I'd expected something like 2 - 10 seconds. 5 minutes is actually really long; we might want to revisit that separately, because it might lead to issues if gas becomes volatile.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah wait, this actually updates more frequently I noticed. Because it is dependent on the
useBalance
query which queries the balance on every new block.