-
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?
Changes from all commits
f629ef4
139ecb8
27e5597
360c2bd
4c0591a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
The function `readFromMemoryOrDisk` intakes a parquet (as components of a filepath) and outputs a Polars dataframe. After running the function, here is the schema for `pool_mint_burn_events` and `pool_swap_events` dataframes. | ||
|
||
**Swaps** | ||
| Column | Polars datatype | | ||
|-------------------|-----------------| | ||
| chain_name | str | | ||
| address | str | | ||
| block_timestamp | datetime[MiS] | | ||
| block_number | i64 | | ||
| transaction_hash | str | | ||
| sender | str | | ||
| recipient | str | | ||
| amount0 | f64 | | ||
| amount1 | f64 | | ||
| sqrtPriceX96 | f64 | | ||
| liquidity | f64 | | ||
| tick | i32 | | ||
| from_address | str | | ||
| to_address | str | | ||
| transaction_index | i64 | | ||
| gas_price | i64 | | ||
| gas_used | i64 | | ||
| l1_fee | i64 | | ||
|
||
**Mint & Burn** | ||
| Column | Polars datatype | | ||
|-------------------|-----------------| | ||
| chain_name | str | | ||
| address | str | | ||
| block_timestamp | datetime[us] | | ||
| block_number | i64 | | ||
| transaction_hash | str | | ||
| log_index | i64 | | ||
| amount | f64 | | ||
| amount0 | f64 | | ||
| amount1 | f64 | | ||
| owner | str | | ||
| tick_lower | i64 | | ||
| tick_upper | i64 | | ||
| type_of_event | f64 | | ||
| to_address | str | | ||
| from_address | str | | ||
| transaction_index | i64 | | ||
| gas_price | i64 | | ||
| gas_used | i64 | | ||
| l1_fee | i64 | |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,3 +151,16 @@ def get_next_sqrtPrice(ratioA, liq, amount, zeroForOne): | |
sqrtPrice_next = get_next_price_amount1(ratioA, liq, amount, zeroForOne) | ||
|
||
return sqrtPrice_next | ||
|
||
def addPrice( | ||
data: pl.DataFrame | ||
) -> pl.DataFrame: | ||
""" | ||
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] | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. also float here |
||
|
||
return df |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,19 @@ def readFromMemoryOrDisk(self, data, data_path, save=False): | |
.with_columns( | ||
as_of=pl.col("block_number") + pl.col("transaction_index") / 1e4 | ||
) | ||
.cast( | ||
{ | ||
"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 commentThe reason will be displayed to describe this comment to others. Learn more. heres as well since X96 is also a uint160 |
||
"liquidity":pl.Float64, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
"tick":pl.Int32, # tick in v3 is int24 | ||
"gas_price":pl.Int64, | ||
"gas_used":pl.Int64, | ||
"l1_fee":pl.Int64 | ||
} | ||
) | ||
.collect() | ||
.sort("as_of") | ||
) | ||
|
@@ -156,6 +169,12 @@ def readFromMemoryOrDisk(self, data, data_path, save=False): | |
"tick_lower": pl.Int64, | ||
"tick_upper": pl.Int64, | ||
"type_of_event": pl.Float64, | ||
"block_timestamp":pl.Datetime, | ||
"amount0":pl.Float64, | ||
"amount1":pl.Float64, | ||
"gas_price":pl.Int64, | ||
"gas_used":pl.Int64, | ||
"l1_fee":pl.Int64 | ||
} | ||
) | ||
.with_columns( | ||
|
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