Get token price from AMM trading pair

Someone could explain me how to calculate the price of a token from the getReserves function which returns two uint112 : reserve0 and reserve1 ?

Assuming you’re referring to Uniswap, see

Yes, I am referring to Uniswap.

function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB);

I still don’t understand how these values are used, I don’t see any associated formula in the doc

You need to be careful what you will do with this price, because getting the price from the current reserves can be vulnerable to flash loan attacks.

Here’s some more links:

1 Like