From 3f78b4ab4f199dce461421ec34b955a0aa3e4008 Mon Sep 17 00:00:00 2001 From: nvonpentz <12549658+nvonpentz@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:56:43 -0400 Subject: [PATCH] Use Ankr to generate token lists --- scripts/index.js | 9 ++++++++ scripts/util.cjs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/scripts/index.js b/scripts/index.js index dc49185..14c63cd 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -30,6 +30,12 @@ async function stageEVMTokenFile(stagingDir, inputTokenFilePath, coingeckoIds) { ) } +async function stageCombinedTokenFile(stagingDir, coingeckoIds) { + const dstPath = path.join(stagingDir, 'token-lists.json') + const tokenLists = await util.generateTokenList(coingeckoIds) + fs.writeFileSync(dstPath, JSON.stringify(tokenLists, null, 2)) +} + async function stageMainnetTokenFile(stagingDir, inputTokenFilePath, coingeckoIds) { // Read in the JSON file located at inputTokenFilePath const tokenList = JSON.parse(fs.readFileSync(inputTokenFilePath, 'utf-8')) @@ -267,6 +273,9 @@ async function stageTokenPackage() { fs.mkdirSync(imagesDstPath) } + // Generate combined token list + await stageCombinedTokenFile(stagingDir, coingeckoIds) + // Add MetaMask tokens for contract-map.json const metamaskTokenPath = path.join('node_modules', '@metamask', 'contract-metadata', 'contract-map.json'); await stageMainnetTokenFile(stagingDir, metamaskTokenPath, coingeckoIds) diff --git a/scripts/util.cjs b/scripts/util.cjs index 1c6d298..27de99f 100644 --- a/scripts/util.cjs +++ b/scripts/util.cjs @@ -280,6 +280,63 @@ const installErrorHandlers = () => { }) } +const generateTokenList = async (coingeckoIds) => { + const ankrApiKey = process.env.ANKR_API_KEY + const ankrSupportedChains = { + "eth": "0x1", + "bsc": "0x38", + "fantom": "0xfa", + "avalanche": "0xa86a", + "polygon": "0x89", + "optimism": "0xa", + }; + + const tokenList = {}; + + for (let chainKey in ankrSupportedChains) { + let chainId = ankrSupportedChains[chainKey]; + + const response = await fetch(`https://rpc.ankr.com/multichain/${ankrApiKey}/?ankr_getCurrencies=`, { + method: 'POST', + headers: { + 'accept': 'application/json', + 'content-type': 'application/json' + }, + body: JSON.stringify({ + "jsonrpc": "2.0", + "method": "ankr_getCurrencies", + "params": { + "blockchain": chainKey + }, + "id": 1 + }) + }); + + const data = await response.json(); + + if (data.result && data.result.currencies) { + let tokens = data.result.currencies; + let tokenMapping = {}; + + for (let token of tokens) { + let coingeckoId = coingeckoIds[chainId] && coingeckoIds[chainId][token.address] ? coingeckoIds[chainId][token.address] : null; + tokenMapping[token.address] = { + "name": token.name, + "logo": token.thumbnail, + "symbol": token.symbol, + "decimals": token.decimals, + "coingeckoId": coingeckoId, + "erc20": true, + }; + } + + tokenList[chainId] = tokenMapping; + } + } + + return tokenList; +} + const generateMainnetTokenList = async (fullTokenList) => { const MAX_TOKEN_LIST_SIZE = 100 const coinGeckoApiBaseUrl = 'https://api.coingecko.com/api/v3' @@ -679,6 +736,7 @@ module.exports = { installErrorHandlers, saveToPNGResize, download, + generateTokenList, generateMainnetTokenList, generateDappLists, addSupportedCoinbaseTokens,