Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NFT detection on custom Mainnet RPC endpoints #1360

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 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 @@ -198,7 +198,6 @@ describe('NftDetectionController', () => {
preferences.setUseNftDetection(false);
expect(nftDetection.config).toStrictEqual({
interval: DEFAULT_INTERVAL,
networkType: 'mainnet',
chainId: '1',
selectedAddress: '',
disabled: true,
Expand Down Expand Up @@ -234,9 +233,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 +255,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 +266,7 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';

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

Expand Down Expand Up @@ -296,7 +295,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 +344,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 +371,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 +382,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 +405,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 +419,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 +488,7 @@ describe('NftDetectionController', () => {
const selectedAddress = '0x1';
nftDetection.configure({
selectedAddress,
networkType: NetworkType.mainnet,
chainId: ChainId.mainnet,
});

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

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

Expand Down Expand Up @@ -693,7 +692,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