Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions v3/README.md
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 |
13 changes: 13 additions & 0 deletions v3/helpers/swap_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Contributor

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


df = data.with_columns(price = pl.Series(values=price_list, dtype=pl.UInt64))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also float here


return df
19 changes: 19 additions & 0 deletions v3/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor

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

"liquidity":pl.Float64,
Copy link
Contributor

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)

"tick":pl.Int32, # tick in v3 is int24
"gas_price":pl.Int64,
"gas_used":pl.Int64,
"l1_fee":pl.Int64
}
)
.collect()
.sort("as_of")
)
Expand All @@ -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(
Expand Down