Skip to content

Commit

Permalink
Merge pull request #319 from akintewe/feature/arrange-serializers
Browse files Browse the repository at this point in the history
Refactor: complete serializers reorganization
  • Loading branch information
djeck1432 authored Nov 25, 2024
2 parents cab73b9 + 11132a6 commit 2352475
Show file tree
Hide file tree
Showing 3 changed files with 424 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from .nostra import (
DebtMintEventData,
DebtBurnEventData,
InterestRateModelEventData,
DebtTransferEventData,
BearingCollateralMintEventData,
BearingCollateralBurnEventData,
)

from .zklend import (
AccumulatorsSyncEventData,
BorrowingEventData,
RepaymentEventData,
DepositEventData,
LiquidationEventData,
WithdrawalEventData,
CollateralEnabledDisabledEventData,
)

__all__ = [
# Nostra serializers
"DebtMintEventData",
"DebtBurnEventData",
"InterestRateModelEventData",
"DebtTransferEventData",
"BearingCollateralMintEventData",
"BearingCollateralBurnEventData",
# zkLend serializers
"AccumulatorsSyncEventData",
"BorrowingEventData",
"RepaymentEventData",
"DepositEventData",
"LiquidationEventData",
"WithdrawalEventData",
"CollateralEnabledDisabledEventData",
]
199 changes: 199 additions & 0 deletions apps/data_handler/handler_tools/data_parser/serializers/nostra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
from decimal import Decimal
from pydantic import BaseModel, ValidationInfo, field_validator
from shared.helpers import add_leading_zeros


class DebtMintEventData(BaseModel):
"""
Data model representing a debt mint event in the system.
Attributes:
user: The user address for whom debt is being minted.
amount: The amount of debt being minted as a Decimal.
"""

user: str
amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class DebtBurnEventData(BaseModel):
"""
Data model representing a debt burn event in the system.
Attributes:
user: The user address for whom debt is being burned.
amount: The amount of debt being burned as a Decimal.
"""

user: str
amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class InterestRateModelEventData(BaseModel):
"""
Data model representing an interest rate model event in the system.
Attributes:
debt_token: The debt token address.
lending_index: The lending index value as a Decimal.
borrow_index: The borrowing index value as a Decimal.
"""

debt_token: str
lending_index: Decimal
borrow_index: Decimal

@field_validator("debt_token")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class DebtTransferEventData(BaseModel):
"""
Data model representing a debt transfer event in the system.
Attributes:
sender: The address of the sender.
recipient: The address of the recipient.
amount: The amount being transferred as a Decimal.
"""

sender: str
recipient: str
amount: Decimal

@field_validator("sender", "recipient")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class BearingCollateralMintEventData(BaseModel):
"""
Data model representing a bearing collateral mint event in the system.
Attributes:
user: The user address for whom collateral is being minted.
amount: The amount of collateral being minted as a Decimal.
"""

user: str
amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)


class BearingCollateralBurnEventData(BaseModel):
"""
Data model representing a bearing collateral burn event in the system.
Attributes:
user: The user address for whom collateral is being burned.
amount: The amount of collateral being burned as a Decimal.
"""

user: str
amount: Decimal

@field_validator("user")
def validate_address(cls, value: str, info: ValidationInfo) -> str:
"""
Validates if the value is a valid address and formats it to have leading zeros.
Args:
value (str): The address string to validate.
info (ValidationInfo): Validation context information.
Raises:
ValueError: If the provided address is invalid.
Returns:
str: Formatted address with leading zeros.
"""
if not value.startswith("0x"):
raise ValueError(f"Invalid address provided for {info.field_name}")
return add_leading_zeros(value)
Loading

0 comments on commit 2352475

Please sign in to comment.