From 3b921145a4f6f742cfedc493b2c42eb6d9fb68ae Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 30 Nov 2021 12:48:02 -0600 Subject: [PATCH 1/4] add openSeaEnabled preference --- src/assets/CollectiblesController.ts | 52 +++++++++++++++++----------- src/user/PreferencesController.ts | 11 ++++++ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/assets/CollectiblesController.ts b/src/assets/CollectiblesController.ts index 6ac7da625c..3df450cb49 100644 --- a/src/assets/CollectiblesController.ts +++ b/src/assets/CollectiblesController.ts @@ -126,6 +126,7 @@ export interface CollectiblesConfig extends BaseConfig { selectedAddress: string; chainId: string; ipfsGateway: string; + openSeaEnabled: boolean; } /** @@ -355,12 +356,15 @@ export class CollectiblesController extends BaseController< ); }); - const openSeaMetadata = await safelyExecute(async () => { - return await this.getCollectibleInformationFromApi( - contractAddress, - tokenId, - ); - }); + let openSeaMetadata; + if (this.config.openSeaEnabled) { + openSeaMetadata = await safelyExecute(async () => { + return await this.getCollectibleInformationFromApi( + contractAddress, + tokenId, + ); + }); + } return { ...openSeaMetadata, @@ -428,11 +432,14 @@ export class CollectiblesController extends BaseController< ); }); - const openSeaContractData = await safelyExecute(async () => { - return await this.getCollectibleContractInformationFromApi( - contractAddress, - ); - }); + let openSeaContractData; + if (this.config.openSeaEnabled) { + openSeaContractData = await safelyExecute(async () => { + return await this.getCollectibleContractInformationFromApi( + contractAddress, + ); + }); + } if (blockchainContractData || openSeaContractData) { return { ...openSeaContractData, ...blockchainContractData }; @@ -787,6 +794,7 @@ export class CollectiblesController extends BaseController< selectedAddress: '', chainId: '', ipfsGateway: IPFS_DEFAULT_GATEWAY_URL, + openSeaEnabled: false, }; this.defaultState = { @@ -803,16 +811,18 @@ export class CollectiblesController extends BaseController< this.getOwnerOf = getOwnerOf; this.balanceOfERC1155Collectible = balanceOfERC1155Collectible; this.uriERC1155Collectible = uriERC1155Collectible; - onPreferencesStateChange(({ selectedAddress, ipfsGateway }) => { - const { allCollectibleContracts, allCollectibles } = this.state; - const { chainId } = this.config; - this.configure({ selectedAddress, ipfsGateway }); - this.update({ - collectibleContracts: - allCollectibleContracts[selectedAddress]?.[chainId] || [], - collectibles: allCollectibles[selectedAddress]?.[chainId] || [], - }); - }); + onPreferencesStateChange( + ({ selectedAddress, ipfsGateway, openSeaEnabled }) => { + const { allCollectibleContracts, allCollectibles } = this.state; + const { chainId } = this.config; + this.configure({ selectedAddress, ipfsGateway, openSeaEnabled }); + this.update({ + collectibleContracts: + allCollectibleContracts[selectedAddress]?.[chainId] || [], + collectibles: allCollectibles[selectedAddress]?.[chainId] || [], + }); + }, + ); onNetworkStateChange(({ provider }) => { const { allCollectibleContracts, allCollectibles } = this.state; diff --git a/src/user/PreferencesController.ts b/src/user/PreferencesController.ts index b0ea94587c..d3493da6d2 100644 --- a/src/user/PreferencesController.ts +++ b/src/user/PreferencesController.ts @@ -47,6 +47,7 @@ export interface PreferencesState extends BaseState { selectedAddress: string; useStaticTokenList: boolean; useCollectibleDetection: boolean; + openSeaEnabled: boolean; } /** @@ -78,6 +79,7 @@ export class PreferencesController extends BaseController< selectedAddress: '', useStaticTokenList: false, useCollectibleDetection: false, + openSeaEnabled: false, }; this.initialize(); } @@ -303,6 +305,15 @@ export class PreferencesController extends BaseController< setUseCollectibleDetection(useCollectibleDetection: boolean) { this.update({ useCollectibleDetection }); } + + /** + * Toggle the opensea enabled setting. + * + * @param useCollectibleDetection - Boolean indicating user preference on using OpenSea's API. + */ + setOpenSeaEnabled(openSeaEnabled: boolean) { + this.update({ openSeaEnabled }); + } } export default PreferencesController; From e36d0f72c7476d93e3820644e889b1ff2036a6ef Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 30 Nov 2021 12:54:07 -0600 Subject: [PATCH 2/4] stop polling when useCollectibleDetection set to false, set useCollectibleDetection to false when openSeaEnabled set to false --- src/assets/CollectibleDetectionController.ts | 3 +++ src/user/PreferencesController.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/assets/CollectibleDetectionController.ts b/src/assets/CollectibleDetectionController.ts index 102e5898ef..93a12945f3 100644 --- a/src/assets/CollectibleDetectionController.ts +++ b/src/assets/CollectibleDetectionController.ts @@ -233,6 +233,9 @@ export class CollectibleDetectionController extends BaseController< this.configure({ selectedAddress, disabled: !useCollectibleDetection }); this.detectCollectibles(); } + if (!useCollectibleDetection) { + this.stop(); + } }); onNetworkStateChange(({ provider }) => { diff --git a/src/user/PreferencesController.ts b/src/user/PreferencesController.ts index d3493da6d2..2ceecb1e0d 100644 --- a/src/user/PreferencesController.ts +++ b/src/user/PreferencesController.ts @@ -313,6 +313,9 @@ export class PreferencesController extends BaseController< */ setOpenSeaEnabled(openSeaEnabled: boolean) { this.update({ openSeaEnabled }); + if (!openSeaEnabled) { + this.update({ useCollectibleDetection: false }); + } } } From fe83e67b1d6f957ffbcb06be9638e9a327d8df7f Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 30 Nov 2021 13:01:48 -0600 Subject: [PATCH 3/4] cleanup --- src/assets/CollectibleDetectionController.ts | 1 + src/user/PreferencesController.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/assets/CollectibleDetectionController.ts b/src/assets/CollectibleDetectionController.ts index 93a12945f3..770bee280c 100644 --- a/src/assets/CollectibleDetectionController.ts +++ b/src/assets/CollectibleDetectionController.ts @@ -233,6 +233,7 @@ export class CollectibleDetectionController extends BaseController< this.configure({ selectedAddress, disabled: !useCollectibleDetection }); this.detectCollectibles(); } + if (!useCollectibleDetection) { this.stop(); } diff --git a/src/user/PreferencesController.ts b/src/user/PreferencesController.ts index 2ceecb1e0d..309f562ad3 100644 --- a/src/user/PreferencesController.ts +++ b/src/user/PreferencesController.ts @@ -309,7 +309,7 @@ export class PreferencesController extends BaseController< /** * Toggle the opensea enabled setting. * - * @param useCollectibleDetection - Boolean indicating user preference on using OpenSea's API. + * @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API. */ setOpenSeaEnabled(openSeaEnabled: boolean) { this.update({ openSeaEnabled }); From 69634ba8ef72bea3f685cae3eb3236e1e43faadb Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 30 Nov 2021 13:11:52 -0600 Subject: [PATCH 4/4] update tests --- src/ComposableController.test.ts | 2 ++ src/assets/CollectibleDetectionController.test.ts | 11 +++++++++++ src/assets/CollectiblesController.test.ts | 5 ++++- src/user/PreferencesController.test.ts | 2 ++ src/user/PreferencesController.ts | 5 +++++ 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ComposableController.test.ts b/src/ComposableController.test.ts index dbd249414a..8daa1f53b7 100644 --- a/src/ComposableController.test.ts +++ b/src/ComposableController.test.ts @@ -166,6 +166,7 @@ describe('ComposableController', () => { selectedAddress: '', useStaticTokenList: false, useCollectibleDetection: false, + openSeaEnabled: false, }, }); }); @@ -236,6 +237,7 @@ describe('ComposableController', () => { selectedAddress: '', useStaticTokenList: false, useCollectibleDetection: false, + openSeaEnabled: false, suggestedAssets: [], tokens: [], }); diff --git a/src/assets/CollectibleDetectionController.test.ts b/src/assets/CollectibleDetectionController.test.ts index 89c8ef095f..6ab5d1a205 100644 --- a/src/assets/CollectibleDetectionController.test.ts +++ b/src/assets/CollectibleDetectionController.test.ts @@ -53,6 +53,7 @@ describe('CollectibleDetectionController', () => { getCollectiblesState: () => collectiblesController.state, }); + preferences.setOpenSeaEnabled(true); preferences.setUseCollectibleDetection(true); nock(OPEN_SEA_HOST) @@ -376,6 +377,16 @@ describe('CollectibleDetectionController', () => { expect(collectiblesController.state.collectibles).toStrictEqual([]); }); + it('should not detect and add collectibles if preferences controller openSeaEnabled is set to false', async () => { + preferences.setOpenSeaEnabled(false); + collectibleDetection.configure({ + networkType: MAINNET, + selectedAddress: '0x9', + }); + collectibleDetection.detectCollectibles(); + expect(collectiblesController.state.collectibles).toStrictEqual([]); + }); + it('should not add collectible if collectible or collectible contract has no information to display', async () => { const collectibleHH2574 = { address: '0xebE4e5E773AFD2bAc25De0cFafa084CFb3cBf1eD', diff --git a/src/assets/CollectiblesController.test.ts b/src/assets/CollectiblesController.test.ts index 55798161f2..02954432e9 100644 --- a/src/assets/CollectiblesController.test.ts +++ b/src/assets/CollectiblesController.test.ts @@ -60,7 +60,10 @@ describe('CollectiblesController', () => { ), }); - preferences.update({ selectedAddress: OWNER_ADDRESS }); + preferences.update({ + selectedAddress: OWNER_ADDRESS, + openSeaEnabled: true, + }); sandbox .stub(collectiblesController, 'isCollectibleOwner' as any) diff --git a/src/user/PreferencesController.test.ts b/src/user/PreferencesController.test.ts index 0daf26c16d..9ae195ea2a 100644 --- a/src/user/PreferencesController.test.ts +++ b/src/user/PreferencesController.test.ts @@ -12,6 +12,7 @@ describe('PreferencesController', () => { selectedAddress: '', useStaticTokenList: false, useCollectibleDetection: false, + openSeaEnabled: false, }); }); @@ -220,6 +221,7 @@ describe('PreferencesController', () => { it('should set useCollectibleDetection', () => { const controller = new PreferencesController(); + controller.setOpenSeaEnabled(true); controller.setUseCollectibleDetection(true); expect(controller.state.useCollectibleDetection).toStrictEqual(true); }); diff --git a/src/user/PreferencesController.ts b/src/user/PreferencesController.ts index 309f562ad3..b19ea66f72 100644 --- a/src/user/PreferencesController.ts +++ b/src/user/PreferencesController.ts @@ -303,6 +303,11 @@ export class PreferencesController extends BaseController< * @param useCollectibleDetection - Boolean indicating user preference on collectible detection. */ setUseCollectibleDetection(useCollectibleDetection: boolean) { + if (useCollectibleDetection && !this.state.openSeaEnabled) { + throw new Error( + 'useCollectibleDetection cannot be enabled if openSeaEnabled is false', + ); + } this.update({ useCollectibleDetection }); }