I have a simple code to get prices from Uniswap V2
const getTokenExchangeRate = async (tokenA, tokenB) => {
return (
await dexRouter.methods.getAmountsOut(toWei("1"), [tokenA, tokenB]).call()
)[1];
};
// usage
const price = await getTokenExchangeRate(LINK, DAI)
priceNormal = web3.utils.fromWei(price)
console.log(priceNormal)
If both coins have equal decimals it works normal, but if there is a difference in decimals I get a huge number. For example
const price = await getTokenExchangeRate(LINK, USDC)
priceNormal = web3.utils.fromWei(price)
console.log(priceNormal)
PS C:\Users\Viktor\Desktop\Uniswap3JS> node test.js
0.000000000009015961
What should I add to get human readable numbers for all possible pairs? Thanks.