# How a price of a token is being calculated on uniswap & other dexes?

I have just started learning about Uniswap. I have a question, How a price of a token is calculated? I know the `x*y = k` which defines the price of 2 assets from a pool but how the value of a single token is being calculated by Uniswap. For example, when we go to the Uniswap app here the price of eth on Uniswap is `1,823.37` atm. How this price is calculated?

The price of anything is calculated against something else. Nothing has value by itself.

Remember the first time man traded a cow for a chicken. One gives a cow value in chickens. So the next time someone will do the same, he knows the price already.

But... chickens and cows have a limited amount in the village ( the pool ) so when one is exchanged in favor of the other, the price raises to keep proportion.

A cow by a cow have no absolute value. Don't forget that the money itself is something that you buy and sell.

When you throw some tokens to the pool, you must give something to represents the other side of the scale ( BNB,ETH or BTC ). The initial proportion of tokens/coins will define the initial price.

When you put one and remove other (to take one you must give other) the proportion changes and the price changes too.

If you have much more on one side than the other, we say it is "undervalued". You need to put much less to buy much more.

This process defines the value of anything we humans trade.

It is duplicate anyway: How the price of a token is defined?

• Unless someone decides to pull your rug. If someone owns the village, he may decide to make a barbecue. Aug 16, 2023 at 12:53
• Thank you for the explanation. And say if I want to swap DAI for WETH do I have to make a liquidity pool or I can just execute by using the existing LPs? Aug 16, 2023 at 12:53
• Just trade it using a swap. You only need to participate in a LP if you want. Aug 16, 2023 at 12:56
• The swap knows how to operate in a existing LP, like a bank do it for us instead trading face to face with someone. The advantage using a swap is that when you exchange things that have no LP directly. The swap use a LP chain to exchange one by other until reach the other side. Lets say we have no ways to exchange Yen for Peso. The swap exchanges Yen to Dollar and then Dollar to Pesos. Aug 16, 2023 at 13:07
• If this answer satisfies you, please accept it. Aug 16, 2023 at 13:17

The price is based on the balance of tokens in the pools reserves. For "price" you can usually just divide one reserve by the other (v2 pools and many others).

To give a quote (real price per unit), the quantity traded in or desired out needs to be known, as the size of the order will effect the average cost.
Similar to how with a centralised order book an asset could trade at \$10, but a large enough buy that takes out all sell orders at \$11 and \$12 would leave the buyer paying somewhere in between.

AMM's are designed to always be able facilitate a trade, you may not get what you want (price impact), but it will be able to give you something because the price adjusts to keep the k constant.

So price on the pool changes as liquidity changes. To work out what you can get for a given amount, you would need to look at the calculation in the contract performing the swaps. In the most famous case of Uniswap v2, the sums are like this:

``````def getAmountOut(amountIn: int, reserveIn: int, reserveOut: int) -> int:
amountInWithFee = amountIn * 997
numerator = amountInWithFee * reserveOut
denominator = (reserveIn * 1000) + amountInWithFee
amountOut = numerator / denominator
return int(amountOut)

def getAmountIn(amountOut: int, reserveIn: int, reserveOut: int) -> int:
numerator = (reserveIn * amountOut) * 1000
denominator = (reserveOut - amountOut) * 997
amountIn = (numerator / denominator) + 1
return int(amountIn)

``````
• Thank you so much for your answer. Can you explain a bit about the calculations? Why it's `997` is it because of the 0.3% fee ? Aug 17, 2023 at 9:32
• Spot on, it is the hardcoded 0.3% fee on each swap, left in the pool to be shared amongst LP providers.
– Maka
Aug 17, 2023 at 12:31
• Also why we are multiplying with `1000` here `(reserveIn * 1000)` & `(reserveIn * amountOut) * 1000` and why is the `+ 1` is here `amountIn = (numerator / denominator) + 1` Aug 17, 2023 at 13:21
• 1: Solidity can't handle fractions natively, so we multiply numerator by 997 and denominator by 1000 for same result. 2: To round up, so it is always enough.
– Maka
Aug 17, 2023 at 16:15