[Ethereum] How to get token info with contract address in Ethereum

abiblockchaintokensweb3-providersweb3js

I use web3 library to interact with Ethereum Network and I want to get token info with his contract address such as tokenName, tokenSymbol, tokenDecimal, tokenLogoURL etc.
I can get almost any data except for the token logo URL.

Here is the code to get name, symbol, decimal:

export async function useGetTokenInfo(chainId: IChainId, tokenAddress: string) {
  const web3: Web3 = new Web3(providers[chainId]);
  const tokenInst = new web3.eth.Contract(tokenABI, tokenAddress);

  const tokenDecimal = await tokenInst.methods.decimals().call();
  const tokenName = await tokenInst.methods.name().call();
  const tokenSymbol = await tokenInst.methods.symbol().call();
}

But I have no any idea to get token Logo as url.
Is there any api to get any Ethereum token(For example ERC20, ERC 721 tokens) logo url?

Best Answer

The token logo URL isn't part of the ERC-20 or ERC-721 specifications (here and here). Some tokens may have included it in their contracts, but most of them won't have.

An alternative would be to use the Coingecko API. For example:

https://api.coingecko.com/api/v3/coins/ethereum

Returns a JSON object including the following logo fields:

"image": {
    "thumb": "https://assets.coingecko.com/coins/images/279/thumb/ethereum.png?1595348880",
    "small": "https://assets.coingecko.com/coins/images/279/small/ethereum.png?1595348880",
    "large": "https://assets.coingecko.com/coins/images/279/large/ethereum.png?1595348880"
  },

You'll need to replace the ethereum query string with the name of the token you're querying. (Which can be found from the list in https://api.coingecko.com/api/v3/coins/list.)

This only works for coins and tokens listed on Coingecko. For anything else, you'll have to find another source.

Related Topic