Skip to content

Commit

Permalink
feat: Add missing events to nostra transform_events
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRoudyk committed Nov 28, 2024
2 parents 7f085f7 + d074d92 commit f253e85
Show file tree
Hide file tree
Showing 7 changed files with 684 additions and 148 deletions.
168 changes: 149 additions & 19 deletions apps/data_handler/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
)
from data_handler.db.models.nostra_events import (
BearingCollateralBurnEventModel,
BearingCollateralMintEventModel,
DebtBurnEventModel,
DebtMintEventModel,
DebtTransferEventModel,
InterestRateModelEventModel,
NonInterestBearingCollateralBurnEventModel,
NonInterestBearingCollateralMintEventModel,
)
from data_handler.db.models.zklend_events import (
AccumulatorsSyncEventModel,
Expand Down Expand Up @@ -862,19 +866,20 @@ def apply_filters(query):

class NostraEventDBConnector(DBConnector):
"""
Provides CRUD operations specifically for Nostra events, such as DebtMint, DebtBurn, DebtTransfer,
BearingCollateralMint, BearingCollateralBurn, and InterestRateModelUpdate events.
Provides CRUD operations specifically for Nostra events, such as
BearingCollateralBurn, BearingCollateralMint, DebtMint, DebtBurn, DebtTransfer,
InterestRateModelUpdate, NonInterestBearingCollateralMint and NonInterestBearingCollateralMint events.
"""

def create_debt_mint_event(
def create_bearing_collateral_burn_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a DebtMintEventModel record in the database.
Creates a BearingCollateralBurnEventModel record in the database.
"""
db = self.Session()
try:
event = DebtMintEventModel(
event = BearingCollateralBurnEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
Expand All @@ -883,10 +888,35 @@ def create_debt_mint_event(
)
db.add(event)
db.commit()
logger.info(f"DebtMint event saved: {event}")
logger.info(f"BearingCollateralBurn event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating DebtMintEventModel: {e}")
logger.error(f"Error creating BearingCollateralBurnEventModel: {e}")
raise e
finally:
db.close()

def create_bearing_collateral_mint_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a BearingCollateralMintEventModel record in the database.
"""
db = self.Session()
try:
event = BearingCollateralMintEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
amount=event_data.get("amount"),
)
db.add(event)
db.commit()
logger.info(f"BearingCollateralMint event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating BearingCollateralMintEventModel: {e}")
raise e
finally:
db.close()
Expand Down Expand Up @@ -916,6 +946,31 @@ def create_debt_burn_event(
finally:
db.close()

def create_debt_mint_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a DebtMintEventModel record in the database.
"""
db = self.Session()
try:
event = DebtMintEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
user=event_data.get("user"),
amount=event_data.get("amount"),
)
db.add(event)
db.commit()
logger.info(f"DebtMint event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating DebtMintEventModel: {e}")
raise e
finally:
db.close()

def create_debt_transfer_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
Expand All @@ -942,15 +997,41 @@ def create_debt_transfer_event(
finally:
db.close()

def create_bearing_collateral_burn_event(
def create_interest_rate_model_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a BearingCollateralBurnEventModel record in the database.
Creates a InterestRateModelEventModel record in the database.
"""
db = self.Session()
try:
event = BearingCollateralBurnEventModel(
event = InterestRateModelEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
debt_token=event_data.get("debt_token"),
lending_index=event_data.get("lending_index"),
borrow_index=event_data.get("borrow_index"),
)
db.add(event)
db.commit()
logger.info(f"InterestRateModel event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating InterestRateModelEventModel: {e}")
raise e
finally:
db.close()

def create_non_interest_bearing_collateral_burn_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a NonInterestBearingCollateralBurnEventModel record in the database.
"""
db = self.Session()
try:
event = NonInterestBearingCollateralBurnEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
Expand All @@ -959,10 +1040,40 @@ def create_bearing_collateral_burn_event(
)
db.add(event)
db.commit()
logger.info(f"BearingCollateralBurn event saved: {event}")
logger.info(f"NonInterestBearingCollateralBurn event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(f"Error creating BearingCollateralBurnEventModel: {e}")
logger.error(
f"Error creating NonInterestBearingCollateralBurnEventModel: {e}"
)
raise e
finally:
db.close()

def create_non_interest_bearing_collateral_mint_event(
self, protocol_id: str, event_name: str, block_number: int, event_data: dict
) -> None:
"""
Creates a NonInterestBearingCollateralMintEventModel record in the database.
"""
db = self.Session()
try:
event = NonInterestBearingCollateralMintEventModel(
protocol_id=protocol_id,
event_name=event_name,
block_number=block_number,
sender=event_data.get("sender"),
recipient=event_data.get("recipient"),
amount=event_data.get("amount"),
)
db.add(event)
db.commit()
logger.info(f"NonInterestBearingCollateralMint event saved: {event}")
except SQLAlchemyError as e:
db.rollback()
logger.error(
f"Error creating NonInterestBearingCollateralMintEventModel: {e}"
)
raise e
finally:
db.close()
Expand All @@ -988,24 +1099,43 @@ def apply_filters(query, model):
query = query.filter(model.block_number == block_number)
return query

debt_mint_query = apply_filters(
db.query(DebtMintEventModel), DebtMintEventModel
bearing_collateral_burn_query = apply_filters(
db.query(BearingCollateralBurnEventModel),
BearingCollateralBurnEventModel,
)
bearing_collateral_mint_query = apply_filters(
db.query(BearingCollateralMintEventModel),
BearingCollateralMintEventModel,
)
debt_burn_query = apply_filters(
db.query(DebtBurnEventModel), DebtBurnEventModel
)
debt_mint_query = apply_filters(
db.query(DebtMintEventModel), DebtMintEventModel
)
debt_transfer_query = apply_filters(
db.query(DebtTransferEventModel), DebtTransferEventModel
)
bearing_collateral_burn_query = apply_filters(
db.query(BearingCollateralBurnEventModel),
BearingCollateralBurnEventModel,
interest_rate_model_query = apply_filters(
db.query(InterestRateModelEventModel), InterestRateModelEventModel
)
non_interest_bearing_collateral_burn_query = apply_filters(
db.query(NonInterestBearingCollateralBurnEventModel),
NonInterestBearingCollateralBurnEventModel,
)
non_interest_bearing_collateral_mint_query = apply_filters(
db.query(NonInterestBearingCollateralMintEventModel),
NonInterestBearingCollateralMintEventModel,
)

combined_query = debt_mint_query.union_all(
combined_query = interest_rate_model_query.union_all(
bearing_collateral_burn_query,
bearing_collateral_mint_query,
debt_burn_query,
debt_mint_query,
debt_transfer_query,
bearing_collateral_burn_query,
non_interest_bearing_collateral_burn_query,
non_interest_bearing_collateral_mint_query,
)
events = combined_query.all()
return events
Expand Down
52 changes: 38 additions & 14 deletions apps/data_handler/db/models/nostra_events.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
from decimal import Decimal

from data_handler.db.models.event import EventBaseModel
from sqlalchemy import Numeric, String
from sqlalchemy.orm import Mapped, mapped_column


class DebtMintEventModel(EventBaseModel):
class BearingCollateralBurnEventModel(EventBaseModel):
"""
Database model for DebtMint event, inheriting from EventBaseModel.
Database model for BearingCollateralBurn event, inheriting from EventBaseModel.
This model stores the user address and the amount burned.
"""

__tablename__ = "bearing_collateral_burn_event"

user: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class BearingCollateralMintEventModel(EventBaseModel):
"""
Database model for BearingCollateral mint event, inheriting from EventBaseModel.
This model stores the user address and the amount minted.
"""

__tablename__ = "debt_mint_event"
__tablename__ = "bearing_collateral_mint_event"

user: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class DebtBurnEventModel(EventBaseModel):
class DebtMintEventModel(EventBaseModel):
"""
Database model for DebtBurn event, inheriting from EventBaseModel.
Database model for DebtMint event, inheriting from EventBaseModel.
This model stores the user address and the amount burned.
This model stores the user address and the amount minted.
"""

__tablename__ = "debt_burn_event"
__tablename__ = "debt_mint_event"

user: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class BearingCollateralBurnEventModel(EventBaseModel):
class DebtBurnEventModel(EventBaseModel):
"""
Database model for BearingCollateralBurn event, inheriting from EventBaseModel.
Database model for DebtBurn event, inheriting from EventBaseModel.
This model stores the user address and the amount burned.
"""

__tablename__ = "bearing_collateral_burn_event"
__tablename__ = "debt_burn_event"

user: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)
Expand Down Expand Up @@ -74,14 +88,24 @@ class InterestRateModelEventModel(EventBaseModel):
borrow_index: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class BearingCollateralMintEventModel(EventBaseModel):
class NonInterestBearingCollateralMintEventModel(EventBaseModel):
"""
Database model for NonInterestBearingCollateralMint event.
"""
Database model for BearingCollateral mint event, inheriting from EventBaseModel.

This model stores the user address and the amount minted.
__tablename__ = "non_interest_bearing_collateral_mint_event"

sender: Mapped[str] = mapped_column(String, nullable=False)
recipient: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class NonInterestBearingCollateralBurnEventModel(EventBaseModel):
"""
Database model for NonInterestBearingCollateralBurn event.
"""

__tablename__ = "bearing_collateral_mint_event"
__tablename__ = "non_interest_bearing_collateral_burn_event"

user: Mapped[str] = mapped_column(String, nullable=False)
amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)
Loading

0 comments on commit f253e85

Please sign in to comment.