Skip to content

Commit

Permalink
add openSeaEnabled preference (#645)
Browse files Browse the repository at this point in the history
* add openSeaEnabled preference

* stop polling when useCollectibleDetection set to false, set useCollectibleDetection to false when openSeaEnabled set to false

* cleanup

* update tests
  • Loading branch information
adonesky1 authored Nov 30, 2021
1 parent 064a9a0 commit dd23b43
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ describe('ComposableController', () => {
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
openSeaEnabled: false,
},
});
});
Expand Down Expand Up @@ -236,6 +237,7 @@ describe('ComposableController', () => {
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
openSeaEnabled: false,
suggestedAssets: [],
tokens: [],
});
Expand Down
11 changes: 11 additions & 0 deletions src/assets/CollectibleDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('CollectibleDetectionController', () => {
getCollectiblesState: () => collectiblesController.state,
});

preferences.setOpenSeaEnabled(true);
preferences.setUseCollectibleDetection(true);

nock(OPEN_SEA_HOST)
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions src/assets/CollectibleDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ export class CollectibleDetectionController extends BaseController<
this.configure({ selectedAddress, disabled: !useCollectibleDetection });
this.detectCollectibles();
}

if (!useCollectibleDetection) {
this.stop();
}
});

onNetworkStateChange(({ provider }) => {
Expand Down
5 changes: 4 additions & 1 deletion src/assets/CollectiblesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ describe('CollectiblesController', () => {
),
});

preferences.update({ selectedAddress: OWNER_ADDRESS });
preferences.update({
selectedAddress: OWNER_ADDRESS,
openSeaEnabled: true,
});

sandbox
.stub(collectiblesController, 'isCollectibleOwner' as any)
Expand Down
52 changes: 31 additions & 21 deletions src/assets/CollectiblesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface CollectiblesConfig extends BaseConfig {
selectedAddress: string;
chainId: string;
ipfsGateway: string;
openSeaEnabled: boolean;
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -787,6 +794,7 @@ export class CollectiblesController extends BaseController<
selectedAddress: '',
chainId: '',
ipfsGateway: IPFS_DEFAULT_GATEWAY_URL,
openSeaEnabled: false,
};

this.defaultState = {
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/user/PreferencesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('PreferencesController', () => {
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
openSeaEnabled: false,
});
});

Expand Down Expand Up @@ -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);
});
Expand Down
19 changes: 19 additions & 0 deletions src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface PreferencesState extends BaseState {
selectedAddress: string;
useStaticTokenList: boolean;
useCollectibleDetection: boolean;
openSeaEnabled: boolean;
}

/**
Expand Down Expand Up @@ -78,6 +79,7 @@ export class PreferencesController extends BaseController<
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
openSeaEnabled: false,
};
this.initialize();
}
Expand Down Expand Up @@ -301,8 +303,25 @@ 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 });
}

/**
* Toggle the opensea enabled setting.
*
* @param openSeaEnabled - Boolean indicating user preference on using OpenSea's API.
*/
setOpenSeaEnabled(openSeaEnabled: boolean) {
this.update({ openSeaEnabled });
if (!openSeaEnabled) {
this.update({ useCollectibleDetection: false });
}
}
}

export default PreferencesController;

0 comments on commit dd23b43

Please sign in to comment.