Skip to content

Commit 1c18228

Browse files
authored
feat: passing discovered accounts data to the metrics event (#33927)
<!-- 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** We need to add the following property to the already existing event `Import Secret Recovery Phrase Completed`: `number_of_solana_accounts_discovered`: ` #number` ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to this page... 2. 3. ## **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** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/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-extension/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.
1 parent a4dde00 commit 1c18228

File tree

4 files changed

+94
-26
lines changed

4 files changed

+94
-26
lines changed

app/scripts/metamask-controller.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5036,9 +5036,15 @@ export default class MetamaskController extends EventEmitter {
50365036
this.accountsController.setSelectedAccount(account.id);
50375037
}
50385038

5039-
await this._addAccountsWithBalance(id, shouldImportSolanaAccount);
5039+
const discoveredAccounts = await this._addAccountsWithBalance(
5040+
id,
5041+
shouldImportSolanaAccount,
5042+
);
50405043

5041-
return newAccountAddress;
5044+
return {
5045+
newAccountAddress,
5046+
discoveredAccounts,
5047+
};
50425048
} finally {
50435049
releaseLock();
50445050
}
@@ -5287,6 +5293,11 @@ export default class MetamaskController extends EventEmitter {
52875293
);
52885294
}
52895295

5296+
const discoveredAccounts = {
5297+
bitcoin: 0,
5298+
solana: 0,
5299+
};
5300+
52905301
///: BEGIN:ONLY_INCLUDE_IF(bitcoin)
52915302
const btcClient = await this._getMultichainWalletSnapClient(
52925303
BITCOIN_WALLET_SNAP_ID,
@@ -5297,6 +5308,8 @@ export default class MetamaskController extends EventEmitter {
52975308
btcScope,
52985309
);
52995310

5311+
discoveredAccounts.bitcoin = btcAccounts.length;
5312+
53005313
// If none accounts got discovered, we still create the first (default) one.
53015314
if (btcAccounts.length === 0) {
53025315
await this._addSnapAccount(entropySource, btcClient, {
@@ -5317,6 +5330,8 @@ export default class MetamaskController extends EventEmitter {
53175330
solScope,
53185331
);
53195332

5333+
discoveredAccounts.solana = solanaAccounts.length;
5334+
53205335
// If none accounts got discovered, we still create the first (default) one.
53215336
if (solanaAccounts.length === 0) {
53225337
await this._addSnapAccount(entropySource, solanaClient, {
@@ -5325,8 +5340,13 @@ export default class MetamaskController extends EventEmitter {
53255340
}
53265341
}
53275342
///: END:ONLY_INCLUDE_IF
5343+
return discoveredAccounts;
53285344
} catch (e) {
53295345
log.warn(`Failed to add accounts with balance. Error: ${e}`);
5346+
return {
5347+
bitcoin: 0,
5348+
solana: 0,
5349+
};
53305350
} finally {
53315351
await this.userStorageController.setHasAccountSyncingSyncedAtLeastOnce(
53325352
true,

ui/pages/multi-srp/import-srp/import-srp.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,35 @@ export const ImportSrp = () => {
100100

101101
const joinedSrp = secretRecoveryPhrase.join(' ');
102102
if (joinedSrp) {
103-
await dispatch(actions.importMnemonicToVault(joinedSrp));
103+
const result = (await dispatch(
104+
actions.importMnemonicToVault(joinedSrp),
105+
)) as unknown as {
106+
newAccountAddress: string;
107+
discoveredAccounts: { bitcoin: number; solana: number };
108+
};
109+
110+
const { discoveredAccounts } = result;
111+
104112
// Clear the secret recovery phrase after importing
105113
setSecretRecoveryPhrase(Array(defaultNumberOfWords).fill(''));
114+
115+
// Track the event with the discovered accounts
116+
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
117+
trackEvent({
118+
event: MetaMetricsEventName.ImportSecretRecoveryPhraseCompleted,
119+
properties: {
120+
// eslint-disable-next-line @typescript-eslint/naming-convention
121+
hd_entropy_index: newHdEntropyIndex,
122+
// eslint-disable-next-line @typescript-eslint/naming-convention
123+
number_of_solana_accounts_discovered: discoveredAccounts?.solana,
124+
// eslint-disable-next-line @typescript-eslint/naming-convention
125+
number_of_bitcoin_accounts_discovered: discoveredAccounts?.bitcoin,
126+
},
127+
});
106128
}
129+
107130
history.push(DEFAULT_ROUTE);
108131
dispatch(setShowNewSrpAddedToast(true));
109-
trackEvent({
110-
event: MetaMetricsEventName.ImportSecretRecoveryPhraseCompleted,
111-
properties: {
112-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
113-
// eslint-disable-next-line @typescript-eslint/naming-convention
114-
hd_entropy_index: newHdEntropyIndex,
115-
},
116-
});
117132
}
118133

119134
const isValidSrp = useMemo(() => {

ui/store/actions.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,6 +3086,27 @@ describe('Actions', () => {
30863086
true,
30873087
);
30883088
});
3089+
3090+
it('returns discovered accounts from background', async () => {
3091+
const store = mockStore();
3092+
const mockResult = {
3093+
newAccountAddress: '9fE6zKgca6K2EEa3yjbcq7zGMusUNqSQeWQNL2YDZ2Yi',
3094+
discoveredAccounts: { bitcoin: 2, solana: 1 },
3095+
};
3096+
3097+
const importMnemonicToVaultStub = sinon.stub().resolves(mockResult);
3098+
3099+
background.getApi.returns({
3100+
importMnemonicToVault: importMnemonicToVaultStub,
3101+
});
3102+
setBackgroundConnection(background.getApi());
3103+
3104+
const result = await store.dispatch(
3105+
actions.importMnemonicToVault('mnemonic'),
3106+
);
3107+
3108+
expect(result).toStrictEqual(mockResult);
3109+
});
30893110
});
30903111

30913112
describe('getTokenStandardAndDetailsByChain', () => {

ui/store/actions.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -508,27 +508,39 @@ export function createNewVaultAndRestore(
508508
};
509509
}
510510

511-
export function importMnemonicToVault(
512-
mnemonic: string,
513-
): ThunkAction<void, MetaMaskReduxState, unknown, AnyAction> {
514-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31879
515-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
516-
return (dispatch: MetaMaskReduxDispatch) => {
511+
export function importMnemonicToVault(mnemonic: string): ThunkAction<
512+
Promise<{
513+
newAccountAddress: string;
514+
discoveredAccounts: { bitcoin: number; solana: number };
515+
}>,
516+
MetaMaskReduxState,
517+
unknown,
518+
AnyAction
519+
> {
520+
return async (dispatch: MetaMaskReduxDispatch) => {
517521
dispatch(showLoadingIndication());
518522
log.debug(`background.importMnemonicToVault`);
519523

520-
return new Promise<void>((resolve, reject) => {
521-
callBackgroundMethod('importMnemonicToVault', [mnemonic], (err) => {
522-
if (err) {
523-
reject(err);
524-
return;
525-
}
526-
resolve();
527-
});
524+
return new Promise<{
525+
newAccountAddress: string;
526+
discoveredAccounts: { bitcoin: number; solana: number };
527+
}>((resolve, reject) => {
528+
callBackgroundMethod(
529+
'importMnemonicToVault',
530+
[mnemonic],
531+
(err, result) => {
532+
if (err) {
533+
reject(err);
534+
return;
535+
}
536+
resolve(result);
537+
},
538+
);
528539
})
529-
.then(async () => {
540+
.then(async (result) => {
530541
dispatch(hideLoadingIndication());
531542
dispatch(setShowNewSrpAddedToast(true));
543+
return result;
532544
})
533545
.catch((err) => {
534546
dispatch(displayWarning(err));

0 commit comments

Comments
 (0)