Skip to content

Adds estimate gas limit for transaction batch using simulation API #5852

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

Merged
merged 38 commits into from
Jun 3, 2025

Conversation

vinistevam
Copy link
Contributor

@vinistevam vinistevam commented May 23, 2025

Explanation

This PR adds simulateGasBatch support to the Transaction Controller, enabling sequential gas estimation for nestedTransactions within a batch. When both useHook and requireApproval are true, the method simulates each transaction, calculates the total and individual gas limits, and stores the result in state for further use in the UI or execution logic.

Changes

Transaction Controller

  • Added simulateGasBatch to estimate gas for each transaction in a batch sequentially.
  • Integrated simulateGasBatch into the addTransactionBatch flow, triggered when useHook and requireApproval are true.
  • Extended state to store total gas limits from simulation.
  • Updated unit tests to cover batch and gas simulation logic.

References

Changelog

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

vinistevam and others added 27 commits May 13, 2025 17:18
…:MetaMask/core into feat/add-batch-transaction-approval-type
@vinistevam vinistevam force-pushed the feat/add-estimate-gas-batch-transaction branch from 8154080 to 097124d Compare May 23, 2025 10:31
@vinistevam vinistevam changed the title WIP: Adds estimate gas limit for transaction batch using simulation API Adds estimate gas limit for transaction batch using simulation API May 23, 2025
@vinistevam vinistevam marked this pull request as ready for review May 23, 2025 10:48
@vinistevam vinistevam requested review from a team as code owners May 23, 2025 10:48
Base automatically changed from feat/add-batch-transaction-approval-type to main May 27, 2025 08:25
Comment on lines 223 to 228
const response = await simulateTransactions(chainId, {
transactions: transactions.map((transaction) => ({
...transaction.params,
from,
})),
});
Copy link
Member

Choose a reason for hiding this comment

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

What happens if simulateTransactions throws SimulationError, do we not want to handle that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If anything other than success happens, it will throw, and I could also handle it and log the simulation error.

Copy link
Member

Choose a reason for hiding this comment

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

The best we could do is throw a cleaner error such as Cannot estimate transaction batch total gas as simulation failed?

matthewwalsh0
matthewwalsh0 previously approved these changes May 28, 2025
);

return {
transactions: transactionsWithGasLimit,
Copy link
Member

Choose a reason for hiding this comment

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

What I meant was can we simplify this function and only return the gasLimit as that's all we need currently?

Comment on lines 223 to 228
const response = await simulateTransactions(chainId, {
transactions: transactions.map((transaction) => ({
...transaction.params,
from,
})),
});
Copy link
Member

Choose a reason for hiding this comment

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

The best we could do is throw a cleaner error such as Cannot estimate transaction batch total gas as simulation failed?

update: UpdateStateCallback;
}): Promise<TransactionBatchMeta> {
if (!isSimulationEnabled()) {
throw new Error('Simulation is not enabled');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new Error('Simulation is not enabled');
throw new Error('Cannot create transaction batch as simulation not supported');

Copy link
Member

Choose a reason for hiding this comment

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

This is something we'll need to consider in any client features that use a transaction batch, as we currently require simulation.

In future we could skip simulation if gas limits are provided on each transaction, or we could just estimate as we go normally and display no gas fee.

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `gas` property to `TransactionBatchMeta`, populated using simulation API ([#5852](https://github.com/MetaMask/core/pull/5852))
Copy link
Member

Choose a reason for hiding this comment

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

My fault, but could we also move the entry on line 33 to unreleased please, missed it in my previous PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that entry is already in the version 56.3.0, otherwise, I can change it in my next PR.

@vinistevam vinistevam merged commit 255ca86 into main Jun 3, 2025
210 checks passed
@vinistevam vinistevam deleted the feat/add-estimate-gas-batch-transaction branch June 3, 2025 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants