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

Use Ankr to generate token lists #86

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions scripts/util.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -679,6 +736,7 @@ module.exports = {
installErrorHandlers,
saveToPNGResize,
download,
generateTokenList,
generateMainnetTokenList,
generateDappLists,
addSupportedCoinbaseTokens,
Expand Down