Skip to content

Commit

Permalink
Replace #getCorrectChainId with #getCorrectChainIdAndNetworkClientId
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Feb 17, 2024
1 parent de5043b commit bc68646
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions packages/assets-controllers/src/TokenDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ export class TokenDetectionController extends StaticIntervalPollingController<
selectedAddress ??
this.messagingSystem.call('AccountsController:getSelectedAccount')
.address;
this.#chainId = this.#getCorrectChainId(networkClientId);
const { chainId } =
this.#getCorrectChainIdAndNetworkClientId(networkClientId);
this.#chainId = chainId;

const { useTokenDetection: defaultUseTokenDetection } =
this.messagingSystem.call('PreferencesController:getState');
Expand Down Expand Up @@ -325,7 +327,8 @@ export class TokenDetectionController extends StaticIntervalPollingController<
const isNetworkClientIdChanged =
this.#networkClientId !== selectedNetworkClientId;

const newChainId = this.#getCorrectChainId(selectedNetworkClientId);
const { chainId: newChainId } =
this.#getCorrectChainIdAndNetworkClientId(selectedNetworkClientId);
this.#isDetectionEnabledForNetwork =
isTokenDetectionSupportedForNetwork(newChainId);

Expand Down Expand Up @@ -397,13 +400,33 @@ export class TokenDetectionController extends StaticIntervalPollingController<
}, this.getIntervalLength());
}

#getCorrectChainId(networkClientId?: NetworkClientId) {
const { chainId } =
this.messagingSystem.call(
#getCorrectChainIdAndNetworkClientId(networkClientId?: NetworkClientId): {
chainId: Hex;
networkClientId: NetworkClientId;
} {
if (networkClientId) {
const networkConfiguration = this.messagingSystem.call(
'NetworkController:getNetworkConfigurationByNetworkClientId',
networkClientId ?? this.#networkClientId,
) ?? {};
return chainId ?? this.#chainId;
networkClientId,
);
if (networkConfiguration) {
return {
chainId: networkConfiguration.chainId,
networkClientId,
};
}
}
const { chainId } = this.messagingSystem.call(
'NetworkController:getProviderConfig',
);
const newNetworkClientId = this.messagingSystem.call(
'NetworkController:findNetworkClientIdByChainId',
this.#chainId,
);
return {
chainId,
networkClientId: newNetworkClientId,
};
}

async _executePoll(
Expand Down Expand Up @@ -459,25 +482,13 @@ export class TokenDetectionController extends StaticIntervalPollingController<
if (!this.isActive) {
return;
}

const addressAgainstWhichToDetect = accountAddress ?? this.#selectedAddress;
let chainIdAgainstWhichToDetect: Hex;
let networkClientIdAgainstWhichToDetect: NetworkClientId;
if (networkClientId) {
const networkConfiguration = this.messagingSystem.call(
'NetworkController:getNetworkConfigurationByNetworkClientId',
networkClientId,
);
chainIdAgainstWhichToDetect = networkConfiguration.chainId;
networkClientIdAgainstWhichToDetect = networkClientId;
} else {
chainIdAgainstWhichToDetect = this.messagingSystem.call(
'NetworkController:getProviderConfig',
).chainId;
networkClientIdAgainstWhichToDetect = this.messagingSystem.call(
'NetworkController:findNetworkClientIdByChainId',
chainIdAgainstWhichToDetect,
);
}
const {
chainId: chainIdAgainstWhichToDetect,
networkClientId: networkClientIdAgainstWhichToDetect,
} = this.#getCorrectChainIdAndNetworkClientId(networkClientId);

if (!isTokenDetectionSupportedForNetwork(chainIdAgainstWhichToDetect)) {
return;
}
Expand Down

0 comments on commit bc68646

Please sign in to comment.