Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
179 changes: 179 additions & 0 deletions src/apis/token-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import nock from 'nock';
import { NetworksChainId } from '../network/NetworkController';
import {
fetchTokenList,
syncTokens,
fetchTokenMetadata,
} from './token-service';

const TOKEN_END_POINT_API = 'https://token-api.airswap-prod.codefi.network';

const sampleTokenList = [
{
address: '0xbbbbca6a901c926f240b89eacb641d8aec7aeafd',
symbol: 'LRC',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
},
{
address: '0xc011a73ee8576fb46f5e1c5751ca3b9fe0af2a6f',
symbol: 'SNX',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Synthetix',
},
{
address: '0x408e41876cccdc0f92210600ef50372656052a38',
symbol: 'REN',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
},
{
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Chainlink',
},
{
address: '0x1f573d6fb3f13d689ff844b4ce37794d79a7ff1c',
symbol: 'BNT',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Bancor',
},
];

const sampleToken = {
address: '0x514910771af9ca656af840dff83e8264ecf986ca',
symbol: 'LINK',
decimals: 18,
occurances: 11,
aggregators: [
'paraswap',
'pmm',
'airswapLight',
'zeroEx',
'bancor',
'coinGecko',
'zapper',
'kleros',
'zerion',
'cmc',
'oneInch',
],
name: 'Chainlink',
};

describe('FetchtokenList', () => {
beforeAll(() => {
nock.disableNetConnect();
});

afterAll(() => {
nock.enableNetConnect();
});

afterEach(() => {
nock.cleanAll();
});

it('should call the tokens api and return the list of tokens', async () => {
nock(TOKEN_END_POINT_API)
.get(`/tokens/${NetworksChainId.mainnet}`)
.reply(200, sampleTokenList)
.persist();

const tokens = await fetchTokenList(NetworksChainId.mainnet);

expect(tokens).toStrictEqual(sampleTokenList);
});
it('should call the api to sync tokens and returns nothing', async () => {
nock(TOKEN_END_POINT_API)
.get(`/sync/${NetworksChainId.mainnet}`)
.reply(200)
.persist();

expect(await syncTokens(NetworksChainId.mainnet)).toBeUndefined();
});
it('should call the api to return the token metadata for eth address provided', async () => {
nock(TOKEN_END_POINT_API)
.get(
`/tokens/${NetworksChainId.mainnet}?address=0x514910771af9ca656af840dff83e8264ecf986ca`,
)
.reply(200, sampleToken)
.persist();

const token = await fetchTokenMetadata(
NetworksChainId.mainnet,
'0x514910771af9ca656af840dff83e8264ecf986ca',
);

expect(token).toStrictEqual(sampleToken);
});
});
72 changes: 72 additions & 0 deletions src/apis/token-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { timeoutFetch } from '../util';

const END_POINT = 'https://token-api.airswap-prod.codefi.network';

function syncTokensURL(chainId: string) {
return `${END_POINT}/sync/${chainId}`;
}
function getTokensURL(chainId: string) {
return `${END_POINT}/tokens/${chainId}`;
}
function getTokenMetadataURL(chainId: string, tokenAddress: string) {
return `${END_POINT}/tokens/${chainId}?address=${tokenAddress}`;
}

/**
* Fetches the list of token metadata for a given network chainId
*
* @returns - Promise resolving token List
*/
export async function fetchTokenList(chainId: string): Promise<Response> {
const tokenURL = getTokensURL(chainId);
const fetchOptions: RequestInit = {
referrer: tokenURL,
referrerPolicy: 'no-referrer-when-downgrade',
method: 'GET',
mode: 'cors',
};
fetchOptions.headers = new window.Headers();
fetchOptions.headers.set('Content-Type', 'application/json');
const tokenResponse = await timeoutFetch(tokenURL, fetchOptions);
return await tokenResponse.json();
}

/**
* Forces a sync of token metadata for a given network chainId.
* Syncing happens every 1 hour in the background, this api can
* be used to force a sync from our side
*/
export async function syncTokens(chainId: string): Promise<void> {
const syncURL = syncTokensURL(chainId);
const fetchOptions: RequestInit = {
referrer: syncURL,
referrerPolicy: 'no-referrer-when-downgrade',
method: 'GET',
mode: 'cors',
};
fetchOptions.headers = new window.Headers();
fetchOptions.headers.set('Content-Type', 'application/json');
await timeoutFetch(syncURL, fetchOptions);
}

/**
* Fetch metadata for the token address provided for a given network chainId
*
* @return Promise resolving token metadata for the tokenAddress provided
*/
export async function fetchTokenMetadata(
chainId: string,
tokenAddress: string,
): Promise<Response> {
const tokenMetadataURL = getTokenMetadataURL(chainId, tokenAddress);
const fetchOptions: RequestInit = {
referrer: tokenMetadataURL,
referrerPolicy: 'no-referrer-when-downgrade',
method: 'GET',
mode: 'cors',
};
fetchOptions.headers = new window.Headers();
fetchOptions.headers.set('Content-Type', 'application/json');
const tokenResponse = await timeoutFetch(tokenMetadataURL, fetchOptions);
return await tokenResponse.json();
}
Loading