feat: improved estimate_price_impact helper function; added factory contract for V3#202
Conversation
|
@ErikBjare ^^ |
ErikBjare
left a comment
There was a problem hiding this comment.
Very nice! Just a couple things to fix
uniswap/uniswap.py
Outdated
| den0 = self.get_token(token_out).decimals | ||
| den1 = self.get_token(token_in).decimals | ||
| sqrtPriceX96 = pool_contract.functions.slot0().call()[0] | ||
| raw_price = (sqrtPriceX96*sqrtPriceX96*10**den1>>(96*2))/(10**den0) |
There was a problem hiding this comment.
Wow, awesome work figuring this out! (I've been meaning to...)
There was a problem hiding this comment.
It's a kind of magic. Long story short
https://ethereum.stackexchange.com/questions/98685/computing-the-uniswap-v3-pair-price-from-q64-96-number
But I have to notice that mentioned snippet works correctly only if both tokens have decimals == 18 that is wrong in the general case. It needs to determine actual values. Also, if we need this price for ETH-USDC pair but pool.token0==USDC and pool.token1==ETH then we need to use denominators in reverse order. That's why it requires this ugly if-else just before the calculation.
Also, the final impact value differs a bit from uniswap's one. Delta is ~0.0x% I suppose they apply the taker's fee somewhere. I don't need absolute precision in my tasks (and I really doubt someone needs it in this particular case) so I've just omitted that fact in my own realization.
Regarding other comments - sure, I'll make a fix.
|
Sorry for the delay. Looks like the typechecking fails, but gonna go ahead and merge this anyway. |
Test code (12 ETH to VXV):
Output:
It is easy to see that there is the only a proper pool (UniV3 1% fee). Impact equal to 1 basically means that pool doesn't exist or has exact 0 liquidity. Also, this method works for direct pools only as it is not trivial to trace all pools' liquidity in the case of multihop.