Skip to content

Commit cb17e99

Browse files
committed
fix: remove nonevm check during selectedAccountChange
1 parent 6c0595d commit cb17e99

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

packages/assets-controllers/src/TokenDetectionController.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,7 @@ export class TokenDetectionController extends StaticIntervalPollingController<
232232
const selectedInternalAccount = this.messagingSystem.call(
233233
'AccountsController:getSelectedAccount',
234234
);
235-
if (isEVMAccount(selectedInternalAccount)) {
236-
this.#selectedAccountId = selectedInternalAccount.id;
237-
}
238-
this.#selectedAccountId = '';
235+
this.#selectedAccountId = selectedInternalAccount.id;
239236
}
240237

241238
const { chainId, networkClientId } =
@@ -288,14 +285,17 @@ export class TokenDetectionController extends StaticIntervalPollingController<
288285
this.messagingSystem.subscribe(
289286
'PreferencesController:stateChange',
290287
async ({ useTokenDetection }) => {
288+
const selectedAccount = this.messagingSystem.call(
289+
'AccountsController:getSelectedAccount',
290+
);
291291
const isDetectionChangedFromPreferences =
292292
this.#isDetectionEnabledFromPreferences !== useTokenDetection;
293293

294294
this.#isDetectionEnabledFromPreferences = useTokenDetection;
295295

296296
if (isDetectionChangedFromPreferences) {
297297
await this.#restartTokenDetection({
298-
selectedAccountId: this.#selectedAccountId,
298+
selectedAccountId: selectedAccount.id,
299299
});
300300
}
301301
},
@@ -304,9 +304,6 @@ export class TokenDetectionController extends StaticIntervalPollingController<
304304
this.messagingSystem.subscribe(
305305
'AccountsController:selectedAccountChange',
306306
async (internalAccount) => {
307-
if (!isEVMAccount(internalAccount)) {
308-
return;
309-
}
310307
const didSelectedAccountIdChanged =
311308
this.#selectedAccountId !== internalAccount.id;
312309
if (didSelectedAccountIdChanged) {

packages/assets-controllers/src/TokenRatesController.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,6 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
239239
}
240240

241241
onSelectedAccountChange(async (internalAccount) => {
242-
if (!isEVMAccount(internalAccount)) {
243-
return;
244-
}
245242
if (this.config.selectedAccountId !== internalAccount.id) {
246243
this.configure({ selectedAccountId: internalAccount.id });
247244
if (this.#pollState === PollState.Active) {
@@ -347,7 +344,12 @@ export class TokenRatesController extends StaticIntervalPollingControllerV1<
347344
* Updates exchange rates for all tokens.
348345
*/
349346
async updateExchangeRates() {
350-
const { chainId, nativeCurrency } = this.config;
347+
const { chainId, nativeCurrency, selectedAccountId } = this.config;
348+
const selectedAccount = this.getInternalAccount(selectedAccountId);
349+
if (!selectedAccount || !isEVMAccount(selectedAccount)) {
350+
return;
351+
}
352+
351353
await this.updateExchangeRatesByChainId({
352354
chainId,
353355
nativeCurrency,

packages/assets-controllers/src/TokensController.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,17 +1888,6 @@ describe('TokensController', () => {
18881888
]);
18891889
stub.restore();
18901890
});
1891-
1892-
it('should not update selectedTokenId if the selected account is not an EVM account', async () => {
1893-
const mockEvmAccount = createMockInternalAccount();
1894-
triggerSelectedAccountChange(mockEvmAccount);
1895-
const mockNonEvmAccount = createMockInternalAccount({
1896-
// @ts-expect-error testing a not evm type
1897-
type: 'bitcoin',
1898-
});
1899-
triggerSelectedAccountChange(mockNonEvmAccount);
1900-
expect(tokensController.config.selectedAccountId).toBe(mockEvmAccount.id);
1901-
});
19021891
});
19031892

19041893
describe('Non evm check', () => {

packages/assets-controllers/src/TokensController.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
AccountsControllerGetAccountAction,
55
AccountsControllerSelectedAccountChangeEvent,
66
} from '@metamask/accounts-controller';
7-
import { isEVMAccount } from '@metamask/accounts-controller/src/utils';
7+
import { isEVMAccount } from '@metamask/accounts-controller';
88
import type { AddApprovalRequest } from '@metamask/approval-controller';
99
import type {
1010
BaseConfig,
@@ -265,12 +265,12 @@ export class TokensController extends BaseControllerV1<
265265
this.messagingSystem.subscribe(
266266
'AccountsController:selectedAccountChange',
267267
(internalAccount) => {
268+
this.configure({ selectedAccountId: internalAccount.id });
268269
if (!isEVMAccount(internalAccount)) {
269270
return;
270271
}
271272
const { allTokens, allIgnoredTokens, allDetectedTokens } = this.state;
272273
const { chainId } = this.config;
273-
this.configure({ selectedAccountId: internalAccount.id });
274274
this.update({
275275
tokens: allTokens[chainId]?.[internalAccount.address] ?? [],
276276
ignoredTokens:
@@ -290,16 +290,17 @@ export class TokensController extends BaseControllerV1<
290290
'AccountsController:getAccount',
291291
selectedAccountId,
292292
);
293+
294+
const { chainId } = providerConfig;
295+
this.abortController.abort();
296+
this.abortController = new AbortController();
297+
this.configure({ chainId });
293298
if (
294299
!selectedInternalAccount ||
295300
!isEVMAccount(selectedInternalAccount)
296301
) {
297302
return;
298303
}
299-
const { chainId } = providerConfig;
300-
this.abortController.abort();
301-
this.abortController = new AbortController();
302-
this.configure({ chainId });
303304
this.update({
304305
tokens: allTokens[chainId]?.[selectedInternalAccount.address] || [],
305306
ignoredTokens:
@@ -367,6 +368,7 @@ export class TokensController extends BaseControllerV1<
367368
selectedAccountId,
368369
);
369370
if (!internalAccount || !isEVMAccount(internalAccount)) {
371+
releaseLock();
370372
return [];
371373
}
372374

0 commit comments

Comments
 (0)