diff --git a/src/scripts/auxLists/coingecko.ts b/src/scripts/auxLists/coingecko.ts index d9cd205..fa9727a 100644 --- a/src/scripts/auxLists/coingecko.ts +++ b/src/scripts/auxLists/coingecko.ts @@ -1,3 +1,4 @@ +import { SupportedChainId } from '@cowprotocol/cow-sdk' import { COINGECKO_CHAINS, fetchWithApiKey, processTokenList, TokenInfo, TOP_TOKENS_COUNT, VS_CURRENCY } from './utils' const COINGECKO_CHAINS_NAMES = Object.values(COINGECKO_CHAINS) @@ -10,11 +11,11 @@ interface TokenIdsMap { let COINGECKO_IDS_MAP: TokenIdsMap = {} -function getTokenListUrl(chain: number): string { +function getTokenListUrl(chain: SupportedChainId): string { return `https://tokens.coingecko.com/${COINGECKO_CHAINS[chain]}/all.json` } -async function getTokenList(chain: number): Promise { +async function getTokenList(chain: SupportedChainId): Promise { const data = await fetchWithApiKey(getTokenListUrl(chain)) return data.tokens } @@ -36,12 +37,16 @@ async function getCoingeckoTokenIds(): Promise { } export async function getCoingeckoTokenIdsMap(): Promise { - let tokenIdsMap = COINGECKO_CHAINS_NAMES.reduce((acc, name) => ({ ...acc, [name]: {} }), {}) + let tokenIdsMap = COINGECKO_CHAINS_NAMES.reduce((acc, name) => (name ? { ...acc, [name]: {} } : acc), {}) try { const tokenIds = await getCoingeckoTokenIds() tokenIds.forEach((token) => { COINGECKO_CHAINS_NAMES.forEach((chain) => { + if (!chain) { + return + } + const address = token.platforms[chain]?.toLowerCase() if (address) { tokenIdsMap[chain][address] = token.id @@ -61,8 +66,13 @@ interface MarketData { total_volume: number } -async function getCoingeckoMarket(chainId: number, tokens: TokenInfo[]): Promise { - const coingeckoIdsForChain = COINGECKO_IDS_MAP[COINGECKO_CHAINS[chainId]] +async function getCoingeckoMarket(chainId: SupportedChainId, tokens: TokenInfo[]): Promise { + const coingeckoChainName = COINGECKO_CHAINS[chainId] + if (!coingeckoChainName) { + return [] + } + + const coingeckoIdsForChain = COINGECKO_IDS_MAP[coingeckoChainName] const ids = tokens.reduce((acc, token) => { const coingeckoId = coingeckoIdsForChain[token.address] return coingeckoId ? `${acc}${coingeckoId},` : acc @@ -83,7 +93,7 @@ interface TokenWithVolume { volume: number } -async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise { +async function getTokenVolumes(chainId: SupportedChainId, tokens: TokenInfo[]): Promise { const chunkSize = 250 const chunks = [] @@ -92,7 +102,13 @@ async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise }) => { const volumeData = await volume @@ -115,7 +131,7 @@ async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise b.volume - a.volume) } -export async function fetchAndProcessCoingeckoTokens(chainId: number): Promise { +export async function fetchAndProcessCoingeckoTokens(chainId: SupportedChainId): Promise { try { COINGECKO_IDS_MAP = Object.keys(COINGECKO_IDS_MAP).length ? COINGECKO_IDS_MAP : await getCoingeckoTokenIdsMap() const tokens = await getTokenList(chainId) diff --git a/src/scripts/auxLists/uniswap.ts b/src/scripts/auxLists/uniswap.ts index 354e355..305dc8f 100644 --- a/src/scripts/auxLists/uniswap.ts +++ b/src/scripts/auxLists/uniswap.ts @@ -1,3 +1,4 @@ +import { SupportedChainId } from '@cowprotocol/cow-sdk' import { getCoingeckoTokenIdsMap } from './coingecko' import { COINGECKO_CHAINS, fetchWithApiKey, processTokenList, TokenInfo } from './utils' @@ -11,7 +12,7 @@ async function getUniswapTokens(): Promise { } async function mapUniMainnetToChainTokens( - chain: number, + chain: SupportedChainId, uniTokens: TokenInfo[], coingeckoTokensForChain: TokenInfo[], ): Promise { @@ -20,9 +21,9 @@ async function mapUniMainnetToChainTokens( // Split uni tokens into mainnet and chain uniTokens.forEach((token) => { - if (token.chainId === +chain) { + if (token.chainId === chain) { chainTokens[token.address.toLowerCase()] = token - } else if (token.chainId === 1) { + } else if (token.chainId === SupportedChainId.MAINNET) { mainnetTokens.push(token) } }) @@ -31,11 +32,17 @@ async function mapUniMainnetToChainTokens( acc[token.address.toLowerCase()] = token return acc }, {}) + const coingeckoMainnetName = COINGECKO_CHAINS[SupportedChainId.MAINNET] + const coingeckoChainName = COINGECKO_CHAINS[chain] mainnetTokens.forEach((token) => { - const coingeckoId = COINGECKO_IDS_MAP[COINGECKO_CHAINS['1']][token.address.toLowerCase()] + if (!coingeckoMainnetName || !coingeckoChainName) { + return + } + + const coingeckoId = COINGECKO_IDS_MAP[coingeckoMainnetName][token.address.toLowerCase()] if (coingeckoId) { - const chainAddress = COINGECKO_IDS_MAP[COINGECKO_CHAINS[chain]][coingeckoId] + const chainAddress = COINGECKO_IDS_MAP[coingeckoChainName][coingeckoId] if (!chainTokens[chainAddress]) { const cgToken = coingeckoTokensMap[chainAddress] if (cgToken) { @@ -48,7 +55,7 @@ async function mapUniMainnetToChainTokens( return Object.values(chainTokens) } -export async function fetchAndProcessUniswapTokens(chainId: number): Promise { +export async function fetchAndProcessUniswapTokens(chainId: SupportedChainId): Promise { try { COINGECKO_IDS_MAP = Object.keys(COINGECKO_IDS_MAP).length ? COINGECKO_IDS_MAP : await getCoingeckoTokenIdsMap() const uniTokens = await getUniswapTokens() diff --git a/src/scripts/auxLists/utils.ts b/src/scripts/auxLists/utils.ts index f55f42e..54d7164 100644 --- a/src/scripts/auxLists/utils.ts +++ b/src/scripts/auxLists/utils.ts @@ -1,3 +1,4 @@ +import { SupportedChainId } from '@cowprotocol/cow-sdk' import { TokenList } from '@uniswap/token-lists' import assert from 'assert' import * as fs from 'fs' @@ -6,25 +7,27 @@ import path from 'path' export const COINGECKO_API_KEY = process.env.COINGECKO_API_KEY assert(COINGECKO_API_KEY, 'COINGECKO_API_KEY env is required') -export const COINGECKO_CHAINS: Record = { - 1: 'ethereum', - 100: 'xdai', - 8453: 'base', - 42161: 'arbitrum-one', +export const COINGECKO_CHAINS: Record = { + [SupportedChainId.MAINNET]: 'ethereum', + [SupportedChainId.GNOSIS_CHAIN]: 'xdai', + [SupportedChainId.BASE]: 'base', + [SupportedChainId.ARBITRUM_ONE]: 'arbitrum-one', + [SupportedChainId.SEPOLIA]: null, } -export const DISPLAY_CHAIN_NAMES: Record = { - 1: 'Ethereum', - 100: 'Gnosis chain', - 8453: 'Base', - 42161: 'Arbitrum one', +export const DISPLAY_CHAIN_NAMES: Record = { + [SupportedChainId.MAINNET]: 'Ethereum', + [SupportedChainId.GNOSIS_CHAIN]: 'Gnosis chain', + [SupportedChainId.BASE]: 'Base', + [SupportedChainId.ARBITRUM_ONE]: 'Arbitrum one', + [SupportedChainId.SEPOLIA]: null, } export const VS_CURRENCY = 'usd' export const TOP_TOKENS_COUNT = 500 export interface TokenInfo { - chainId: number + chainId: SupportedChainId address: string name: string symbol: string @@ -47,11 +50,11 @@ function getEmptyList(): Partial { } } -function getListName(chain: number, prefix: string, count?: number): string { +function getListName(chain: SupportedChainId, prefix: string, count?: number): string { return `${prefix}${count ? ` top ${count}` : ''} on ${DISPLAY_CHAIN_NAMES[chain]}` } -function getOutputPath(prefix: string, chainId: number): string { +function getOutputPath(prefix: string, chainId: SupportedChainId): string { return `src/public/${prefix}.${chainId}.json` } @@ -65,7 +68,7 @@ export function getLocalTokenList(listPath: string, defaultEmptyList: Partial