-
-
Notifications
You must be signed in to change notification settings - Fork 261
Adding TokenListController to fetch the token list from token services API #478
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
Merged
Merged
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 3c9e6f0
integrating TokenListcontroller with AssetsDetectioncontroller and test
NiranjanaBinoy eb4c114
fixing lint error
NiranjanaBinoy f9ad337
updating the tests
NiranjanaBinoy bb1040c
fixing fetchOptions.headers check in TokenListController and check fo…
NiranjanaBinoy 720eac3
separating the api interaction from controller
NiranjanaBinoy 62ca7b4
fixing the lint and test errors after the rebase
NiranjanaBinoy 07227be
fixing the type for defaultstate from any
NiranjanaBinoy 8f76dbf
exporting the controller
NiranjanaBinoy bedcbd3
adding new api end points and related functions
NiranjanaBinoy aeed783
change in fetchtokenList logic and removing topAssets endpoint and re…
NiranjanaBinoy bfa6485
changing the state variable from to to avoid overwriting in extension
NiranjanaBinoy 239e12c
adding cache and removing tokens with less than 2 occurance and symbo…
NiranjanaBinoy f5a1209
removing console.log
NiranjanaBinoy be997b5
updating cache on sync and updation if data avalable is undefined, fe…
NiranjanaBinoy 5b7f953
updating DEFAULT_INTERVAL to 1 hour and DEFAULT_THRESHOLD to 30 minutes
NiranjanaBinoy 57a1799
fixing test failure
NiranjanaBinoy 084cae7
fixing duplicate symbol filtering and adding related test cases
NiranjanaBinoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.