Skip to content

Commit

Permalink
Support NFT detection on custom Mainnet RPC endpoints
Browse files Browse the repository at this point in the history
The NFT detection controller now supports NFT detection on any Ethereum
Mainnet networks, including custom RPC endpoints. Previously this was
only supported on the built-in Ethereum Mainnet. This restriction was
likely not intentional in the first place (at least I can't think of a
reason for it).

This relates to #1209
  • Loading branch information
Gudahtt committed May 10, 2023
1 parent 08ad0b8 commit e897f11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
28 changes: 14 additions & 14 deletions packages/assets-controllers/src/NftDetectionController.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as sinon from 'sinon';
import nock from 'nock';
import { PreferencesController } from '@metamask/preferences-controller';
import { OPENSEA_PROXY_URL, NetworkType } from '@metamask/controller-utils';
import { OPENSEA_PROXY_URL, ChainId } from '@metamask/controller-utils';
import { NftController } from './NftController';
import { AssetsContractController } from './AssetsContractController';
import { NftDetectionController } from './NftDetectionController';
Expand Down Expand Up @@ -234,9 +234,9 @@ describe('NftDetectionController', () => {
});

it('should detect mainnet correctly', () => {
nftDetection.configure({ networkType: NetworkType.mainnet });
nftDetection.configure({ chainId: ChainId.mainnet });
expect(nftDetection.isMainnet()).toStrictEqual(true);
nftDetection.configure({ networkType: NetworkType.goerli });
nftDetection.configure({ chainId: ChainId.goerli });
expect(nftDetection.isMainnet()).toStrictEqual(false);
});

Expand All @@ -256,7 +256,7 @@ describe('NftDetectionController', () => {
addNft: nftController.addNft.bind(nftController),
getNftState: () => nftController.state,
},
{ interval: 10, networkType: NetworkType.goerli },
{ interval: 10, chainId: ChainId.goerli },
);
expect(mockNfts.called).toBe(false);
resolve('');
Expand All @@ -267,7 +267,7 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';

nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});

Expand Down Expand Up @@ -296,7 +296,7 @@ describe('NftDetectionController', () => {
it('should detect, add NFTs and do nor remove not detected NFTs correctly', async () => {
const selectedAddress = '0x1';
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});
nftController.configure({ selectedAddress });
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('NftDetectionController', () => {
it('should not autodetect NFTs that exist in the ignoreList', async () => {
const selectedAddress = '0x2';
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress: '0x2',
});
nftController.configure({ selectedAddress });
Expand All @@ -372,7 +372,7 @@ describe('NftDetectionController', () => {
it('should not detect and add NFTs if there is no selectedAddress', async () => {
const selectedAddress = '';
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});
const { chainId } = nftDetection.config;
Expand All @@ -383,7 +383,7 @@ describe('NftDetectionController', () => {

it('should not detect and add NFTs to the wrong selectedAddress', async () => {
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress: '0x9',
});
const { chainId } = nftDetection.config;
Expand All @@ -406,7 +406,7 @@ describe('NftDetectionController', () => {
preferences.setUseNftDetection(false);
const selectedAddress = '0x9';
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});
const { chainId } = nftController.config;
Expand All @@ -420,7 +420,7 @@ describe('NftDetectionController', () => {
preferences.setOpenSeaEnabled(false);
const selectedAddress = '0x9';
nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});
const { chainId } = nftController.config;
Expand Down Expand Up @@ -489,7 +489,7 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';
nftDetection.configure({
selectedAddress,
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
});

nftController.configure({
Expand Down Expand Up @@ -656,7 +656,7 @@ describe('NftDetectionController', () => {
});

nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});

Expand Down Expand Up @@ -693,7 +693,7 @@ describe('NftDetectionController', () => {
.replyWithError(new Error('UNEXPECTED ERROR'));

nftDetection.configure({
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
selectedAddress,
});

Expand Down
7 changes: 2 additions & 5 deletions packages/assets-controllers/src/NftDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type { PreferencesState } from '@metamask/preferences-controller';
import {
OPENSEA_PROXY_URL,
OPENSEA_API_URL,
NetworkType,
fetchWithErrorHandling,
toChecksumHexAddress,
ChainId,
} from '@metamask/controller-utils';
import type { NftController, NftState, NftMetadata } from './NftController';

Expand Down Expand Up @@ -122,7 +122,6 @@ export interface ApiNftCreator {
*/
export interface NftDetectionConfig extends BaseConfig {
interval: number;
networkType: NetworkType;
chainId: `0x${string}` | `${number}` | number;
selectedAddress: string;
}
Expand Down Expand Up @@ -239,7 +238,6 @@ export class NftDetectionController extends BaseController<
super(config, state);
this.defaultConfig = {
interval: DEFAULT_INTERVAL,
networkType: NetworkType.mainnet,
chainId: '1',
selectedAddress: '',
disabled: true,
Expand Down Expand Up @@ -268,7 +266,6 @@ export class NftDetectionController extends BaseController<

onNetworkStateChange(({ providerConfig }) => {
this.configure({
networkType: providerConfig.type,
chainId: providerConfig.chainId as NftDetectionConfig['chainId'],
});
});
Expand Down Expand Up @@ -319,7 +316,7 @@ export class NftDetectionController extends BaseController<
*
* @returns Whether current network is mainnet.
*/
isMainnet = (): boolean => this.config.networkType === NetworkType.mainnet;
isMainnet = (): boolean => this.config.chainId === ChainId.mainnet;

/**
* Triggers asset ERC721 token auto detection on mainnet. Any newly detected NFTs are
Expand Down

0 comments on commit e897f11

Please sign in to comment.