Skip to content

Commit fac35b8

Browse files
feat: account syncing various updates (#28541)
## **Description** This PR adds various account syncing house keeping improvements in order to be ready for re-enablement in a subsequent PR. The number of files changed by this PR is large, but none affects a user facing feature, since account syncing is disabled in production. - Bump `@metamask/profile-sync-controller` to version `1.0.2` - Add two new state keys linked to `UserStorageController`, `isAccountSyncingReadyToBeDispatched` and `hasAccountSyncingSyncedAtLeastOnce` - Wait for `_addAccountsWithBalance` to finish adding accounts after onboarding, then set `isAccountSyncingReadyToBeDispatched` to `true` - Use `USER_STORAGE_FEATURE_NAMES` exported constant from `@metamask/profile-sync-controller` to define user storage paths everywhere (no more magic strings) - Add batch delete delete support for E2E util `UserStorageMockttpController` - Update all account sync E2E tests in order to wait for account sync to have successfully been completed once before going on with the rest of the instructions. ⚠️ Please note that this PR does **NOT** re-enable account syncing in production. This will be done in a subsequent PR. ## **Related issues** ## **Manual testing steps** 1. Import your SRP 2. Add accounts, rename accounts 3. Uninstall & reinstall 4. Import the same SRP 5. Verify that previous updates made in step 2 are there ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [x] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [x] 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. --------- Co-authored-by: MetaMask Bot <[email protected]>
1 parent 220e932 commit fac35b8

28 files changed

+582
-326
lines changed

app/scripts/constants/sentry-state.ts

+2
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ export const SENTRY_BACKGROUND_STATE = {
389389
UserStorageController: {
390390
isProfileSyncingEnabled: true,
391391
isProfileSyncingUpdateLoading: false,
392+
hasAccountSyncingSyncedAtLeastOnce: false,
393+
isAccountSyncingReadyToBeDispatched: false,
392394
},
393395
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
394396
...MMI_SENTRY_BACKGROUND_STATE,

app/scripts/metamask-controller.js

+53-34
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,16 @@ export default class MetamaskController extends EventEmitter {
15981598
},
15991599
});
16001600
},
1601+
onAccountSyncErroneousSituation: (profileId, situationMessage) => {
1602+
this.metaMetricsController.trackEvent({
1603+
category: MetaMetricsEventCategory.ProfileSyncing,
1604+
event: MetaMetricsEventName.AccountsSyncErroneousSituation,
1605+
properties: {
1606+
profile_id: profileId,
1607+
situation_message: situationMessage,
1608+
},
1609+
});
1610+
},
16011611
},
16021612
},
16031613
env: {
@@ -1768,10 +1778,11 @@ export default class MetamaskController extends EventEmitter {
17681778
if (!prevCompletedOnboarding && currCompletedOnboarding) {
17691779
const { address } = this.accountsController.getSelectedAccount();
17701780

1771-
this._addAccountsWithBalance();
1781+
await this._addAccountsWithBalance();
17721782

17731783
this.postOnboardingInitialization();
17741784
this.triggerNetworkrequests();
1785+
17751786
// execute once the token detection on the post-onboarding
17761787
await this.tokenDetectionController.detectTokens({
17771788
selectedAddress: address,
@@ -4441,43 +4452,51 @@ export default class MetamaskController extends EventEmitter {
44414452
}
44424453

44434454
async _addAccountsWithBalance() {
4444-
// Scan accounts until we find an empty one
4445-
const chainId = getCurrentChainId({
4446-
metamask: this.networkController.state,
4447-
});
4448-
const ethQuery = new EthQuery(this.provider);
4449-
const accounts = await this.keyringController.getAccounts();
4450-
let address = accounts[accounts.length - 1];
4451-
4452-
for (let count = accounts.length; ; count++) {
4453-
const balance = await this.getBalance(address, ethQuery);
4454-
4455-
if (balance === '0x0') {
4456-
// This account has no balance, so check for tokens
4457-
await this.tokenDetectionController.detectTokens({
4458-
chainIds: [chainId],
4459-
selectedAddress: address,
4460-
});
4455+
try {
4456+
// Scan accounts until we find an empty one
4457+
const chainId = getCurrentChainId({
4458+
metamask: this.networkController.state,
4459+
});
4460+
const ethQuery = new EthQuery(this.provider);
4461+
const accounts = await this.keyringController.getAccounts();
4462+
let address = accounts[accounts.length - 1];
4463+
4464+
for (let count = accounts.length; ; count++) {
4465+
const balance = await this.getBalance(address, ethQuery);
44614466

4462-
const tokens =
4463-
this.tokensController.state.allTokens?.[chainId]?.[address];
4464-
const detectedTokens =
4465-
this.tokensController.state.allDetectedTokens?.[chainId]?.[address];
4466-
4467-
if (
4468-
(tokens?.length ?? 0) === 0 &&
4469-
(detectedTokens?.length ?? 0) === 0
4470-
) {
4471-
// This account has no balance or tokens
4472-
if (count !== 1) {
4473-
await this.removeAccount(address);
4467+
if (balance === '0x0') {
4468+
// This account has no balance, so check for tokens
4469+
await this.tokenDetectionController.detectTokens({
4470+
chainIds: [chainId],
4471+
selectedAddress: address,
4472+
});
4473+
4474+
const tokens =
4475+
this.tokensController.state.allTokens?.[chainId]?.[address];
4476+
const detectedTokens =
4477+
this.tokensController.state.allDetectedTokens?.[chainId]?.[address];
4478+
4479+
if (
4480+
(tokens?.length ?? 0) === 0 &&
4481+
(detectedTokens?.length ?? 0) === 0
4482+
) {
4483+
// This account has no balance or tokens
4484+
if (count !== 1) {
4485+
await this.removeAccount(address);
4486+
}
4487+
break;
44744488
}
4475-
break;
44764489
}
4477-
}
44784490

4479-
// This account has assets, so check the next one
4480-
address = await this.keyringController.addNewAccount(count);
4491+
// This account has assets, so check the next one
4492+
address = await this.keyringController.addNewAccount(count);
4493+
}
4494+
} catch (e) {
4495+
log.warn(`Failed to add accounts with balance. Error: ${e}`);
4496+
} finally {
4497+
await this.userStorageController.setIsAccountSyncingReadyToBeDispatched(
4498+
true,
4499+
);
44814500
}
44824501
}
44834502

lavamoat/browserify/beta/policy.json

+2-30
Original file line numberDiff line numberDiff line change
@@ -1919,40 +1919,12 @@
19191919
},
19201920
"@metamask/network-controller>@metamask/eth-json-rpc-provider": {
19211921
"packages": {
1922-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": true,
1923-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1922+
"@metamask/json-rpc-engine": true,
1923+
"@metamask/rpc-errors": true,
19241924
"@metamask/safe-event-emitter": true,
19251925
"uuid": true
19261926
}
19271927
},
1928-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": {
1929-
"packages": {
1930-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1931-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1932-
"@metamask/safe-event-emitter": true
1933-
}
1934-
},
1935-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": {
1936-
"packages": {
1937-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1938-
"@metamask/rpc-errors>fast-safe-stringify": true
1939-
}
1940-
},
1941-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": {
1942-
"globals": {
1943-
"TextDecoder": true,
1944-
"TextEncoder": true
1945-
},
1946-
"packages": {
1947-
"@metamask/utils>@metamask/superstruct": true,
1948-
"@metamask/utils>@scure/base": true,
1949-
"@metamask/utils>pony-cause": true,
1950-
"@noble/hashes": true,
1951-
"browserify>buffer": true,
1952-
"nock>debug": true,
1953-
"semver": true
1954-
}
1955-
},
19561928
"@metamask/network-controller>@metamask/json-rpc-engine": {
19571929
"packages": {
19581930
"@metamask/network-controller>@metamask/rpc-errors": true,

lavamoat/browserify/flask/policy.json

+2-30
Original file line numberDiff line numberDiff line change
@@ -1919,40 +1919,12 @@
19191919
},
19201920
"@metamask/network-controller>@metamask/eth-json-rpc-provider": {
19211921
"packages": {
1922-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": true,
1923-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1922+
"@metamask/json-rpc-engine": true,
1923+
"@metamask/rpc-errors": true,
19241924
"@metamask/safe-event-emitter": true,
19251925
"uuid": true
19261926
}
19271927
},
1928-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": {
1929-
"packages": {
1930-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1931-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1932-
"@metamask/safe-event-emitter": true
1933-
}
1934-
},
1935-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": {
1936-
"packages": {
1937-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1938-
"@metamask/rpc-errors>fast-safe-stringify": true
1939-
}
1940-
},
1941-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": {
1942-
"globals": {
1943-
"TextDecoder": true,
1944-
"TextEncoder": true
1945-
},
1946-
"packages": {
1947-
"@metamask/utils>@metamask/superstruct": true,
1948-
"@metamask/utils>@scure/base": true,
1949-
"@metamask/utils>pony-cause": true,
1950-
"@noble/hashes": true,
1951-
"browserify>buffer": true,
1952-
"nock>debug": true,
1953-
"semver": true
1954-
}
1955-
},
19561928
"@metamask/network-controller>@metamask/json-rpc-engine": {
19571929
"packages": {
19581930
"@metamask/network-controller>@metamask/rpc-errors": true,

lavamoat/browserify/main/policy.json

+2-30
Original file line numberDiff line numberDiff line change
@@ -1919,40 +1919,12 @@
19191919
},
19201920
"@metamask/network-controller>@metamask/eth-json-rpc-provider": {
19211921
"packages": {
1922-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": true,
1923-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1922+
"@metamask/json-rpc-engine": true,
1923+
"@metamask/rpc-errors": true,
19241924
"@metamask/safe-event-emitter": true,
19251925
"uuid": true
19261926
}
19271927
},
1928-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": {
1929-
"packages": {
1930-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
1931-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1932-
"@metamask/safe-event-emitter": true
1933-
}
1934-
},
1935-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": {
1936-
"packages": {
1937-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
1938-
"@metamask/rpc-errors>fast-safe-stringify": true
1939-
}
1940-
},
1941-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": {
1942-
"globals": {
1943-
"TextDecoder": true,
1944-
"TextEncoder": true
1945-
},
1946-
"packages": {
1947-
"@metamask/utils>@metamask/superstruct": true,
1948-
"@metamask/utils>@scure/base": true,
1949-
"@metamask/utils>pony-cause": true,
1950-
"@noble/hashes": true,
1951-
"browserify>buffer": true,
1952-
"nock>debug": true,
1953-
"semver": true
1954-
}
1955-
},
19561928
"@metamask/network-controller>@metamask/json-rpc-engine": {
19571929
"packages": {
19581930
"@metamask/network-controller>@metamask/rpc-errors": true,

lavamoat/browserify/mmi/policy.json

+2-30
Original file line numberDiff line numberDiff line change
@@ -2011,40 +2011,12 @@
20112011
},
20122012
"@metamask/network-controller>@metamask/eth-json-rpc-provider": {
20132013
"packages": {
2014-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": true,
2015-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
2014+
"@metamask/json-rpc-engine": true,
2015+
"@metamask/rpc-errors": true,
20162016
"@metamask/safe-event-emitter": true,
20172017
"uuid": true
20182018
}
20192019
},
2020-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/json-rpc-engine": {
2021-
"packages": {
2022-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": true,
2023-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
2024-
"@metamask/safe-event-emitter": true
2025-
}
2026-
},
2027-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/rpc-errors": {
2028-
"packages": {
2029-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": true,
2030-
"@metamask/rpc-errors>fast-safe-stringify": true
2031-
}
2032-
},
2033-
"@metamask/network-controller>@metamask/eth-json-rpc-provider>@metamask/utils": {
2034-
"globals": {
2035-
"TextDecoder": true,
2036-
"TextEncoder": true
2037-
},
2038-
"packages": {
2039-
"@metamask/utils>@metamask/superstruct": true,
2040-
"@metamask/utils>@scure/base": true,
2041-
"@metamask/utils>pony-cause": true,
2042-
"@noble/hashes": true,
2043-
"browserify>buffer": true,
2044-
"nock>debug": true,
2045-
"semver": true
2046-
}
2047-
},
20482020
"@metamask/network-controller>@metamask/json-rpc-engine": {
20492021
"packages": {
20502022
"@metamask/network-controller>@metamask/rpc-errors": true,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@
338338
"@metamask/post-message-stream": "^8.0.0",
339339
"@metamask/ppom-validator": "0.35.1",
340340
"@metamask/preinstalled-example-snap": "^0.2.0",
341-
"@metamask/profile-sync-controller": "^0.9.7",
341+
"@metamask/profile-sync-controller": "^1.0.2",
342342
"@metamask/providers": "^14.0.2",
343343
"@metamask/queued-request-controller": "^7.0.1",
344344
"@metamask/rate-limit-controller": "^6.0.0",

shared/constants/metametrics.ts

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ export enum MetaMetricsEventName {
631631
AccountRenamed = 'Account Renamed',
632632
AccountsSyncAdded = 'Accounts Sync Added',
633633
AccountsSyncNameUpdated = 'Accounts Sync Name Updated',
634+
AccountsSyncErroneousSituation = 'Accounts Sync Erroneous Situation',
634635
ActivityDetailsOpened = 'Activity Details Opened',
635636
ActivityDetailsClosed = 'Activity Details Closed',
636637
AnalyticsPreferenceSelected = 'Analytics Preference Selected',

0 commit comments

Comments
 (0)