Skip to content
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

Feat: Add a helper function that adds price to a swap events dataframe #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mud2monarch
Copy link
Member

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.

@mud2monarch
Copy link
Member Author

ah i think i forgot to pull before committing. ruh roh.

@aadams aadams self-requested a review May 16, 2024 23:05
Copy link
Collaborator

@aadams aadams left a 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]
Copy link
Collaborator

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))
Copy link
Collaborator

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,
Copy link
Collaborator

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,
Copy link
Collaborator

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

@mud2monarch
Copy link
Member Author

TODO: all fixes + would be nice to use a consistent datetime granularity across swaps and mint/burn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants