My ultimate goal is to get the current price in ethereum of a token. To do this, I was using etherscan’s api to get the contract ABI and then using the ABI and token decimals, get the current price of the token in ETH.
However, now I would like to get this data without using an API call and instead utilize the web3 library function calls. How can I get the ABI of an ethereum contract address only using the web3 library?
For reference, here is the code I am using to get the current price of a token in ethereum. The getAbi method needs to be fixed because it is currently using an api call so I will not include it:
async function getTokenPriceInEth(tokenAddress) {
try {
const uniswap_v2_router = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
const uniswap_v2_router_abi = await getAbi(uniswap_v2_router);
if (!uniswap_v2_router_abi) {
throw new Error('Failed to fetch Uniswap router ABI');
}
const router = new web3.eth.Contract(uniswap_v2_router_abi, uniswap_v2_router);
const tokenAbi = await getAbi(tokenAddress);
if (!tokenAbi) {
throw new Error(`Failed to fetch ABI for token: ${tokenAddress}`);
}
const tokenContract = new web3.eth.Contract(tokenAbi, tokenAddress);
const tokenDecimals = await tokenContract.methods.decimals().call();
const amountIn = '1' + '0'.repeat(parseInt(tokenDecimals));
const WETH_ADDRESS = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // Ethereum Mainnet WETH address
const path = [tokenAddress, WETH_ADDRESS];
const amounts = await router.methods.getAmountsOut(amountIn, path).call();
const ethAmount = web3.utils.fromWei(amounts[1], 'ether');
const tokenPriceInEth = parseFloat(ethAmount);
return tokenPriceInEth;
} catch (error) {
return 0;
}
}