Skip to content

Commit 9d23fe3

Browse files
chore: cherry-pick: PR #25797 (#25976)
<!-- 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** Cherry-pick of #25797 ## **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: remove old erc-20 balances that fully get swapped ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **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 Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/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-mobile/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.
2 parents b2344ac + 9a77edc commit 9d23fe3

3 files changed

Lines changed: 114 additions & 64 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
diff --git a/dist/TokenBalancesController.cjs b/dist/TokenBalancesController.cjs
2+
index 9c91f20ce150a49fc9c914ca25cc6885e8800f34..075edb48321640a2abed5b038fbcb2e17661071e 100644
3+
--- a/dist/TokenBalancesController.cjs
4+
+++ b/dist/TokenBalancesController.cjs
5+
@@ -123,7 +123,7 @@ class TokenBalancesController extends (0, polling_controller_1.StaticIntervalPol
6+
return this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
7+
});
8+
_TokenBalancesController_createAccountsApiFetcher.set(this, () => {
9+
- const originalFetcher = new api_balance_fetcher_1.AccountsApiBalanceFetcher(__classPrivateFieldGet(this, _TokenBalancesController_platform, "f"), __classPrivateFieldGet(this, _TokenBalancesController_getProvider, "f"));
10+
+ const originalFetcher = new api_balance_fetcher_1.AccountsApiBalanceFetcher(__classPrivateFieldGet(this, _TokenBalancesController_platform, "f"), __classPrivateFieldGet(this, _TokenBalancesController_getProvider, "f"), () => this.state.tokenBalances);
11+
return {
12+
// Dynamically check allowExternalServices() at call time, not just at construction time
13+
supports: (chainId) => __classPrivateFieldGet(this, _TokenBalancesController_allowExternalServices, "f").call(this) &&
14+
diff --git a/dist/multi-chain-accounts-service/api-balance-fetcher.cjs b/dist/multi-chain-accounts-service/api-balance-fetcher.cjs
15+
index 50d8ee9fb47ecc6e08f6322095e7689b8e3bd5a7..bc6da747274e5ebdc7b8ae052ae7ff7b88d2988d 100644
16+
--- a/dist/multi-chain-accounts-service/api-balance-fetcher.cjs
17+
+++ b/dist/multi-chain-accounts-service/api-balance-fetcher.cjs
18+
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19+
var __importDefault = (this && this.__importDefault) || function (mod) {
20+
return (mod && mod.__esModule) ? mod : { "default": mod };
21+
};
22+
-var _AccountsApiBalanceFetcher_instances, _AccountsApiBalanceFetcher_platform, _AccountsApiBalanceFetcher_getProvider, _AccountsApiBalanceFetcher_fetchStakedBalances, _AccountsApiBalanceFetcher_fetchBalances;
23+
+var _AccountsApiBalanceFetcher_instances, _AccountsApiBalanceFetcher_platform, _AccountsApiBalanceFetcher_getProvider, _AccountsApiBalanceFetcher_getUserTokens, _AccountsApiBalanceFetcher_fetchStakedBalances, _AccountsApiBalanceFetcher_fetchBalances;
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.AccountsApiBalanceFetcher = void 0;
26+
const contracts_1 = require("@ethersproject/contracts");
27+
@@ -31,12 +31,14 @@ const ACCOUNTS_API_TIMEOUT_MS = 10000;
28+
const checksum = (addr) => (0, controller_utils_1.toChecksumHexAddress)(addr);
29+
const toCaipAccount = (chainId, account) => (0, assetsUtil_1.accountAddressToCaipReference)(chainId, account);
30+
class AccountsApiBalanceFetcher {
31+
- constructor(platform = 'extension', getProvider) {
32+
+ constructor(platform = 'extension', getProvider, getUserTokens) {
33+
_AccountsApiBalanceFetcher_instances.add(this);
34+
_AccountsApiBalanceFetcher_platform.set(this, 'extension');
35+
_AccountsApiBalanceFetcher_getProvider.set(this, void 0);
36+
+ _AccountsApiBalanceFetcher_getUserTokens.set(this, void 0);
37+
__classPrivateFieldSet(this, _AccountsApiBalanceFetcher_platform, platform, "f");
38+
__classPrivateFieldSet(this, _AccountsApiBalanceFetcher_getProvider, getProvider, "f");
39+
+ __classPrivateFieldSet(this, _AccountsApiBalanceFetcher_getUserTokens, getUserTokens, "f");
40+
}
41+
supports(chainId) {
42+
return constants_1.SUPPORTED_NETWORKS_ACCOUNTS_API_V4.includes(chainId);
43+
@@ -91,7 +93,8 @@ class AccountsApiBalanceFetcher {
44+
});
45+
// Ensure native token entries exist for all addresses on all requested chains
46+
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
47+
- const nativeBalancesFromAPI = new Map(); // key: `${address}-${chainId}`
48+
+ const nativeBalancesFromAPI = new Map(); // key: `${accountAddress}-${chainId}`
49+
+ const nonNativeBalancesFromAPI = new Map(); // key: `${accountAddress}-${tokenAddress}-${chainId}`
50+
// Process regular API balances
51+
if (apiResponse.balances) {
52+
const apiBalances = apiResponse.balances.flatMap((b) => {
53+
@@ -127,6 +130,9 @@ class AccountsApiBalanceFetcher {
54+
if (token === ZERO_ADDRESS && value !== undefined) {
55+
nativeBalancesFromAPI.set(`${finalAccount}-${chainId}`, value);
56+
}
57+
+ if (token !== ZERO_ADDRESS && value !== undefined) {
58+
+ nonNativeBalancesFromAPI.set(`${finalAccount.toLowerCase()}-${token.toLowerCase()}-${chainId}`, value);
59+
+ }
60+
return [
61+
{
62+
success: value !== undefined,
63+
@@ -156,6 +162,29 @@ class AccountsApiBalanceFetcher {
64+
}
65+
});
66+
});
67+
+ // Add zero erc-20 balance entries for addresses that API didn't return
68+
+ if (__classPrivateFieldGet(this, _AccountsApiBalanceFetcher_getUserTokens, "f")) {
69+
+ const userTokens = __classPrivateFieldGet(this, _AccountsApiBalanceFetcher_getUserTokens, "f").call(this);
70+
+ Object.entries(userTokens).forEach(([account, chains]) => {
71+
+ Object.entries(chains).forEach(([chainId, tokens]) => {
72+
+ Object.entries(tokens).forEach(([tokenAddress]) => {
73+
+ const tokenLowerCase = tokenAddress.toLowerCase();
74+
+ const key = `${account.toLowerCase()}-${tokenLowerCase}-${chainId}`;
75+
+ const isERC = tokenAddress !== ZERO_ADDRESS;
76+
+ const existingBalance = nonNativeBalancesFromAPI.get(key);
77+
+ if (isERC && !existingBalance) {
78+
+ results.push({
79+
+ success: true,
80+
+ value: new bn_js_1.default('0'),
81+
+ account: account,
82+
+ token: tokenLowerCase,
83+
+ chainId: chainId,
84+
+ });
85+
+ }
86+
+ });
87+
+ });
88+
+ });
89+
+ }
90+
// Add staked balances
91+
results.push(...stakedBalances);
92+
return {
93+
@@ -165,7 +194,7 @@ class AccountsApiBalanceFetcher {
94+
}
95+
}
96+
exports.AccountsApiBalanceFetcher = AccountsApiBalanceFetcher;
97+
-_AccountsApiBalanceFetcher_platform = new WeakMap(), _AccountsApiBalanceFetcher_getProvider = new WeakMap(), _AccountsApiBalanceFetcher_instances = new WeakSet(), _AccountsApiBalanceFetcher_fetchStakedBalances = async function _AccountsApiBalanceFetcher_fetchStakedBalances(addrs) {
98+
+_AccountsApiBalanceFetcher_platform = new WeakMap(), _AccountsApiBalanceFetcher_getProvider = new WeakMap(), _AccountsApiBalanceFetcher_getUserTokens = new WeakMap(), _AccountsApiBalanceFetcher_instances = new WeakSet(), _AccountsApiBalanceFetcher_fetchStakedBalances = async function _AccountsApiBalanceFetcher_fetchStakedBalances(addrs) {
99+
// Return empty array if no provider is available for blockchain calls
100+
if (!__classPrivateFieldGet(this, _AccountsApiBalanceFetcher_getProvider, "f")) {
101+
return [];

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@
183183
"qs": "6.14.1",
184184
"@playwright/test": "^1.57.0",
185185
"@metamask/transaction-controller@npm:^61.0.0": "patch:@metamask/transaction-controller@npm%3A62.9.0#~/.yarn/patches/@metamask-transaction-controller-npm-62.9.0-5c8f871530.patch",
186-
"@metamask/transaction-controller@npm:^62.9.2": "patch:@metamask/transaction-controller@npm%3A62.9.0#~/.yarn/patches/@metamask-transaction-controller-npm-62.9.0-5c8f871530.patch"
186+
"@metamask/transaction-controller@npm:^62.9.2": "patch:@metamask/transaction-controller@npm%3A62.9.0#~/.yarn/patches/@metamask-transaction-controller-npm-62.9.0-5c8f871530.patch",
187+
"@metamask/assets-controllers@npm:^96.0.0": "patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch",
188+
"@metamask/assets-controllers@npm:^97.0.0": "patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch"
187189
},
188190
"dependencies": {
189191
"@config-plugins/detox": "^9.0.0",
@@ -205,7 +207,7 @@
205207
"@metamask/analytics-controller": "^1.0.0",
206208
"@metamask/app-metadata-controller": "^2.0.0",
207209
"@metamask/approval-controller": "^8.0.0",
208-
"@metamask/assets-controllers": "^98.0.0",
210+
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch",
209211
"@metamask/base-controller": "^9.0.0",
210212
"@metamask/bitcoin-wallet-snap": "^1.9.0",
211213
"@metamask/bridge-controller": "^64.8.0",

yarn.lock

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7424,63 +7424,9 @@ __metadata:
74247424
languageName: node
74257425
linkType: hard
74267426

7427-
"@metamask/assets-controllers@npm:^96.0.0":
7428-
version: 96.0.0
7429-
resolution: "@metamask/assets-controllers@npm:96.0.0"
7430-
dependencies:
7431-
"@ethereumjs/util": "npm:^9.1.0"
7432-
"@ethersproject/abi": "npm:^5.7.0"
7433-
"@ethersproject/address": "npm:^5.7.0"
7434-
"@ethersproject/bignumber": "npm:^5.7.0"
7435-
"@ethersproject/contracts": "npm:^5.7.0"
7436-
"@ethersproject/providers": "npm:^5.7.0"
7437-
"@metamask/abi-utils": "npm:^2.0.3"
7438-
"@metamask/account-tree-controller": "npm:^4.0.0"
7439-
"@metamask/accounts-controller": "npm:^35.0.2"
7440-
"@metamask/approval-controller": "npm:^8.0.0"
7441-
"@metamask/base-controller": "npm:^9.0.0"
7442-
"@metamask/contract-metadata": "npm:^2.4.0"
7443-
"@metamask/controller-utils": "npm:^11.18.0"
7444-
"@metamask/core-backend": "npm:^5.0.0"
7445-
"@metamask/eth-query": "npm:^4.0.0"
7446-
"@metamask/keyring-api": "npm:^21.0.0"
7447-
"@metamask/keyring-controller": "npm:^25.0.0"
7448-
"@metamask/messenger": "npm:^0.3.0"
7449-
"@metamask/metamask-eth-abis": "npm:^3.1.1"
7450-
"@metamask/multichain-account-service": "npm:^5.1.0"
7451-
"@metamask/network-controller": "npm:^29.0.0"
7452-
"@metamask/permission-controller": "npm:^12.2.0"
7453-
"@metamask/phishing-controller": "npm:^16.1.0"
7454-
"@metamask/polling-controller": "npm:^16.0.2"
7455-
"@metamask/preferences-controller": "npm:^22.0.0"
7456-
"@metamask/profile-sync-controller": "npm:^27.0.0"
7457-
"@metamask/rpc-errors": "npm:^7.0.2"
7458-
"@metamask/snaps-controllers": "npm:^17.2.0"
7459-
"@metamask/snaps-sdk": "npm:^10.3.0"
7460-
"@metamask/snaps-utils": "npm:^11.7.0"
7461-
"@metamask/transaction-controller": "npm:^62.9.2"
7462-
"@metamask/utils": "npm:^11.9.0"
7463-
"@types/bn.js": "npm:^5.1.5"
7464-
"@types/uuid": "npm:^8.3.0"
7465-
async-mutex: "npm:^0.5.0"
7466-
bitcoin-address-validation: "npm:^2.2.3"
7467-
bn.js: "npm:^5.2.1"
7468-
immer: "npm:^9.0.6"
7469-
lodash: "npm:^4.17.21"
7470-
multiformats: "npm:^9.9.0"
7471-
reselect: "npm:^5.1.1"
7472-
single-call-balance-checker-abi: "npm:^1.0.0"
7473-
uuid: "npm:^8.3.2"
7474-
peerDependencies:
7475-
"@metamask/providers": ^22.0.0
7476-
webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0
7477-
checksum: 10/c5cf7363972b2f267ba96a925fd74eaee3eebde8bf470af7d4c49589b33b34fc9b8574289e4592cbce13e941201893d2ad20018da0dada8025317db0ce33df0f
7478-
languageName: node
7479-
linkType: hard
7480-
7481-
"@metamask/assets-controllers@npm:^97.0.0":
7482-
version: 97.0.0
7483-
resolution: "@metamask/assets-controllers@npm:97.0.0"
7427+
"@metamask/assets-controllers@npm:98.0.0":
7428+
version: 98.0.0
7429+
resolution: "@metamask/assets-controllers@npm:98.0.0"
74847430
dependencies:
74857431
"@ethereumjs/util": "npm:^9.1.0"
74867432
"@ethersproject/abi": "npm:^5.7.0"
@@ -7513,6 +7459,7 @@ __metadata:
75137459
"@metamask/snaps-controllers": "npm:^17.2.0"
75147460
"@metamask/snaps-sdk": "npm:^10.3.0"
75157461
"@metamask/snaps-utils": "npm:^11.7.0"
7462+
"@metamask/storage-service": "npm:^0.0.1"
75167463
"@metamask/transaction-controller": "npm:^62.9.2"
75177464
"@metamask/utils": "npm:^11.9.0"
75187465
"@types/bn.js": "npm:^5.1.5"
@@ -7529,13 +7476,13 @@ __metadata:
75297476
peerDependencies:
75307477
"@metamask/providers": ^22.0.0
75317478
webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0
7532-
checksum: 10/44f6adc0f3263a17c2be49aff7c558f0478c41f8c0318c03db5706451284ccf56c5534d56366c6504538e89eda8aa86669310467a170276f28875c17bcbd6367
7479+
checksum: 10/a2a3564ae4cb5349a134c40844db62db43d8aefa7ac00c8d6a31ace63573b6ea2a77988e1daae7719a99a79736f4dee3af7886d85829cd2efd68dd55b55fb5f6
75337480
languageName: node
75347481
linkType: hard
75357482

7536-
"@metamask/assets-controllers@npm:^98.0.0":
7483+
"@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch":
75377484
version: 98.0.0
7538-
resolution: "@metamask/assets-controllers@npm:98.0.0"
7485+
resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch::version=98.0.0&hash=a57226"
75397486
dependencies:
75407487
"@ethereumjs/util": "npm:^9.1.0"
75417488
"@ethersproject/abi": "npm:^5.7.0"
@@ -7585,7 +7532,7 @@ __metadata:
75857532
peerDependencies:
75867533
"@metamask/providers": ^22.0.0
75877534
webextension-polyfill: ^0.10.0 || ^0.11.0 || ^0.12.0
7588-
checksum: 10/a2a3564ae4cb5349a134c40844db62db43d8aefa7ac00c8d6a31ace63573b6ea2a77988e1daae7719a99a79736f4dee3af7886d85829cd2efd68dd55b55fb5f6
7535+
checksum: 10/70d7682d77ac4c340ccb5131a71d1c597da04b0417abfd983bb97303c301ee581970ade8c549dae86c2214d3a630227c97699c5a1baa15472c431095e51c5eba
75897536
languageName: node
75907537
linkType: hard
75917538

@@ -34682,7 +34629,7 @@ __metadata:
3468234629
"@metamask/analytics-controller": "npm:^1.0.0"
3468334630
"@metamask/app-metadata-controller": "npm:^2.0.0"
3468434631
"@metamask/approval-controller": "npm:^8.0.0"
34685-
"@metamask/assets-controllers": "npm:^98.0.0"
34632+
"@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A98.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-98.0.0-ebc0dfef4f.patch"
3468634633
"@metamask/auto-changelog": "npm:^5.3.0"
3468734634
"@metamask/base-controller": "npm:^9.0.0"
3468834635
"@metamask/bitcoin-wallet-snap": "npm:^1.9.0"

0 commit comments

Comments
 (0)