@@ -2,6 +2,12 @@ import BN from 'bn.js';
2
2
import { produce } from 'immer' ;
3
3
import { isEqual } from 'lodash' ;
4
4
5
+ import type {
6
+ BalanceUpdate ,
7
+ AccountActivityServiceBalanceUpdatedEvent ,
8
+ AccountActivityServiceStatusChangedEvent
9
+ } from '@metamask/backend-platform' ;
10
+
5
11
import { Web3Provider } from '@ethersproject/providers' ;
6
12
import type {
7
13
AccountsControllerGetSelectedAccountAction ,
@@ -76,26 +82,9 @@ export type TokenBalancesControllerGetStateAction = ControllerGetStateAction<
76
82
TokenBalancesControllerState
77
83
> ;
78
84
79
- export type TokenBalancesControllerUpdateChainPollingConfigsAction = {
80
- type : `TokenBalancesController:updateChainPollingConfigs`;
81
- handler : TokenBalancesController [ 'updateChainPollingConfigs' ] ;
82
- } ;
83
-
84
- export type TokenBalancesControllerGetChainPollingConfigAction = {
85
- type : `TokenBalancesController:getChainPollingConfig`;
86
- handler : TokenBalancesController [ 'getChainPollingConfig' ] ;
87
- } ;
88
-
89
- export type TokenBalancesControllerGetDefaultPollingIntervalAction = {
90
- type : `TokenBalancesController:getDefaultPollingInterval`;
91
- handler : TokenBalancesController [ 'getDefaultPollingInterval' ] ;
92
- } ;
93
85
94
86
export type TokenBalancesControllerActions =
95
- | TokenBalancesControllerGetStateAction
96
- | TokenBalancesControllerUpdateChainPollingConfigsAction
97
- | TokenBalancesControllerGetChainPollingConfigAction
98
- | TokenBalancesControllerGetDefaultPollingIntervalAction ;
87
+ | TokenBalancesControllerGetStateAction ;
99
88
100
89
export type TokenBalancesControllerStateChangeEvent =
101
90
ControllerStateChangeEvent < typeof CONTROLLER , TokenBalancesControllerState > ;
@@ -124,8 +113,8 @@ export type AllowedEvents =
124
113
| PreferencesControllerStateChangeEvent
125
114
| NetworkControllerStateChangeEvent
126
115
| KeyringControllerAccountRemovedEvent
127
- | { type : 'AccountActivityService:balanceUpdated' ; payload : any [ ] }
128
- | { type : 'AccountActivityService:statusChanged' ; payload : [ { chainIds : string [ ] ; status : 'up' | 'down' ; } ] } ;
116
+ | AccountActivityServiceBalanceUpdatedEvent
117
+ | AccountActivityServiceStatusChangedEvent ;
129
118
130
119
export type TokenBalancesControllerMessenger = RestrictedMessenger <
131
120
typeof CONTROLLER ,
@@ -284,21 +273,6 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
284
273
this . #onAccountActivityStatusChanged. bind ( this ) ,
285
274
) ;
286
275
287
- // Register action handlers for polling interval control
288
- this . messagingSystem . registerActionHandler (
289
- `TokenBalancesController:updateChainPollingConfigs` ,
290
- this . updateChainPollingConfigs . bind ( this ) ,
291
- ) ;
292
-
293
- this . messagingSystem . registerActionHandler (
294
- `TokenBalancesController:getChainPollingConfig` ,
295
- this . getChainPollingConfig . bind ( this ) ,
296
- ) ;
297
-
298
- this . messagingSystem . registerActionHandler (
299
- `TokenBalancesController:getDefaultPollingInterval` ,
300
- this . getDefaultPollingInterval . bind ( this ) ,
301
- ) ;
302
276
}
303
277
304
278
#chainIdsWithTokens( ) : ChainIdHex [ ] {
@@ -809,6 +783,7 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
809
783
/**
810
784
* Handle real-time balance updates from AccountActivityService
811
785
* Processes balance updates and updates the token balance state
786
+ * If any balance update has an error, triggers fallback polling for the chain
812
787
*/
813
788
readonly #onAccountActivityBalanceUpdate = async ( {
814
789
address,
@@ -817,10 +792,7 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
817
792
} : {
818
793
address : string ;
819
794
chain : string ;
820
- updates : Array < {
821
- asset : { type : string ; unit : string } ;
822
- postBalance : { amount : string } ;
823
- } > ;
795
+ updates : BalanceUpdate [ ] ;
824
796
} ) => {
825
797
826
798
const chainParts = chain . split ( ':' ) ;
@@ -843,6 +815,13 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
843
815
for ( const update of updates ) {
844
816
const { asset, postBalance } = update ;
845
817
818
+ // Check if there's an error in the balance update
819
+ if ( postBalance . error ) {
820
+ console . warn ( `Balance update has error for ${ asset . unit } :` , postBalance . error , '- will trigger fallback polling' ) ;
821
+ shouldPoll = true ;
822
+ break ;
823
+ }
824
+
846
825
// Extract token address from asset type (e.g., "eip155:1/erc20:0x...")
847
826
let tokenAddress : string ;
848
827
@@ -877,7 +856,7 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
877
856
shouldPoll = true ;
878
857
}
879
858
880
- // Single fallback polling call for any error (validation or processing)
859
+ // Single fallback polling call for any error (balance errors, validation errors, or processing errors )
881
860
if ( shouldPoll ) {
882
861
try {
883
862
console . log ( `Triggering fallback poll for chain ${ chain } (${ chainId } )` ) ;
@@ -980,13 +959,6 @@ export class TokenBalancesController extends StaticIntervalPollingController<{
980
959
this . #statusChangeDebouncer. timer = null ;
981
960
}
982
961
983
- // Unregister action handlers
984
- this . messagingSystem . unregisterActionHandler (
985
- `TokenBalancesController:updateChainPollingConfigs` ,
986
- ) ;
987
- this . messagingSystem . unregisterActionHandler (
988
- `TokenBalancesController:getChainPollingConfig` ,
989
- ) ;
990
962
991
963
super . destroy ( ) ;
992
964
}
0 commit comments