@@ -5,13 +5,15 @@ import type {
5
5
AccountGroupType ,
6
6
AccountSelector ,
7
7
MultichainAccountWalletId ,
8
+ MultichainAccountGroup ,
9
+ Bip44Account ,
8
10
} from '@metamask/account-api' ;
9
11
import type { MultichainAccountWalletStatus } from '@metamask/account-api' ;
10
12
import { type AccountId } from '@metamask/accounts-controller' ;
11
13
import type { StateMetadata } from '@metamask/base-controller' ;
12
14
import { BaseController } from '@metamask/base-controller' ;
13
15
import type { TraceCallback } from '@metamask/controller-utils' ;
14
- import { isEvmAccountType } from '@metamask/keyring-api' ;
16
+ import { isEvmAccountType , KeyringAccount } from '@metamask/keyring-api' ;
15
17
import type { InternalAccount } from '@metamask/keyring-internal-api' ;
16
18
17
19
import type { BackupAndSyncEmitAnalyticsEventParams } from './backup-and-sync/analytics' ;
@@ -223,6 +225,20 @@ export class AccountTreeController extends BaseController<
223
225
} ,
224
226
) ;
225
227
228
+ this . messagingSystem . subscribe (
229
+ 'MultichainAccountService:multichainAccountGroupCreated' ,
230
+ ( group ) => {
231
+ this . #handleMultichainAccountGroupCreatedOrUpdated( group ) ;
232
+ } ,
233
+ ) ;
234
+
235
+ this . messagingSystem . subscribe (
236
+ 'MultichainAccountService:multichainAccountGroupUpdated' ,
237
+ ( group ) => {
238
+ this . #handleMultichainAccountGroupCreatedOrUpdated( group ) ;
239
+ } ,
240
+ ) ;
241
+
226
242
this . #registerMessageHandlers( ) ;
227
243
}
228
244
@@ -616,6 +632,25 @@ export class AccountTreeController extends BaseController<
616
632
}
617
633
}
618
634
635
+ /**
636
+ * Handles multichain account group created/updated event from
637
+ * the MultichainAccountService.
638
+ *
639
+ * @param multichainAccountGroup - Multichain account group being that got created or updated.
640
+ */
641
+ #handleMultichainAccountGroupCreatedOrUpdated(
642
+ multichainAccountGroup : MultichainAccountGroup <
643
+ Bip44Account < KeyringAccount >
644
+ > ,
645
+ ) : void {
646
+ // Trigger atomic sync for wallet and group (wallet will be synced only if it does
647
+ // not exist yet)
648
+ this . #backupAndSyncService. enqueueSingleWalletAndGroupSync (
649
+ multichainAccountGroup . wallet . id ,
650
+ multichainAccountGroup . id ,
651
+ ) ;
652
+ }
653
+
619
654
/**
620
655
* Helper method to prune a group if it holds no accounts and additionally
621
656
* prune the wallet if it holds no groups. This action should take place
@@ -679,11 +714,6 @@ export class AccountTreeController extends BaseController<
679
714
// the union tag `result.wallet.type`.
680
715
} as AccountWalletObject ;
681
716
wallet = wallets [ walletId ] ;
682
-
683
- // Trigger atomic sync for new wallet (only for entropy wallets)
684
- if ( wallet . type === AccountWalletType . Entropy ) {
685
- this . #backupAndSyncService. enqueueSingleWalletSync ( walletId ) ;
686
- }
687
717
}
688
718
689
719
const groupId = result . group . id ;
@@ -705,11 +735,6 @@ export class AccountTreeController extends BaseController<
705
735
706
736
// Map group ID to its containing wallet ID for efficient direct access
707
737
this . #groupIdToWalletId. set ( groupId , walletId ) ;
708
-
709
- // Trigger atomic sync for new group (only for entropy wallets)
710
- if ( wallet . type === AccountWalletType . Entropy ) {
711
- this . #backupAndSyncService. enqueueSingleGroupSync ( groupId ) ;
712
- }
713
738
} else {
714
739
group . accounts . push ( account . id ) ;
715
740
}
0 commit comments