- Notifications
You must be signed in to change notification settings - Fork386
feat: improved estimate_price_impact helper function; added factory contract for V3#202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@ErikBjare ^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Wow, awesome work figuring this out! (I've been meaning to...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
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 havedecimals
== 18 that is wrong in the general case. It needs to determine actual values. Also, if we need this price for ETH-USDC pair butpool.token0
==USDC andpool.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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Sorry for the delay. Looks like the typechecking fails, but gonna go ahead and merge this anyway. |
Uh oh!
There was an error while loading.Please reload this page.
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.