Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate token list controller data #527

Merged
merged 30 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1607dc5
Add utils function to convert contract metadata logo to iconUrl. Retu…
Cal-L Jul 19, 2021
04151ac
Provide ContractMap type for legacy tokens. Update TokenListControlle…
Cal-L Jul 19, 2021
1b69240
Provide root path for TokenListController. NOTE: Static tokens will r…
Cal-L Jul 21, 2021
ea8b8a1
Export Token as TokenListToken from TokenListController to prevent co…
Cal-L Jul 21, 2021
0a4cad4
Clean up TokenListController. Update static logo path.
Cal-L Jul 22, 2021
d5d6588
Clean up TokenListController. Fix TokenListController tests.
Cal-L Jul 23, 2021
bccf433
Merge branch 'main' of https://github.com/MetaMask/controllers into i…
Cal-L Jul 23, 2021
03be70f
Fix github actions linting
Cal-L Jul 23, 2021
3b1caaa
Fix more github actions linting
Cal-L Jul 23, 2021
febf28f
Fix even more github actions linting
Cal-L Jul 23, 2021
5fe61f9
Fix last github actions linting
Cal-L Jul 23, 2021
905a878
Allow for more time for token list controller API calls before timeout
Cal-L Jul 27, 2021
b02ee36
Merge branch 'main' of https://github.com/MetaMask/controllers into i…
Cal-L Jul 27, 2021
6c06471
Fix linting error
Cal-L Jul 27, 2021
5a47cab
Add back newline to EOF package.json
Cal-L Jul 27, 2021
93c879c
Update contract metadata version
Cal-L Jul 28, 2021
57ab2fb
Merge in main
Cal-L Jul 28, 2021
d422e1e
Up token list timeout to 10s
Cal-L Jul 28, 2021
6ef6fbe
Update split logic
Cal-L Jul 29, 2021
5d7c648
Merge in main. Fix tests.
Cal-L Jul 29, 2021
b90f0e2
Add getFileExt utility and test
rickycodes Jul 30, 2021
cc831ec
Add back caret to contract-metadata
Cal-L Jul 30, 2021
3976e97
Merge branch 'improvement/consolidate-token-list-controller-data' of …
Cal-L Jul 30, 2021
74bc433
Merge branch 'main' of https://github.com/MetaMask/controllers into i…
Cal-L Jul 30, 2021
caccfe1
Update description for fetchFromCache function
Cal-L Aug 3, 2021
4826446
Remove iconPath
Cal-L Aug 3, 2021
ae43e83
Merge branch 'main' of https://github.com/MetaMask/controllers into i…
Cal-L Aug 5, 2021
989f4c0
Merge branch 'main' of https://github.com/MetaMask/controllers into i…
Cal-L Aug 5, 2021
ea7d8dd
Remove unused enum & comment
Cal-L Aug 5, 2021
dd35db9
Merge branch 'main' into improvement/consolidate-token-list-controlle…
Gudahtt Aug 5, 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
6 changes: 5 additions & 1 deletion src/apis/token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ function getTokenMetadataURL(chainId: string, tokenAddress: string) {
return `${END_POINT}/token/${chainId}?address=${tokenAddress}`;
}

// Token list averages 1.6 MB in size
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
// timeoutFetch by default has a 500ms timeout, which will almost always timeout given the response size.
const timeout = 10000;

/**
* Fetches the list of token metadata for a given network chainId
*
Expand Down Expand Up @@ -61,7 +65,7 @@ async function queryApi(apiURL: string): Promise<Response> {
};
fetchOptions.headers = new window.Headers();
fetchOptions.headers.set('Content-Type', 'application/json');
return await timeoutFetch(apiURL, fetchOptions);
return await timeoutFetch(apiURL, fetchOptions, timeout);
}

/**
Expand Down
98 changes: 39 additions & 59 deletions src/assets/TokenListController.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stub } from 'sinon';
import nock from 'nock';
import contractmap from '@metamask/contract-metadata';
import contractMap from '@metamask/contract-metadata';
import { ControllerMessenger } from '../ControllerMessenger';
import {
NetworkController,
Expand All @@ -11,17 +11,27 @@ import {
TokenListController,
TokenListStateChange,
GetTokenListState,
TokenListMap,
ContractMap,
} from './TokenListController';

const name = 'TokenListController';
const TOKEN_END_POINT_API = 'https://token-api.airswap-prod.codefi.network';
const timestamp = Date.now();

const staticTokenList: any = {};
for (const tokenAddress in contractmap) {
const { erc20, logo, ...token } = contractmap[tokenAddress];
const staticTokenList: TokenListMap = {};
for (const tokenAddress in contractMap) {
const { erc20, logo: filePath, ...token } = (contractMap as ContractMap)[
tokenAddress
];
if (erc20) {
staticTokenList[tokenAddress] = { ...token, iconUrl: logo };
staticTokenList[tokenAddress] = {
...token,
address: tokenAddress,
iconUrl: filePath,
occurrences: null,
aggregators: null,
};
}
}
const sampleMainnetTokenList = [
Expand Down Expand Up @@ -90,48 +100,6 @@ const sampleMainnetTokenList = [
},
];
const sampleWithDuplicateSymbols = [
{
address: '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f',
symbol: 'SNX',
decimals: 18,
occurrences: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Synthetix',
iconUrl: 'https://airswap-token-images.s3.amazonaws.com/SNX.png',
},
{
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'SNX',
decimals: 18,
occurrences: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Chainlink',
iconUrl: 'https://s3.amazonaws.com/airswap-token-images/LINK.png',
},
{
address: '0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c',
symbol: 'BNT',
Expand Down Expand Up @@ -185,15 +153,6 @@ const sampleWithLessThan2Occurences = [
name: 'Chainlink',
iconUrl: 'https://s3.amazonaws.com/airswap-token-images/LINK.png',
},
{
address: '0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c',
symbol: 'BNT',
decimals: 18,
occurrences: 1,
aggregators: ['paraswap'],
name: 'Bancor',
iconUrl: 'https://s3.amazonaws.com/airswap-token-images/BNT.png',
},
];
const sampleBinanceTokenList = [
{
Expand Down Expand Up @@ -716,7 +675,14 @@ describe('TokenListController', () => {
});
expect(controller.state).toStrictEqual(existingState);
await controller.start();
expect(controller.state).toStrictEqual(sampleSingleChainState);
expect(controller.state.tokenList).toStrictEqual(
sampleSingleChainState.tokenList,
);
expect(
controller.state.tokensChainsCache[NetworksChainId.mainnet].data,
).toStrictEqual(
sampleSingleChainState.tokensChainsCache[NetworksChainId.mainnet].data,
);
controller.destroy();
});

Expand Down Expand Up @@ -831,7 +797,14 @@ describe('TokenListController', () => {
});
expect(controller.state).toStrictEqual(outdatedExistingState);
await controller.start();
expect(controller.state).toStrictEqual(sampleSingleChainState);
expect(controller.state.tokenList).toStrictEqual(
sampleSingleChainState.tokenList,
);
expect(
controller.state.tokensChainsCache[NetworksChainId.mainnet].data,
).toStrictEqual(
sampleSingleChainState.tokensChainsCache[NetworksChainId.mainnet].data,
);
controller.destroy();
});

Expand Down Expand Up @@ -884,7 +857,14 @@ describe('TokenListController', () => {
});
expect(controller.state).toStrictEqual(existingState);
await controller.start();
expect(controller.state).toStrictEqual(sampleSingleChainState);
expect(controller.state.tokenList).toStrictEqual(
sampleSingleChainState.tokenList,
);
expect(
controller.state.tokensChainsCache[NetworksChainId.mainnet].data,
).toStrictEqual(
sampleTwoChainState.tokensChainsCache[NetworksChainId.mainnet].data,
);
network.update({
provider: {
type: 'rpc',
Expand Down
Loading