diff --git a/packages/kit/src/utils/networks.ts b/packages/kit/src/utils/networks.ts index 7529c94d..b418f6b8 100644 --- a/packages/kit/src/utils/networks.ts +++ b/packages/kit/src/utils/networks.ts @@ -1,49 +1,63 @@ -export const getChainColor = (chainId: number, mode: 'dark' | 'light' = 'light') => { +import { networks, ChainId } from '@0xsequence/network' + +export const getNetworkColor = (chainId: number, mode: 'dark' | 'light' = 'light') => { switch (chainId) { - case 1: + case ChainId.MAINNET: return mode === 'light' ? '#abf' : '#abf' - case 137: + case ChainId.POLYGON: return mode === 'light' ? '#c7a6ff' : '#c7a6ff' - case 42161: + case ChainId.ARBITRUM: return mode === 'light' ? '#52A7E6' : '#52A7E6' - case 10: + case ChainId.OPTIMISM: return mode === 'light' ? '#DB3132' : '#DB3132' - case 56: + case ChainId.BSC: return mode === 'light' ? '#CB9C1D' : '#EEB445' - case 43114: + case ChainId.AVALANCHE: return mode === 'light' ? '#E84142' : '#E84142' - case 100: + case ChainId.GNOSIS: return mode === 'light' ? '#00193C' : '#D8E8FF' - case 5: + case ChainId.GOERLI: return mode === 'light' ? '#A77A00' : '#FFA700' - case 80001: + case ChainId.POLYGON_MUMBAI: + case ChainId.POLYGON_AMOY: return mode === 'light' ? '#D68828' : '#FFA700' default: return mode === 'light' ? '#abf' : '#abf' } } -export const getChainBGColor = (chainId: number, mode: 'dark' | 'light' = 'light') => { +export const getNetworkBackgroundColor = (chainId: number, mode: 'dark' | 'light' = 'light') => { switch (chainId) { - case 1: + case ChainId.MAINNET: return mode === 'light' ? '#132362' : '#132362' - case 137: + case ChainId.POLYGON: return mode === 'light' ? '#350881' : '#350881' - case 42161: + case ChainId.ARBITRUM: return mode === 'light' ? '#EDF7FF' : '#0C3754' - case 10: + case ChainId.OPTIMISM: return mode === 'light' ? '#FFEAE9' : '#390B0C' - case 56: + case ChainId.BSC: return mode === 'light' ? '#FFE8AB' : '#554018' - case 43114: + case ChainId.AVALANCHE: return mode === 'light' ? '#FBDFDF' : '#390B0C' - case 100: + case ChainId.GNOSIS: return mode === 'light' ? '#D8E8FF' : '#00193C' - case 5: + case ChainId.GOERLI: return mode === 'light' ? '#FFD871' : '#554018' - case 80001: + case ChainId.POLYGON_MUMBAI: + case ChainId.POLYGON_AMOY: return mode === 'light' ? '#FFE8CD' : '#554018' default: return mode === 'light' ? '#132362' : '#132362' } } + +export const getNetwork = (chainId: number) => { + const network = networks[chainId as ChainId] + + if (!network) { + throw new Error(`Unknown network chainId: ${chainId}`) + } + + return network +} diff --git a/packages/kit/src/utils/tokens.ts b/packages/kit/src/utils/tokens.ts index 64392fda..42aa938a 100644 --- a/packages/kit/src/utils/tokens.ts +++ b/packages/kit/src/utils/tokens.ts @@ -1,3 +1,4 @@ +import { ChainId } from '@0xsequence/network' import { Chain } from 'wagmi/chains' export interface NativeTokenInfo { @@ -14,7 +15,7 @@ interface TokenInfos { } export const nativeTokenInfos: TokenInfos = { - 1: { + [ChainId.MAINNET]: { name: 'Ethereum', symbol: 'ETH', logoURI: 'https://assets.coingecko.com/coins/images/279/thumb/ethereum.png', @@ -22,7 +23,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Etherscan', blockExplorerUrl: 'https://etherscan.io' }, - 5: { + [ChainId.GOERLI]: { name: 'Goerli', symbol: 'ETH', decimals: 18, @@ -30,7 +31,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Etherscan (Goerli)', blockExplorerUrl: 'https://goerli.etherscan.io' }, - 10: { + [ChainId.OPTIMISM]: { name: 'Optimism', symbol: 'OP', logoURI: 'https://assets.coingecko.com/coins/images/25244/small/Optimism.png', @@ -38,7 +39,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Etherscan (Optimism)', blockExplorerUrl: 'https://optimistic.etherscan.io' }, - 56: { + [ChainId.BSC]: { name: 'BNB', symbol: 'BNB', logoURI: 'https://assets.coingecko.com/coins/images/825/thumb/bnb-icon2_2x.png', @@ -46,7 +47,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'BscScan', blockExplorerUrl: 'https://bscscan.com' }, - 100: { + [ChainId.GNOSIS]: { name: 'Gnosis', symbol: 'GNO', logoURI: 'https://assets.coingecko.com/coins/images/662/small/logo_square_simple_300px.png', @@ -54,7 +55,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerUrl: 'https://gnosisscan.io', blockExplorerName: 'Gnosis Scan' }, - 137: { + [ChainId.POLYGON]: { name: 'Polygon', symbol: 'MATIC', logoURI: 'https://assets.coingecko.com/coins/images/4713/thumb/matic-token-icon.png', @@ -62,7 +63,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Polyscan', blockExplorerUrl: 'https://polygonscan.com' }, - 1101: { + [ChainId.POLYGON_ZKEVM]: { name: 'Polygon zkEVM', symbol: 'ETH', logoURI: 'https://assets.coingecko.com/coins/images/4713/thumb/matic-token-icon.png', @@ -70,7 +71,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'PolygonScan', blockExplorerUrl: 'https://zkevm.polygonscan.com' }, - 42161: { + [ChainId.ARBITRUM]: { name: 'Arbitrum', symbol: 'ARB', logoURI: 'https://assets.coingecko.com/asset_platforms/images/33/small/arbitrum-one.png', @@ -78,7 +79,7 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Arbiscan', blockExplorerUrl: 'https://arbiscan.io' }, - 43114: { + [ChainId.AVALANCHE]: { name: 'Avalanche', symbol: 'AVAX', logoURI: 'https://assets.coingecko.com/coins/images/12559/small/Avalanche_Circle_RedWhite_Trans.png', @@ -86,13 +87,21 @@ export const nativeTokenInfos: TokenInfos = { blockExplorerName: 'Snowtrace', blockExplorerUrl: 'https://snowtrace.io' }, - 80001: { + [ChainId.POLYGON_MUMBAI]: { name: 'Mumbai', symbol: 'MATIC', logoURI: 'https://assets.coingecko.com/coins/images/4713/thumb/matic-token-icon.png', decimals: 18, blockExplorerName: 'Polyscan (Mumbai)', blockExplorerUrl: 'https://mumbai.polygonscan.com' + }, + [ChainId.POLYGON_AMOY]: { + name: 'Amoy', + symbol: 'MATIC', + logoURI: 'https://assets.coingecko.com/coins/images/4713/thumb/matic-token-icon.png', + decimals: 18, + blockExplorerName: 'Polyscan (Amoy)', + blockExplorerUrl: 'https://amoy.polygonscan.com' } } diff --git a/packages/wallet/src/shared/NetworkBadge.tsx b/packages/wallet/src/shared/NetworkBadge.tsx index 02556f53..e76138f3 100644 --- a/packages/wallet/src/shared/NetworkBadge.tsx +++ b/packages/wallet/src/shared/NetworkBadge.tsx @@ -1,9 +1,10 @@ import React from 'react' import { Box, Image, Text } from '@0xsequence/design-system' -import { getNativeTokenInfoByChainId, getChainColor, getChainBGColor, getNetworkConfigAndClients } from '@0xsequence/kit' +import { getNativeTokenInfoByChainId, getNetwork, getNetworkColor, getNetworkBackgroundColor } from '@0xsequence/kit' import { useConfig } from 'wagmi' import { capitalize } from '../utils' +import { networks } from '@0xsequence/network' interface NetworkBadgeProps { chainId: number @@ -11,10 +12,10 @@ interface NetworkBadgeProps { export const NetworkBadge = ({ chainId }: NetworkBadgeProps) => { const { chains } = useConfig() - const { network } = getNetworkConfigAndClients(chainId) + const network = getNetwork(chainId) const nativeTokenInfo = getNativeTokenInfoByChainId(chainId, chains) - const chainColor = getChainColor(chainId) - const chainBGColor = getChainBGColor(chainId) + const chainColor = getNetworkColor(chainId) + const chainBGColor = getNetworkBackgroundColor(chainId) return (