-
-
Notifications
You must be signed in to change notification settings - Fork 206
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 active transactions every 3000 ms #5189
base: main
Are you sure you want to change the base?
Conversation
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
f159aea
to
ea69888
Compare
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/TransactionController.test.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
packages/transaction-controller/src/helpers/ResimulateHelper.ts
Outdated
Show resolved
Hide resolved
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
@metamaskbot publish-preview |
Preview builds have been published. See these instructions for more information about preview builds. Expand for full list of packages and versions.
|
onTransactionsUpdate: (listener) => { | ||
this.messagingSystem.subscribe( | ||
'TransactionController:stateChange', | ||
(_newState, patches: Patch[]) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're using the patches which is an argument specific to the stateChange
event, but there is actually a third selector
argument on subscribe
itself that lets you filter the event data and only fires the event if that value changes.
https://github.com/MetaMask/core/blob/main/packages/base-controller/src/Messenger.ts#L341
|
||
#start(transactionMeta: TransactionMeta) { | ||
const { id: transactionId } = transactionMeta; | ||
if (!transactionMeta.isActive || this.#timeoutIds.has(transactionId)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, if we only call #start
in one place after checking active, is there a need to check it again here?
}; | ||
|
||
// Start the first execution | ||
const timeoutId = setTimeout(listener, RESIMULATE_INTERVAL_MS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, could we have a #queueUpdate
method to remove the duplication in creating the timeouts?
|
||
#stop(transactionMeta: TransactionMeta) { | ||
const { id: transactionId } = transactionMeta; | ||
if (transactionMeta.isActive || !this.#timeoutIds.has(transactionId)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, also here, is the isActive
validation needed if already checked before calling?
(tx) => tx.id === transactionId, | ||
); | ||
|
||
if (transactionMeta && transactionMeta.isActive) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, could this be transactionMeta?.isActive
?
this.#stop({ | ||
id: transactionId, | ||
isActive: false, | ||
} as unknown as TransactionMeta); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we remove the cast and just have a transactionId
argument on #stop
?
.finally(() => { | ||
// Schedule the next execution | ||
if (this.#timeoutIds.has(transactionId)) { | ||
const timeoutId = setTimeout(listener, RESIMULATE_INTERVAL_MS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's very rare we'll have more than one transaction, but worth confirming this means the updates for different transactions won't be in sync.
This should be fine as the simulation isn't a batch request.
} | ||
|
||
this.#removeListener(transactionId); | ||
log(`Stopped resimulating transaction ${transactionId} every 3 seconds`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, we're still hardcoding the 3
.
Explanation
This PR adds
ResimulateHelper
, which focuses ontransactionMeta.isActive
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
isActive
property ontransactionMeta
isActive
property is expected to set by client.isActive
istrue
.setTransactionActive
function to update theisActive
property ontransactionMeta
.Checklist