I'm trying to get the implied price of ETH/USDC using the reserves of WETH and USDC on Uniswap. My code looks like this:
const UNISWAP_FACTORY_ADDR = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
async function main(){
var factory = new web3.eth.Contract(factoryABI.abi, UNISWAP_FACTORY_ADDR);
var pairAddress = await factory.methods.getPair(WETH, USDC).call();
var pair = new web3.eth.Contract(pairABI.abi, pairAddress);
var reserves = await pair.methods.getReserves().call();
console.log(pairAddress);
console.log(reserves);
console.log(reserves[1] / reserves[0]);
}
My output though looks like this:
0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc
Result {
'0': '108170733532523',
'1': '24147939422729143528620',
'2': '1636133844',
reserve0: '108170733532523',
reserve1: '24147939422729143528620',
blockTimestampLast: '1636133844'
}
223239120.54704463
Why am I getting a price value of 223239120? The current price of ETH is ~$4500, so it doesnt look like my issue is just a decimal problem. What am I doing wrong here? Thanks!