In the web application I am working on , I need to find the current price of an ERC20 token listed on etherscan . How can this be done using code or some etherscan api ? Any help is appreciated.
4 Answers
To be honest it depends on exchanges. Different exchanges will have different trade value and It depending on demand. Unfortunately there is no common value for token. Usually there will be 1-5 some times 15% price different because it depending on country/exchange/local law/exchange commission etc..,
Yes there will be api is available. Refer below link's
https://api.coinmarketcap.com/v1/ticker/
https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=EOS,USD,EUR
You can Moralis API to get the pricing of tokens on Etherscan that exists on Ethereum blockchain. As long as they are listed in one of the DEX that Moralis support on Ethereum, you should be able to get the price of the ERC20 token.
First, you'll need to install the Moralis SDK:
npm install moralis
Then, sign up to https://admin.moralis.io to get your API key and add the following code to fetch the price:
import Moralis from 'moralis';
try {
await Moralis.start({
apiKey: "YOUR_API_KEY"
});
const response = await Moralis.EvmApi.token.getTokenPrice({
"chain": "0x1",
"address": "YOUR_TOKEN_ADDRESS"
});
console.log(response.raw);
} catch (e) {
console.error(e);
}
Once completed, you should be able to get the token prices. Moralis also offers here getMultipleTokenPrices API in case you need to fetch multiple token's prices.
Hope this helps!
P.S. I work at Moralis
For ERC-20 tokens, I assume you are looking for example how to find the market price of Basic Attention Token (BAT), an ERC-20 token deployed on Ethereum.
My suggestion is to try the Crypto API from CoinGecko
Reason being that, token names/symbols are tricky. There can be many "BAT" symbol tokens and say you don't want the hassle of maintaining a mapping of ids.
The CoinGecko API allows you to query token using contract address which is unique to a given token
Knowing the contract address for Basic Attention Token 0x0d8775f648430679a709e98d2b0cb6250d2887ef
You can query it with https://api.coingecko.com/api/v3/coins/ethereum/contract/0x0d8775f648430679a709e98d2b0cb6250d2887ef
As long as you know the ERC-20 contract address, you can query almost any market data so long its supported on CoinGecko in which it is quite likely the case.
-
The problem is coingecko doesn t supports as much tokens as Etherscan. Jun 5, 2022 at 2:04
You can also use chainbase free api to fetch current price of erc20 token.
API : https://docs.chainbase.com/reference/gettokenprice
suggest code
network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
token_addr = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; // Take USDT as an example.
fetch(`https://api.chainbase.online/v1/token/price?chain_id=${network_id}&contract_address=${token_addr}`, {
method: 'GET',
headers: {
'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
'accept': 'application/json'
}
}).then(response => response.json())
.then(data => console.log(data.data))
.catch(error => console.error(error));