diff --git a/packages/assets-controllers/src/NftDetectionController.ts b/packages/assets-controllers/src/NftDetectionController.ts index a27cb636d98..a736eaea14b 100644 --- a/packages/assets-controllers/src/NftDetectionController.ts +++ b/packages/assets-controllers/src/NftDetectionController.ts @@ -408,29 +408,11 @@ export class NftDetectionController extends StaticIntervalPollingController< this.#disabled = disabled; this.#getNftState = getNftState; - onPreferencesStateChange(({ selectedAddress, useNftDetection }) => { - const { selectedAddress: currentSelectedAddress } = this.state; - if ( - selectedAddress !== currentSelectedAddress || - !useNftDetection !== this.#disabled - ) { - this.update((state) => { - state.selectedAddress = selectedAddress; - }); - this.#disabled = !useNftDetection; - if (useNftDetection) { - this.start(); - } else { - this.stop(); - } - } - }); - onNetworkStateChange(({ providerConfig }) => { - this.update((state) => { - state.chainId = providerConfig.chainId; - }); - }); + onPreferencesStateChange(this.#onPreferencesStateChange.bind(this)); + + onNetworkStateChange(this.#onNetworkStateChange.bind(this)); + this.#addNft = addNft; this.setIntervalLength(this.#interval); } @@ -494,6 +476,45 @@ export class NftDetectionController extends StaticIntervalPollingController< return networkClient.configuration.chainId === ChainId.mainnet; }; + /** + * Handles the state change of the preference controller. + * @param preferencesState - The new state of the preference controller. + * @param preferencesState.selectedAddress - The current selected address of the preference controller. + * @param preferencesState.useNftDetection - Boolean indicating user preference on NFT detection. + */ + #onPreferencesStateChange({ + selectedAddress, + useNftDetection, + }: PreferencesState) { + const { selectedAddress: currentSelectedAddress } = this.state; + if ( + selectedAddress !== currentSelectedAddress || + !useNftDetection !== this.#disabled + ) { + this.update((state) => { + state.selectedAddress = selectedAddress; + }); + this.#disabled = !useNftDetection; + if (useNftDetection) { + this.start(); + } else { + this.stop(); + } + } + } + + /** + * Handles the event when the network changes. + * + * @param networkState - The updated network state. + * @param networkState.providerConfig - RPC URL and network name provider settings of the currently connected network + */ + #onNetworkStateChange({ providerConfig }: NetworkState) { + this.update((state) => { + state.chainId = providerConfig.chainId; + }); + } + #getOwnerNftApi({ address, next }: { address: string; next?: string }) { return `${NFT_API_BASE_URL}/users/${address}/tokens?chainIds=1&limit=50&includeTopBid=true&continuation=${ next ?? ''