From f32714908f546b1fe33c85b652af2ad7ff837681 Mon Sep 17 00:00:00 2001 From: Brian Bergeron Date: Fri, 5 Apr 2024 12:38:42 -0700 Subject: [PATCH] fix: token detection and import (#23798) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** Fixes issues with detecting and manually importing tokens, caused by recent controller changes. Hotfix for 11.14.0 This state publish had an undefined variable, and had the wrong function signature: Screenshot 2024-03-28 at 6 04 41 PM Which prevented the controllers from processing account switches. A test was failing because RPC requests were sent before onboarding. Delayed the enabling of token detection controller to `triggerNetworkrequests`. Also patching this fix for detection during account switch on mainnet when autodetection=off: https://github.com/MetaMask/core/pull/4133 [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/23798?quickstart=1) ## **Related issues** Fixes: - https://github.com/MetaMask/metamask-extension/issues/23526 - https://github.com/MetaMask/metamask-extension/issues/23612 ## **Manual testing steps** ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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/develop/.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. --- ...ts-controllers-npm-26.0.0-17c0e9432c.patch | 24 ++++++++++ app/scripts/metamask-controller.js | 18 ++++---- package.json | 2 +- yarn.lock | 46 ++++++++++++++++++- 4 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 .yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch diff --git a/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch b/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch new file mode 100644 index 000000000000..a84784cded69 --- /dev/null +++ b/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch @@ -0,0 +1,24 @@ +diff --git a/dist/TokenDetectionController.js b/dist/TokenDetectionController.js +index 9aa09140d47424217eac118aebca9031e5d2a236..8100c432e7e01dbefcb5f53db3c58e51f120a51d 100644 +--- a/dist/TokenDetectionController.js ++++ b/dist/TokenDetectionController.js +@@ -230,8 +230,7 @@ _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_ + const isDetectionChangedFromPreferences = __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f") !== useTokenDetection; + __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f"); + __classPrivateFieldSet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, useTokenDetection, "f"); +- if (useTokenDetection && +- (isSelectedAddressChanged || isDetectionChangedFromPreferences)) { ++ if (isSelectedAddressChanged || isDetectionChangedFromPreferences) { + yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this, { + selectedAddress: __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f"), + }); +@@ -239,8 +238,7 @@ _TokenDetectionController_intervalId = new WeakMap(), _TokenDetectionController_ + })); + this.messagingSystem.subscribe('AccountsController:selectedAccountChange', ({ address: newSelectedAddress }) => __awaiter(this, void 0, void 0, function* () { + const isSelectedAddressChanged = __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f") !== newSelectedAddress; +- if (isSelectedAddressChanged && +- __classPrivateFieldGet(this, _TokenDetectionController_isDetectionEnabledFromPreferences, "f")) { ++ if (isSelectedAddressChanged) { + __classPrivateFieldSet(this, _TokenDetectionController_selectedAddress, newSelectedAddress, "f"); + yield __classPrivateFieldGet(this, _TokenDetectionController_instances, "m", _TokenDetectionController_restartTokenDetection).call(this, { + selectedAddress: __classPrivateFieldGet(this, _TokenDetectionController_selectedAddress, "f"), diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 398ee8343ddd..375026336adf 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -508,8 +508,8 @@ export default class MetamaskController extends EventEmitter { const preferencesMessenger = this.controllerMessenger.getRestricted({ name: 'PreferencesController', - allowedActions: ['PreferencesController:getState'], - allowedEvents: ['PreferencesController:stateChange'], + allowedActions: [], + allowedEvents: [], }); this.preferencesController = new PreferencesController({ @@ -2216,6 +2216,7 @@ export default class MetamaskController extends EventEmitter { triggerNetworkrequests() { this.accountTracker.start(); this.txController.startIncomingTransactionPolling(); + this.tokenDetectionController.enable(); if (this.preferencesController.store.getState().useCurrencyRateCheck) { this.currencyRateController.startPollingByNetworkClientId( this.networkController.state.selectedNetworkClientId, @@ -2229,6 +2230,7 @@ export default class MetamaskController extends EventEmitter { stopNetworkRequests() { this.accountTracker.stop(); this.txController.stopIncomingTransactionPolling(); + this.tokenDetectionController.disable(); if (this.preferencesController.store.getState().useCurrencyRateCheck) { this.currencyRateController.stopAllPolling(); } @@ -2507,8 +2509,9 @@ export default class MetamaskController extends EventEmitter { // TODO: Remove once the preferences controller has been replaced with the core monorepo implementation this.controllerMessenger.publish( - `${this.preferencesController.name}:stateChange`, - [state, []], + 'PreferencesController:stateChange', + state, + [], ); }); @@ -3721,6 +3724,8 @@ export default class MetamaskController extends EventEmitter { this.txController.clearUnapprovedTransactions(); + this.tokenDetectionController.enable(); + // create new vault const vault = await this.keyringController.createNewVaultAndRestore( password, @@ -5644,11 +5649,6 @@ export default class MetamaskController extends EventEmitter { */ set isClientOpen(open) { this._isClientOpen = open; - if (open) { - this.tokenDetectionController.enable(); - } else { - this.tokenDetectionController.disable(); - } } /* eslint-enable accessor-pairs */ diff --git a/package.json b/package.json index da505802aee7..be541bd59c99 100644 --- a/package.json +++ b/package.json @@ -269,7 +269,7 @@ "@metamask/address-book-controller": "^3.1.7", "@metamask/announcement-controller": "^5.0.1", "@metamask/approval-controller": "^6.0.0", - "@metamask/assets-controllers": "^26.0.0", + "@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A26.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch", "@metamask/base-controller": "^4.1.0", "@metamask/browser-passworder": "^4.3.0", "@metamask/contract-metadata": "^2.3.1", diff --git a/yarn.lock b/yarn.lock index cdb3654ce1ee..a909a707e116 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3998,7 +3998,7 @@ __metadata: languageName: node linkType: hard -"@metamask/assets-controllers@npm:^26.0.0": +"@metamask/assets-controllers@npm:26.0.0": version: 26.0.0 resolution: "@metamask/assets-controllers@npm:26.0.0" dependencies: @@ -4040,6 +4040,48 @@ __metadata: languageName: node linkType: hard +"@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A26.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch": + version: 26.0.0 + resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A26.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch::version=26.0.0&hash=cf1d54" + dependencies: + "@ethereumjs/util": "npm:^8.1.0" + "@ethersproject/address": "npm:^5.7.0" + "@ethersproject/bignumber": "npm:^5.7.0" + "@ethersproject/contracts": "npm:^5.7.0" + "@ethersproject/providers": "npm:^5.7.0" + "@metamask/abi-utils": "npm:^2.0.2" + "@metamask/accounts-controller": "npm:^11.0.0" + "@metamask/approval-controller": "npm:^5.1.3" + "@metamask/base-controller": "npm:^4.1.1" + "@metamask/contract-metadata": "npm:^2.4.0" + "@metamask/controller-utils": "npm:^8.0.4" + "@metamask/eth-query": "npm:^4.0.0" + "@metamask/keyring-controller": "npm:^13.0.0" + "@metamask/metamask-eth-abis": "npm:3.0.0" + "@metamask/network-controller": "npm:^17.2.1" + "@metamask/polling-controller": "npm:^5.0.1" + "@metamask/preferences-controller": "npm:^8.0.0" + "@metamask/rpc-errors": "npm:^6.2.1" + "@metamask/utils": "npm:^8.3.0" + "@types/bn.js": "npm:^5.1.5" + "@types/uuid": "npm:^8.3.0" + async-mutex: "npm:^0.2.6" + bn.js: "npm:^5.2.1" + cockatiel: "npm:^3.1.2" + lodash: "npm:^4.17.21" + multiformats: "npm:^9.5.2" + single-call-balance-checker-abi: "npm:^1.0.0" + uuid: "npm:^8.3.2" + peerDependencies: + "@metamask/accounts-controller": ^11.0.0 + "@metamask/approval-controller": ^5.1.2 + "@metamask/keyring-controller": ^13.0.0 + "@metamask/network-controller": ^17.2.0 + "@metamask/preferences-controller": ^8.0.0 + checksum: 44e0ce87c9a2e4e161771c212460947629502ad6e72542e87a29a3de6e9c6665190396a2ae7d8206e57ad091cb80ad6634dfbfd254632a5ee6833c496f1ad0ad + languageName: node + linkType: hard + "@metamask/auto-changelog@npm:^2.1.0": version: 2.6.1 resolution: "@metamask/auto-changelog@npm:2.6.1" @@ -24815,7 +24857,7 @@ __metadata: "@metamask/address-book-controller": "npm:^3.1.7" "@metamask/announcement-controller": "npm:^5.0.1" "@metamask/approval-controller": "npm:^6.0.0" - "@metamask/assets-controllers": "npm:^26.0.0" + "@metamask/assets-controllers": "patch:@metamask/assets-controllers@npm%3A26.0.0#~/.yarn/patches/@metamask-assets-controllers-npm-26.0.0-17c0e9432c.patch" "@metamask/auto-changelog": "npm:^2.1.0" "@metamask/base-controller": "npm:^4.1.0" "@metamask/browser-passworder": "npm:^4.3.0"