Skip to content

Commit

Permalink
improv: use profile-sync-controller config for user storage paths + w…
Browse files Browse the repository at this point in the history
…ait for _addAccountsWithBalance before dispatching the first account sync
  • Loading branch information
mathieuartu committed Nov 19, 2024
1 parent 2367e4e commit 8a6691c
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 190 deletions.
8 changes: 8 additions & 0 deletions app/scripts/controllers/preferences-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export type PreferencesControllerState = Omit<
enableMV3TimestampSave: boolean;
useExternalServices: boolean;
textDirection?: string;
hasFinishedAddingAccountsWithBalance?: boolean;
};

/**
Expand Down Expand Up @@ -454,6 +455,7 @@ const controllerMetadata = {
},
isMultiAccountBalancesEnabled: { persist: true, anonymous: true },
showIncomingTransactions: { persist: true, anonymous: true },
hasFinishedAddingAccountsWithBalance: { persist: true, anonymous: true },
};

export class PreferencesController extends BaseController<
Expand Down Expand Up @@ -1057,6 +1059,12 @@ export class PreferencesController extends BaseController<
}
///: END:ONLY_INCLUDE_IF

setHasFinishedAddingAccountsWithBalance(value: boolean): void {
this.update((state) => {
state.hasFinishedAddingAccountsWithBalance = value;
});
}

#handleAccountsControllerSync(
newAccountsControllerState: AccountsControllerState,
): void {
Expand Down
68 changes: 37 additions & 31 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ export default class MetamaskController extends EventEmitter {
if (!prevCompletedOnboarding && currCompletedOnboarding) {
const { address } = this.accountsController.getSelectedAccount();

this._addAccountsWithBalance();
await this._addAccountsWithBalance();

this.postOnboardingInitialization();
this.triggerNetworkrequests();
Expand Down Expand Up @@ -4394,42 +4394,48 @@ export default class MetamaskController extends EventEmitter {
}

async _addAccountsWithBalance() {
// Scan accounts until we find an empty one
const chainId = getCurrentChainId({
metamask: this.networkController.state,
});
const ethQuery = new EthQuery(this.provider);
const accounts = await this.keyringController.getAccounts();
let address = accounts[accounts.length - 1];
try {
// Scan accounts until we find an empty one
const chainId = getCurrentChainId({
metamask: this.networkController.state,
});
const ethQuery = new EthQuery(this.provider);
const accounts = await this.keyringController.getAccounts();
let address = accounts[accounts.length - 1];

for (let count = accounts.length; ; count++) {
const balance = await this.getBalance(address, ethQuery);
for (let count = accounts.length; ; count++) {
const balance = await this.getBalance(address, ethQuery);

if (balance === '0x0') {
// This account has no balance, so check for tokens
await this.tokenDetectionController.detectTokens({
selectedAddress: address,
});
if (balance === '0x0') {
// This account has no balance, so check for tokens
await this.tokenDetectionController.detectTokens({
selectedAddress: address,
});

const tokens =
this.tokensController.state.allTokens?.[chainId]?.[address];
const detectedTokens =
this.tokensController.state.allDetectedTokens?.[chainId]?.[address];

if (
(tokens?.length ?? 0) === 0 &&
(detectedTokens?.length ?? 0) === 0
) {
// This account has no balance or tokens
if (count !== 1) {
await this.removeAccount(address);
const tokens =
this.tokensController.state.allTokens?.[chainId]?.[address];
const detectedTokens =
this.tokensController.state.allDetectedTokens?.[chainId]?.[address];

if (
(tokens?.length ?? 0) === 0 &&
(detectedTokens?.length ?? 0) === 0
) {
// This account has no balance or tokens
if (count !== 1) {
await this.removeAccount(address);
}
break;
}
break;
}
}

// This account has assets, so check the next one
address = await this.keyringController.addNewAccount(count);
// This account has assets, so check the next one
address = await this.keyringController.addNewAccount(count);
}
} catch (e) {
log.warn(`Failed to add accounts with balance. Error: ${e}`);
} finally {
this.preferencesController.setHasFinishedAddingAccountsWithBalance(true);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@
"@metamask/post-message-stream": "^8.0.0",
"@metamask/ppom-validator": "0.35.1",
"@metamask/preinstalled-example-snap": "^0.2.0",
"@metamask/profile-sync-controller": "^0.9.7",
"@metamask/profile-sync-controller": "^1.0.1",
"@metamask/providers": "^14.0.2",
"@metamask/queued-request-controller": "^7.0.1",
"@metamask/rate-limit-controller": "^6.0.0",
Expand Down
Loading

0 comments on commit 8a6691c

Please sign in to comment.