Skip to content

Commit

Permalink
Add preference to enable/disable collectible detection (#638)
Browse files Browse the repository at this point in the history
* add preference to enable/disable collectible detection

* update tests
  • Loading branch information
adonesky1 authored and MajorLift committed Oct 11, 2023
1 parent 625a731 commit 775c55b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('ComposableController', () => {
lostIdentities: {},
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
},
});
});
Expand Down Expand Up @@ -234,6 +235,7 @@ describe('ComposableController', () => {
provider: { type: 'mainnet', chainId: NetworksChainId.mainnet },
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
suggestedAssets: [],
tokens: [],
});
Expand Down
16 changes: 15 additions & 1 deletion src/assets/CollectibleDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ describe('CollectibleDetectionController', () => {
getCollectiblesState: () => collectiblesController.state,
});

preferences.setUseCollectibleDetection(true);

nock(OPEN_SEA_HOST)
.get(`${OPEN_SEA_PATH}/assets?owner=0x2&offset=0&limit=50`)
.reply(200, {
Expand Down Expand Up @@ -191,10 +193,12 @@ describe('CollectibleDetectionController', () => {
});

it('should set default config', () => {
preferences.setUseCollectibleDetection(false);
expect(collectibleDetection.config).toStrictEqual({
interval: DEFAULT_INTERVAL,
networkType: 'mainnet',
selectedAddress: '',
disabled: true,
});
});

Expand All @@ -219,8 +223,8 @@ describe('CollectibleDetectionController', () => {
},
{ interval: 10 },
);
collectiblesDetectionController.configure({ disabled: false });
collectiblesDetectionController.start();

expect(mockCollectibles.calledOnce).toBe(true);
setTimeout(() => {
expect(mockCollectibles.calledTwice).toBe(true);
Expand Down Expand Up @@ -360,6 +364,16 @@ describe('CollectibleDetectionController', () => {
expect(collectiblesController.state.collectibles).toStrictEqual([]);
});

it('should not detect and add collectibles if preferences controller useCollectibleDetection is set to false', async () => {
preferences.setUseCollectibleDetection(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
16 changes: 12 additions & 4 deletions src/assets/CollectibleDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,21 @@ export class CollectibleDetectionController extends BaseController<
interval: DEFAULT_INTERVAL,
networkType: MAINNET,
selectedAddress: '',
disabled: true,
};
this.initialize();
this.getCollectiblesState = getCollectiblesState;
onPreferencesStateChange(({ selectedAddress }) => {
const actualSelectedAddress = this.config.selectedAddress;
if (selectedAddress !== actualSelectedAddress) {
this.configure({ selectedAddress });
onPreferencesStateChange(({ selectedAddress, useCollectibleDetection }) => {
const {
selectedAddress: previouslySelectedAddress,
disabled,
} = this.config;

if (
selectedAddress !== previouslySelectedAddress ||
!useCollectibleDetection !== disabled
) {
this.configure({ selectedAddress, disabled: !useCollectibleDetection });
this.detectCollectibles();
}
});
Expand Down
7 changes: 7 additions & 0 deletions src/user/PreferencesController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('PreferencesController', () => {
lostIdentities: {},
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
});
});

Expand Down Expand Up @@ -216,4 +217,10 @@ describe('PreferencesController', () => {
controller.setUseStaticTokenList(true);
expect(controller.state.useStaticTokenList).toStrictEqual(true);
});

it('should set useCollectibleDetection', () => {
const controller = new PreferencesController();
controller.setUseCollectibleDetection(true);
expect(controller.state.useCollectibleDetection).toStrictEqual(true);
});
});
13 changes: 12 additions & 1 deletion src/user/PreferencesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface PreferencesState extends BaseState {
lostIdentities: { [address: string]: ContactEntry };
selectedAddress: string;
useStaticTokenList: boolean;
useCollectibleDetection: boolean;
}

/**
Expand Down Expand Up @@ -76,6 +77,7 @@ export class PreferencesController extends BaseController<
lostIdentities: {},
selectedAddress: '',
useStaticTokenList: false,
useCollectibleDetection: false,
};
this.initialize();
}
Expand Down Expand Up @@ -287,11 +289,20 @@ export class PreferencesController extends BaseController<
/**
* Toggle the token detection setting to use dynamic token list.
*
* @param useStaticTokenList - IPFS gateway string.
* @param useStaticTokenList - Boolean indicating user preference on token detection.
*/
setUseStaticTokenList(useStaticTokenList: boolean) {
this.update({ useStaticTokenList });
}

/**
* Toggle the collectible detection setting.
*
* @param useCollectibleDetection - Boolean indicating user preference on collectible detection.
*/
setUseCollectibleDetection(useCollectibleDetection: boolean) {
this.update({ useCollectibleDetection });
}
}

export default PreferencesController;

0 comments on commit 775c55b

Please sign in to comment.