Skip to content

Adds transactionBatches into transaction controller state #5793

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

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

vinistevam
Copy link
Contributor

@vinistevam vinistevam commented May 13, 2025

Explanation

This PR introduces support for batch transactions in the Transaction Controller by adding a new ApprovalType and extending the state to handle TransactionBatches. These changes enable enhanced metadata management for sequential batch flows, including UI updates for gas estimation and future automation capabilities.

Changes

Controller Utils

  • Added a new ApprovalType to the enum: TransactionBatch, to support batch transactions.

Transaction Controller

  • Introduced a new state property: transactionBatches, to store metadata for transaction batches.
  • Added a private method: #addBatchMetadata, responsible for populating batch-specific metadata.
  • Created a new type: TransactionBatchMeta, to manage metadata for transaction batches.
  • Added the #trimBatchTransactionsForState method to handle TransactionBatchMeta.
  • Update unit tests

Rationale

The introduction of TransactionBatchMeta allows for a clean separation of metadata for batch transactions, which conceptually differ from individual transactions. This ensures:

  • Accurate metadata at the time of batch creation, as TransactionMeta for individual transactions may not yet exist.
  • Future support for advanced features such as transaction simulations and dynamic gas fee updates for batches, enabling better client-side functionality and automation.

These changes lay the groundwork for improved handling of batch transactions and pave the way for future enhancements.

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 vinistevam force-pushed the feat/add-batch-transaction-approval-type branch 2 times, most recently from c3f8a6b to 49e9a89 Compare May 14, 2025 16:13
@vinistevam vinistevam marked this pull request as ready for review May 14, 2025 16:14
@vinistevam vinistevam requested review from a team as code owners May 14, 2025 16:14
@pedronfigueiredo pedronfigueiredo force-pushed the feat/add-batch-transaction-approval-type branch from 31069c4 to e62592d Compare May 14, 2025 16:24
@@ -169,6 +169,7 @@ export enum ApprovalType {
WalletConnect = 'wallet_connect',
WalletRequestPermissions = 'wallet_requestPermissions',
WatchAsset = 'wallet_watchAsset',
TransactionBatch = 'transaction_batch',
Copy link
Member

Choose a reason for hiding this comment

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

Minor, alphabetical?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -2596,6 +2606,15 @@ export class TransactionController extends BaseController<
});
}

#addBatchMetadata(transactionBatchMeta: TransactionBatchMeta) {
Copy link
Member

Choose a reason for hiding this comment

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

In the interest of modularity, could we provide the update callback directly so the state is encapsulated within the batch module?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed #addBatchMetadata as we agreed to not persist the transactionBatches for now.

* @param transactions - The transactions to be applied to the state.
* @returns The trimmed list of transactions.
*/
#trimBatchTransactionsForState(
Copy link
Member

Choose a reason for hiding this comment

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

Can this also go in batch.ts file for the sake for modularity?

Plus can we simplify this to just add at the end, and delete from the start once we exceed some limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed #trimBatchTransactionsForState, becasue we are not saving the transactionBatches yet

@@ -490,6 +490,39 @@ export type TransactionMeta = {
};
};

export type TransactionBatchMeta = {
Copy link
Member

Choose a reason for hiding this comment

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

JSDoc for type itself?

id: string;

/**
* Data for any nested transactions.
Copy link
Member

Choose a reason for hiding this comment

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

Technically not "nested" as that is EIP-7702 only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

const batchId = generateBatchId();

if (requireApproval) {
const txBatchMeta: TransactionBatchMeta = {
Copy link
Member

Choose a reason for hiding this comment

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

Assuming this will grow, should we create a newBatchMetadata function to just return the object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created newBatchMetadata function.

async function requestApproval(
txBatchMeta: TransactionBatchMeta,
messenger: TransactionControllerMessenger,
{ shouldShowRequest }: { shouldShowRequest: boolean },
Copy link
Member

Choose a reason for hiding this comment

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

As we only have one usage, should we remove this property and hardcode below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

id,
origin: origin || ORIGIN_METAMASK,
requestData,
expectsResult: true,
Copy link
Member

Choose a reason for hiding this comment

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

If we're setting expectsResult to true, it means the client will wait until we call one of the result callbacks.

Meaning we need to invoke success or error returned from this function once the batch is complete or failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, should I save the resultCallbacks and call success when tx is confirmed or error when tx fails, similar to the pattern of collectHook?
Applied fixes.

@@ -490,6 +490,39 @@ export type TransactionMeta = {
};
};

export type TransactionBatchMeta = {
Copy link
Member

Choose a reason for hiding this comment

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

Should we export this in index.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, type exported.

const batchId = generateBatchId();

if (requireApproval) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we also check useHook is true at this stage as EIP-7702 uses the standard confirmation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@vinistevam vinistevam changed the title Adds and populate transactionBatches into transaction controller state Adds transactionBatches into transaction controller state May 19, 2025
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.

3 participants