import { ChainId, NEXT_PUBLIC_CHAIN_ID } from '..' import { Token, TokenList } from './types' export const serializeToken = (token: Token, chainId: ChainId = NEXT_PUBLIC_CHAIN_ID): Token => { let data = { ...token } if (chainId === ChainId.MAINNET) { data = { ...data, tokenAddress: token[ChainId.MAINNET] ? token[ChainId.MAINNET] : token[ChainId.TESTNET], } } else if (chainId === ChainId.TESTNET) { data = { ...data, tokenAddress: token[ChainId.TESTNET] ? token[ChainId.TESTNET] : token[ChainId.MAINNET] } } return data } export function getTokenByChainId(defaultToken: TokenList, chainId: ChainId = NEXT_PUBLIC_CHAIN_ID): T { // If testnet - return list comprised of testnetTokens wherever they exist, and mainnetTokens where they don't const tokens = Object.keys(defaultToken).reduce((accum, key) => { return { ...accum, [key]: serializeToken(defaultToken[key as keyof typeof defaultToken], chainId) } }, {} as T) return tokens }