“The price of a given token on a given exchange can be calculated as the exchange contract's token_balance divided by its ether_balance.”
This description means:
On-chain price: In opposed to off-chain price, which is what you may see on various exchanges, and which can of course be different on every exchange.
Spot price: This is the rate that you will get for 1 wei, but it doesn't tell you how much you will get in return for more than 1 wei.
It is important to understand the difference between rate and return here.
A rate tells you how much you will get for 1 unit.
This is a common method of interaction when you go to any exchange on any street in any country.
For example, you go to an exchange in London and ask how much is the dollar rate, and they will tell you 1 pound = 2 dollars.
In this real-world example, the terms rate and return are equivalent, because the rate is linear, which means that for 2 pounds you'll get 4 dollars, for 3 pounds you'll get 6 dollars and so on.
On UniSwap's trading system (as in many other trading systems on the blockchain), rate and return are not equivalent.
For example, if your ETH/TKN spot-price is 10 on UniSwap, then it means that for 1 wei of your TKN, you will get 10 wei of ETH.
But for 1234 wei of your TKN, you will necessarily get less than 12340 wei of ETH.
This is because your conversion is subjected to slippage (loss).
It may lead you to think that this spot-price is a charade (a hoax).
But it is nevertheless useful for some measurements of a pool.
However, you should definitely not rely on this rate in order to calculate the expected return for some given amount.
In order to do that, you may use Y * x / (X + x)
, where:
x
is your input amount of source tokens
X
is the balance of the pool in the source token
Y
is the balance of the pool in the target token
Note that as your input amount gets closer to 1, the expected return becomes closer to the rate (i.e., the spot-price, which as quoted from your question at the top of this answer, is Y / X
).
Whoever provides the liquidity has the option to take his share of the liquidity out of the pool, at any time they want. Liquidity is never "sold off" as such, since Uniswap price is based on a bonding price curve. So the trading price simply changes between trades, reflecting which asset of the pair is bought more and which sold.
There is also no "correct" amount of liquidity you need to provide. You can provide as little as you want, or as much as you want. Remember that anyone else can also provide liquidity. Also remember, that if you provide only very little liquidity, the price will change fast and people will most likely stop trading. So the more liquidity there is, the more stable the price.
Neither asset of the pair can ever be bought fully - there is always a little bit left. So, at least in theory, the pool will remain functional always, no matter what the asset ratio is and no matter how much liquidity there is.
Best Answer
price0CumulativeLast
andprice1CumulativeLast
are indeed the latest prices of their respective tokens in Ether.getReserves
is a function that returns the current liquidity of both tokens and the timestamp of the last liquidity event.kLast
is a public variable that stores the product amount of the first token and the second token as of immediately after the most recent liquidity event. It may be useful in calculating the current price of the tokens being traded.