Skip to content

Commit 8a53979

Browse files
release(runway): cherry-pick fix: patch TokenBalancesController to resolve missing balance state (#38140)
- fix: cp-13.10.1 patch TokenBalancesController to resolve missing balance state (#38126) ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/38126?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: fix missing native token balances in wallet balance ## **Related issues** Fixes: #38114 ## **Manual testing steps** Onboard or existing user with AccountsAPI enabled 1. Go to a network with native + erc tokens 2. Notice aggregated wallet balance - it should have both native + erc tokens aggregated together for the balance. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> https://www.loom.com/share/aeccc8e3cef4479da0e9d44fc5ebf4fa ## **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/main/.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/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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Patches `TokenBalancesController` to use lowercase account addresses for balance reads/writes and wires the patched `@metamask/[email protected]` via Yarn patch. > > - **Assets Controllers Patch (`@metamask/[email protected]`)**: > - Normalize `account` to lowercase in `TokenBalancesController` when reading/updating `d.tokenBalances` (cjs/mjs builds), ensuring balance updates are applied. > - **Dependencies**: > - Switch `@metamask/assets-controllers` in `package.json` to a Yarn patch source and add patched entry in `yarn.lock`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 75c9d95. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> [a2fe8c4](a2fe8c4) Co-authored-by: Prithpal Sooriya <[email protected]>
1 parent 8f631e6 commit 8a53979

File tree

3 files changed

+103
-3
lines changed

3 files changed

+103
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
diff --git a/dist/TokenBalancesController.cjs b/dist/TokenBalancesController.cjs
2+
index 24c2b2d22d3678352fd2ea4932181f3f11ffe41a..e63d3bcd80d68391d713394ec7f340b80fd8b79d 100644
3+
--- a/dist/TokenBalancesController.cjs
4+
+++ b/dist/TokenBalancesController.cjs
5+
@@ -527,14 +527,16 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
6+
}
7+
// Update with actual fetched balances only if the value has changed
8+
aggregated.forEach(({ success, value, account, token, chainId }) => {
9+
- var _a, _b, _c;
10+
+ var _a, _b;
11+
if (success && value !== undefined) {
12+
+ // Ensure all accounts we add/update are in lower-case
13+
+ const lowerCaseAccount = account.toLowerCase();
14+
const newBalance = (0, controller_utils_1.toHex)(value);
15+
const tokenAddress = checksum(token);
16+
- const currentBalance = d.tokenBalances[account]?.[chainId]?.[tokenAddress];
17+
+ const currentBalance = d.tokenBalances[lowerCaseAccount]?.[chainId]?.[tokenAddress];
18+
// Only update if the balance has actually changed
19+
if (currentBalance !== newBalance) {
20+
- ((_c = ((_a = d.tokenBalances)[_b = account] ?? (_a[_b] = {})))[chainId] ?? (_c[chainId] = {}))[tokenAddress] = newBalance;
21+
+ ((_b = ((_a = d.tokenBalances)[lowerCaseAccount] ?? (_a[lowerCaseAccount] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = newBalance;
22+
}
23+
}
24+
});
25+
diff --git a/dist/TokenBalancesController.mjs b/dist/TokenBalancesController.mjs
26+
index 420934fb16ed6151b5b80fb7be04f81352b4aaf4..d735379adcaf77cbd46b4659f9bfd32046abebdf 100644
27+
--- a/dist/TokenBalancesController.mjs
28+
+++ b/dist/TokenBalancesController.mjs
29+
@@ -523,14 +523,16 @@ export class TokenBalancesController extends StaticIntervalPollingController() {
30+
}
31+
// Update with actual fetched balances only if the value has changed
32+
aggregated.forEach(({ success, value, account, token, chainId }) => {
33+
- var _a, _b, _c;
34+
+ var _a, _b;
35+
if (success && value !== undefined) {
36+
+ // Ensure all accounts we add/update are in lower-case
37+
+ const lowerCaseAccount = account.toLowerCase();
38+
const newBalance = toHex(value);
39+
const tokenAddress = checksum(token);
40+
- const currentBalance = d.tokenBalances[account]?.[chainId]?.[tokenAddress];
41+
+ const currentBalance = d.tokenBalances[lowerCaseAccount]?.[chainId]?.[tokenAddress];
42+
// Only update if the balance has actually changed
43+
if (currentBalance !== newBalance) {
44+
- ((_c = ((_a = d.tokenBalances)[_b = account] ?? (_a[_b] = {})))[chainId] ?? (_c[chainId] = {}))[tokenAddress] = newBalance;
45+
+ ((_b = ((_a = d.tokenBalances)[lowerCaseAccount] ?? (_a[lowerCaseAccount] = {})))[chainId] ?? (_b[chainId] = {}))[tokenAddress] = newBalance;
46+
}
47+
}
48+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
"@metamask/address-book-controller": "^7.0.0",
272272
"@metamask/announcement-controller": "^8.0.0",
273273
"@metamask/approval-controller": "^8.0.0",
274-
"@metamask/assets-controllers": "^88.0.0",
274+
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A88.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-88.0.0-3dfc0ab8f1.patch",
275275
"@metamask/base-controller": "^9.0.0",
276276
"@metamask/bitcoin-wallet-snap": "^1.6.0",
277277
"@metamask/bridge-controller": "^60.1.0",

yarn.lock

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5591,7 +5591,7 @@ __metadata:
55915591
languageName: node
55925592
linkType: hard
55935593

5594-
"@metamask/assets-controllers@npm:^88.0.0":
5594+
"@metamask/assets-controllers@npm:88.0.0":
55955595
version: 88.0.0
55965596
resolution: "@metamask/assets-controllers@npm:88.0.0"
55975597
dependencies:
@@ -5643,6 +5643,58 @@ __metadata:
56435643
languageName: node
56445644
linkType: hard
56455645

5646+
"@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A88.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-88.0.0-3dfc0ab8f1.patch":
5647+
version: 88.0.0
5648+
resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A88.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-88.0.0-3dfc0ab8f1.patch::version=88.0.0&hash=ab5ccb"
5649+
dependencies:
5650+
"@ethereumjs/util": "npm:^9.1.0"
5651+
"@ethersproject/abi": "npm:^5.7.0"
5652+
"@ethersproject/address": "npm:^5.7.0"
5653+
"@ethersproject/bignumber": "npm:^5.7.0"
5654+
"@ethersproject/contracts": "npm:^5.7.0"
5655+
"@ethersproject/providers": "npm:^5.7.0"
5656+
"@metamask/abi-utils": "npm:^2.0.3"
5657+
"@metamask/base-controller": "npm:^9.0.0"
5658+
"@metamask/contract-metadata": "npm:^2.4.0"
5659+
"@metamask/controller-utils": "npm:^11.15.0"
5660+
"@metamask/eth-query": "npm:^4.0.0"
5661+
"@metamask/keyring-api": "npm:^21.0.0"
5662+
"@metamask/messenger": "npm:^0.3.0"
5663+
"@metamask/metamask-eth-abis": "npm:^3.1.1"
5664+
"@metamask/polling-controller": "npm:^15.0.0"
5665+
"@metamask/rpc-errors": "npm:^7.0.2"
5666+
"@metamask/snaps-sdk": "npm:^9.0.0"
5667+
"@metamask/snaps-utils": "npm:^11.0.0"
5668+
"@metamask/utils": "npm:^11.8.1"
5669+
"@types/bn.js": "npm:^5.1.5"
5670+
"@types/uuid": "npm:^8.3.0"
5671+
async-mutex: "npm:^0.5.0"
5672+
bitcoin-address-validation: "npm:^2.2.3"
5673+
bn.js: "npm:^5.2.1"
5674+
immer: "npm:^9.0.6"
5675+
lodash: "npm:^4.17.21"
5676+
multiformats: "npm:^9.9.0"
5677+
reselect: "npm:^5.1.1"
5678+
single-call-balance-checker-abi: "npm:^1.0.0"
5679+
uuid: "npm:^8.3.2"
5680+
peerDependencies:
5681+
"@metamask/account-tree-controller": ^3.0.0
5682+
"@metamask/accounts-controller": ^34.0.0
5683+
"@metamask/approval-controller": ^8.0.0
5684+
"@metamask/core-backend": ^4.0.0
5685+
"@metamask/keyring-controller": ^24.0.0
5686+
"@metamask/network-controller": ^25.0.0
5687+
"@metamask/permission-controller": ^12.0.0
5688+
"@metamask/phishing-controller": ^15.0.0
5689+
"@metamask/preferences-controller": ^21.0.0
5690+
"@metamask/providers": ^22.0.0
5691+
"@metamask/snaps-controllers": ^14.0.0
5692+
"@metamask/transaction-controller": ^61.0.0
5693+
webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0
5694+
checksum: 10/17587884760c7856d4459c2f60a1acff0d2430b4ae0f9710bff15076e7d21f726d2a1380781d427d32859f1e6e7568eda48e277093d3b85aaffab82021a14c0c
5695+
languageName: node
5696+
linkType: hard
5697+
56465698
"@metamask/auth-network-utils@npm:^0.3.0, @metamask/auth-network-utils@npm:^0.3.1":
56475699
version: 0.3.1
56485700
resolution: "@metamask/auth-network-utils@npm:0.3.1"
@@ -32492,7 +32544,7 @@ __metadata:
3249232544
"@metamask/announcement-controller": "npm:^8.0.0"
3249332545
"@metamask/api-specs": "npm:^0.13.0"
3249432546
"@metamask/approval-controller": "npm:^8.0.0"
32495-
"@metamask/assets-controllers": "npm:^88.0.0"
32547+
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A88.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-88.0.0-3dfc0ab8f1.patch"
3249632548
"@metamask/auto-changelog": "npm:^5.1.0"
3249732549
"@metamask/base-controller": "npm:^9.0.0"
3249832550
"@metamask/bitcoin-wallet-snap": "npm:^1.6.0"

0 commit comments

Comments
 (0)