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
Changes from 1 commit
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
203 changes: 180 additions & 23 deletions apps/data_handler/handlers/events/zklend/transform_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from data_handler.db.models.zklend_events import (
AccumulatorsSyncEventModel,
LiquidationEventModel,
RepaymentEventModel,
BorrowingEventModel,
DepositEventModel,
WithdrawalEventModel,
CollateralEnabledDisabledEventModel
)
from data_handler.db.crud import ZkLendEventDBConnector
from data_handler.handler_tools.constants import ProtocolAddresses
Expand All @@ -24,24 +29,84 @@
EVENT_MAPPING: Dict[str, Tuple[Callable, str, Type[Base]]] = {
"AccumulatorsSync": (
ZklendDataParser.parse_accumulators_sync_event,
"create_accumulator_event",
"save_accumulators_sync_event",
AccumulatorsSyncEventModel
),
"Liquidation": (
ZklendDataParser.parse_liquidation_event,
"create_liquidation_event",
LiquidationEventModel
),
"zklend::market::Market::AccumulatorsSync": (
ZklendDataParser.parse_accumulators_sync_event,
"create_accumulator_event",
"save_accumulators_sync_event",
AccumulatorsSyncEventModel
),
"Liquidation": (
ZklendDataParser.parse_liquidation_event,
"save_liquidation_event",
LiquidationEventModel
),
"zklend::market::Market::Liquidation": (
ZklendDataParser.parse_liquidation_event,
"create_liquidation_event",
"save_liquidation_event",
LiquidationEventModel
),
"Repayment": (
ZklendDataParser.parse_repayment_event,
"save_repayment_event",
RepaymentEventModel
),
"zklend::market::Market::Repayment": (
ZklendDataParser.parse_repayment_event,
"save_repayment_event",
RepaymentEventModel
),
"Borrowing": (
ZklendDataParser.parse_borrowing_event,
"save_borrowing_event",
BorrowingEventModel
),
"zklend::market::Market::Borrowing": (
ZklendDataParser.parse_borrowing_event,
"save_borrowing_event",
BorrowingEventModel
),
"Deposit": (
ZklendDataParser.parse_deposit_event,
"save_deposit_event",
DepositEventModel
),
"zklend::market::Market::Deposit": (
ZklendDataParser.parse_deposit_event,
"save_deposit_event",
DepositEventModel
),
"Withdrawal": (
ZklendDataParser.parse_withdrawal_event,
"save_withdrawal_event",
WithdrawalEventModel
),
"zklend::market::Market::Withdrawal": (
ZklendDataParser.parse_withdrawal_event,
"save_withdrawal_event",
WithdrawalEventModel
),
"CollateralEnabled": (
ZklendDataParser.parse_collateral_enabled_disabled_event,
"save_collateral_enabled_disabled_event",
CollateralEnabledDisabledEventModel
),
"zklend::market::Market::CollateralEnabled": (
ZklendDataParser.parse_collateral_enabled_disabled_event,
"save_collateral_enabled_disabled_event",
CollateralEnabledDisabledEventModel
),
"CollateralDisabled": (
ZklendDataParser.parse_collateral_enabled_disabled_event,
"save_collateral_enabled_disabled_event",
CollateralEnabledDisabledEventModel
),
"zklend::market::Market::CollateralDisabled": (
ZklendDataParser.parse_collateral_enabled_disabled_event,
"save_collateral_enabled_disabled_event",
CollateralEnabledDisabledEventModel
),
}


Expand All @@ -50,7 +115,7 @@ class ZklendTransformer:
A class that is used to transform Zklend events into database models.
"""

EVENT_MAPPING: Dict[str, Tuple[Callable, Type[BaseModel], Type[Base]]] = EVENT_MAPPING
EVENT_MAPPING: Dict[str, Tuple[Callable, str, Type[Base]]] = EVENT_MAPPING
PROTOCOL_ADDRESSES: str = ProtocolAddresses.ZKLEND_MARKET_ADDRESSES
PROTOCOL_TYPE: ProtocolIDs = ProtocolIDs.ZKLEND
PAGINATION_SIZE: int = 1000
Expand Down Expand Up @@ -82,31 +147,123 @@ def fetch_and_transform_events(self, from_address: str, min_block: int, max_bloc
for event in response:
event_type = event.get("key_name")
if event_type in self.EVENT_MAPPING:
parser_func, method_name, model_class = self.EVENT_MAPPING[event_type]
parser_func, save_to_db_method_name, model_class = self.EVENT_MAPPING[event_type]
parsed_data = parser_func(event["data"])
db_model = model_class(**parsed_data.model_dump())
self.db_connector[method_name](db_model)
getattr(self, save_to_db_method_name)(db_model)
else:
logger.info(f"Event type {event_type} not supported, yet...")

def save_accumulators_sync_event(self, event: Dict[str, Any]) -> None:
def save_accumulators_sync_event(self, event_model: AccumulatorsSyncEventModel) -> None:
"""
Save an accumulators sync event to the database.
"""
parser_func, _, model_class = self.EVENT_MAPPING["zklend::market::Market::AccumulatorsSync"]
parsed_data = parser_func(event)
db_model = model_class(**parsed_data.model_dump())
self.db_connector.create_accumulator_event(db_model)
self.db_connector.create_accumulator_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"token": event_model.token,
"lending_accumulator": event_model.lending_accumulator,
"debt_accumulator": event_model.debt_accumulator
}
)

def save_liquidation_event(self, event: Dict[str, Any]) -> None:
def save_liquidation_event(self, event_model: LiquidationEventModel) -> None:
"""
Save a liquidation event to the database.
"""
parser_func, _, model_class = self.EVENT_MAPPING["zklend::market::Market::Liquidation"]
parsed_data = parser_func(event)
db_model = model_class(**parsed_data.model_dump())

self.db_connector.create_liquidation_event(db_model)
self.db_connector.create_liquidation_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"liquidator": event_model.liquidator,
"user": event_model.user,
"debt_token": event_model.debt_token,
"debt_raw_amount": event_model.debt_raw_amount,
"debt_face_amount": event_model.debt_face_amount,
"collateral_token": event_model.collateral_token,
"collateral_amount": event_model.collateral_amount
}
)

def save_borrowing_event(self, event_model: BorrowingEventModel) -> None:
"""
Save a borrowing event to the database.
"""
self.db_connector.create_borrowing_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"user": event_model.user,
"token": event_model.token,
"raw_amount": event_model.raw_amount,
"face_amount": event_model.face_amount
}
)

def save_deposit_event(self, event_model: DepositEventModel) -> None:
"""
Save a deposit event to the database.
"""
self.db_connector.create_deposit_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"user": event_model.user,
"token": event_model.token,
"face_amount": event_model.face_amount
}
)

def save_withdrawal_event(self, event_model: WithdrawalEventModel) -> None:
"""
Save a withdrawal event to the database.
"""
self.db_connector.create_withdrawal_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"user": event_model.user,
"token": event_model.token,
"face_amount": event_model.face_amount
}
)

def save_collateral_enabled_disabled_event(self, event_model: CollateralEnabledDisabledEventModel) -> None:
"""
Save a collateral enabled/disabled event to the database.
"""
self.db_connector.create_collateral_enabled_disabled_event(
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"user": event_model.user,
"token": event_model.token
}
)

def save_repayment_event(self, event_model: RepaymentEventModel) -> None:
"""
Save a repayment event to the database.
"""
self.db_connector.create_repayment_event(
djeck1432 marked this conversation as resolved.
Show resolved Hide resolved
protocol_id=self.PROTOCOL_TYPE,
event_name=event_model.event_name,
block_number=event_model.block_number,
event_data={
"repayer": event_model.repayer,
"beneficiary": event_model.beneficiary,
"token": event_model.token,
"raw_amount": event_model.raw_amount,
"face_amount": event_model.face_amount
}
)

def run(self) -> None:
"""
Expand All @@ -131,4 +288,4 @@ def run(self) -> None:
This is the init function for when ZklendTransformer class is called directly.
"""
transformer = ZklendTransformer()
transformer.run()
transformer.run()
Loading