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: standardize event data for mint and burn none-interest #305

Closed
Closed
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
57 changes: 49 additions & 8 deletions apps/data_handler/handler_tools/data_parser/nostra.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
InterestRateModelEventData,
DebtTransferEventData,
BearingCollateralMintEventData,
BearingCollateralBurnEventData
BearingCollateralBurnEventData,
NonInterestBearingCollateralMintEventData,
NonInterestBearingCollateralBurnEventData
)


Expand All @@ -22,17 +24,14 @@ def parse_interest_rate_model_event(
) -> InterestRateModelEventData:
"""
Parses the interest rate model event data into a human-readable format.

The event data is fetched from on-chain logs and is structured in the following way:
- event_data[0]: The debt token address (as a hexadecimal string).
- event_data[5]: The lending interest rate index (as a hexadecimal in 18 decimal places).
- event_data[7]: The borrow interest rate index (as a hexadecimal in 18 decimal places).

Args:
event_data (List[Any]): A list containing the raw event data.
Expected order: [debt_token, lending_rate, _, borrow_rate, _,
lending_index, _, borrow_index, _]

Returns:
InterestRateModelEventData: A model with the parsed event data.
"""
Expand All @@ -41,12 +40,54 @@ def parse_interest_rate_model_event(
lending_index=event_data[5],
borrow_index=event_data[7]
)

@classmethod
def parse_non_interest_bearing_collateral_mint_event(
cls, event_data: list[Any]
) -> NonInterestBearingCollateralMintEventData:
"""
Parses the non-interest bearing collateral mint event data into a human-readable format.

The event data is structured as follows:
- event_data[0]: sender address
- event_data[1]: recipient address
- event_data[2]: raw amount

def parse_non_interest_bearing_collateral_mint_event(self):
pass
Args:
event_data (list[Any]): A list containing the raw event data with 3 elements:
sender, recipient, and raw amount.

def parse_non_interest_bearing_collateral_burn_event(self):
pass
Returns:
NonInterestBearingCollateralMintEventData: A model with the parsed event data.
"""
return NonInterestBearingCollateralMintEventData(
sender=event_data[0],
recipient=event_data[1],
raw_amount=event_data[2]
)

@classmethod
def parse_non_interest_bearing_collateral_burn_event(
cls, event_data: list[Any]
) -> NonInterestBearingCollateralBurnEventData:
"""
Parses the non-interest bearing collateral burn event data into a human-readable format.

The event data is structured as follows:
- event_data[0]: user address
- event_data[1]: face amount

Args:
event_data (list[Any]): A list containing the raw event data with 2 elements:
user and face amount.

Returns:
NonInterestBearingCollateralBurnEventData: A model with the parsed event data.
"""
return NonInterestBearingCollateralBurnEventData(
user=event_data[0],
face_amount=event_data[1]
)

def parse_interest_bearing_collateral_mint_event(self, event_data: list[Any]) -> BearingCollateralMintEventData:
"""
Expand Down
105 changes: 105 additions & 0 deletions apps/data_handler/handler_tools/data_parser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,111 @@ def validate_numeric_string(cls, value: str, info: ValidationInfo) -> Decimal:
raise ValueError(
f"{info.field_name} field is not a valid hexadecimal number"
)

class NonInterestBearingCollateralMintEventData(BaseModel):
"""
Serializer for non-interest bearing collateral mint event data.

Attributes:
sender (str): The address of the sender
recipient (str): The address of the recipient
raw_amount (str): The raw amount being transferred
"""
sender: str
recipient: str
raw_amount: Decimal

@field_validator("sender", "recipient")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates that the provided address starts with '0x' and
formats it with leading zeros.

Args:
value (str): The address string to validate.

Returns:
str: The validated and formatted address.

Raises:
ValueError: If the provided address does not start with '0x'.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)

@field_validator("raw_amount")
def validate_numeric_string(cls, value: str, info: ValidationInfo) -> Decimal:
"""
Validates that the provided amount is numeric and converts it to a Decimal.

Args:
value (str): The amount string to validate.

Returns:
Decimal: The validated and converted amount as a Decimal.

Raises:
ValueError: If the provided amount is not numeric.
"""
try:
return Decimal(int(value, 16))
except ValueError:
raise ValueError(
f"{info.field_name} field is not a valid hexadecimal number"
)

class NonInterestBearingCollateralBurnEventData(BaseModel):
"""
Serializer for non-interest bearing collateral burn event data.

Attributes:
user (str): The address of the user
face_amount (str): The face amount being burned
"""
user: str
face_amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates that the provided address starts with '0x' and
formats it with leading zeros.

Args:
value (str): The address string to validate.

Returns:
str: The validated and formatted address.

Raises:
ValueError: If the provided address does not start with '0x'.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)

@field_validator("face_amount")
def validate_numeric_string(cls, value: str, info: ValidationInfo) -> Decimal:
"""
Validates that the provided amount is numeric and converts it to a Decimal.

Args:
value (str): The amount string to validate.

Returns:
Decimal: The validated and converted amount as a Decimal.

Raises:
ValueError: If the provided amount is not numeric.
"""
try:
return Decimal(int(value, 16))
except ValueError:
raise ValueError(
f"{info.field_name} field is not a valid hexadecimal number"
)


class InterestRateModelEventData(BaseModel):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
DebtTransferEventData,
BearingCollateralMintEventData,
BearingCollateralBurnEventData,
NonInterestBearingCollateralMintEventData,
NonInterestBearingCollateralBurnEventData
)

from .zklend import (
Expand All @@ -25,6 +27,8 @@
"DebtTransferEventData",
"BearingCollateralMintEventData",
"BearingCollateralBurnEventData",
"NonInterestBearingCollateralMintEventData",
"NonInterestBearingCollateralBurnEventData",
# zkLend serializers
"AccumulatorsSyncEventData",
"BorrowingEventData",
Expand Down
94 changes: 94 additions & 0 deletions apps/data_handler/handler_tools/data_parser/serializers/nostra.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,97 @@ def validate_address(cls, value: str, info: ValidationInfo) -> str:
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class NonInterestBearingCollateralMintEventData(BaseModel):
"""
Serializer for non-interest bearing collateral mint event data.

Attributes:
sender (str): The address of the sender
recipient (str): The address of the recipient
raw_amount (Decimal): The raw amount being transferred
"""
sender: str
recipient: str
raw_amount: Decimal

@field_validator("sender", "recipient")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates that the provided address starts with '0x' and
formats it with leading zeros.
Args:
value (str): The address string to validate.
Returns:
str: The validated and formatted address.
Raises:
ValueError: If the provided address does not start with '0x'.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)

@field_validator("raw_amount")
def validate_numeric_string(cls, value: str, info: ValidationInfo) -> Decimal:
"""
Validates that the provided amount is numeric and converts it to a Decimal.
Args:
value (str): The amount string to validate.
Returns:
Decimal: The validated and converted amount as a Decimal.
Raises:
ValueError: If the provided amount is not numeric.
"""
try:
return Decimal(int(value, 16))
except ValueError:
raise ValueError(
f"{info.field_name} field is not a valid hexadecimal number"
)


class NonInterestBearingCollateralBurnEventData(BaseModel):
"""
Serializer for non-interest bearing collateral burn event data.

Attributes:
user (str): The address of the user
face_amount (Decimal): The face amount being burned
"""
user: str
face_amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates that the provided address starts with '0x' and
formats it with leading zeros.
Args:
value (str): The address string to validate.
Returns:
str: The validated and formatted address.
Raises:
ValueError: If the provided address does not start with '0x'.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)

@field_validator("face_amount")
def validate_numeric_string(cls, value: str, info: ValidationInfo) -> Decimal:
"""
Validates that the provided amount is numeric and converts it to a Decimal.
Args:
value (str): The amount string to validate.
Returns:
Decimal: The validated and converted amount as a Decimal.
Raises:
ValueError: If the provided amount is not numeric.
"""
try:
return Decimal(int(value, 16))
except ValueError:
raise ValueError(
f"{info.field_name} field is not a valid hexadecimal number"
)
Loading