forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCurrencyLogoURIs.ts
More file actions
98 lines (91 loc) · 3.02 KB
/
useCurrencyLogoURIs.ts
File metadata and controls
98 lines (91 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { ChainId } from '@uniswap/sdk-core'
import useHttpLocations from 'hooks/useHttpLocations'
import { useMemo } from 'react'
import { isAddress } from 'utils'
import EthereumLogo from '../../assets/images/ethereum-logo.png'
import AvaxLogo from '../../assets/svg/avax_logo.svg'
import BnbLogo from '../../assets/svg/bnb-logo.svg'
import CeloLogo from '../../assets/svg/celo_logo.svg'
import MaticLogo from '../../assets/svg/matic-token-icon.svg'
import GnosisLogo from '../../assets/svg/xdai_logo.svg'
import { isCelo, NATIVE_CHAIN_ID, nativeOnChain } from '../../constants/tokens'
type Network = 'ethereum' | 'arbitrum' | 'optimism' | 'polygon' | 'smartchain' | 'celo' | 'avalanchec' | 'gnosis'
export function chainIdToNetworkName(networkId: ChainId): Network {
switch (networkId) {
case ChainId.MAINNET:
return 'ethereum'
case ChainId.ARBITRUM_ONE:
return 'arbitrum'
case ChainId.OPTIMISM:
return 'optimism'
case ChainId.POLYGON:
return 'polygon'
case ChainId.BNB:
return 'smartchain'
case ChainId.CELO:
return 'celo'
case ChainId.AVALANCHE:
return 'avalanchec'
case ChainId.GNOSIS:
return 'gnosis'
default:
return 'ethereum'
}
}
export function getNativeLogoURI(chainId: ChainId = ChainId.MAINNET): string {
switch (chainId) {
case ChainId.POLYGON:
case ChainId.POLYGON_MUMBAI:
return MaticLogo
case ChainId.BNB:
return BnbLogo
case ChainId.CELO:
case ChainId.CELO_ALFAJORES:
return CeloLogo
case ChainId.AVALANCHE:
return AvaxLogo
case ChainId.GNOSIS:
return GnosisLogo
default:
return EthereumLogo
}
}
function getTokenLogoURI(address: string, chainId: ChainId = ChainId.MAINNET): string | void {
const networkName = chainIdToNetworkName(chainId)
const networksWithUrls = [ChainId.ARBITRUM_ONE, ChainId.MAINNET, ChainId.OPTIMISM, ChainId.BNB, ChainId.AVALANCHE]
if (isCelo(chainId) && address === nativeOnChain(chainId).wrapped.address) {
return CeloLogo
}
if (networksWithUrls.includes(chainId)) {
return `https://raw.githubusercontent.com/Uniswap/assets/master/blockchains/${networkName}/assets/${address}/logo.png`
}
}
export default function useCurrencyLogoURIs(
currency:
| {
isNative?: boolean
isToken?: boolean
address?: string
chainId: number
logoURI?: string | null
}
| null
| undefined
): string[] {
const locations = useHttpLocations(currency?.logoURI)
return useMemo(() => {
const logoURIs = [...locations]
if (currency) {
if (currency.isNative || currency.address === NATIVE_CHAIN_ID) {
logoURIs.push(getNativeLogoURI(currency.chainId))
} else if (currency.isToken || currency.address) {
const checksummedAddress = isAddress(currency.address)
const logoURI = checksummedAddress && getTokenLogoURI(checksummedAddress, currency.chainId)
if (logoURI) {
logoURIs.push(logoURI)
}
}
}
return logoURIs
}, [currency, locations])
}