Skip to content
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

chore: Accounts/PreferencesController state syncing #9564

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

owencraston
Copy link
Contributor

@owencraston owencraston commented May 6, 2024

Description

  1. What is the reason for the change?
    The methods Engine.setAccountLabel and Engine.setSelectedAddress were added in in these two ( one and two) Prs. These mehtods are used to keep the shared state between the AccountsController and PreferenfesController in sync. However is not a good method since it is prone to error and requires a lot of context. This pr removes these methods in favour of automated state sync listeners.
  2. What is the improvement/solution?
    Listen for state change on the PreferencesController and automatically update the relevant state inside the accounts controller.
    These listeners will ensure that the selected address and the account names stay in sync.

Related issues

Fixes: https://github.com/MetaMask/accounts-planning/issues/445

Manual testing steps

  1. create a wallet
  2. open the wallet view
  3. add a new account
  4. the state logs should indicate this new selected address
  5. the state between the preferences controller and the accounts controller should match
  6. change the account's name
  7. the account name should also get updated in the accounts controller.

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

github-actions bot commented May 6, 2024

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@owencraston owencraston force-pushed the accounts-and-preferences-state-sync branch 2 times, most recently from 855f642 to fc37b9f Compare May 9, 2024 21:27
@owencraston owencraston force-pushed the accounts-and-preferences-state-sync branch from daa0d74 to dd5a939 Compare May 17, 2024 20:19
@owencraston owencraston force-pushed the accounts-and-preferences-state-sync branch from dd5a939 to b95cef4 Compare May 20, 2024 07:26
@owencraston owencraston marked this pull request as ready for review May 20, 2024 07:26
@owencraston owencraston requested a review from a team as a code owner May 20, 2024 07:26
} from '@metamask/preferences-controller';

export function syncSelectedAddress(
preferencesState: PreferencesState,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just passing the selectedAddress here? Since you don't really need the entire state of the PreferencesController

Comment on lines +1376 to +1386
this.controllerMessenger.subscribe(
'PreferencesController:stateChange',
(preferencesState: PreferencesState) => {
syncSelectedAddress(
preferencesState,
() => accountsController,
() => preferencesController,
);
syncAccountName(preferencesState, () => accountsController);
},
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have a subscription to the other direction too?

}

export function syncAccountName(
preferencesState: PreferencesState,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here since we're just using identities

const preferencesController = getPreferencesController();
const selectedAddressFromPreferences = preferencesState.selectedAddress;
const { selectedAccount: currentAccountId, accounts: internalAccounts } =
accountsController.state.internalAccounts;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to get internalAccounts without accessing the state directly?

Comment on lines +45 to +47
for (const accountId in accountsController.state.internalAccounts.accounts) {
const account =
accountsController.state.internalAccounts.accounts[accountId];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment here about getting internalAccounts without the need to access the state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants