Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b83f9bc
controller to fetch the token list from metaswaps api
NiranjanaBinoy May 26, 2021
3c9e6f0
integrating TokenListcontroller with AssetsDetectioncontroller and test
NiranjanaBinoy May 27, 2021
eb4c114
fixing lint error
NiranjanaBinoy May 27, 2021
f9ad337
updating the tests
NiranjanaBinoy May 28, 2021
bb1040c
fixing fetchOptions.headers check in TokenListController and check fo…
NiranjanaBinoy May 31, 2021
720eac3
separating the api interaction from controller
NiranjanaBinoy Jun 1, 2021
62ca7b4
fixing the lint and test errors after the rebase
NiranjanaBinoy Jun 3, 2021
07227be
fixing the type for defaultstate from any
NiranjanaBinoy Jun 4, 2021
8f76dbf
exporting the controller
NiranjanaBinoy Jun 5, 2021
bedcbd3
adding new api end points and related functions
NiranjanaBinoy Jun 16, 2021
aeed783
change in fetchtokenList logic and removing topAssets endpoint and re…
NiranjanaBinoy Jun 16, 2021
bfa6485
changing the state variable from to to avoid overwriting in extension
NiranjanaBinoy Jun 22, 2021
239e12c
adding cache and removing tokens with less than 2 occurance and symbo…
NiranjanaBinoy Jun 25, 2021
f5a1209
removing console.log
NiranjanaBinoy Jun 25, 2021
be997b5
updating cache on sync and updation if data avalable is undefined, fe…
NiranjanaBinoy Jun 27, 2021
5b7f953
updating DEFAULT_INTERVAL to 1 hour and DEFAULT_THRESHOLD to 30 minutes
NiranjanaBinoy Jun 27, 2021
57a1799
fixing test failure
NiranjanaBinoy Jun 27, 2021
084cae7
fixing duplicate symbol filtering and adding related test cases
NiranjanaBinoy Jun 28, 2021
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
Prev Previous commit
Next Next commit
integrating TokenListcontroller with AssetsDetectioncontroller and test
  • Loading branch information
NiranjanaBinoy committed Jun 17, 2021
commit 3c9e6f0bc42a9db347223a53d93cee9b2ecb2ccc
43 changes: 36 additions & 7 deletions src/assets/AssetsDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import {
NetworksChainId,
} from '../network/NetworkController';
import { PreferencesController } from '../user/PreferencesController';
import { ControllerMessenger } from '../ControllerMessenger';
import { AssetsController } from './AssetsController';
import { AssetsContractController } from './AssetsContractController';
import { AssetsDetectionController } from './AssetsDetectionController';
import {
TokenListController,
GetTokenListState,
TokenListStateChange,
} from './TokenListController';

const DEFAULT_INTERVAL = 180000;
const MAINNET = 'mainnet';
Expand All @@ -18,19 +24,35 @@ const TOKENS = [{ address: '0xfoO', symbol: 'bar', decimals: 2 }];
const OPEN_SEA_HOST = 'https://api.opensea.io';
const OPEN_SEA_PATH = '/api/v1';

function getTokenListMessenger() {
const controllerMessenger = new ControllerMessenger<
GetTokenListState,
TokenListStateChange
>();
const messenger = controllerMessenger.getRestricted<
'TokenListController',
never,
never
>({
name: 'TokenListController',
});
return messenger;
}

describe('AssetsDetectionController', () => {
let assetsDetection: AssetsDetectionController;
let preferences: PreferencesController;
let network: NetworkController;
let assets: AssetsController;
let tokenList: TokenListController;
let assetsContract: AssetsContractController;
let getBalancesInSingleCall: SinonStub<
Parameters<AssetsContractController['getBalancesInSingleCall']>,
ReturnType<AssetsContractController['getBalancesInSingleCall']>
>;
const sandbox = createSandbox();

beforeEach(() => {
beforeEach(async () => {
preferences = new PreferencesController();
network = new NetworkController();
assetsContract = new AssetsContractController();
Expand All @@ -43,6 +65,9 @@ describe('AssetsDetectionController', () => {
assetsContract,
),
});
const messenger = getTokenListMessenger();
tokenList = new TokenListController({ messenger });
await tokenList.start();
getBalancesInSingleCall = sandbox.stub();
assetsDetection = new AssetsDetectionController({
onAssetsStateChange: (listener) => assets.subscribe(listener),
Expand All @@ -53,6 +78,7 @@ describe('AssetsDetectionController', () => {
addTokens: assets.addTokens.bind(assets),
addCollectible: assets.addCollectible.bind(assets),
getAssetsState: () => assets.state,
getTokenListState: () => tokenList.state,
});

nock(OPEN_SEA_HOST)
Expand Down Expand Up @@ -153,6 +179,7 @@ describe('AssetsDetectionController', () => {
afterEach(() => {
nock.cleanAll();
sandbox.reset();
tokenList.destroy();
});

it('should set default config', () => {
Expand Down Expand Up @@ -187,6 +214,7 @@ describe('AssetsDetectionController', () => {
addTokens: assets.addTokens.bind(assets),
addCollectible: assets.addCollectible.bind(assets),
getAssetsState: () => assets.state,
getTokenListState: () => tokenList.state,
},
{ interval: 10 },
);
Expand Down Expand Up @@ -232,6 +260,7 @@ describe('AssetsDetectionController', () => {
addTokens: assets.addTokens.bind(assets),
addCollectible: assets.addCollectible.bind(assets),
getAssetsState: () => assets.state,
getTokenListState: () => tokenList.state,
},
{ interval: 10, networkType: ROPSTEN },
);
Expand Down Expand Up @@ -447,15 +476,15 @@ describe('AssetsDetectionController', () => {
it('should detect tokens correctly', async () => {
assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x1' });
getBalancesInSingleCall.resolves({
'0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1),
'0x514910771af9ca656af840dff83e8264ecf986ca': new BN(1),
});
await assetsDetection.detectTokens();
expect(assets.state.tokens).toStrictEqual([
{
address: '0x6810e776880C02933D47DB1b9fc05908e5386b96',
address: '0x514910771AF9Ca656af840dff83E8264EcF986CA',
symbol: 'LINK',
decimals: 18,
image: undefined,
symbol: 'GNO',
},
]);
});
Expand Down Expand Up @@ -525,19 +554,19 @@ describe('AssetsDetectionController', () => {
it('should not autodetect tokens that exist in the ignoreList', async () => {
assetsDetection.configure({ networkType: MAINNET, selectedAddress: '0x1' });
getBalancesInSingleCall.resolves({
'0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1),
'0x514910771af9ca656af840dff83e8264ecf986ca': new BN(1),
});
await assetsDetection.detectTokens();

assets.removeAndIgnoreToken('0x6810e776880C02933D47DB1b9fc05908e5386b96');
assets.removeAndIgnoreToken('0x514910771af9ca656af840dff83e8264ecf986ca');
await assetsDetection.detectTokens();
expect(assets.state.tokens).toStrictEqual([]);
});

it('should not detect tokens if there is no selectedAddress set', async () => {
assetsDetection.configure({ networkType: MAINNET });
getBalancesInSingleCall.resolves({
'0x6810e776880C02933D47DB1b9fc05908e5386b96': new BN(1),
'0x514910771af9ca656af840dff83e8264ecf986ca': new BN(1),
});
await assetsDetection.detectTokens();
expect(assets.state.tokens).toStrictEqual([]);
Expand Down
17 changes: 11 additions & 6 deletions src/assets/AssetsDetectionController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contractMap from '@metamask/contract-metadata';
import BaseController, { BaseConfig, BaseState } from '../BaseController';
import type { NetworkState, NetworkType } from '../network/NetworkController';
import type { PreferencesState } from '../user/PreferencesController';
Expand All @@ -11,6 +10,7 @@ import type {
} from './AssetsController';
import type { AssetsContractController } from './AssetsContractController';
import { Token } from './TokenRatesController';
import { TokenListState } from './TokenListController';

const DEFAULT_INTERVAL = 180000;

Expand Down Expand Up @@ -182,6 +182,8 @@ export class AssetsDetectionController extends BaseController<

private getAssetsState: () => AssetsState;

private getTokenListState: () => TokenListState;

/**
* Creates a AssetsDetectionController instance
*
Expand All @@ -207,6 +209,7 @@ export class AssetsDetectionController extends BaseController<
addTokens,
addCollectible,
getAssetsState,
getTokenListState,
}: {
onAssetsStateChange: (
listener: (assetsState: AssetsState) => void,
Expand All @@ -222,6 +225,7 @@ export class AssetsDetectionController extends BaseController<
addTokens: AssetsController['addTokens'];
addCollectible: AssetsController['addCollectible'];
getAssetsState: () => AssetsState;
getTokenListState: () => TokenListState;
},
config?: Partial<AssetsDetectionConfig>,
state?: Partial<BaseState>,
Expand All @@ -235,6 +239,7 @@ export class AssetsDetectionController extends BaseController<
};
this.initialize();
this.getAssetsState = getAssetsState;
this.getTokenListState = getTokenListState;
this.addTokens = addTokens;
onAssetsStateChange(({ tokens }) => {
this.configure({ tokens });
Expand Down Expand Up @@ -304,10 +309,10 @@ export class AssetsDetectionController extends BaseController<
const tokensAddresses = this.config.tokens.map(
/* istanbul ignore next*/ (token) => token.address,
);
const { tokens } = this.getTokenListState();
const tokensToDetect: string[] = [];
for (const address in contractMap) {
const contract = contractMap[address];
if (contract.erc20 && !tokensAddresses.includes(address)) {
for (const address in tokens) {
if (!(address in tokensAddresses)) {
tokensToDetect.push(address);
}
}
Expand Down Expand Up @@ -335,8 +340,8 @@ export class AssetsDetectionController extends BaseController<
if (!ignored) {
tokensToAdd.push({
address: tokenAddress,
decimals: contractMap[tokenAddress].decimals,
symbol: contractMap[tokenAddress].symbol,
decimals: tokens[tokenAddress].decimals,
symbol: tokens[tokenAddress].symbol,
});
}
}
Expand Down
17 changes: 3 additions & 14 deletions src/assets/TokenListController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Patch } from 'immer';
import nock from 'nock';
import { ControllerMessenger } from '../ControllerMessenger';
import {
Expand All @@ -9,22 +8,12 @@ import {

const name = 'TokenListController';

type OtherStateChange = {
type: `OtherController:stateChange`;
payload: [{ stuff: string }, Patch[]];
};

type GetOtherState = {
type: `OtherController:getState`;
handler: () => { stuff: string };
};

function getRestrictedMessenger() {
// The 'Other' types are included to demonstrate that this all works with a
// controller messenger that includes types from other controllers.
const controllerMessenger = new ControllerMessenger<
GetTokenListState | GetOtherState,
TokenListStateChange | OtherStateChange
GetTokenListState,
TokenListStateChange
>();
const messenger = controllerMessenger.getRestricted<
'TokenListController',
Expand All @@ -48,8 +37,8 @@ describe('TokenListController', () => {
messenger,
});
await controller.start();
expect(Object.keys(controller.state.tokens).length).toBeGreaterThan(0);
// controller.stop();
controller.destroy();

});
});
Loading