Skip to content

Commit

Permalink
Merge pull request #294 from 0xElectrifier/master
Browse files Browse the repository at this point in the history
Added data serializer for process_debt_mint_event and process_debt_burn_event functions
  • Loading branch information
djeck1432 authored Nov 22, 2024
2 parents 187de39 + 6715b96 commit 04dfe08
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 8 deletions.
43 changes: 39 additions & 4 deletions apps/data_handler/handler_tools/data_parser/nostra.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
"""
This module contains the logic to parse the nostra data to human-readable format.
"""

from data_handler.handler_tools.data_parser.serializers import (
DebtMintEventData,
DebtBurnEventData
)


class NostraDataParser:
Expand All @@ -23,8 +30,36 @@ def parse_interest_bearing_collateral_burn_event(self):
def parse_debt_transfer_event(self):
pass

def parse_debt_mint_event(self):
pass
def parse_debt_mint_event(self, event_data: list[Any]) -> DebtMintEventData:
"""
Parses the debt mint event data into a human-readable format using the
DebtMintEventData serializer.
Args:
event_data (List[Any]): A list containing the raw debt mint event data,
typically with 2 elements: user and amount.
Returns:
DebtMintEventData: A Pydantic model with the parsed and validated event data in a human-readable format.
"""
return DebtMintEventData(
user=event_data[0],
token=event_data[1]
)

def parse_debt_burn_event(self, event_data: list[Any]) -> DebtBurnEventData:
"""
Parses the debt burn event data into a human-readable format using the
DebtBurnEventData serializer.
Args:
event_data (List[Any]): A list containing the raw debt burn event data,
typically with 2 elements: user and amount.
def parse_debt_burn_event(self):
pass
Returns:
DebtBurnEventData: A Pydantic model with the parsed and validated event data in a human-readable format.
"""
return DebtBurnEventData(
user=event_data[0],
amount=event_data[1]
)
112 changes: 112 additions & 0 deletions apps/data_handler/handler_tools/data_parser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,115 @@ def validate_valid_addresses(cls, value: str, info: ValidationInfo) -> str:
if not value.startswith("0x"):
raise ValueError("Invalid address provided for %s" % info.field_name)
return add_leading_zeros(value)


class DebtMintEventData(BaseModel):
"""
Class for representing debt mint event data.
Attributes:
user (str): The address of the user associated with the debt mint event.
amount (str): The amount minted in the debt mint event.
Returns:
DebtMintEventData: A Pydantic model with the parsed and validated event data in a human-readable format.
"""

user: str
amount: str

@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("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 DebtBurnEventData(BaseModel):
"""
Class for representing debt burn event data.
Attributes:
user (str): The address of the user associated with the debt burn event.
amount (str): The amount burned in the debt burn event.
Returns:
DebtBurnEventData: A Pydantic model with the parsed and validated event data in a human-readable format.
"""

user: str
amount: str

@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("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"
)
9 changes: 5 additions & 4 deletions apps/data_handler/handlers/loan_states/nostra_alpha/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from data_handler.handlers.helpers import blockchain_call, get_addresses, get_symbol
from data_handler.handlers.settings import TokenSettings
from data_handler.handlers.state import NOSTRA_ALPHA_SPECIFIC_TOKEN_SETTINGS
from data_handler.handler_tools.data_parser.nostra import NostraDataParser

from shared.constants import ProtocolIDs
from shared.helpers import add_leading_zeros
Expand Down Expand Up @@ -606,8 +607,8 @@ def process_debt_mint_event(self, event: pd.Series) -> None:
# The order of the values in the `data` column is: `user`, `amount`, ``.
# Example:
# https://starkscan.co/event/0x030d23c4769917bc673875e107ebdea31711e2bdc45e658125dbc2e988945f69_4.
user = add_leading_zeros(event["data"][0])
face_amount = decimal.Decimal(str(int(event["data"][1], base=16)))
data = NostraDataParser.parse_debt_mint_event(event["data"])
user, face_amount = data.user, data.amount
else:
raise ValueError("Event = {} has an unexpected structure.".format(event))
if user == self.DEFERRED_BATCH_CALL_ADAPTER_ADDRESS:
Expand Down Expand Up @@ -637,8 +638,8 @@ def process_debt_burn_event(self, event: pd.Series) -> None:
# The order of the values in the `data` column is: `user`, `amount`, ``.
# Example:
# https://starkscan.co/event/0x002e4ee376785f687f32715d8bbed787b6d0fa9775dc9329ca2185155a139ca3_5.
user = add_leading_zeros(event["data"][0])
face_amount = decimal.Decimal(str(int(event["data"][1], base=16)))
data = NostraDataParser.parse_debt_mint_event(event["data"])
user, face_amount = data.user, data.amount
else:
raise ValueError("Event = {} has an unexpected structure.".format(event))
if user == self.DEFERRED_BATCH_CALL_ADAPTER_ADDRESS:
Expand Down

0 comments on commit 04dfe08

Please sign in to comment.