I would like (if possible) to infer a token price from the blockchain using UniSwap data.
After reading the following article, I understood that it is feasible:
https://blocklytics.org/blog/uniswap-api/
“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.”
My first attempt was to check some transaction data, for example this transaction:
https://etherscan.io/tx/0x10de93fd474b5c45035ea5d9ef42172ba6026e024cf77275a72c2b291b118a6d
This page explains in a more readable way (for me) the transaction:
https://etherscan.io/address/0x2bf5a5ba29e60682fc56b2fcf9ce07bef4f6196f#events
I understand we have:
uint256 tokens_sold
25661215840000000000
uint256 eth_bought
3014603688879169830
So this would mean a ratio of:
25661215840000000000 / 3014603688879169830 = 8.512302
For this token swap NMR / ETH.
This seems close to the current ration between these two tokens (prices from CoinMarketCap this morning):
203.64 / 23.77 = 8.567101
Is this approach correct?
Also, when I look at the same transaction data in BigQuery, I get:
Input = 0x95e3c50b000000000000000000000000000000000000000000000001641ef3d8f7d3c00000000000000000000000000000000000000000000000000029a07940a8276bdd000000000000000000000000000000000000000000000000000000005ecbd296
which I can decompose into:
0x95e3c50b = MethodID
000000000000000000000000000000000000000000000001641ef3d8f7d3c000
= 25661215840000000000 in Decimal
This seems fine: I find the same tokens_sold as above.
00000000000000000000000000000000000000000000000029a07940a8276bdd
= 2999530670434773981
What is this?
000000000000000000000000000000000000000000000000000000005ecbd296
= 1590416022
What is this?
I am struggling to find the same eth_bought amount as above: 3014603688879169830.
Can someone help?
Best Answer
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 tokensX
is the balance of the pool in the source tokenY
is the balance of the pool in the target tokenNote 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
).