Skip to content

Commit

Permalink
fix: token detection and import (#23798)
Browse files Browse the repository at this point in the history
## **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:

<img width="597" alt="Screenshot 2024-03-28 at 6 04 41 PM"
src="https://github.com/MetaMask/metamask-extension/assets/3500406/15cd8341-725b-49d8-bd4d-bf8539185214">

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:
MetaMask/core#4133

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/23798?quickstart=1)


## **Related issues**

Fixes:
- #23526
- #23612


## **Manual testing steps**


## **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 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.
  • Loading branch information
bergeron authored Apr 5, 2024
1 parent 58f380c commit f327149
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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"),
18 changes: 9 additions & 9 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
[],
);
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 */

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 44 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f327149

Please sign in to comment.