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: Re-simulate focused transactions every 3000 ms #5189

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

Conversation

OGPoyraz
Copy link
Member

@OGPoyraz OGPoyraz commented Jan 23, 2025

Explanation

This PR adds ResimulateHelper, which focuses on transactionMeta.isFocused property and re-simulates transaction depending on that value.

In order to capsulate re-simulation logic, this PR also relocates other utility functions under the new created ResimulationHelper.ts file.

References

Fixes: https://github.com/MetaMask/MetaMask-planning/issues/3922
Extension PR: MetaMask/metamask-extension#29878

Changelog

@metamask/transaction-controller

  • ADDED: Adds ability of re-simulating transaction depending on the isFocused property on transactionMeta
    • isFocused property is expected to set by client.
    • Re-simulation of transactions will occur every 3 seconds if isFocused is true.
  • Adds updateTransactionFocus function to update the isFocused property on transactionMeta.

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 highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.2-preview-9df90b5",
  "@metamask-previews/address-book-controller": "6.0.2-preview-9df90b5",
  "@metamask-previews/announcement-controller": "7.0.2-preview-9df90b5",
  "@metamask-previews/approval-controller": "7.1.2-preview-9df90b5",
  "@metamask-previews/assets-controllers": "46.0.1-preview-9df90b5",
  "@metamask-previews/base-controller": "7.1.1-preview-9df90b5",
  "@metamask-previews/build-utils": "3.0.2-preview-9df90b5",
  "@metamask-previews/composable-controller": "10.0.0-preview-9df90b5",
  "@metamask-previews/controller-utils": "11.4.5-preview-9df90b5",
  "@metamask-previews/ens-controller": "15.0.1-preview-9df90b5",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-9df90b5",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-9df90b5",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-9df90b5",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-9df90b5",
  "@metamask-previews/keyring-controller": "19.0.4-preview-9df90b5",
  "@metamask-previews/logging-controller": "6.0.3-preview-9df90b5",
  "@metamask-previews/message-manager": "12.0.0-preview-9df90b5",
  "@metamask-previews/multichain": "2.1.0-preview-9df90b5",
  "@metamask-previews/multichain-transactions-controller": "0.0.1-preview-9df90b5",
  "@metamask-previews/name-controller": "8.0.2-preview-9df90b5",
  "@metamask-previews/network-controller": "22.1.1-preview-9df90b5",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-9df90b5",
  "@metamask-previews/permission-controller": "11.0.5-preview-9df90b5",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-9df90b5",
  "@metamask-previews/phishing-controller": "12.3.1-preview-9df90b5",
  "@metamask-previews/polling-controller": "12.0.2-preview-9df90b5",
  "@metamask-previews/preferences-controller": "15.0.1-preview-9df90b5",
  "@metamask-previews/profile-sync-controller": "4.1.1-preview-9df90b5",
  "@metamask-previews/queued-request-controller": "9.0.0-preview-9df90b5",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-9df90b5",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-9df90b5",
  "@metamask-previews/selected-network-controller": "21.0.0-preview-9df90b5",
  "@metamask-previews/signature-controller": "23.2.0-preview-9df90b5",
  "@metamask-previews/token-search-discovery-controller": "1.0.0-preview-9df90b5",
  "@metamask-previews/transaction-controller": "43.0.0-preview-9df90b5",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-9df90b5"
}

@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.2-preview-4dd7520a",
  "@metamask-previews/address-book-controller": "6.0.2-preview-4dd7520a",
  "@metamask-previews/announcement-controller": "7.0.2-preview-4dd7520a",
  "@metamask-previews/approval-controller": "7.1.2-preview-4dd7520a",
  "@metamask-previews/assets-controllers": "46.0.1-preview-4dd7520a",
  "@metamask-previews/base-controller": "7.1.1-preview-4dd7520a",
  "@metamask-previews/build-utils": "3.0.2-preview-4dd7520a",
  "@metamask-previews/composable-controller": "10.0.0-preview-4dd7520a",
  "@metamask-previews/controller-utils": "11.4.5-preview-4dd7520a",
  "@metamask-previews/ens-controller": "15.0.1-preview-4dd7520a",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-4dd7520a",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-4dd7520a",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-4dd7520a",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-4dd7520a",
  "@metamask-previews/keyring-controller": "19.0.4-preview-4dd7520a",
  "@metamask-previews/logging-controller": "6.0.3-preview-4dd7520a",
  "@metamask-previews/message-manager": "12.0.0-preview-4dd7520a",
  "@metamask-previews/multichain": "2.1.0-preview-4dd7520a",
  "@metamask-previews/multichain-transactions-controller": "0.0.1-preview-4dd7520a",
  "@metamask-previews/name-controller": "8.0.2-preview-4dd7520a",
  "@metamask-previews/network-controller": "22.1.1-preview-4dd7520a",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-4dd7520a",
  "@metamask-previews/permission-controller": "11.0.5-preview-4dd7520a",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-4dd7520a",
  "@metamask-previews/phishing-controller": "12.3.1-preview-4dd7520a",
  "@metamask-previews/polling-controller": "12.0.2-preview-4dd7520a",
  "@metamask-previews/preferences-controller": "15.0.1-preview-4dd7520a",
  "@metamask-previews/profile-sync-controller": "4.1.1-preview-4dd7520a",
  "@metamask-previews/queued-request-controller": "9.0.0-preview-4dd7520a",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-4dd7520a",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-4dd7520a",
  "@metamask-previews/selected-network-controller": "21.0.0-preview-4dd7520a",
  "@metamask-previews/signature-controller": "23.2.0-preview-4dd7520a",
  "@metamask-previews/token-search-discovery-controller": "1.0.0-preview-4dd7520a",
  "@metamask-previews/transaction-controller": "43.0.0-preview-4dd7520a",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-4dd7520a"
}

@OGPoyraz OGPoyraz marked this pull request as ready for review January 24, 2025 12:47
@OGPoyraz OGPoyraz requested review from a team as code owners January 24, 2025 12:47
jpuri
jpuri previously approved these changes Jan 24, 2025
@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.2-preview-f3d5e317",
  "@metamask-previews/address-book-controller": "6.0.2-preview-f3d5e317",
  "@metamask-previews/announcement-controller": "7.0.2-preview-f3d5e317",
  "@metamask-previews/approval-controller": "7.1.2-preview-f3d5e317",
  "@metamask-previews/assets-controllers": "46.0.1-preview-f3d5e317",
  "@metamask-previews/base-controller": "7.1.1-preview-f3d5e317",
  "@metamask-previews/build-utils": "3.0.2-preview-f3d5e317",
  "@metamask-previews/composable-controller": "10.0.0-preview-f3d5e317",
  "@metamask-previews/controller-utils": "11.4.5-preview-f3d5e317",
  "@metamask-previews/ens-controller": "15.0.1-preview-f3d5e317",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-f3d5e317",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-f3d5e317",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-f3d5e317",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-f3d5e317",
  "@metamask-previews/keyring-controller": "19.0.4-preview-f3d5e317",
  "@metamask-previews/logging-controller": "6.0.3-preview-f3d5e317",
  "@metamask-previews/message-manager": "12.0.0-preview-f3d5e317",
  "@metamask-previews/multichain": "2.1.0-preview-f3d5e317",
  "@metamask-previews/multichain-transactions-controller": "0.0.1-preview-f3d5e317",
  "@metamask-previews/name-controller": "8.0.2-preview-f3d5e317",
  "@metamask-previews/network-controller": "22.1.1-preview-f3d5e317",
  "@metamask-previews/notification-services-controller": "0.16.0-preview-f3d5e317",
  "@metamask-previews/permission-controller": "11.0.5-preview-f3d5e317",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-f3d5e317",
  "@metamask-previews/phishing-controller": "12.3.1-preview-f3d5e317",
  "@metamask-previews/polling-controller": "12.0.2-preview-f3d5e317",
  "@metamask-previews/preferences-controller": "15.0.1-preview-f3d5e317",
  "@metamask-previews/profile-sync-controller": "4.1.1-preview-f3d5e317",
  "@metamask-previews/queued-request-controller": "9.0.0-preview-f3d5e317",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-f3d5e317",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-f3d5e317",
  "@metamask-previews/selected-network-controller": "21.0.0-preview-f3d5e317",
  "@metamask-previews/signature-controller": "23.2.0-preview-f3d5e317",
  "@metamask-previews/token-search-discovery-controller": "1.0.0-preview-f3d5e317",
  "@metamask-previews/transaction-controller": "43.0.0-preview-f3d5e317",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-f3d5e317"
}

@OGPoyraz OGPoyraz force-pushed the 3922-re-simulate-transactions-for-every-new-block branch from f159aea to ea69888 Compare January 29, 2025 12:57
@OGPoyraz
Copy link
Member Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "21.0.2-preview-bd04b248",
  "@metamask-previews/address-book-controller": "6.0.2-preview-bd04b248",
  "@metamask-previews/announcement-controller": "7.0.2-preview-bd04b248",
  "@metamask-previews/approval-controller": "7.1.2-preview-bd04b248",
  "@metamask-previews/assets-controllers": "46.0.1-preview-bd04b248",
  "@metamask-previews/base-controller": "7.1.1-preview-bd04b248",
  "@metamask-previews/build-utils": "3.0.2-preview-bd04b248",
  "@metamask-previews/composable-controller": "10.0.0-preview-bd04b248",
  "@metamask-previews/controller-utils": "11.4.5-preview-bd04b248",
  "@metamask-previews/ens-controller": "15.0.1-preview-bd04b248",
  "@metamask-previews/eth-json-rpc-provider": "4.1.7-preview-bd04b248",
  "@metamask-previews/gas-fee-controller": "22.0.2-preview-bd04b248",
  "@metamask-previews/json-rpc-engine": "10.0.2-preview-bd04b248",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.6-preview-bd04b248",
  "@metamask-previews/keyring-controller": "19.0.4-preview-bd04b248",
  "@metamask-previews/logging-controller": "6.0.3-preview-bd04b248",
  "@metamask-previews/message-manager": "12.0.0-preview-bd04b248",
  "@metamask-previews/multichain": "2.1.0-preview-bd04b248",
  "@metamask-previews/multichain-transactions-controller": "0.0.1-preview-bd04b248",
  "@metamask-previews/name-controller": "8.0.2-preview-bd04b248",
  "@metamask-previews/network-controller": "22.1.1-preview-bd04b248",
  "@metamask-previews/notification-services-controller": "0.17.0-preview-bd04b248",
  "@metamask-previews/permission-controller": "11.0.5-preview-bd04b248",
  "@metamask-previews/permission-log-controller": "3.0.2-preview-bd04b248",
  "@metamask-previews/phishing-controller": "12.3.1-preview-bd04b248",
  "@metamask-previews/polling-controller": "12.0.2-preview-bd04b248",
  "@metamask-previews/preferences-controller": "15.0.1-preview-bd04b248",
  "@metamask-previews/profile-sync-controller": "4.1.1-preview-bd04b248",
  "@metamask-previews/queued-request-controller": "9.0.0-preview-bd04b248",
  "@metamask-previews/rate-limit-controller": "6.0.2-preview-bd04b248",
  "@metamask-previews/remote-feature-flag-controller": "1.3.0-preview-bd04b248",
  "@metamask-previews/selected-network-controller": "21.0.0-preview-bd04b248",
  "@metamask-previews/signature-controller": "23.2.0-preview-bd04b248",
  "@metamask-previews/token-search-discovery-controller": "1.0.0-preview-bd04b248",
  "@metamask-previews/transaction-controller": "43.0.0-preview-bd04b248",
  "@metamask-previews/user-operation-controller": "22.0.0-preview-bd04b248"
}

@OGPoyraz OGPoyraz changed the title feat: Resimulate transactions for every new block feat: Re-simulate focused transactions every 3000 ms Jan 29, 2025
@@ -1857,6 +1871,29 @@ export class TransactionController extends BaseController<
return this.getTransaction(txId);
}

/**
* Update the isFocus state of a transaction.
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs a new empty line after this to satisfy lint:

Suggested change
* Update the isFocus state of a transaction.
* Update the isFocus state of a transaction.

}
};

const intervalId = setInterval(listener, RESIMULATE_INTERVAL_MS);
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is producing a lint warning because setInterval does not await the promise. Perhaps that is intentional and so we need to disable the rule?

Suggested change
const intervalId = setInterval(listener, RESIMULATE_INTERVAL_MS);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
const intervalId = setInterval(listener, RESIMULATE_INTERVAL_MS);

Comment on lines +32 to +44
updateSimulationData: (transactionMeta: TransactionMeta) => void;
};

export class ResimulateHelper {
// Map of transactionId <=> isActive
readonly #activeResimulations: Map<string, boolean> = new Map();

// Map of transactionId <=> intervalId
readonly #intervalIds: Map<string, NodeJS.Timeout> = new Map();

readonly #getTransactions: () => TransactionMeta[];

readonly #updateSimulationData: (transactionMeta: TransactionMeta) => void;
Copy link
Contributor

Choose a reason for hiding this comment

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

Does updateSimulationData need to expect a function that returns a Promise?

Suggested change
updateSimulationData: (transactionMeta: TransactionMeta) => void;
};
export class ResimulateHelper {
// Map of transactionId <=> isActive
readonly #activeResimulations: Map<string, boolean> = new Map();
// Map of transactionId <=> intervalId
readonly #intervalIds: Map<string, NodeJS.Timeout> = new Map();
readonly #getTransactions: () => TransactionMeta[];
readonly #updateSimulationData: (transactionMeta: TransactionMeta) => void;
updateSimulationData: (transactionMeta: TransactionMeta) => Promise<void>;
};
export class ResimulateHelper {
// Map of transactionId <=> isActive
readonly #activeResimulations: Map<string, boolean> = new Map();
// Map of transactionId <=> intervalId
readonly #intervalIds: Map<string, NodeJS.Timeout> = new Map();
readonly #getTransactions: () => TransactionMeta[];
readonly #updateSimulationData: (transactionMeta: TransactionMeta) => Promise<void>;

@@ -86,7 +86,7 @@ import {
getTransactionLayer1GasFee,
updateTransactionLayer1GasFee,
} from './utils/layer1-gas-fee-flow';
import { shouldResimulate } from './utils/resimulate';
import { shouldResimulate } from './helpers/ResimulateHelper';
Copy link
Contributor

@mcmire mcmire Jan 29, 2025

Choose a reason for hiding this comment

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

In order to keep the list of imports sorted and satisfy lint, this line should be moved up to go below the following line:

import { PendingTransactionTracker } from './helpers/PendingTransactionTracker';

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