Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockscout/apps/block_scout_web/assets/js/lib/token_icon.js

60 lines
1.4 KiB

function getTokenIconUrl (chainID, addressHash) {
let chainName = null
switch (chainID) {
case '1':
chainName = 'ethereum'
break
case '99':
chainName = 'poa'
break
case '100':
chainName = 'xdai'
break
default:
chainName = null
break
}
if (chainName) {
return `https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/${chainName}/assets/${addressHash}/logo.png`
} else {
return null
}
}
function appendTokenIcon ($tokenIconContainer, chainID, addressHash, foreignChainID, foreignAddressHash, displayTokenIcons, size) {
const iconSize = size || 20
let tokenIconURL = null
if (foreignChainID) {
tokenIconURL = getTokenIconUrl(foreignChainID.toString(), foreignAddressHash)
} else if (chainID) {
tokenIconURL = getTokenIconUrl(chainID.toString(), addressHash)
}
if (displayTokenIcons) {
checkLink(tokenIconURL)
.then(checkTokenIconLink => {
if (checkTokenIconLink) {
if ($tokenIconContainer) {
const img = new Image(iconSize, iconSize)
img.src = tokenIconURL
$tokenIconContainer.append(img)
}
}
})
}
}
async function checkLink (url) {
if (url) {
try {
const res = await fetch(url)
return res.ok
} catch (_error) {
return false
}
} else {
return false
}
}
export { appendTokenIcon, checkLink, getTokenIconUrl }