Skip to content
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

Merged
merged 2 commits into from
Oct 17, 2023

Conversation

dohaki
Copy link
Contributor

@dohaki dohaki commented Oct 16, 2023

This PR makes setting the max. bridgable amount for native token deposits more accurate and reliable by:

  • Removing the usage of a fallback value gas costs and using infinite retry
  • Using a pre-computed value for gas expenditure instead of querying via an additional RPC call

@vercel
Copy link

vercel bot commented Oct 16, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
frontend-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2023 1:12pm
goerli-frontend-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 16, 2023 1:12pm

src/views/Bridge/hooks/useMaxBalance.ts Show resolved Hide resolved
const gasPrice = await provider.getGasPrice();
return gasPrice.mul(paddedGasEstimation);
return gasPrice.mul(gasExpenditureDeposit);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getGasPrice() can be unreliable because it's mostly ignorant of the gas pricing strategies of each chain. It's probably fine as an approximation, but should we apply a multiplier? I think something really coarse would be fine - i.e. 3x or so. Given that the deposit cost is so low it will then reserve only a small amount of the native token, and that should be fine by users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it makes sense to me. Changed here f20b1af

console.error(err);
return max(balance.sub(fallbackEstimatedGasCosts), 0);
});
await estimateGasCostsForDeposit(selectedRoute).then(
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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.

- comment on empty message
- use multiplier
Copy link
Member

@nicholaspai nicholaspai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does seem like it'll make the FE faster but doesn't it make estimations less accurate (in the PR you say it makes it more accurate) because we use a hardcoded gas cost?

@pxrl
Copy link
Contributor

pxrl commented Oct 16, 2023

This does seem like it'll make the FE faster but doesn't it make estimations less accurate (in the PR you say it makes it more accurate) because we use a hardcoded gas cost?

Yeah, it would lead to a bit less accuracy vs. a successful gas estimation. The thinking behind this PR is that there have been several reports of falling back to the default gas buffer of 0.01 ETH, mostly on deposits originating on zkSync. Secondarily, incurring the cost of an estimation on every single deposit is not really ideal - it's both faster and cheaper to use a fixed cost. As far as I understand, the user's wallet should anyway simulate this against their own RPCs before they sign it.

@dohaki dohaki merged commit 0b0783d into master Oct 17, 2023
6 checks passed
@dohaki dohaki deleted the do-not-use-fallback-gas-costs branch October 17, 2023 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants