-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from jaykayudo/refactor/standard-liquidation-…
…event-data Standardized liquidation event data
- Loading branch information
Showing
5 changed files
with
248 additions
and
44 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 |
---|---|---|
@@ -1 +1,58 @@ | ||
from decimal import Decimal | ||
|
||
from pydantic import BaseModel, ValidationInfo, field_validator | ||
|
||
from shared.helpers import add_leading_zeros | ||
|
||
|
||
class LiquidationEventData(BaseModel): | ||
""" | ||
Class for converting liquadation event to an object model. | ||
Attributes: | ||
liquidator: The address of the liquidator. | ||
user: The address of the user. | ||
debt_token: The address of the debt token. | ||
debt_raw_amount: A numeric string of the debt_raw_amount converted to decimal. | ||
debt_face_amount: A numeric string of the debt_face_amount converted to decimal. | ||
collateral_token: The address of collateral token. | ||
collateral_amount: A numeric string of the collateral_amount converted to decimal. | ||
""" | ||
|
||
liquidator: str | ||
user: str | ||
debt_token: str | ||
debt_raw_amount: str | ||
debt_face_amount: str | ||
collateral_token: str | ||
collateral_amount: str | ||
|
||
@field_validator("liquidator", "user", "debt_token", "collateral_token") | ||
def validate_valid_addresses(cls, value: str, info: ValidationInfo) -> str: | ||
""" | ||
Check if the value is an address and format it to having leading zeros. | ||
Raises: | ||
ValueError | ||
Returns: | ||
str | ||
""" | ||
if not value.startswith("0x"): | ||
raise ValueError("Invalid address provided for %s" % info.field_name) | ||
return add_leading_zeros(value) | ||
|
||
@field_validator("debt_raw_amount", "debt_face_amount", "collateral_amount") | ||
def validate_valid_numbers(cls, value: str, info: ValidationInfo) -> Decimal: | ||
""" | ||
Check if the value is a digit and convert it to a decimal from base 16 int conversion. | ||
Raises: | ||
ValueError | ||
Returns: | ||
Decimal | ||
""" | ||
if not value.isdigit(): | ||
raise ValueError("%s field is not numeric" % info.field_name) | ||
return Decimal(str(int(value, base=16))) |
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
Oops, something went wrong.