-
-
Notifications
You must be signed in to change notification settings - Fork 252
feat(TokenBalancesController): Integrate Websocket updates in TokenBalancesController #6784
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
Conversation
695f8e8
to
b06186b
Compare
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
All alerts resolved. Learn more about Socket for GitHub. This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. Ignoring alerts on:
|
9e3af3f
to
54419f6
Compare
a83a25c
to
bb5bd02
Compare
This PR integrates the TokenBalancesController with the @metamask/core-backend package to enable real-time balance updates via WebSocket connections. Key Changes: - Add real-time balance updates via WebSocket integration with AccountActivityService - Add support for dynamic polling based on WebSocket connection status - Add TokenDetectionController:addDetectedTokensViaWs action handler - Add @metamask/core-backend as a dependency and peer dependency - Extract parseAssetType into token-balances-utils for better code organization - Update documentation and tests Breaking Changes: - TokenBalancesController messenger must now allow AccountActivityService events - TokenBalancesController messenger must now allow TokenDetectionController:addDetectedTokensViaWs action - Change TokenBalancesController default polling interval to 30 seconds (was 180 seconds)
bb5bd02
to
d3a8b3b
Compare
packages/assets-controllers/src/TokenDetectionController.test.ts
Outdated
Show resolved
Hide resolved
@SocketSecurity ignore npm/@metamask/[email protected] This package is accepted to allow WS usage for these asset controllers.
|
); | ||
|
||
// postBalance.amount is in hex format (raw units) | ||
const balanceHex = postBalance.amount as Hex; |
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.
Bug: Invalid Hex Assertion in Balance Update
The postBalance.amount
value is type-asserted as Hex
without validating its existence or format. If the AccountActivityService
provides a balance update where postBalance.amount
is missing or not a valid hex string, this could lead to unexpected behavior when the value is used downstream.
@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.
|
Tested the app and working successfully MetaMask/metamask-extension#36782 |
App tested in mobile too MetaMask/metamask-mobile#21050 |
This PR integrates the
TokenBalancesController
with the new@metamask/core-backend
package to enable real-time balance updates via WebSocket connections. This provides users with instant balance updates without relying solely on periodic polling.Context & Dependencies
@metamask/core-backend
package withBackendWebSocketService
andAccountActivityService
.Current State & Problem
Currently, the
TokenBalancesController
relies exclusively on periodic REST API polling to detect balance changes. This approach has several limitations:Solution
This PR enhances
TokenBalancesController
to:AccountActivityService
is availableKey Implementation Details
Integration with AccountActivityService:
AccountActivityService:balanceUpdated
events via messengerConnection State Coordination:
BackendWebSocketService:connectionStateChanged
eventsReferences
Checklist
@metamask/assets-controllers
Note
Integrates TokenBalancesController with AccountActivityService for real-time balance updates, adds status-driven polling (with debounce/jitter) and WS-based token import, updates docs/tests, and introduces core-backend dependency with BREAKING messenger and interval changes.
AccountActivityService:balanceUpdated
andstatusChanged
for real-time updates and status-driven polling.30s
(down) and5m
when WebSocket is up.AccountTracker
, imports untracked tokens viaTokenDetectionController:addDetectedTokensViaWs
.caipChainIdToHex
,parseAssetType
), handlesallIgnoredTokens
, and cleans up timers on destroy.addDetectedTokensViaWs
to add WS-detected tokens from cache; minor cleanups.AccountActivityService:balanceUpdated
,AccountActivityService:statusChanged
, andTokenDetectionController:addDetectedTokensViaWs
.30s
(was180s
).docs/real-time-balance-updates-flow.md
and updates README.@metamask/core-backend
as dependency and peerDependency; updates tsconfig references and yarn.lock.Written by Cursor Bugbot for commit 5dac77e. This will update automatically on new commits. Configure here.