This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Schema] Order Rewards to use JSON field for variable data. (#18)
The schema proposed here is [order_uid: str | tx_hash: str | solver: str | data ] where (currently) data takes the following form: Data { surplus_fee: str (we can make make this nullable, or use zero in place of null) amount: float safe_liquidity: boolean (nullable) }
- Loading branch information
Showing
5 changed files
with
111 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Model for Order Rewards Data""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
from pandas import DataFrame | ||
|
||
|
||
@dataclass | ||
class OrderRewards: | ||
""" | ||
This class provides a transformation interface for the Dataframe we fetch from the orderbook | ||
""" | ||
|
||
@classmethod | ||
def from_pdf_to_dune_records(cls, rewards_df: DataFrame) -> list[dict[str, Any]]: | ||
"""Converts Pandas DataFrame into the expected stream type for Dune""" | ||
return [ | ||
{ | ||
"order_uid": row["order_uid"], | ||
"tx_hash": row["tx_hash"], | ||
"solver": row["solver"], | ||
"data": { | ||
"surplus_fee": str(row["surplus_fee"]), | ||
"amount": float(row["amount"]), | ||
"safe_liquidity": row["safe_liquidity"], | ||
}, | ||
} | ||
for row in rewards_df.to_dict(orient="records") | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import unittest | ||
|
||
import pandas as pd | ||
|
||
from src.models.order_rewards_schema import OrderRewards | ||
|
||
|
||
class TestModelOrderRewards(unittest.TestCase): | ||
def test_order_rewards_transformation(self): | ||
rewards_df = pd.DataFrame( | ||
{ | ||
"order_uid": ["0x01", "0x02", "0x03"], | ||
"solver": ["0x51", "0x52", "0x53"], | ||
"tx_hash": ["0x71", "0x72", "0x73"], | ||
"surplus_fee": [12345678910111213, 0, 0], | ||
"amount": [40.70410, 39.00522, 0], | ||
"safe_liquidity": [None, True, False], | ||
} | ||
) | ||
|
||
self.assertEqual( | ||
[ | ||
{ | ||
"order_uid": "0x01", | ||
"solver": "0x51", | ||
"tx_hash": "0x71", | ||
"data": { | ||
"surplus_fee": "12345678910111213", | ||
"amount": 40.70410, | ||
"safe_liquidity": None, | ||
}, | ||
}, | ||
{ | ||
"order_uid": "0x02", | ||
"solver": "0x52", | ||
"tx_hash": "0x72", | ||
"data": { | ||
"surplus_fee": "0", | ||
"amount": 39.00522, | ||
"safe_liquidity": True, | ||
}, | ||
}, | ||
{ | ||
"order_uid": "0x03", | ||
"solver": "0x53", | ||
"tx_hash": "0x73", | ||
"data": { | ||
"surplus_fee": "0", | ||
"amount": 0.0, | ||
"safe_liquidity": False, | ||
}, | ||
}, | ||
], | ||
OrderRewards.from_pdf_to_dune_records(rewards_df), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |