Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for displayed chains icons based on ethereum-lists/chains #44

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions components/chain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import classes from './chain.module.css'
import stores from '../../stores/index.js'
import { getProvider } from '../../utils'

import Icon from './icon'
import useEthereumListsIcon from './useEthereumListsIcon';

import {
ERROR,
CONNECT_WALLET,
Expand All @@ -20,6 +23,7 @@ export default function Chain({ chain }) {
const router = useRouter()

const [ account, setAccount ] = useState(null)
const { url: iconUrl } = useEthereumListsIcon(chain.icon)

useEffect(() => {
const accountConfigure = () => {
Expand Down Expand Up @@ -96,8 +100,8 @@ export default function Chain({ chain }) {
return (
<Paper elevation={ 1 } className={ classes.chainContainer } key={ chain.chainId }>
<div className={ classes.chainNameContainer }>
<img
src='/connectors/icn-asd.svg'
<Icon
src={iconUrl ? iconUrl : '/connectors/icn-asd.svg'}
onError={e => {
e.target.onerror = null;
e.target.src = "/chains/unknown-logo.png";
Expand Down
17 changes: 17 additions & 0 deletions components/chain/icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

export default function Icon( { src , ...props }) {
const IPFS_SCHEMA = "ipfs://"

const parseUrl = (url) => {
if (url?.startsWith(IPFS_SCHEMA)) {
const cid = src.replace(IPFS_SCHEMA, "")
return `https://ipfs.io/ipfs/${cid}`
} else {
return url
}
}

return (
<img src={parseUrl(src)} {...props} />
)
}
8 changes: 8 additions & 0 deletions components/chain/useEthereumListsIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import useSWR from 'swr'

const fetcher = (...args) => fetch(...args).then(res => res.json())

export default function useEthereumListsIcon(iconName) {
const { data: iconData, error: iconError } = useSWR(iconName ? `https://raw.githubusercontent.com/ethereum-lists/chains/master/_data/icons/${iconName}.json` : null, fetcher)
return { url: iconData?.shift()?.url, error: iconError }
}