Skip to content

Commit b2d81c4

Browse files
Kriys94salimtb
andauthored
feat(TokenBalanceController): Dynamic polling interval per chain (#6357)
## Explanation ### Current State The `TokenBalancesController` currently uses a single global polling interval for all chains, with the implementation of *WebSocket-based balance updates** we need **dynamic interval management** per chain (for websocket-supported chains and for non-supported chains): - **WebSocket-supported chains** should use minimal polling when connected (WebSocket provides real-time updates) - **WebSocket-disconnected chains** need frequent polling as fallback - **WebSocket connection status changes dynamically**, requiring runtime interval adjustments - The current single-interval approach cannot adapt to changing WebSocket connectivity states ### Solution This PR introduces **dynamic per-chain polling intervals** that integrate seamlessly with the **WebSocket service's connectivity management**: 1. **Dynamic Interval Control**: WebSocket service can adjust polling intervals in real-time via messenger actions based on connection status 2. **Connectivity-Aware Polling**: Connected chains use long intervals (WebSocket handles updates), disconnected chains use short intervals (polling fallback) 3. **Interval Grouping Strategy**: Chains with the same polling interval are batched together, preserving AccountsAPI efficiency 4. **Messenger Integration**: `updateChainPollingConfigs` action enables WebSocket service to control intervals dynamically ### ⚠️ **IMPORTANT: This is a Non-Breaking Change** **All existing code continues to work exactly as it does today**: - Existing `TokenBalancesController` instantiation without `chainPollingIntervals` works unchanged - Default polling behavior remains identical (single global interval) - All existing APIs (`startPolling`, `stopPolling`, `updateBalances`, etc.) work as before - No changes required in MetaMask Extension or any consuming code The new functionality is **purely additive** and **opt-in**. ### WebSocket Service Integration Flow ```typescript // WebSocket service manages intervals based on connection status: // When WebSocket connects, it calls: this.controllerMessenger.call('TokenBalancesController:updateChainPollingConfigs', { '0x1': { interval: 300000 } // 5min - WebSocket handles real-time updates }); // When WebSocket disconnects from a chain: this.controllerMessenger.call('TokenBalancesController:updateChainPollingConfigs', { '0x1': { interval: 30000 } // 30s - fallback to frequent polling }); ``` ### Technical Implementation - **Real-time Adaptation**: WebSocket service dynamically adjusts intervals based on connection health - **Efficient Batching**: Chains are automatically regrouped by interval when WebSocket service updates configs - **Seamless Fallback**: Disconnected chains immediately switch to frequent polling without manual intervention - **Connection Recovery**: When WebSocket reconnects, polling automatically reduces to backup frequency - **Backward Compatibility**: All existing functionality preserved - new features only activate when explicitly configured ### Example Runtime Behavior ```typescript // Current behavior (unchanged): new TokenBalancesController({ messenger, interval: 30000 }) // → All chains poll every 30s (exactly as today) // New opt-in behavior: new TokenBalancesController({ messenger, interval: 30000, // Default fallback chainPollingIntervals: { '0x1': { interval: 300000 } } // Ethereum: 5min }) // → Ethereum polls every 5min, other chains every 30s ``` ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - Added comprehensive test coverage for per-chain polling intervals and dynamic updates - Added tests for interval grouping, runtime configuration changes, and batching behavior - **All existing tests continue to pass unchanged** - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - Added JSDoc comments for messenger actions and configuration options - [x] I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary - **Note**: **NO BREAKING CHANGES** - this is a purely additive enhancement - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes - **Note**: **NO BREAKING CHANGES** - existing code works unchanged, WebSocket service integration uses new opt-in messenger actions --------- Co-authored-by: Salim TOUBAL <[email protected]>
1 parent e4f8a47 commit b2d81c4

File tree

3 files changed

+1659
-3
lines changed

3 files changed

+1659
-3
lines changed

packages/assets-controllers/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Changed
1111

12+
- Enhance `TokenBalancesController` with internal dynamic polling per chain support, enabling configurable polling intervals for different networks with automatic interval grouping for improved performance (transparent to existing API) ([#6357](https://github.com/MetaMask/core/pull/6357))
1213
- Bump `@metamask/base-controller` from `^8.2.0` to `^8.3.0` ([#6465](https://github.com/MetaMask/core/pull/6465))
1314

1415
## [74.3.2]

0 commit comments

Comments
 (0)