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 2 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
53 changes: 50 additions & 3 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 Down Expand Up @@ -41,8 +43,53 @@ def parse_interest_rate_model_event(
borrow_index=event_data[7]
)

def parse_non_interest_bearing_collateral_mint_event(self):
pass
@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

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

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_non_interest_bearing_collateral_burn_event(self):
pass
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 @@ -445,6 +445,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
20 changes: 18 additions & 2 deletions apps/data_handler/handlers/loan_states/nostra_alpha/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def process_non_interest_bearing_collateral_mint_event(self, event: pd.Series) -
sender = add_leading_zeros(event["data"][0])
Copy link
Collaborator

Choose a reason for hiding this comment

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

why you didn't remove these fields?

recipient = add_leading_zeros(event["data"][1])
raw_amount = decimal.Decimal(str(int(event["data"][2], base=16)))
parsed_event = NostraDataParser.parse_non_interest_bearing_collateral_mint_event(event["data"])
else:
raise ValueError("Event = {} has an unexpected structure.".format(event))
if self.ZERO_ADDRESS in {sender, recipient}:
Expand All @@ -398,6 +399,7 @@ def process_non_interest_bearing_collateral_mint_event(self, event: pd.Series) -
)
)


def process_collateral_mint_event(self, event: pd.Series) -> None:
"""Process collateral addition event for a loan."""
if event["keys"] == [self.MINT_KEY]:
Expand Down Expand Up @@ -500,26 +502,40 @@ def process_non_interest_bearing_collateral_burn_event(self, event: pd.Series) -
# Example:
# https://starkscan.co/event/0x00744177ee88dd3d96dda1784e2dff50f0c989b7fd48755bc42972af2e951dd6_1.
user = event["data"][0]
if user == self.IGNORE_USER:
parsed_event = NostraDataParser.parse_non_interest_bearing_collateral_burn_event(event["data"])
if user == self.IGNORE_USER | parsed_event == self.IGNORE_USER:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did you change this code?
and why parsed event return only user data here if it should return more values?

return
token = self.ADDRESSES_TO_TOKENS[event["from_address"]]
raw_amount = parsed_event.face_amount / self.collateral_interest_rate_models.values[token]
face_amount = decimal.Decimal(str(int(event["data"][1], base=16)))
raw_amount = face_amount / self.collateral_interest_rate_models.values[token]
# add additional info block and timestamp
self.loan_entities[user].extra_info.block = event["block_number"]
self.loan_entities[user].extra_info.timestamp = event["timestamp"]
self.loan_entities[parsed_event.user].extra_info.block = event["block_number"]
self.loan_entities[parsed_event.user].extra_info.timestamp = event["timestamp"]

self.loan_entities[user].non_interest_bearing_collateral.increase_value(
token=token, value=-raw_amount
)
self.loan_entities[parsed_event.user].non_interest_bearing_collateral.increase_value(
token=token, value=-raw_amount
)
self.loan_entities[user].collateral.values = {
token: (
self.loan_entities[user].non_interest_bearing_collateral.values[token] +
self.loan_entities[user].interest_bearing_collateral.values[token]
)
for token in NOSTRA_ALPHA_SPECIFIC_TOKEN_SETTINGS
}
if user == self.verbose_user:
self.loan_entities[parsed_event.user].collateral.values = {
token: (
self.loan_entities[parsed_event.user].non_interest_bearing_collateral.values[token] +
self.loan_entities[parsed_event.user].interest_bearing_collateral.values[token]
)
for token in NOSTRA_ALPHA_SPECIFIC_TOKEN_SETTINGS
}
if user == self.verbose_user | parsed_event.user == self.verbose_user:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did you change it

logging.info(
"In block number = {}, non-interest-bearing collateral of raw amount = {} of token = {} was withdrawn."
.format(
Expand Down