Skip to content
64 changes: 63 additions & 1 deletion src/AssetsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ describe('AssetsController', () => {
allTokens: {},
collectibleContracts: [],
collectibles: [],
ignoredCollectibles: [],
ignoredTokens: [],
suggestedAssets: [],
tokens: []
});
Expand Down Expand Up @@ -175,10 +177,12 @@ describe('AssetsController', () => {
});
});

it('should remove token', async () => {
it('should remove token and add it to the ignoredTokenList', async () => {
await assetsController.addToken('foo', 'bar', 2);
expect(assetsController.state.ignoredTokens.length).toBe(0);
assetsController.removeToken('0xfoO');
expect(assetsController.state.tokens.length).toBe(0);
expect(assetsController.state.ignoredTokens.length).toBe(1);
});

it('should remove token by selected address', async () => {
Expand Down Expand Up @@ -523,4 +527,62 @@ describe('AssetsController', () => {
});
});
});

it('should not add duplicate tokens to the ignoredToken list', async () => {
await assetsController.addToken('0xfoO', 'bar', 2);
await assetsController.addToken('0xfAA', 'bar', 3);
expect(assetsController.state.ignoredTokens.length).toBe(0);
expect(assetsController.state.tokens.length).toBe(2);
assetsController.removeToken('0xfoO');
expect(assetsController.state.tokens.length).toBe(1);
expect(assetsController.state.ignoredTokens.length).toBe(1);
await assetsController.addToken('0xfoO', 'bar', 2);
expect(assetsController.state.ignoredTokens.length).toBe(1);
assetsController.removeToken('0xfoO');
expect(assetsController.state.ignoredTokens.length).toBe(1);
});

it('should not add duplicate collectibles to the ignoredCollectibles list', async () => {
await assetsController.addCollectible('foo', 1, { name: 'name', image: 'image', description: 'description' });
await assetsController.addCollectible('foo', 2, { name: 'name', image: 'image', description: 'description' });

expect(assetsController.state.collectibles.length).toBe(2);
expect(assetsController.state.ignoredCollectibles.length).toBe(0);

assetsController.removeCollectible('0xfoO', 1);
expect(assetsController.state.collectibles.length).toBe(1);
expect(assetsController.state.ignoredCollectibles.length).toBe(1);

await assetsController.addCollectible('foo', 1, { name: 'name', image: 'image', description: 'description' });
expect(assetsController.state.collectibles.length).toBe(2);
expect(assetsController.state.ignoredCollectibles.length).toBe(1);

assetsController.removeCollectible('0xfoO', 1);
expect(assetsController.state.collectibles.length).toBe(1);
expect(assetsController.state.ignoredCollectibles.length).toBe(1);
});

it('should be able to clear the ignoredToken list', async () => {
await assetsController.addToken('0xfoO', 'bar', 2);
expect(assetsController.state.ignoredTokens.length).toBe(0);
assetsController.removeToken('0xfoO');
expect(assetsController.state.tokens.length).toBe(0);
expect(assetsController.state.ignoredTokens.length).toBe(1);
assetsController.clearIgnoredTokens();
expect(assetsController.state.ignoredTokens.length).toBe(0);
});

it('should be able to clear the ignoredCollectibles list', async () => {
await assetsController.addCollectible('0xfoO', 1, { name: 'name', image: 'image', description: 'description' });

expect(assetsController.state.collectibles.length).toBe(1);
expect(assetsController.state.ignoredCollectibles.length).toBe(0);

assetsController.removeCollectible('0xfoO', 1);
expect(assetsController.state.collectibles.length).toBe(0);
expect(assetsController.state.ignoredCollectibles.length).toBe(1);

assetsController.clearIgnoredCollectibles();
expect(assetsController.state.ignoredCollectibles.length).toBe(0);
});
});
57 changes: 49 additions & 8 deletions src/AssetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@ export interface SuggestedAssetMeta {
* @property collectibles - List of collectibles associated with the active vault
* @property suggestedAssets - List of suggested assets associated with the active vault
* @property tokens - List of tokens associated with the active vault
* @property ignoredTokens - List of tokens that should be ignored
* @property ignoredCollectibles - List of collectibles that should be ignored
*/
export interface AssetsState extends BaseState {
allTokens: { [key: string]: { [key: string]: Token[] } };
allCollectibleContracts: { [key: string]: { [key: string]: CollectibleContract[] } };
allCollectibles: { [key: string]: { [key: string]: Collectible[] } };
collectibleContracts: CollectibleContract[];
collectibles: Collectible[];
ignoredTokens: Token[];
ignoredCollectibles: Collectible[];
suggestedAssets: SuggestedAssetMeta[];
tokens: Token[];
}
Expand Down Expand Up @@ -417,15 +421,28 @@ export class AssetsController extends BaseController<AssetsConfig, AssetsState>
*/
private removeIndividualCollectible(address: string, tokenId: number) {
address = toChecksumAddress(address);
const { allCollectibles, collectibles } = this.state;
const { allCollectibles, collectibles, ignoredCollectibles } = this.state;
const { networkType, selectedAddress } = this.config;
const newCollectibles = collectibles.filter(
(collectible) => !(collectible.address === address && collectible.tokenId === tokenId)
);
const newIgnoredCollectibles = [...ignoredCollectibles];
const newCollectibles = collectibles.filter((collectible) => {
if (collectible.address === address && collectible.tokenId === tokenId) {
const alreadyIgnored = newIgnoredCollectibles.find(
(c) => c.address === address && c.tokenId === tokenId
);
!alreadyIgnored && newIgnoredCollectibles.push(collectible);
return false;
}
return true;
});

const addressCollectibles = allCollectibles[selectedAddress];
const newAddressCollectibles = { ...addressCollectibles, ...{ [networkType]: newCollectibles } };
const newAllCollectibles = { ...allCollectibles, ...{ [selectedAddress]: newAddressCollectibles } };
this.update({ allCollectibles: newAllCollectibles, collectibles: newCollectibles });
this.update({
allCollectibles: newAllCollectibles,
collectibles: newCollectibles,
ignoredCollectibles: newIgnoredCollectibles
});
}

/**
Expand Down Expand Up @@ -490,6 +507,8 @@ export class AssetsController extends BaseController<AssetsConfig, AssetsState>
allTokens: {},
collectibleContracts: [],
collectibles: [],
ignoredCollectibles: [],
ignoredTokens: [],
suggestedAssets: [],
tokens: []
};
Expand Down Expand Up @@ -651,13 +670,21 @@ export class AssetsController extends BaseController<AssetsConfig, AssetsState>
*/
removeToken(address: string) {
address = toChecksumAddress(address);
const { allTokens, tokens } = this.state;
const { allTokens, tokens, ignoredTokens } = this.state;
const { networkType, selectedAddress } = this.config;
const newTokens = tokens.filter((token) => token.address !== address);
const newIgnoredTokens = [...ignoredTokens];
const newTokens = tokens.filter((token) => {
if (token.address === address) {
const alreadyIgnored = newIgnoredTokens.find((t) => t.address === address);
!alreadyIgnored && newIgnoredTokens.push(token);
return false;
}
return true;
});
const addressTokens = allTokens[selectedAddress];
const newAddressTokens = { ...addressTokens, ...{ [networkType]: newTokens } };
const newAllTokens = { ...allTokens, ...{ [selectedAddress]: newAddressTokens } };
this.update({ allTokens: newAllTokens, tokens: newTokens });
this.update({ allTokens: newAllTokens, tokens: newTokens, ignoredTokens: newIgnoredTokens });
}

/**
Expand All @@ -676,6 +703,20 @@ export class AssetsController extends BaseController<AssetsConfig, AssetsState>
}
}

/**
* Removes all tokens from the ignored list
*/
clearIgnoredTokens() {
this.update({ ignoredTokens: [] });
}

/**
* Removes all collectibles from the ignored list
*/
clearIgnoredCollectibles() {
this.update({ ignoredCollectibles: [] });
}

/**
* Extension point called if and when this controller is composed
* with other controllers using a ComposableController
Expand Down
45 changes: 44 additions & 1 deletion src/AssetsDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PreferencesController } from './PreferencesController';
import { ComposableController } from './ComposableController';
import { AssetsController } from './AssetsController';
import { AssetsContractController } from './AssetsContractController';
import { getOnce } from 'fetch-mock';
import { getOnce, get } from 'fetch-mock';
import { stub } from 'sinon';

const BN = require('ethereumjs-util').BN;
Expand Down Expand Up @@ -46,6 +46,26 @@ describe('AssetsDetectionController', () => {
{ overwriteRoutes: true, method: 'GET' }
);

get(
OPEN_SEA_API + 'assets?owner=0x2',
() => ({
body: JSON.stringify({
assets: [
{
asset_contract: {
address: '0x1d963688fe2209a98db35c67a041524822cf04ff'
},
description: 'Description 2577',
image_preview_url: 'image/2577.png',
name: 'ID 2577',
token_id: '2577'
}
]
})
}),
{ overwriteRoutes: true, method: 'GET' }
);

getOnce(
OPEN_SEA_API + 'assets?owner=0x1',
() => ({
Expand Down Expand Up @@ -210,6 +230,17 @@ describe('AssetsDetectionController', () => {
]);
});

it('should not autodetect collectibles that exist in the ignoreList', async () => {
assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x2' });
await assetsDetection.detectCollectibles();
expect(assets.state.collectibles.length).toEqual(1);
expect(assets.state.ignoredCollectibles.length).toEqual(0);
assets.removeCollectible('0x1d963688fe2209a98db35c67a041524822cf04ff', 2577);
await assetsDetection.detectCollectibles();
expect(assets.state.collectibles.length).toEqual(0);
expect(assets.state.ignoredCollectibles.length).toEqual(1);
});

it('should not detect and add collectibles if there is no selectedAddress', async () => {
assetsDetection.configure({ networkType: MAINNET });
await assetsDetection.detectCollectibles();
Expand Down Expand Up @@ -358,6 +389,18 @@ describe('AssetsDetectionController', () => {
]);
});

it('should not autodetect tokens that exist in the ignoreList', async () => {
assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x1' });
sandbox
.stub(assetsContract, 'getBalancesInSingleCall')
.returns({ '0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1) });
await assetsDetection.detectTokens();

assets.removeToken('0x6810e776880C02933D47DB1b9fc05908e5386b96');
await assetsDetection.detectTokens();
expect(assets.state.tokens).toEqual([]);
});

it('should not detect tokens if there is no selectedAddress set', async () => {
assetsDetection.configure({ networkType: MAINNET });
sandbox
Expand Down
51 changes: 36 additions & 15 deletions src/AssetsDetectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,20 @@ export class AssetsDetectionController extends BaseController<AssetsDetectionCon
}
const balances = await assetsContractController.getBalancesInSingleCall(selectedAddress, tokensToDetect);
const assetsController = this.context.AssetsController as AssetsController;
const { ignoredTokens } = assetsController.state;
for (const tokenAddress in balances) {
await assetsController.addToken(
tokenAddress,
contractMap[tokenAddress].symbol,
contractMap[tokenAddress].decimals
);
let ignored;
/* istanbul ignore else */
if (ignoredTokens.length) {
ignored = ignoredTokens.find((token) => token.address === tokenAddress);
}
if (!ignored) {
await assetsController.addToken(
tokenAddress,
contractMap[tokenAddress].symbol,
contractMap[tokenAddress].decimals
);
}
}
}

Expand All @@ -186,6 +194,7 @@ export class AssetsDetectionController extends BaseController<AssetsDetectionCon
return;
}
const assetsController = this.context.AssetsController as AssetsController;
const { ignoredCollectibles } = assetsController.state;
const collectibles = await this.getOwnerCollectibles();
const addCollectiblesPromises = collectibles.map(async (collectible: ApiCollectibleResponse) => {
const {
Expand All @@ -195,16 +204,28 @@ export class AssetsDetectionController extends BaseController<AssetsDetectionCon
description,
asset_contract: { address }
} = collectible;
await assetsController.addCollectible(
address,
Number(token_id),
{
description,
image: image_preview_url,
name
},
true
);

let ignored;
/* istanbul ignore else */
if (ignoredCollectibles.length) {
ignored = ignoredCollectibles.find((c) => {
/* istanbul ignore next */
return c.address === address && c.tokenId === Number(token_id);
});
}
/* istanbul ignore else */
if (!ignored) {
await assetsController.addCollectible(
address,
Number(token_id),
{
description,
image: image_preview_url,
name
},
true
);
}
});
await Promise.all(addCollectiblesPromises);
}
Expand Down
6 changes: 6 additions & 0 deletions src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('ComposableController', () => {
allTokens: {},
collectibleContracts: [],
collectibles: [],
ignoredCollectibles: [],
ignoredTokens: [],
suggestedAssets: [],
tokens: []
},
Expand Down Expand Up @@ -76,6 +78,8 @@ describe('ComposableController', () => {
featureFlags: {},
frequentRpcList: [],
identities: {},
ignoredCollectibles: [],
ignoredTokens: [],
lostIdentities: {},
nativeCurrency: 'eth',
network: 'loading',
Expand Down Expand Up @@ -114,6 +118,8 @@ describe('ComposableController', () => {
featureFlags: {},
frequentRpcList: [],
identities: {},
ignoredCollectibles: [],
ignoredTokens: [],
lostIdentities: {},
nativeCurrency: 'eth',
network: 'loading',
Expand Down