From 5a24cdfa8bf50021ff592f5a84f5f26313146de7 Mon Sep 17 00:00:00 2001 From: Daniel <80175477+dan437@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:14:40 +0100 Subject: [PATCH] feat: Use a dynamic interval value for smart transaction status polling (#12978) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** We want to use a dynamic interval value for smart transaction status polling, which is returned by backend. That way we can easily change it if needed and use the most optimal value. ## **Related issues** Fixes: ## **Manual testing steps** 1. Make sure smart transactions are enabled in Advanced Settings 2. Be on Ethereum Mainnet 3. Submit a smart transaction 4. Check that a network request for checking smart transaction status happens every second ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- .../smart-publish-hook.test.ts | 42 ++++++++++++++++++- .../smart-transactions/smart-publish-hook.ts | 7 ++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/util/smart-transactions/smart-publish-hook.test.ts b/app/util/smart-transactions/smart-publish-hook.test.ts index 3953ad17ee5..a247bece910 100644 --- a/app/util/smart-transactions/smart-publish-hook.test.ts +++ b/app/util/smart-transactions/smart-publish-hook.test.ts @@ -11,7 +11,10 @@ import { WalletDevice, } from '@metamask/transaction-controller'; import SmartTransactionsController from '@metamask/smart-transactions-controller'; -import { type SmartTransaction, ClientId } from '@metamask/smart-transactions-controller/dist/types'; +import { + type SmartTransaction, + ClientId, +} from '@metamask/smart-transactions-controller/dist/types'; import { AllowedActions, @@ -113,11 +116,15 @@ type WithRequestOptions = { type WithRequestCallback = ({ request, controllerMessenger, + getFeesSpy, + submitSignedTransactionsSpy, + smartTransactionsController, }: { request: SubmitSmartTransactionRequestMocked; controllerMessenger: SubmitSmartTransactionRequestMocked['controllerMessenger']; getFeesSpy: jest.SpyInstance; submitSignedTransactionsSpy: jest.SpyInstance; + smartTransactionsController: SmartTransactionsController; }) => ReturnValue; type WithRequestArgs = @@ -194,6 +201,7 @@ function withRequest( expectedDeadline: 45, maxDeadline: 150, mobileReturnTxHashAsap: false, + batchStatusPollingInterval: 1000, }, mobile_active: true, extension_active: true, @@ -208,6 +216,7 @@ function withRequest( request, getFeesSpy, submitSignedTransactionsSpy, + smartTransactionsController, }); } @@ -392,7 +401,7 @@ describe('submitSmartTransactionHook', () => { const result = await submitSmartTransactionHook(request); expect(result).toEqual({ transactionHash }); - const { txParams, chainId } = request.transactionMeta; + const { txParams, chainId } = request.transactionMeta; expect( request.transactionController.approveTransactionsWithSameNonce, @@ -657,4 +666,33 @@ describe('submitSmartTransactionHook', () => { ); }); }); + it('sets the status refresh interval if provided in feature flags', async () => { + withRequest(async ({ request, smartTransactionsController }) => { + const setStatusRefreshIntervalSpy = jest.spyOn( + smartTransactionsController, + 'setStatusRefreshInterval', + ); + + request.featureFlags.smartTransactions.batchStatusPollingInterval = 2000; + + await submitSmartTransactionHook(request); + + expect(setStatusRefreshIntervalSpy).toHaveBeenCalledWith(2000); + }); + }); + + it('does not set the status refresh interval if not provided in feature flags', async () => { + withRequest(async ({ request, smartTransactionsController }) => { + const setStatusRefreshIntervalSpy = jest.spyOn( + smartTransactionsController, + 'setStatusRefreshInterval', + ); + + request.featureFlags.smartTransactions.batchStatusPollingInterval = 0; + + await submitSmartTransactionHook(request); + + expect(setStatusRefreshIntervalSpy).not.toHaveBeenCalled(); + }); + }); }); diff --git a/app/util/smart-transactions/smart-publish-hook.ts b/app/util/smart-transactions/smart-publish-hook.ts index 16ecfa2bdf9..df837618412 100644 --- a/app/util/smart-transactions/smart-publish-hook.ts +++ b/app/util/smart-transactions/smart-publish-hook.ts @@ -53,6 +53,7 @@ export interface SubmitSmartTransactionRequest { expectedDeadline: number; maxDeadline: number; mobileReturnTxHashAsap: boolean; + batchStatusPollingInterval: number; } | Record; }; @@ -75,6 +76,7 @@ class SmartTransactionHook { expectedDeadline?: number; maxDeadline?: number; mobileReturnTxHashAsap?: boolean; + batchStatusPollingInterval?: number; }; }; #shouldUseSmartTransaction: boolean; @@ -186,6 +188,11 @@ class SmartTransactionHook { return useRegularTransactionSubmit; } + const batchStatusPollingInterval = this.#featureFlags?.smartTransactions?.batchStatusPollingInterval; + if (batchStatusPollingInterval) { + this.#smartTransactionsController.setStatusRefreshInterval(batchStatusPollingInterval); + } + const submitTransactionResponse = await this.#signAndSubmitTransactions({ getFeesResponse, });