-
-
Notifications
You must be signed in to change notification settings - Fork 278
fix(assets-controllers): refetch snap balances after vault unlock when fetch was skipped while locked #8579
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
Open
khanti42
wants to merge
4
commits into
main
Choose a base branch
from
fix/multichain-balances-refetch-after-unlock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,10 @@ | |
| CaipAssetType, | ||
| AccountBalancesUpdatedEventPayload, | ||
| } from '@metamask/keyring-api'; | ||
| import type { KeyringControllerGetStateAction } from '@metamask/keyring-controller'; | ||
| import type { | ||
| KeyringControllerGetStateAction, | ||
| KeyringControllerStateChangeEvent, | ||
| } from '@metamask/keyring-controller'; | ||
| import type { InternalAccount } from '@metamask/keyring-internal-api'; | ||
| import { KeyringClient } from '@metamask/keyring-snap-client'; | ||
| import type { Messenger } from '@metamask/messenger'; | ||
|
|
@@ -105,7 +108,8 @@ | |
| | AccountsControllerAccountAddedEvent | ||
| | AccountsControllerAccountRemovedEvent | ||
| | AccountsControllerAccountBalancesUpdatesEvent | ||
| | MultichainAssetsControllerAccountAssetListUpdatedEvent; | ||
| | MultichainAssetsControllerAccountAssetListUpdatedEvent | ||
| | KeyringControllerStateChangeEvent; | ||
| /** | ||
| * Messenger type for the MultichainBalancesController. | ||
| */ | ||
|
|
@@ -190,6 +194,31 @@ | |
| await this.#handleOnAccountAssetListUpdated(updatedAccountAssets); | ||
| }, | ||
| ); | ||
|
|
||
| // When the keyring transitions from locked → unlocked, fetch balances for | ||
| // any non-EVM account that had its balance fetch skipped while locked. | ||
| let previouslyUnlocked = this.messenger.call( | ||
|
Check failure on line 200 in packages/assets-controllers/src/MultichainBalancesController/MultichainBalancesController.ts
|
||
| 'KeyringController:getState', | ||
| ).isUnlocked; | ||
| this.messenger.subscribe( | ||
| 'KeyringController:stateChange', | ||
| ({ isUnlocked }) => { | ||
| const justUnlocked = isUnlocked && !previouslyUnlocked; | ||
| previouslyUnlocked = isUnlocked; | ||
| if (!justUnlocked) { | ||
| return; | ||
| } | ||
| for (const account of this.#listAccounts()) { | ||
| const hasBalance = | ||
| this.state.balances[account.id] && | ||
| Object.keys(this.state.balances[account.id]).length > 0; | ||
| if (!hasBalance) { | ||
| // eslint-disable-next-line no-void | ||
| void this.updateBalance(account.id); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -392,7 +421,7 @@ | |
| this.update((state: Draft<MultichainBalancesControllerState>) => { | ||
| Object.entries(balanceUpdate.balances).forEach( | ||
| ([accountId, assetBalances]) => { | ||
| if (accountId in state.balances) { | ||
| Object.assign(state.balances[accountId], assetBalances); | ||
| } | ||
| }, | ||
|
|
@@ -406,7 +435,7 @@ | |
| * @param accountId - The account ID being removed. | ||
| */ | ||
| async #handleOnAccountRemoved(accountId: string): Promise<void> { | ||
| if (accountId in this.state.balances) { | ||
| this.update((state: Draft<MultichainBalancesControllerState>) => { | ||
| delete state.balances[accountId]; | ||
| }); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unlock handler skips accounts with existing partial balances
Medium Severity
The
hasBalancecheck only triggersupdateBalancefor accounts with a completely empty balance map. Sincebalancesstate is persisted (persist: true), an account can have existing balance entries from a prior session while new assets get added viaaccountAssetListUpdatedwhile locked. On unlock, the account already has entries sohasBalanceistrue, and the newly-added assets never get their balances fetched — the exact scenario this PR aims to fix, but only partially addressed.Reviewed by Cursor Bugbot for commit 9b21bf2. Configure here.