Skip to content

Commit f3c5b99

Browse files
authored
fix: don't poll for swap status (MetaMask#5831)
## Explanation After a swap is submitted the status controller keeps calling getTxStatus. This is unnecessary and can cause clients to keep polling until the user clears their activity log <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- THIS SECTION IS NO LONGER NEEDED. The process for updating changelogs has changed. Please consult the "Updating changelogs" section of the Contributing doc for more. --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent dbe5097 commit f3c5b99

3 files changed

Lines changed: 68 additions & 9 deletions

File tree

packages/bridge-status-controller/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Bump `@metamask/bridge-controller` peer dependency to `^25.0.1` ([#5811](https://github.com/MetaMask/core/pull/5811))
1717
- Bump `@metamask/controller-utils` to `^11.9.0` ([#5812](https://github.com/MetaMask/core/pull/5812))
1818

19+
### Fixed
20+
21+
- Don't start or restart getTxStatus polling if transaction is a swap ([#5831](https://github.com/MetaMask/core/pull/5831))
22+
1923
## [21.0.0]
2024

2125
### Changed

packages/bridge-status-controller/src/bridge-status-controller.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,38 @@ const MockTxHistory = {
398398
completionTime: undefined,
399399
},
400400
}),
401+
getPendingSwap: ({
402+
txMetaId = 'swapTxMetaId1',
403+
srcTxHash = '0xsrcTxHash1',
404+
account = '0xaccount1',
405+
srcChainId = 42161,
406+
destChainId = 42161,
407+
} = {}): Record<string, BridgeHistoryItem> => ({
408+
[txMetaId]: {
409+
txMetaId,
410+
quote: getMockQuote({ srcChainId, destChainId }),
411+
startTime: 1729964825189,
412+
estimatedProcessingTimeInSeconds: 15,
413+
slippagePercentage: 0,
414+
account,
415+
status: MockStatusResponse.getPending({
416+
srcTxHash,
417+
srcChainId,
418+
}),
419+
targetContractAddress: '0x23981fC34e69eeDFE2BD9a0a9fCb0719Fe09DbFC',
420+
initialDestAssetBalance: undefined,
421+
pricingData: {
422+
amountSent: '1.234',
423+
amountSentInUsd: undefined,
424+
quotedGasInUsd: undefined,
425+
quotedReturnInUsd: undefined,
426+
},
427+
approvalTxId: undefined,
428+
isStxEnabled: false,
429+
hasApprovalTx: false,
430+
completionTime: undefined,
431+
},
432+
}),
401433
getComplete: ({
402434
txMetaId = 'bridgeTxMetaId1',
403435
srcTxHash = '0xsrcTxHash1',
@@ -606,6 +638,7 @@ describe('BridgeStatusController', () => {
606638
txHistory: {
607639
...MockTxHistory.getPending(),
608640
...MockTxHistory.getUnknown(),
641+
...MockTxHistory.getPendingSwap(),
609642
},
610643
},
611644
clientId: BridgeClientId.EXTENSION,

packages/bridge-status-controller/src/bridge-status-controller.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
228228
const srcTxMetaId = historyItem.txMetaId;
229229
const pollingToken = this.#pollingTokensByTxMetaId[srcTxMetaId];
230230
return !pollingToken;
231+
})
232+
// Swap txs don't need to have their statuses polled
233+
.filter((historyItem) => {
234+
const isBridgeTx = isCrossChain(
235+
historyItem.quote.srcChainId,
236+
historyItem.quote.destChainId,
237+
);
238+
return isBridgeTx;
231239
});
232240

233241
incompleteHistoryItems.forEach((historyItem) => {
@@ -241,12 +249,7 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
241249
});
242250
};
243251

244-
/**
245-
* Starts polling for the bridge tx status
246-
*
247-
* @param startPollingForBridgeTxStatusArgs - The args to start polling for the bridge tx status
248-
*/
249-
startPollingForBridgeTxStatus = (
252+
readonly #addTxToHistory = (
250253
startPollingForBridgeTxStatusArgs: StartPollingForBridgeTxStatusArgsSerialized,
251254
) => {
252255
const {
@@ -296,10 +299,29 @@ export class BridgeStatusController extends StaticIntervalPollingController<Brid
296299
// Use the txMeta.id as the key so we can reference the txMeta in TransactionController
297300
state.txHistory[bridgeTxMeta.id] = txHistoryItem;
298301
});
302+
};
299303

300-
this.#pollingTokensByTxMetaId[bridgeTxMeta.id] = this.startPolling({
301-
bridgeTxMetaId: bridgeTxMeta.id,
302-
});
304+
/**
305+
* Starts polling for the bridge tx status
306+
*
307+
* @param txHistoryMeta - The parameters for creating the history item
308+
*/
309+
startPollingForBridgeTxStatus = (
310+
txHistoryMeta: StartPollingForBridgeTxStatusArgsSerialized,
311+
) => {
312+
const { quoteResponse, bridgeTxMeta } = txHistoryMeta;
313+
314+
this.#addTxToHistory(txHistoryMeta);
315+
316+
const isBridgeTx = isCrossChain(
317+
quoteResponse.quote.srcChainId,
318+
quoteResponse.quote.destChainId,
319+
);
320+
if (isBridgeTx) {
321+
this.#pollingTokensByTxMetaId[bridgeTxMeta.id] = this.startPolling({
322+
bridgeTxMetaId: bridgeTxMeta.id,
323+
});
324+
}
303325
};
304326

305327
// This will be called after you call this.startPolling()

0 commit comments

Comments
 (0)