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

fix: update handleAccountRemoved logic #4322

Merged
merged 15 commits into from
Jun 25, 2024

Conversation

montelaidev
Copy link
Contributor

@montelaidev montelaidev commented May 27, 2024

Explanation

This PR modifies the handleAccountRemoved function to update the selected account to the most recent one, if the account being removed is currently the selected account, all within the same update.

References

Related to MetaMask/metamask-mobile#9749

Changelog

@metamask/accounts-controller

  • FIXED: Updates handleAccountRemoved to update the selected account to the most recent one, if the account being removed is currently the selected account, all within the same step.
  • CHANGED: Refactored getSelectedAccount to handle case when there are no accounts from getAccountExpect

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@montelaidev montelaidev marked this pull request as ready for review May 27, 2024 11:55
@montelaidev montelaidev requested a review from a team as a code owner May 27, 2024 11:56
owencraston
owencraston previously approved these changes May 27, 2024
@owencraston
Copy link
Contributor

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "14.0.0-preview-2008174",
  "@metamask-previews/address-book-controller": "4.0.1-preview-2008174",
  "@metamask-previews/announcement-controller": "6.1.0-preview-2008174",
  "@metamask-previews/approval-controller": "6.0.2-preview-2008174",
  "@metamask-previews/assets-controllers": "29.0.0-preview-2008174",
  "@metamask-previews/base-controller": "5.0.2-preview-2008174",
  "@metamask-previews/build-utils": "2.0.1-preview-2008174",
  "@metamask-previews/chain-controller": "0.0.0-preview-2008174",
  "@metamask-previews/composable-controller": "6.0.1-preview-2008174",
  "@metamask-previews/controller-utils": "9.1.0-preview-2008174",
  "@metamask-previews/ens-controller": "10.0.1-preview-2008174",
  "@metamask-previews/eth-json-rpc-provider": "3.0.2-preview-2008174",
  "@metamask-previews/gas-fee-controller": "15.1.2-preview-2008174",
  "@metamask-previews/json-rpc-engine": "8.0.2-preview-2008174",
  "@metamask-previews/json-rpc-middleware-stream": "7.0.1-preview-2008174",
  "@metamask-previews/keyring-controller": "16.0.0-preview-2008174",
  "@metamask-previews/logging-controller": "3.0.1-preview-2008174",
  "@metamask-previews/message-manager": "8.0.2-preview-2008174",
  "@metamask-previews/name-controller": "6.0.1-preview-2008174",
  "@metamask-previews/network-controller": "18.1.1-preview-2008174",
  "@metamask-previews/notification-controller": "5.0.1-preview-2008174",
  "@metamask-previews/permission-controller": "9.0.2-preview-2008174",
  "@metamask-previews/permission-log-controller": "2.0.1-preview-2008174",
  "@metamask-previews/phishing-controller": "9.0.3-preview-2008174",
  "@metamask-previews/polling-controller": "6.0.2-preview-2008174",
  "@metamask-previews/preferences-controller": "11.0.0-preview-2008174",
  "@metamask-previews/profile-sync-controller": "0.0.0-preview-2008174",
  "@metamask-previews/queued-request-controller": "0.10.0-preview-2008174",
  "@metamask-previews/rate-limit-controller": "5.0.1-preview-2008174",
  "@metamask-previews/selected-network-controller": "13.0.0-preview-2008174",
  "@metamask-previews/signature-controller": "16.0.0-preview-2008174",
  "@metamask-previews/transaction-controller": "29.1.0-preview-2008174",
  "@metamask-previews/user-operation-controller": "10.0.0-preview-2008174"
}

owencraston added a commit to MetaMask/metamask-mobile that referenced this pull request May 28, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

1. What is the reason for the change?
- This error occurs one a fresh install after importing an account from
SRP that has multiple accounts WITH balances (In my case I was testing
an account that had 5 addresses with balances). The reason for this is
because when importing an SRP, each account gets imported one by one
until we find an address that has a zero balance. [Once we find the
address with a zero balance, we remove that account and the account
import is
complete](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L49-L61).
This unveiled an issue in the AccountsController (which is not being
used to display the wallet info) because when a new account is added we
[eagerly set the latest account as the selected
account](https://github.com/MetaMask/core/blob/v123.0.0/packages/accounts-controller/src/AccountsController.ts#L751).
This caused two issues.
- The selected account after an import would be the last account in the
list. This differs from production which defaults to the first account.
- Since the last account in the list was set to selected, when it gets
removed in this step
[here](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L59),
the UI/Controller are still referencing the account that just got
deleted, causing a race condition and resulting in the crash we see in
the video.

3. What is the improvement/solution?
- To solve this bug I am doing one simple thing, not setting the latest
added account to selected. This comes in the form of a patch due to the
urgency of this fix but we will work to find a better more long term
solution in the latest accounts controller release.
- We are already working on a longer term fix
[here](MetaMask/core#4322).
- This does not effect the selected account logic in the UI since the
[front end is already manually setting an account to selected when a new
account is
added](https://github.com/MetaMask/metamask-mobile/blob/e0a4bec1ce82415a3be892e719194ff829beb05f/app/components/Views/AddAccountActions/AddAccountActions.tsx#L44).
This can be seen in my vide below.

## **Related issues**

Fixes: #9749

## **Manual testing steps**

1. Run this branch without any previous instance of metamask installed
3. import a wallet from SRP that has multiple addresses with a balance.
This is essential to truly test this feature.
4. after importing the account, you should be sent to the wallet home
screen
5. wait for things to load because this screen is very very slow.
6. NOTICE that the selected account is always account 1
7. open the account picker and notice that their are multiple addresses
created with the correct balances
8. there should be no crash
9. click on the account picker and add a new account
10. this account should get created and should now become the selected
account
11. force close the app by swiping it away and then reopen
12. upon unlocking the wallet the selected account should be the same
account that was selected before.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**



https://github.com/MetaMask/metamask-mobile/assets/22918444/2396608d-c30e-4421-88e3-e14b21a799e8



### **After**



https://github.com/MetaMask/metamask-mobile/assets/22918444/9b0ec7d2-4d48-4142-b2b8-dd680a392b3a

NOTE: The performance of this home screen is terrible and is not related
to the AccountsController. @Cal-L has [separate incoming fix for
this](#9753).

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
metamaskbot pushed a commit to MetaMask/metamask-mobile that referenced this pull request May 28, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

1. What is the reason for the change?
- This error occurs one a fresh install after importing an account from
SRP that has multiple accounts WITH balances (In my case I was testing
an account that had 5 addresses with balances). The reason for this is
because when importing an SRP, each account gets imported one by one
until we find an address that has a zero balance. [Once we find the
address with a zero balance, we remove that account and the account
import is
complete](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L49-L61).
This unveiled an issue in the AccountsController (which is not being
used to display the wallet info) because when a new account is added we
[eagerly set the latest account as the selected
account](https://github.com/MetaMask/core/blob/v123.0.0/packages/accounts-controller/src/AccountsController.ts#L751).
This caused two issues.
- The selected account after an import would be the last account in the
list. This differs from production which defaults to the first account.
- Since the last account in the list was set to selected, when it gets
removed in this step
[here](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L59),
the UI/Controller are still referencing the account that just got
deleted, causing a race condition and resulting in the crash we see in
the video.

3. What is the improvement/solution?
- To solve this bug I am doing one simple thing, not setting the latest
added account to selected. This comes in the form of a patch due to the
urgency of this fix but we will work to find a better more long term
solution in the latest accounts controller release.
- We are already working on a longer term fix
[here](MetaMask/core#4322).
- This does not effect the selected account logic in the UI since the
[front end is already manually setting an account to selected when a new
account is
added](https://github.com/MetaMask/metamask-mobile/blob/e0a4bec1ce82415a3be892e719194ff829beb05f/app/components/Views/AddAccountActions/AddAccountActions.tsx#L44).
This can be seen in my vide below.

## **Related issues**

Fixes: #9749

## **Manual testing steps**

1. Run this branch without any previous instance of metamask installed
3. import a wallet from SRP that has multiple addresses with a balance.
This is essential to truly test this feature.
4. after importing the account, you should be sent to the wallet home
screen
5. wait for things to load because this screen is very very slow.
6. NOTICE that the selected account is always account 1
7. open the account picker and notice that their are multiple addresses
created with the correct balances
8. there should be no crash
9. click on the account picker and add a new account
10. this account should get created and should now become the selected
account
11. force close the app by swiping it away and then reopen
12. upon unlocking the wallet the selected account should be the same
account that was selected before.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**



https://github.com/MetaMask/metamask-mobile/assets/22918444/2396608d-c30e-4421-88e3-e14b21a799e8



### **After**



https://github.com/MetaMask/metamask-mobile/assets/22918444/9b0ec7d2-4d48-4142-b2b8-dd680a392b3a

NOTE: The performance of this home screen is terrible and is not related
to the AccountsController. @Cal-L has [separate incoming fix for
this](#9753).

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
Andepande pushed a commit to MetaMask/metamask-mobile that referenced this pull request May 28, 2024
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

1. What is the reason for the change?
- This error occurs one a fresh install after importing an account from
SRP that has multiple accounts WITH balances (In my case I was testing
an account that had 5 addresses with balances). The reason for this is
because when importing an SRP, each account gets imported one by one
until we find an address that has a zero balance. [Once we find the
address with a zero balance, we remove that account and the account
import is
complete](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L49-L61).
This unveiled an issue in the AccountsController (which is not being
used to display the wallet info) because when a new account is added we
[eagerly set the latest account as the selected
account](https://github.com/MetaMask/core/blob/v123.0.0/packages/accounts-controller/src/AccountsController.ts#L751).
This caused two issues.
- The selected account after an import would be the last account in the
list. This differs from production which defaults to the first account.
- Since the last account in the list was set to selected, when it gets
removed in this step
[here](https://github.com/MetaMask/metamask-mobile/blob/main/app/util/importAdditionalAccounts.js#L59),
the UI/Controller are still referencing the account that just got
deleted, causing a race condition and resulting in the crash we see in
the video.

3. What is the improvement/solution?
- To solve this bug I am doing one simple thing, not setting the latest
added account to selected. This comes in the form of a patch due to the
urgency of this fix but we will work to find a better more long term
solution in the latest accounts controller release.
- We are already working on a longer term fix
[here](MetaMask/core#4322).
- This does not effect the selected account logic in the UI since the
[front end is already manually setting an account to selected when a new
account is
added](https://github.com/MetaMask/metamask-mobile/blob/e0a4bec1ce82415a3be892e719194ff829beb05f/app/components/Views/AddAccountActions/AddAccountActions.tsx#L44).
This can be seen in my vide below.

## **Related issues**

Fixes: #9749

## **Manual testing steps**

1. Run this branch without any previous instance of metamask installed
3. import a wallet from SRP that has multiple addresses with a balance.
This is essential to truly test this feature.
4. after importing the account, you should be sent to the wallet home
screen
5. wait for things to load because this screen is very very slow.
6. NOTICE that the selected account is always account 1
7. open the account picker and notice that their are multiple addresses
created with the correct balances
8. there should be no crash
9. click on the account picker and add a new account
10. this account should get created and should now become the selected
account
11. force close the app by swiping it away and then reopen
12. upon unlocking the wallet the selected account should be the same
account that was selected before.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**



https://github.com/MetaMask/metamask-mobile/assets/22918444/2396608d-c30e-4421-88e3-e14b21a799e8



### **After**



https://github.com/MetaMask/metamask-mobile/assets/22918444/9b0ec7d2-4d48-4142-b2b8-dd680a392b3a

NOTE: The performance of this home screen is terrible and is not related
to the AccountsController. @Cal-L has [separate incoming fix for
this](#9753).

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] 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](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
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.
@owencraston
Copy link
Contributor

I think that we should still merge this PR since it is a better long term fix for the related issue.

@owencraston owencraston reopened this May 29, 2024
@montelaidev montelaidev force-pushed the fix/set-selectedAccount-and-remove-in-the-same-step branch from 2008174 to c439acc Compare June 4, 2024 07:34
@owencraston
Copy link
Contributor

@metamaskbot publish-preview

Copy link
Contributor

github-actions bot commented Jun 4, 2024

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "16.0.0-preview-c439acc5",
  "@metamask-previews/address-book-controller": "5.0.0-preview-c439acc5",
  "@metamask-previews/announcement-controller": "7.0.0-preview-c439acc5",
  "@metamask-previews/approval-controller": "7.0.0-preview-c439acc5",
  "@metamask-previews/assets-controllers": "32.0.0-preview-c439acc5",
  "@metamask-previews/base-controller": "6.0.0-preview-c439acc5",
  "@metamask-previews/build-utils": "3.0.0-preview-c439acc5",
  "@metamask-previews/chain-controller": "0.0.0-preview-c439acc5",
  "@metamask-previews/composable-controller": "7.0.0-preview-c439acc5",
  "@metamask-previews/controller-utils": "11.0.0-preview-c439acc5",
  "@metamask-previews/ens-controller": "12.0.0-preview-c439acc5",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-c439acc5",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-c439acc5",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-c439acc5",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-c439acc5",
  "@metamask-previews/keyring-controller": "17.0.0-preview-c439acc5",
  "@metamask-previews/logging-controller": "5.0.0-preview-c439acc5",
  "@metamask-previews/message-manager": "10.0.0-preview-c439acc5",
  "@metamask-previews/name-controller": "8.0.0-preview-c439acc5",
  "@metamask-previews/network-controller": "19.0.0-preview-c439acc5",
  "@metamask-previews/notification-controller": "6.0.0-preview-c439acc5",
  "@metamask-previews/permission-controller": "10.0.0-preview-c439acc5",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-c439acc5",
  "@metamask-previews/phishing-controller": "10.0.0-preview-c439acc5",
  "@metamask-previews/polling-controller": "8.0.0-preview-c439acc5",
  "@metamask-previews/preferences-controller": "13.0.0-preview-c439acc5",
  "@metamask-previews/profile-sync-controller": "0.0.0-preview-c439acc5",
  "@metamask-previews/queued-request-controller": "0.12.0-preview-c439acc5",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-c439acc5",
  "@metamask-previews/selected-network-controller": "15.0.0-preview-c439acc5",
  "@metamask-previews/signature-controller": "18.0.0-preview-c439acc5",
  "@metamask-previews/transaction-controller": "32.0.0-preview-c439acc5",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-c439acc5"
}

Copy link
Contributor

@owencraston owencraston left a comment

Choose a reason for hiding this comment

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

I am having issues testing this in mobile so I cannot confirm if this fixes our issues but the code looks good to me.

owencraston
owencraston previously approved these changes Jun 12, 2024
@ccharly
Copy link
Contributor

ccharly commented Jun 18, 2024

@metamaskbot publish-preview

@ccharly
Copy link
Contributor

ccharly commented Jun 18, 2024

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "17.0.0-preview-db28cc44",
  "@metamask-previews/address-book-controller": "5.0.0-preview-db28cc44",
  "@metamask-previews/announcement-controller": "7.0.0-preview-db28cc44",
  "@metamask-previews/approval-controller": "7.0.0-preview-db28cc44",
  "@metamask-previews/assets-controllers": "33.0.0-preview-db28cc44",
  "@metamask-previews/base-controller": "6.0.0-preview-db28cc44",
  "@metamask-previews/build-utils": "3.0.0-preview-db28cc44",
  "@metamask-previews/chain-controller": "0.1.0-preview-db28cc44",
  "@metamask-previews/composable-controller": "7.0.0-preview-db28cc44",
  "@metamask-previews/controller-utils": "11.0.0-preview-db28cc44",
  "@metamask-previews/ens-controller": "12.0.0-preview-db28cc44",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-db28cc44",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-db28cc44",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-db28cc44",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-db28cc44",
  "@metamask-previews/keyring-controller": "17.1.0-preview-db28cc44",
  "@metamask-previews/logging-controller": "5.0.0-preview-db28cc44",
  "@metamask-previews/message-manager": "10.0.0-preview-db28cc44",
  "@metamask-previews/name-controller": "8.0.0-preview-db28cc44",
  "@metamask-previews/network-controller": "19.0.0-preview-db28cc44",
  "@metamask-previews/notification-controller": "6.0.0-preview-db28cc44",
  "@metamask-previews/notification-services-controller": "0.0.0-preview-db28cc44",
  "@metamask-previews/permission-controller": "10.0.0-preview-db28cc44",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-db28cc44",
  "@metamask-previews/phishing-controller": "10.0.0-preview-db28cc44",
  "@metamask-previews/polling-controller": "8.0.0-preview-db28cc44",
  "@metamask-previews/preferences-controller": "13.0.0-preview-db28cc44",
  "@metamask-previews/profile-sync-controller": "0.0.0-preview-db28cc44",
  "@metamask-previews/queued-request-controller": "0.12.0-preview-db28cc44",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-db28cc44",
  "@metamask-previews/selected-network-controller": "15.0.2-preview-db28cc44",
  "@metamask-previews/signature-controller": "18.0.0-preview-db28cc44",
  "@metamask-previews/transaction-controller": "33.0.0-preview-db28cc44",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-db28cc44"
}

this.update((currentState: Draft<AccountsControllerState>) => {
delete currentState.internalAccounts.accounts[accountId];
const updatedState = deepCloneDraft(currentState);
Copy link
Member

Choose a reason for hiding this comment

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

Why would we deep clone the state here? This is an Immer draft, it's perfectly safe to mutate. That's the entire purpose of Immer drafts, to convert mutations into immutable state changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its more to get around the type error. MetaMask/utils#168. I left a comment here to remove it once the error is gone

Copy link
Member

Choose a reason for hiding this comment

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

I am not following. How does deep cloning state get around at type error? I understand that the deepCloneDraft function itself has type assertions that may be needed due to a type error, but deep cloning happens at runtime. We don't need to do anything at runtime to work around type errors that happen at build time. We work around this type error everywhere else using a ts-expect-error comment, when it does get in our way.

Why would we deep clone here at all? Why would anyone ever want to deep clone an Immer draft? It seems like a misunderstanding of what this draft is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, we should use ts-expect-error instead. Its been changed in this commit 2ff7fce

@@ -731,25 +731,25 @@ export class AccountsController extends BaseController<

// handle if the selected account was deleted
if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the selected account is already being updated in this block. So now it's being updated twice redundantly.

Could you elaborate further on what this was intended to accomplish?

Copy link
Member

Choose a reason for hiding this comment

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

I see, it was to ensure that the account was removed in the first update. That makes sense, though surely we can refactor this to avoid making the same state update again redundantly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The onKeyringStateChange has been changed to only update once at the end. 3f27d64

@montelaidev
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "17.0.0-preview-bc4ac8fa",
  "@metamask-previews/address-book-controller": "5.0.0-preview-bc4ac8fa",
  "@metamask-previews/announcement-controller": "7.0.0-preview-bc4ac8fa",
  "@metamask-previews/approval-controller": "7.0.0-preview-bc4ac8fa",
  "@metamask-previews/assets-controllers": "33.0.0-preview-bc4ac8fa",
  "@metamask-previews/base-controller": "6.0.0-preview-bc4ac8fa",
  "@metamask-previews/build-utils": "3.0.0-preview-bc4ac8fa",
  "@metamask-previews/chain-controller": "0.1.0-preview-bc4ac8fa",
  "@metamask-previews/composable-controller": "7.0.0-preview-bc4ac8fa",
  "@metamask-previews/controller-utils": "11.0.0-preview-bc4ac8fa",
  "@metamask-previews/ens-controller": "12.0.0-preview-bc4ac8fa",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-bc4ac8fa",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-bc4ac8fa",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-bc4ac8fa",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-bc4ac8fa",
  "@metamask-previews/keyring-controller": "17.1.0-preview-bc4ac8fa",
  "@metamask-previews/logging-controller": "5.0.0-preview-bc4ac8fa",
  "@metamask-previews/message-manager": "10.0.0-preview-bc4ac8fa",
  "@metamask-previews/name-controller": "8.0.0-preview-bc4ac8fa",
  "@metamask-previews/network-controller": "19.0.0-preview-bc4ac8fa",
  "@metamask-previews/notification-controller": "6.0.0-preview-bc4ac8fa",
  "@metamask-previews/notification-services-controller": "0.0.0-preview-bc4ac8fa",
  "@metamask-previews/permission-controller": "10.0.0-preview-bc4ac8fa",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-bc4ac8fa",
  "@metamask-previews/phishing-controller": "10.0.0-preview-bc4ac8fa",
  "@metamask-previews/polling-controller": "8.0.0-preview-bc4ac8fa",
  "@metamask-previews/preferences-controller": "13.0.0-preview-bc4ac8fa",
  "@metamask-previews/profile-sync-controller": "0.0.0-preview-bc4ac8fa",
  "@metamask-previews/queued-request-controller": "0.12.0-preview-bc4ac8fa",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-bc4ac8fa",
  "@metamask-previews/selected-network-controller": "15.0.2-preview-bc4ac8fa",
  "@metamask-previews/signature-controller": "18.0.0-preview-bc4ac8fa",
  "@metamask-previews/transaction-controller": "33.0.0-preview-bc4ac8fa",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-bc4ac8fa"
}

@montelaidev
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "17.0.0-preview-129e011c",
  "@metamask-previews/address-book-controller": "5.0.0-preview-129e011c",
  "@metamask-previews/announcement-controller": "7.0.0-preview-129e011c",
  "@metamask-previews/approval-controller": "7.0.0-preview-129e011c",
  "@metamask-previews/assets-controllers": "33.0.0-preview-129e011c",
  "@metamask-previews/base-controller": "6.0.0-preview-129e011c",
  "@metamask-previews/build-utils": "3.0.0-preview-129e011c",
  "@metamask-previews/chain-controller": "0.1.0-preview-129e011c",
  "@metamask-previews/composable-controller": "7.0.0-preview-129e011c",
  "@metamask-previews/controller-utils": "11.0.0-preview-129e011c",
  "@metamask-previews/ens-controller": "12.0.0-preview-129e011c",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-129e011c",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-129e011c",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-129e011c",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-129e011c",
  "@metamask-previews/keyring-controller": "17.1.0-preview-129e011c",
  "@metamask-previews/logging-controller": "5.0.0-preview-129e011c",
  "@metamask-previews/message-manager": "10.0.0-preview-129e011c",
  "@metamask-previews/name-controller": "8.0.0-preview-129e011c",
  "@metamask-previews/network-controller": "19.0.0-preview-129e011c",
  "@metamask-previews/notification-controller": "6.0.0-preview-129e011c",
  "@metamask-previews/notification-services-controller": "0.1.0-preview-129e011c",
  "@metamask-previews/permission-controller": "10.0.0-preview-129e011c",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-129e011c",
  "@metamask-previews/phishing-controller": "10.0.0-preview-129e011c",
  "@metamask-previews/polling-controller": "8.0.0-preview-129e011c",
  "@metamask-previews/preferences-controller": "13.0.0-preview-129e011c",
  "@metamask-previews/profile-sync-controller": "0.1.0-preview-129e011c",
  "@metamask-previews/queued-request-controller": "1.0.0-preview-129e011c",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-129e011c",
  "@metamask-previews/selected-network-controller": "15.0.2-preview-129e011c",
  "@metamask-previews/signature-controller": "18.0.0-preview-129e011c",
  "@metamask-previews/transaction-controller": "33.0.0-preview-129e011c",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-129e011c"
}

@montelaidev
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "17.0.0-preview-ed5d0223",
  "@metamask-previews/address-book-controller": "5.0.0-preview-ed5d0223",
  "@metamask-previews/announcement-controller": "7.0.0-preview-ed5d0223",
  "@metamask-previews/approval-controller": "7.0.0-preview-ed5d0223",
  "@metamask-previews/assets-controllers": "33.0.0-preview-ed5d0223",
  "@metamask-previews/base-controller": "6.0.0-preview-ed5d0223",
  "@metamask-previews/build-utils": "3.0.0-preview-ed5d0223",
  "@metamask-previews/chain-controller": "0.1.0-preview-ed5d0223",
  "@metamask-previews/composable-controller": "7.0.0-preview-ed5d0223",
  "@metamask-previews/controller-utils": "11.0.0-preview-ed5d0223",
  "@metamask-previews/ens-controller": "12.0.0-preview-ed5d0223",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-ed5d0223",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-ed5d0223",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-ed5d0223",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-ed5d0223",
  "@metamask-previews/keyring-controller": "17.1.0-preview-ed5d0223",
  "@metamask-previews/logging-controller": "5.0.0-preview-ed5d0223",
  "@metamask-previews/message-manager": "10.0.0-preview-ed5d0223",
  "@metamask-previews/name-controller": "8.0.0-preview-ed5d0223",
  "@metamask-previews/network-controller": "19.0.0-preview-ed5d0223",
  "@metamask-previews/notification-controller": "6.0.0-preview-ed5d0223",
  "@metamask-previews/notification-services-controller": "0.1.0-preview-ed5d0223",
  "@metamask-previews/permission-controller": "10.0.0-preview-ed5d0223",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-ed5d0223",
  "@metamask-previews/phishing-controller": "10.0.0-preview-ed5d0223",
  "@metamask-previews/polling-controller": "8.0.0-preview-ed5d0223",
  "@metamask-previews/preferences-controller": "13.0.0-preview-ed5d0223",
  "@metamask-previews/profile-sync-controller": "0.1.0-preview-ed5d0223",
  "@metamask-previews/queued-request-controller": "1.0.0-preview-ed5d0223",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-ed5d0223",
  "@metamask-previews/selected-network-controller": "15.0.2-preview-ed5d0223",
  "@metamask-previews/signature-controller": "18.0.0-preview-ed5d0223",
  "@metamask-previews/transaction-controller": "33.0.0-preview-ed5d0223",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-ed5d0223"
}

Comment on lines +1869 to +1882
expect(accountsController.getSelectedAccount()).toStrictEqual({
id: '',
address: '',
options: {},
methods: [],
type: EthAccountType.Eoa,
metadata: {
name: '',
keyring: {
type: '',
},
importTime: 0,
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I've seen multiple times already, we should probably export a const EMPTY_ACCOUNT alongside the AccountsController so that we can check against this value (in the tests, but potentially elsewhere too).

Overall, I really feel this is a bit fragile, but this is not the scope of this PR.

I'll try to make a separate PR to address this.

@montelaidev
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/accounts-controller": "17.0.0-preview-1f998a49",
  "@metamask-previews/address-book-controller": "5.0.0-preview-1f998a49",
  "@metamask-previews/announcement-controller": "7.0.0-preview-1f998a49",
  "@metamask-previews/approval-controller": "7.0.0-preview-1f998a49",
  "@metamask-previews/assets-controllers": "33.0.0-preview-1f998a49",
  "@metamask-previews/base-controller": "6.0.0-preview-1f998a49",
  "@metamask-previews/build-utils": "3.0.0-preview-1f998a49",
  "@metamask-previews/chain-controller": "0.1.0-preview-1f998a49",
  "@metamask-previews/composable-controller": "7.0.0-preview-1f998a49",
  "@metamask-previews/controller-utils": "11.0.0-preview-1f998a49",
  "@metamask-previews/ens-controller": "12.0.0-preview-1f998a49",
  "@metamask-previews/eth-json-rpc-provider": "4.0.0-preview-1f998a49",
  "@metamask-previews/gas-fee-controller": "17.0.0-preview-1f998a49",
  "@metamask-previews/json-rpc-engine": "9.0.0-preview-1f998a49",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.0-preview-1f998a49",
  "@metamask-previews/keyring-controller": "17.1.0-preview-1f998a49",
  "@metamask-previews/logging-controller": "5.0.0-preview-1f998a49",
  "@metamask-previews/message-manager": "10.0.0-preview-1f998a49",
  "@metamask-previews/name-controller": "8.0.0-preview-1f998a49",
  "@metamask-previews/network-controller": "19.0.0-preview-1f998a49",
  "@metamask-previews/notification-controller": "6.0.0-preview-1f998a49",
  "@metamask-previews/notification-services-controller": "0.1.0-preview-1f998a49",
  "@metamask-previews/permission-controller": "10.0.0-preview-1f998a49",
  "@metamask-previews/permission-log-controller": "3.0.0-preview-1f998a49",
  "@metamask-previews/phishing-controller": "10.0.0-preview-1f998a49",
  "@metamask-previews/polling-controller": "8.0.0-preview-1f998a49",
  "@metamask-previews/preferences-controller": "13.0.0-preview-1f998a49",
  "@metamask-previews/profile-sync-controller": "0.1.0-preview-1f998a49",
  "@metamask-previews/queued-request-controller": "1.0.0-preview-1f998a49",
  "@metamask-previews/rate-limit-controller": "6.0.0-preview-1f998a49",
  "@metamask-previews/selected-network-controller": "15.0.2-preview-1f998a49",
  "@metamask-previews/signature-controller": "18.0.0-preview-1f998a49",
  "@metamask-previews/transaction-controller": "33.0.0-preview-1f998a49",
  "@metamask-previews/user-operation-controller": "12.0.0-preview-1f998a49"
}

@montelaidev montelaidev merged commit 5761ee7 into main Jun 25, 2024
116 checks passed
@montelaidev montelaidev deleted the fix/set-selectedAccount-and-remove-in-the-same-step branch June 25, 2024 13:47
@montelaidev montelaidev mentioned this pull request Jun 25, 2024
3 tasks
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.

5 participants