-
Notifications
You must be signed in to change notification settings - Fork 19
Feat: Add a helper function that adds price to a swap events dataframe #9
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
ah i think i forgot to pull before committing. ruh roh. |
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 PR
Helper function to add a new column with sqrtPriceX96 converted to price | ||
""" | ||
sqrtPrice_list = data.select(pl.col("sqrtPriceX96")).to_series().to_list() | ||
price_list = [(int(i)/(2 ** 96))**2 for i in sqrtPrice_list] |
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.
i think i would prefer floats here. sometimes prices are very small because they are inverted to your expectation/high supply.
i think you should also be able to apply map_batches
sqrtPrice_list = data.select(pl.col("sqrtPriceX96")).to_series().to_list() | ||
price_list = [(int(i)/(2 ** 96))**2 for i in sqrtPrice_list] | ||
|
||
df = data.with_columns(price = pl.Series(values=price_list, dtype=pl.UInt64)) |
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.
also float here
"amount0":pl.Float64, | ||
"amount1":pl.Float64, | ||
"sqrtPriceX96":pl.Float64, | ||
"liquidity":pl.Float64, |
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.
might be nice to see if this is convertible to a no precision loss value since liquidity is must be less than an int128, but in practice is quite a bit smaller than that (due to the max liquidity in tick math)
"block_timestamp":pl.Datetime, | ||
"amount0":pl.Float64, | ||
"amount1":pl.Float64, | ||
"sqrtPriceX96":pl.Float64, |
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.
heres as well since X96 is also a uint160
TODO: all fixes + would be nice to use a consistent datetime granularity across swaps and mint/burn |
Working with pool price is likely a common task for many explorations. However, Polars only supports uints up to 64 bytes, whereas sqrtPriceX96 can be a uint up to 160 bytes, and is commonly > 2^64. Therefore, working with sqrtPriceX96 in Polars natively causes overflows at the rust level, and casting to Float64 is insufficient to make it work (as far as Grace and I could figure out)
It would be helpful to be able to add a
price
column that converts down to a more manageable number (i.e., taking 2^160 down to 2^64).This is not yet ready. It doesn't guarantee a lack of overflows, as the maximum value of
price
is 2^128.