Skip to content

Commit

Permalink
refactor: use SupportedChainId
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Dec 13, 2024
1 parent 1eb91c7 commit 3c92687
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
32 changes: 24 additions & 8 deletions src/scripts/auxLists/coingecko.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<TokenInfo[]> {
async function getTokenList(chain: SupportedChainId): Promise<TokenInfo[]> {
const data = await fetchWithApiKey(getTokenListUrl(chain))
return data.tokens
}
Expand All @@ -36,12 +37,16 @@ async function getCoingeckoTokenIds(): Promise<CoingeckoToken[]> {
}

export async function getCoingeckoTokenIdsMap(): Promise<TokenIdsMap> {
let tokenIdsMap = COINGECKO_CHAINS_NAMES.reduce<TokenIdsMap>((acc, name) => ({ ...acc, [name]: {} }), {})
let tokenIdsMap = COINGECKO_CHAINS_NAMES.reduce<TokenIdsMap>((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
Expand All @@ -61,8 +66,13 @@ interface MarketData {
total_volume: number
}

async function getCoingeckoMarket(chainId: number, tokens: TokenInfo[]): Promise<MarketData[]> {
const coingeckoIdsForChain = COINGECKO_IDS_MAP[COINGECKO_CHAINS[chainId]]
async function getCoingeckoMarket(chainId: SupportedChainId, tokens: TokenInfo[]): Promise<MarketData[]> {
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
Expand All @@ -83,7 +93,7 @@ interface TokenWithVolume {
volume: number
}

async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise<TokenWithVolume[]> {
async function getTokenVolumes(chainId: SupportedChainId, tokens: TokenInfo[]): Promise<TokenWithVolume[]> {
const chunkSize = 250
const chunks = []

Expand All @@ -92,7 +102,13 @@ async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise<To
chunks.push({ tokens: chunk, volume: getCoingeckoMarket(chainId, chunk) })
}

const ids = COINGECKO_IDS_MAP[COINGECKO_CHAINS[chainId]]
const coingeckoChainName = COINGECKO_CHAINS[chainId]

if (!coingeckoChainName) {
return []
}

const ids = COINGECKO_IDS_MAP[coingeckoChainName]
const volumes = await Promise.all(
chunks.map(async ({ tokens, volume }: { tokens: TokenInfo[]; volume: Promise<MarketData[]> }) => {
const volumeData = await volume
Expand All @@ -115,7 +131,7 @@ async function getTokenVolumes(chainId: number, tokens: TokenInfo[]): Promise<To
return volumes.flat().sort((a, b) => b.volume - a.volume)
}

export async function fetchAndProcessCoingeckoTokens(chainId: number): Promise<void> {
export async function fetchAndProcessCoingeckoTokens(chainId: SupportedChainId): Promise<void> {
try {
COINGECKO_IDS_MAP = Object.keys(COINGECKO_IDS_MAP).length ? COINGECKO_IDS_MAP : await getCoingeckoTokenIdsMap()
const tokens = await getTokenList(chainId)
Expand Down
19 changes: 13 additions & 6 deletions src/scripts/auxLists/uniswap.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SupportedChainId } from '@cowprotocol/cow-sdk'
import { getCoingeckoTokenIdsMap } from './coingecko'
import { COINGECKO_CHAINS, fetchWithApiKey, processTokenList, TokenInfo } from './utils'

Expand All @@ -11,7 +12,7 @@ async function getUniswapTokens(): Promise<TokenInfo[]> {
}

async function mapUniMainnetToChainTokens(
chain: number,
chain: SupportedChainId,
uniTokens: TokenInfo[],
coingeckoTokensForChain: TokenInfo[],
): Promise<TokenInfo[]> {
Expand All @@ -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)
}
})
Expand All @@ -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) {
Expand All @@ -48,7 +55,7 @@ async function mapUniMainnetToChainTokens(
return Object.values(chainTokens)
}

export async function fetchAndProcessUniswapTokens(chainId: number): Promise<void> {
export async function fetchAndProcessUniswapTokens(chainId: SupportedChainId): Promise<void> {
try {
COINGECKO_IDS_MAP = Object.keys(COINGECKO_IDS_MAP).length ? COINGECKO_IDS_MAP : await getCoingeckoTokenIdsMap()
const uniTokens = await getUniswapTokens()
Expand Down
33 changes: 18 additions & 15 deletions src/scripts/auxLists/utils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<number, string> = {
1: 'ethereum',
100: 'xdai',
8453: 'base',
42161: 'arbitrum-one',
export const COINGECKO_CHAINS: Record<SupportedChainId, string | null> = {
[SupportedChainId.MAINNET]: 'ethereum',
[SupportedChainId.GNOSIS_CHAIN]: 'xdai',
[SupportedChainId.BASE]: 'base',
[SupportedChainId.ARBITRUM_ONE]: 'arbitrum-one',
[SupportedChainId.SEPOLIA]: null,
}

export const DISPLAY_CHAIN_NAMES: Record<number, string> = {
1: 'Ethereum',
100: 'Gnosis chain',
8453: 'Base',
42161: 'Arbitrum one',
export const DISPLAY_CHAIN_NAMES: Record<SupportedChainId, string | null> = {
[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
Expand All @@ -47,11 +50,11 @@ function getEmptyList(): Partial<TokenList> {
}
}

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`
}

Expand All @@ -65,7 +68,7 @@ export function getLocalTokenList(listPath: string, defaultEmptyList: Partial<To
}

interface SaveUpdatedTokensParams {
chainId: number
chainId: SupportedChainId
prefix: string
logo: string
tokens: TokenInfo[]
Expand Down Expand Up @@ -126,7 +129,7 @@ export function saveUpdatedTokens({ chainId, prefix, logo, tokens, listName }: S
}

interface ProcessTokenListParams {
chainId: number
chainId: SupportedChainId
tokens: TokenInfo[]
prefix: string
logo: string
Expand Down

0 comments on commit 3c92687

Please sign in to comment.