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

feat: allow passing gasPrice to getTransactionCost #3608

Merged
merged 4 commits into from
Jan 23, 2025

Conversation

danielbate
Copy link
Member

@danielbate danielbate commented Jan 22, 2025

Release notes

In this release, we:

  • Allowed passing gasPrice to getTransactionCost functions to reduce possible network calls

Summary

In the Fuel Wallet, a user can set a custom gas limit or tip for a transaction. It then checks if that changes the fee. If it has, it will re-estimate and fund the transaction.

This leads to two gas price fetches. One in the initial fee calculation, and one in the re-estimation.

By allowing the getTransactionCost to take a gasPrice parameter, we can reduce the number of network calls for this flow.

The expected flow could then look like so:

// Some user input that could change the fee
const tip = userInput.tip;
transactionRequest.tip = tip;

// Calculate the new fee, also fetches and returns the gas price
const { maxFee, gasPrice } = await provider.estimateTxGasAndFee({
    transactionRequest,
});

// Check if the fee has increased and if so, re-estimate and re-fund
if (maxFee.gt(transactionRequest.maxFee)) {
    const cost = await wallet.getTransactionCost(proposedTxRequest, {
        estimateTxDependencies: true,
        gasPrice, // passing this means we will not fetch gas price again
    });
    await wallet.fund(proposedTxRequest, { cost });
}

Checklist

  • All changes are covered by tests (or not applicable)
  • All changes are documented (or not applicable)
  • I reviewed the entire PR myself (preferably, on GH UI)
  • I described all Breaking Changes (or there's none)

@danielbate danielbate added the feat Issue is a feature label Jan 22, 2025
@danielbate danielbate self-assigned this Jan 22, 2025
Copy link

vercel bot commented Jan 22, 2025

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

Name Status Preview Comments Updated (UTC)
fuels-template ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 23, 2025 1:02pm
ts-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 23, 2025 1:02pm
ts-docs-api ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 23, 2025 1:02pm

@danielbate danielbate marked this pull request as ready for review January 22, 2025 17:53
Copy link
Contributor

@Torres-ssf Torres-ssf left a comment

Choose a reason for hiding this comment

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

Later, we should ask them why getTransactionCost is being called on their side.

The primary purpose of getTransactionCost is to calculate the script gas used. Changes related to the transaction fee, such as adding more UTXOs or adjusting the tip, do not modify the script gas used. As a result, there’s no need to rerun this method when they already have the gasUsed, which would otherwise trigger at least another dry-run unnecessarily.

@danielbate
Copy link
Member Author

danielbate commented Jan 23, 2025

@Torres-ssf I will make a note for when I'm going to try implement this there, but one issue is that the wallet is not aware of the state of the transaction on load. It doesn't know whether it has the correct gasUsed until simulated.

Still, there may be a smarter way to handle this, I'll have a think.

@danielbate danielbate enabled auto-merge (squash) January 23, 2025 12:34
Copy link
Contributor

Coverage Report:

Lines Branches Functions Statements
77.05%(+0.01%) 70.65%(+0.01%) 75.1%(+0%) 77.05%(+0.01%)
Changed Files:
Ok File (✨=New File) Lines Branches Functions Statements
🔴 packages/account/src/providers/provider.ts 68.46%
(-0.09%)
59%
(+0.54%)
69.89%
(+0%)
68.2%
(-0.08%)

@danielbate danielbate merged commit e5251e2 into master Jan 23, 2025
22 of 24 checks passed
@danielbate danielbate deleted the db/feat/pass-gas-price-get-tx-cost branch January 23, 2025 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants