Skip to content

Commit

Permalink
chore: add standard liquidation event serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jaykayudo committed Oct 24, 2024
1 parent f2bf02e commit d1aa5f6
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 44 deletions.
26 changes: 26 additions & 0 deletions data_handler/handler_tools/data_parser/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
from decimal import Decimal

from pydantic import BaseModel, ValidationInfo, field_validator

from shared.helpers import add_leading_zeros


class LiquidationEventData(BaseModel):
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):
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):
if not value.isdigit():
raise ValueError("%s field is not numeric" % info.field_name)
return Decimal(str(int(value, base=16)))
23 changes: 21 additions & 2 deletions data_handler/handler_tools/data_parser/zklend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from serializers import LiquidationEventData


class ZklendDataParser:
"""
Parses the zkLend data to human-readable format.
Expand Down Expand Up @@ -36,5 +39,21 @@ def parse_repayment_event(cls, event_data):

@classmethod
def parse_liquidation_event(cls, event_data):
# TODO: Implement parsing logic for Liquidation event
pass
"""
Convert the event list to a Liquidation event data object
:event_data - List of length 7 of the event data
Returns
LiquidationEventData
"""
event_data = LiquidationEventData(
liquidator=event_data[0],
user=event_data[1],
debt_token=event_data[2],
debt_raw_amount=event_data[3],
debt_face_amount=event_data[4],
collateral_token=event_data[5],
collateral_amount=event_data[6],
)
return event_data
77 changes: 36 additions & 41 deletions data_handler/handlers/loan_states/zklend/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Optional

import pandas as pd
from handler_tools.data_parser.zklend import ZklendDataParser
from handlers import blockchain_call
from handlers.helpers import get_async_symbol
from handlers.loan_states.zklend import TokenSettings
Expand Down Expand Up @@ -327,41 +328,35 @@ def process_liquidation_event(self, event: pd.Series) -> None:
# The order of the arguments is: `liquidator`, `user`, `debt_token`, `debt_raw_amount`, `debt_face_amount`,
# `collateral_token`, `collateral_amount`.
# Example: https://starkscan.co/event/0x07b8ec709df1066d9334d56b426c45440ca1f1bb841285a5d7b33f9d1008f256_5.
user = add_leading_zeros(event["data"][1])
debt_token = add_leading_zeros(event["data"][2])
debt_raw_amount = decimal.Decimal(str(int(event["data"][3], base=16)))

collateral_token = add_leading_zeros(event["data"][5])
collateral_face_amount = decimal.Decimal(str(int(event["data"][6], base=16)))

data = ZklendDataParser.parse_liquidation_event(event["data"])
collateral_raw_amount = (
collateral_face_amount
/ self.interest_rate_models.collateral[collateral_token]
data.collateral_amount
/ self.interest_rate_models.collateral[data.collateral_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[data.user].extra_info.block = event["block_number"]
self.loan_entities[data.user].extra_info.timestamp = event["timestamp"]

self.loan_entities[user].debt.increase_value(
token=debt_token, value=-debt_raw_amount
self.loan_entities[data.user].debt.increase_value(
token=data.debt_token, value=-data.debt_raw_amount
)
self.loan_entities[user].deposit.increase_value(
token=collateral_token, value=-collateral_raw_amount
self.loan_entities[data.user].deposit.increase_value(
token=data.collateral_token, value=-collateral_raw_amount
)
if self.loan_entities[user].collateral_enabled[collateral_token]:
self.loan_entities[user].collateral.increase_value(
token=collateral_token, value=-collateral_raw_amount
if self.loan_entities[data.user].collateral_enabled[data.collateral_token]:
self.loan_entities[data.user].collateral.increase_value(
token=data.collateral_token, value=-collateral_raw_amount
)
if user == self.verbose_user:
if data.user == self.verbose_user:
logging.info(
"In block number = {}, debt of raw amount = {} of token = {} and collateral of raw amount = {} of "
"token = {} were liquidated.".format(
event["block_number"],
debt_raw_amount,
debt_token,
data.debt_raw_amount,
data.debt_token,
collateral_raw_amount,
collateral_token,
data.collateral_token,
)
)

Expand Down Expand Up @@ -457,16 +452,16 @@ async def collect_token_parameters(self) -> None:
collateral_token_symbol = await get_async_symbol(
token_address=collateral_token_address
)
self.token_parameters.collateral[underlying_collateral_token_address] = (
ZkLendCollateralTokenParameters(
address=collateral_token_address,
decimals=int(reserve_data[1]),
symbol=collateral_token_symbol,
underlying_symbol=underlying_collateral_token_symbol,
underlying_address=underlying_collateral_token_address,
collateral_factor=reserve_data[4] / 1e27,
liquidation_bonus=reserve_data[14] / 1e27,
)
self.token_parameters.collateral[
underlying_collateral_token_address
] = ZkLendCollateralTokenParameters(
address=collateral_token_address,
decimals=int(reserve_data[1]),
symbol=collateral_token_symbol,
underlying_symbol=underlying_collateral_token_symbol,
underlying_address=underlying_collateral_token_address,
collateral_factor=reserve_data[4] / 1e27,
liquidation_bonus=reserve_data[14] / 1e27,
)
for underlying_debt_token_address in debt_tokens:
underlying_debt_token_symbol = await get_async_symbol(
Expand All @@ -483,13 +478,13 @@ async def collect_token_parameters(self) -> None:
)
debt_token_address = add_leading_zeros(hex(reserve_data[2]))
debt_token_symbol = await get_async_symbol(token_address=debt_token_address)
self.token_parameters.debt[underlying_debt_token_address] = (
ZkLendDebtTokenParameters(
address=debt_token_address,
decimals=int(reserve_data[1]),
symbol=debt_token_symbol,
underlying_symbol=underlying_debt_token_symbol,
underlying_address=underlying_debt_token_address,
debt_factor=reserve_data[5] / 1e27,
)
self.token_parameters.debt[
underlying_debt_token_address
] = ZkLendDebtTokenParameters(
address=debt_token_address,
decimals=int(reserve_data[1]),
symbol=debt_token_symbol,
underlying_symbol=underlying_debt_token_symbol,
underlying_address=underlying_debt_token_address,
debt_factor=reserve_data[5] / 1e27,
)
Loading

0 comments on commit d1aa5f6

Please sign in to comment.