Skip to content

fix: use correct accountTreeController API in sortMultichainAccountsByLastSelected#40927

Merged
EdouardBougon merged 1 commit intomainfrom
fix/non-evm-account-changed-sort-by-last-selected
Mar 18, 2026
Merged

fix: use correct accountTreeController API in sortMultichainAccountsByLastSelected#40927
EdouardBougon merged 1 commit intomainfrom
fix/non-evm-account-changed-sort-by-last-selected

Conversation

@EdouardBougon
Copy link
Contributor

@EdouardBougon EdouardBougon commented Mar 16, 2026

Description

Commit 7ca825d (chore: integrate MultichainAccountService changes (#38265)) replaced this.multichainAccountService.getAccountContext() with this.accountTreeController.getAccountContext() in sortMultichainAccountsByLastSelected, but the two methods return different shapes:

  • multichainAccountService.getAccountContext() returned an object with a group property exposing a .get({ scopes }) method to retrieve accounts by scope.
  • accountTreeController.getAccountContext() returns { walletId, groupId, sortOrder } — there is no group property.

As a result, context?.group?.get({ scopes: [EthScope.Eoa] }) always evaluated to undefined, causing getLastSelected() to always return undefined for every address. The sort became a no-op (0 - 0 = 0), meaning the wrong account (or none) was emitted on page reload for non-EVM networks.

This broke notifyNonEVMAccountChangedForCurrentAccount for both Tron and Solana, as well as _getSelectedMultichainAccountAddress which also relies on sortMultichainAccountsByLastSelected.

The fix adapts the code to the new accountTreeController API:

  1. Retrieve the groupId from getAccountContext(account.id)
  2. Fetch the group object via getAccountGroupObject(groupId)
  3. Iterate the group's accounts to find the EVM EOA account (the only one carrying metadata.lastSelected)

Changelog

CHANGELOG entry: Fixed non-EVM account changed notifications not selecting the correct account on page reload for Tron and Solana

Description

Open in GitHub Codespaces

Changelog

CHANGELOG entry:

Related issues

Fixes:

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

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.

Note

Medium Risk
Touches multichain account selection/sorting logic used on reload; incorrect grouping or type detection could still misorder accounts across networks, but the change is localized and covered by updated unit tests.

Overview
Fixes sortMultichainAccountsByLastSelected to work with the current accountTreeController.getAccountContext return shape by resolving groupId to a group via getAccountGroupObject and then deriving lastSelected from the group’s EVM EOA account.

Updates/expands the unit tests to mock the new controller interactions (getAccountGroupObject, accountsController.getAccount) and adds coverage for missing context/group scenarios so non-EVM account selection is correctly prioritized on reload.

Written by Cursor Bugbot for commit bf5e0ae. This will update automatically on new commits. Configure here.

@github-actions
Copy link
Contributor

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.

@metamaskbot metamaskbot added the team-wallet-standard Team responsible of the MetaMask Wallet Standard implementation label Mar 16, 2026
@EdouardBougon EdouardBougon requested a review from hmalik88 March 16, 2026 15:50
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

@metamaskbotv2
Copy link
Contributor

metamaskbotv2 bot commented Mar 16, 2026

Builds ready [1bff459]
⚡ Performance Benchmarks
👆 Interaction Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Load New Accountload_new_account2802712887283288
total2802712887283288
Confirm Txconfirm_tx6031600460562060426056
total6031600460562060426056
Bridge User Actionsbridge_load_page26923231229293312
bridge_load_asset_picker25121928925252289
bridge_search_token74573276010744760
total1239109913268113071326
🔌 Startup Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Standard HomeuiStartup13651124164310514121576
load113193413849311731320
domContentLoaded112492613789311691303
domInteractive2815110202484
firstPaint195621175171209343
backgroundConnect19617923611200216
firstReactRender20126262132
initialActions1011224
loadScripts9447291190929851116
setupStore1364361623
numNetworkReqs393190173185
Power User HomeuiStartup5495216215209219262487979
load13361085183113213821659
domContentLoaded13101072178612213501619
domInteractive39211733134115
firstPaint218911393191269435
backgroundConnect164729811083204819454323
firstReactRender24174452634
initialActions105113
loadScripts1076865150211311161372
setupStore1565071730
numNetworkReqs2059234457244305
🧭 User Journey Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Onboarding Import WalletimportWalletToSocialScreen2192192200219220
srpButtonToSrpForm97969819798
confirmSrpToPwForm23222412424
pwFormToMetricsScreen16151601616
metricsToWalletReadyScreen17161701717
doneButtonToHomeScreen61358866329605663
openAccountMenuToAccountListLoaded2920290429331029262933
total3909388239583039113958
Onboarding New WalletcreateWalletToSocialScreen2192172212220221
srpButtonToPwForm1111101121112112
createPwToRecoveryScreen889099
skipBackupToMetricsScreen41384314243
agreeButtonToOnboardingSuccess17161811718
doneButtonToAssetList53248856027542560
total92688295427936954
Asset DetailsassetClickToPriceChart705093168093
total705093168093
Solana Asset DetailsassetClickToPriceChart1328320550176205
total1328320550176205
Import Srp HomeloginToHomeScreen2314220024398123522439
openAccountMenuAfterLogin584372116572
homeAfterImportWithNewWallet1275493240085422342400
total36652916465580846454655
Send TransactionsopenSendPageFromHome30293213032
selectTokenToSendFormLoaded32313413434
reviewTransactionToConfirmationPage1116851142122213091421
total1193911154023713631540
SwapopenSwapPageFromHome70331143298114
fetchAndDisplaySwapQuotes270827022711327102711
total2775274228243027892824
🌐 Dapp Page Load Benchmarks

Current Commit: 1bff459 | Date: 3/16/2026

📄 Localhost MetaMask Test Dapp

Samples: 100

Summary

  • pageLoadTime-> current mean value: 1.04s (±43ms) 🟡 | historical mean value: 1.05s ⬇️ (historical data)
  • domContentLoaded-> current mean value: 734ms (±63ms) 🟢 | historical mean value: 738ms ⬇️ (historical data)
  • firstContentfulPaint-> current mean value: 83ms (±43ms) 🟢 | historical mean value: 87ms ⬇️ (historical data)

📈 Detailed Results

Metric Mean Std Dev Min Max P95 P99
pageLoadTime 1.04s 43ms 1.02s 1.36s 1.08s 1.36s
domContentLoaded 734ms 63ms 712ms 1.31s 766ms 1.31s
firstPaint 83ms 43ms 64ms 504ms 92ms 504ms
firstContentfulPaint 83ms 43ms 64ms 504ms 92ms 504ms
largestContentfulPaint 0ms 0ms 0ms 0ms 0ms 0ms
Bundle size diffs
  • background: 189 Bytes (0%)
  • ui: 5 Bytes (0%)
  • common: 20 Bytes (0%)

@github-actions github-actions bot added size-M and removed size-S labels Mar 16, 2026
@EdouardBougon EdouardBougon force-pushed the fix/non-evm-account-changed-sort-by-last-selected branch from a5e5f5f to ce30e24 Compare March 16, 2026 19:04
@metamaskbotv2
Copy link
Contributor

metamaskbotv2 bot commented Mar 16, 2026

Builds ready [ce30e24]
⚡ Performance Benchmarks
👆 Interaction Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Load New Accountload_new_account380279517101485517
total380279517101485517
Confirm Txconfirm_tx6072603161082760786108
total6072603161082760786108
Bridge User Actionsbridge_load_page23321725516249255
bridge_load_asset_picker2702692711271271
bridge_search_token76375177810770778
total1269125212881512851288
🔌 Startup Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Standard HomeuiStartup13751117174111114171584
load113392814779911591330
domContentLoaded112692314669711561316
domInteractive2816123202479
firstPaint175611096147207300
backgroundConnect19417823811197214
firstReactRender18124751928
initialActions107124
loadScripts9467521274949731132
setupStore1263351522
numNetworkReqs393191173283
Power User HomeuiStartup54202221180772681633510179
load13581146178112814051687
domContentLoaded13341135173912113711585
domInteractive38211772933114
firstPaint226871601167290370
backgroundConnect193831214648248628836810
firstReactRender25184252634
initialActions105114
loadScripts1099924146511111311356
setupStore1773761932
numNetworkReqs1408530442152227
🧭 User Journey Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Onboarding Import WalletimportWalletToSocialScreen2242202294229229
srpButtonToSrpForm94939519595
confirmSrpToPwForm22222302323
pwFormToMetricsScreen16151601616
metricsToWalletReadyScreen16161701617
doneButtonToHomeScreen60658962114618621
openAccountMenuToAccountListLoaded2921289929431829332943
total39593864413810039954138
Onboarding New WalletcreateWalletToSocialScreen2182182190219219
srpButtonToPwForm1081081080108108
createPwToRecoveryScreen888088
skipBackupToMetricsScreen39383903939
agreeButtonToOnboardingSuccess17171701717
doneButtonToAssetList52349157839578578
total925731110712810161107
Asset DetailsassetClickToPriceChart705586117886
total705586117886
Solana Asset DetailsassetClickToPriceChart1235218850171188
total1235218850171188
Import Srp HomeloginToHomeScreen2309223524177824172417
openAccountMenuAfterLogin61516876868
homeAfterImportWithNewWallet2331226324226724222422
total47014548479010847904790
Send TransactionsopenSendPageFromHome481969206369
selectTokenToSendFormLoaded64616526565
reviewTransactionToConfirmationPage11711060133610411801336
total1273118514359612411435
SwapopenSwapPageFromHome1121081153115115
fetchAndDisplaySwapQuotes268826832697626952697
total279427812807927972807
🌐 Dapp Page Load Benchmarks

Current Commit: ce30e24 | Date: 3/16/2026

📄 Localhost MetaMask Test Dapp

Samples: 100

Summary

  • pageLoadTime-> current mean value: 1.07s (±47ms) 🟡 | historical mean value: 1.04s ⬆️ (historical data)
  • domContentLoaded-> current mean value: 755ms (±65ms) 🟢 | historical mean value: 737ms ⬆️ (historical data)
  • firstContentfulPaint-> current mean value: 98ms (±132ms) 🟢 | historical mean value: 88ms ⬆️ (historical data)

📈 Detailed Results

Metric Mean Std Dev Min Max P95 P99
pageLoadTime 1.07s 47ms 1.03s 1.43s 1.13s 1.43s
domContentLoaded 755ms 65ms 722ms 1.34s 794ms 1.34s
firstPaint 98ms 132ms 68ms 1.41s 100ms 1.41s
firstContentfulPaint 98ms 132ms 68ms 1.41s 100ms 1.41s
largestContentfulPaint 0ms 0ms 0ms 0ms 0ms 0ms
Bundle size diffs
  • background: 189 Bytes (0%)
  • ui: 5 Bytes (0%)
  • common: 20 Bytes (0%)

…tsByLastSelected`

## **Description**

Commit 7ca825d (`chore: integrate MultichainAccountService changes
(#38265)`) replaced `this.multichainAccountService.getAccountContext()`
with `this.accountTreeController.getAccountContext()` in
`sortMultichainAccountsByLastSelected`, but the two methods return
different shapes:

- `multichainAccountService.getAccountContext()` returned an object with
  a `group` property exposing a `.get({ scopes })` method to retrieve
  accounts by scope.
- `accountTreeController.getAccountContext()` returns
  `{ walletId, groupId, sortOrder }` — there is no `group` property.

As a result, `context?.group?.get({ scopes: [EthScope.Eoa] })` always
evaluated to `undefined`, causing `getLastSelected()` to always return
`undefined` for every address. The sort became a no-op (`0 - 0 = 0`),
meaning the wrong account (or none) was emitted on page reload for
non-EVM networks.

This broke `notifyNonEVMAccountChangedForCurrentAccount` for both Tron
and Solana, as well as `_getSelectedMultichainAccountAddress` which also
relies on `sortMultichainAccountsByLastSelected`.

The fix adapts the code to the new `accountTreeController` API:
1. Retrieve the `groupId` from `getAccountContext(account.id)`
2. Fetch the group object via `getAccountGroupObject(groupId)`
3. Iterate the group's accounts to find the EVM EOA account (the only
   one carrying `metadata.lastSelected`)

## **Changelog**

CHANGELOG entry: Fixed non-EVM account changed notifications not
selecting the correct account on page reload for Tron and Solana

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@EdouardBougon EdouardBougon force-pushed the fix/non-evm-account-changed-sort-by-last-selected branch from ce30e24 to bf5e0ae Compare March 17, 2026 10:11
@sonarqubecloud
Copy link

@metamaskbotv2
Copy link
Contributor

metamaskbotv2 bot commented Mar 17, 2026

Builds ready [bf5e0ae]
⚡ Performance Benchmarks
👆 Interaction Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Load New Accountload_new_account2752732772275277
total2752732772275277
Confirm Txconfirm_tx606560606068360656068
total606560606068360656068
Bridge User Actionsbridge_load_page25221728626264286
bridge_load_asset_picker26020933547286335
bridge_search_token75473277616765776
total1289122713565513521356
🔌 Startup Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Standard HomeuiStartup14361238202910714621617
load1193100116619812181369
domContentLoaded118699616539612141361
domInteractive3018173232583
firstPaint1556953084208268
backgroundConnect21619931115221244
firstReactRender21135372235
initialActions108124
loadScripts98680414509610131164
setupStore1463861723
numNetworkReqs393185153876
Power User HomeuiStartup56802033169773135648313350
load12311086171311512561466
domContentLoaded12111080167610812231422
domInteractive3519185292998
firstPaint229811178122267325
backgroundConnect223530311924243226758507
firstReactRender25175772738
initialActions107124
loadScripts1003887147310010231199
setupStore1474561727
numNetworkReqs1376032954141267
🧭 User Journey Benchmarks
BenchmarkMetricMean (ms)Min (ms)Max (ms)Std Dev (ms)P75 (ms)P95 (ms)
Onboarding Import WalletimportWalletToSocialScreen2182172201219220
srpButtonToSrpForm93939409494
confirmSrpToPwForm22222202222
pwFormToMetricsScreen16151601616
metricsToWalletReadyScreen16151601616
doneButtonToHomeScreen63158566834659668
openAccountMenuToAccountListLoaded2916289529381629222938
total3927390539461739423946
Onboarding New WalletcreateWalletToSocialScreen2192162253220225
srpButtonToPwForm1071041092109109
createPwToRecoveryScreen888088
skipBackupToMetricsScreen39393903939
agreeButtonToOnboardingSuccess16151601616
doneButtonToAssetList58849072193674721
total99387611028110621102
Asset DetailsassetClickToPriceChart94919629696
total94919629696
Solana Asset DetailsassetClickToPriceChart1003714343139143
total1003714343139143
Import Srp HomeloginToHomeScreen223922272247922472247
openAccountMenuAfterLogin633994227294
homeAfterImportWithNewWallet1856506243678422862436
total42903317471856545854718
Send TransactionsopenSendPageFromHome35254673946
selectTokenToSendFormLoaded35284253642
reviewTransactionToConfirmationPage1022850122013611391220
total1101916130813611921308
SwapopenSwapPageFromHome1052615747140157
fetchAndDisplaySwapQuotes2686267427071326952707
total2791273428353528142835
🌐 Dapp Page Load Benchmarks

Current Commit: bf5e0ae | Date: 3/17/2026

📄 Localhost MetaMask Test Dapp

Samples: 100

Summary

  • pageLoadTime-> current mean value: 1.05s (±52ms) 🟡 | historical mean value: 1.03s ⬆️ (historical data)
  • domContentLoaded-> current mean value: 738ms (±64ms) 🟢 | historical mean value: 727ms ⬆️ (historical data)
  • firstContentfulPaint-> current mean value: 93ms (±129ms) 🟢 | historical mean value: 90ms ⬆️ (historical data)

📈 Detailed Results

Metric Mean Std Dev Min Max P95 P99
pageLoadTime 1.05s 52ms 1.02s 1.46s 1.07s 1.46s
domContentLoaded 738ms 64ms 710ms 1.30s 753ms 1.30s
firstPaint 93ms 129ms 60ms 1.38s 88ms 1.38s
firstContentfulPaint 93ms 129ms 60ms 1.38s 88ms 1.38s
largestContentfulPaint 0ms 0ms 0ms 0ms 0ms 0ms
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 189 Bytes (0%)
  • ui: 3.72 KiB (0.04%)
  • common: 215 Bytes (0%)

if (!group) {
return undefined;
}
// Find the EVM EOA account in this group, as it's the only one with lastSelected
Copy link
Contributor

Choose a reason for hiding this comment

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

😅 we should probably add this property to the group rather than just one address/account on the group no? cc @gantunesr

Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

LGTM!

@EdouardBougon EdouardBougon enabled auto-merge March 18, 2026 16:46
@EdouardBougon EdouardBougon added this pull request to the merge queue Mar 18, 2026
Merged via the queue into main with commit 07fe425 Mar 18, 2026
183 checks passed
@EdouardBougon EdouardBougon deleted the fix/non-evm-account-changed-sort-by-last-selected branch March 18, 2026 18:38
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2026
@metamaskbot metamaskbot added the release-13.24.0 Issue or pull request that will be included in release 13.24.0 label Mar 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-13.24.0 Issue or pull request that will be included in release 13.24.0 size-M team-wallet-standard Team responsible of the MetaMask Wallet Standard implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants