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: add missing models to Transformzklend #278

Merged
Merged
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
150 changes: 150 additions & 0 deletions apps/data_handler/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
from data_handler.db.models.zklend_events import (
AccumulatorsSyncEventModel,
LiquidationEventModel,
RepaymentEventModel,
DepositEventModel,
CollateralEnabledDisabledEventModel,
BorrowingEventModel,
WithdrawalEventModel
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -630,6 +635,151 @@ def create_liquidation_event(
finally:
db.close()

def create_repayment_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a RepaymentEventModel record in the database.
:param protocol_id: The protocol ID for the event.
:param event_name: The name of the event.
:param block_number: The block number associated with the event.
:param event_data: A dictionary containing 'user', 'amount'.
"""
db = self.Session()
try:
event = RepaymentEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
repayer=event_data.get("repayer"),
beneficiary=event_data.get("beneficiary"),
token=event_data.get("token"),
raw_amount=event_data.get("raw_amount"),
face_amount=event_data.get("face_amount"),
)
db.add(event)
db.commit()
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating RepaymentEventModel: {e}")
raise e
finally:
db.close()

def create_borrowing_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a BorrowingEventModel record in the database.
:param protocol_id: The protocol ID for the event.
:param event_name: The name of the event.
:param block_number: The block number associated with the event.
:param event_data: A dictionary containing 'user', 'token', 'raw_amount', 'face_amount'.
"""
db = self.Session()
try:
event = BorrowingEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
token=event_data.get("token"),
raw_amount=event_data.get("raw_amount"),
face_amount=event_data.get("face_amount"),
)
db.add(event)
db.commit()
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating BorrowingEventModel: {e}")
raise e
finally:
db.close()

def create_deposit_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a DepositEventModel record in the database.
:param protocol_id: The protocol ID for the event.
:param event_name: The name of the event.
:param block_number: The block number associated with the event.
:param event_data: A dictionary containing 'user', 'token', 'face_amount'.
"""
db = self.Session()
try:
event = DepositEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
token=event_data.get("token"),
face_amount=event_data.get("face_amount"),
)
db.add(event)
db.commit()
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating DepositEventModel: {e}")
raise e

def create_withdrawal_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a WithdrawalEventModel record in the database.
:param protocol_id: The protocol ID for the event.
:param event_name: The name of the event.
:param block_number: The block number associated with the event.
:param event_data: A dictionary containing 'user', 'token', 'face_amount'.
"""
db = self.Session()
try:
event = WithdrawalEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
token=event_data.get("token"),
amount=event_data.get("amount"),
)
db.add(event)
db.commit()
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating WithdrawalEventModel: {e}")
raise e
finally:
db.close()

def create_collateral_enabled_disabled_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a CollateralEnabledDisabledEventModel record in the database.
:param protocol_id: The protocol ID for the event.
:param event_name: The name of the event.
:param block_number: The block number associated with the event.
:param event_data: A dictionary containing 'user', 'token'.
"""
db = self.Session()
try:
event = CollateralEnabledDisabledEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
token=event_data.get("token"),
)
db.add(event)
db.commit()
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating CollateralEnabledDisabledEventModel: {e}")
raise e
finally:
db.close()

def get_all_events(
self,
protocol_id: Optional[str] = None,
Expand Down
4 changes: 1 addition & 3 deletions apps/data_handler/handler_tools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class ProtocolAddresses:
This class contains the addresses of the contracts that are used
"""

ZKLEND_MARKET_ADDRESSES: str = field(
default_factory=lambda: "0x04c0a5193d58f74fbace4b74dcf65481e734ed1714121bdc571da345540efa05"
)
ZKLEND_MARKET_ADDRESSES: str = "0x04c0a5193d58f74fbace4b74dcf65481e734ed1714121bdc571da345540efa05"
HASHSTACK_V0_ADDRESSES: Set[str] = field(
default_factory=lambda:
{"0x03dcf5c72ba60eb7b2fe151032769d49dd3df6b04fa3141dffd6e2aa162b7a6e"}
Expand Down
15 changes: 9 additions & 6 deletions apps/data_handler/handler_tools/data_parser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def validate_addresses(cls, value: str) -> str:
return add_leading_zeros(value)

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

Expand All @@ -308,10 +308,13 @@ def validate_amount(cls, value: str) -> Decimal:
Raises:
ValueError: If the provided amount is not numeric.
"""
if not value.isdigit():
raise ValueError("Amount field is not numeric")
return Decimal(value)

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


class CollateralEnabledDisabledEventData(BaseModel):
""" Data model representing a collateral enabled/disabled event in the system. """
Expand All @@ -329,4 +332,4 @@ 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)
return add_leading_zeros(value)
6 changes: 3 additions & 3 deletions apps/data_handler/handler_tools/data_parser/zklend.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ def parse_collateral_enabled_disabled_event(
CollateralEnabledDisabledEventData: A model with the parsed event data.
"""
return CollateralEnabledDisabledEventData(
user=event_data["data"][0],
token=event_data["data"][1],
)
user=event_data[0],
token=event_data[1],
)
Loading
Loading