Skip to content

Commit

Permalink
Merge pull request #224 from Gerson2102/stadardize-event
Browse files Browse the repository at this point in the history
Standardize event data for accumulators’ data
  • Loading branch information
djeck1432 authored Oct 25, 2024
2 parents d3a88aa + fa4679d commit 34847ac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
32 changes: 31 additions & 1 deletion data_handler/handler_tools/data_parser/serializers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
from decimal import Decimal

from pydantic import BaseModel, ValidationInfo, field_validator

from shared.helpers import add_leading_zeros


class DataAccumulatorsSyncEvent(BaseModel):
"""
Model to parse and validate data for AccumulatorsSync event.
This model validates and converts the lending and debt accumulators from hexadecimal
strings to `Decimal` format, scaled by `1e27`.
Attributes:
token (str): The token address as a hexadecimal string.
lending_accumulator (Decimal): The lending accumulator value, converted from hex to Decimal.
debt_accumulator (Decimal): The debt accumulator value, converted from hex to Decimal.
"""

token: str
lending_accumulator: Decimal
debt_accumulator: Decimal

@field_validator("lending_accumulator", "debt_accumulator", mode="before")
def hex_to_decimal(cls, v: str) -> Decimal:
"""
Converts a hexadecimal string to a Decimal value, scaled by 1e27.
Args:
v (str): The hexadecimal string to be converted.
Returns:
Decimal: The converted decimal value scaled by 1e27.
"""
return Decimal(int(v, 16)) / Decimal("1e27")


class LiquidationEventData(BaseModel):
"""
Class for converting liquadation event to an object model.
Expand Down
35 changes: 31 additions & 4 deletions data_handler/handler_tools/data_parser/zklend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from serializers import LiquidationEventData
from decimal import Decimal
from typing import Any

from serializers import DataAccumulatorsSyncEvent, LiquidationEventData


class ZklendDataParser:
Expand All @@ -7,9 +10,33 @@ class ZklendDataParser:
"""

@classmethod
def parse_accumulators_sync_event(cls, event_data):
# TODO: Implement parsing logic for AccumulatorsSync event
pass
def parse_accumulators_sync_event(
cls, event_data: list[Any]
) -> DataAccumulatorsSyncEvent:
"""
Parses the AccumulatorsSync event data into a human-readable format using the AccumulatorsSyncEvent serializer.
The event data is fetched from on-chain logs and is structured in the following way:
- event_data[0]: The token address (as a hexadecimal string).
- event_data[1]: Lending accumulator value (as a hexadecimal string).
- event_data[2]: Debt accumulator value (as a hexadecimal string).
Example of raw event data can be found on Starkscan:
https://starkscan.co/event/0x029628b89875a98c1c64ae206e7eb65669cb478a24449f3485f5e98aba6204dc_0
Args:
event_data (list[Any]): A list containing the raw event data, typically with 3 elements:
token, lending accumulator (hexadecimal string), and debt accumulator (hexadecimal string).
Returns:
AccumulatorsSyncEvent: A Pydantic model with the parsed and validated event data in a human-readable format.
"""
parsed_event = DataAccumulatorsSyncEvent(
token=event_data[0],
lending_accumulator=event_data[1],
debt_accumulator=event_data[2],
)
return parsed_event

@classmethod
def parse_deposit_event(cls, event_data):
Expand Down
15 changes: 8 additions & 7 deletions data_handler/handlers/loan_states/zklend/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from handlers import blockchain_call
from handlers.helpers import get_async_symbol
from handlers.loan_states.zklend import TokenSettings
from handler_tools.data_parser.zklend import ZklendDataParser

from db.crud import InitializerDBConnector
from shared.helpers import add_leading_zeros
Expand Down Expand Up @@ -165,13 +166,13 @@ def process_accumulators_sync_event(self, event: pd.Series) -> None:
# The order of the values in the `data` column is: `token`, `lending_accumulator`, `debt_accumulator`.
# Example: https://starkscan.co/event/0x029628b89875a98c1c64ae206e7eb65669cb478a24449f3485f5e98aba6204dc_0.
# TODO: Integrate the ZEND token once it's allowed to be borrowed or used as collateral.
token = add_leading_zeros(event["data"][0])
collateral_interest_rate_index = decimal.Decimal(
str(int(event["data"][1], base=16))
) / decimal.Decimal("1e27")
debt_interest_rate_index = decimal.Decimal(
str(int(event["data"][2], base=16))
) / decimal.Decimal("1e27")
parsed_event_data = ZklendDataParser.parse_accumulators_sync_event(
event["data"]
)

token = add_leading_zeros(parsed_event_data.token)
collateral_interest_rate_index = parsed_event_data.lending_accumulator
debt_interest_rate_index = parsed_event_data.debt_accumulator

self.interest_rate_models.collateral[token] = collateral_interest_rate_index
self.interest_rate_models.debt[token] = debt_interest_rate_index
Expand Down

0 comments on commit 34847ac

Please sign in to comment.