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/Zklend_models #273

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
77 changes: 77 additions & 0 deletions apps/data_handler/db/models/zklend_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,80 @@ class LiquidationEventData(EventBaseModel):
debt_face_amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)
collateral_token: Mapped[str] = mapped_column(String, nullable=False)
collateral_amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class RepaymentEventData(EventBaseModel):
"""
Database model for Repayment event, inheriting from EventBaseModel.

This model stores details of a repayment event, including the addresses involved
and the amounts in raw and face formats.
"""

__tablename__ = "repayment_event_data"
binayak9932 marked this conversation as resolved.
Show resolved Hide resolved

repayer: Mapped[str] = mapped_column(String, nullable=False)
beneficiary: Mapped[str] = mapped_column(String, nullable=False)
token: Mapped[str] = mapped_column(String, nullable=False)
raw_amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)
face_amount: Mapped[Decimal] = mapped_column(Numeric(38, 18), nullable=False)


class DepositEventData(EventBaseModel):
"""
Database model for Deposit event, inheriting from EventBaseModel.

This model stores details of a deposit event, including the user address,
token, and the face amount of the deposit.
"""

__tablename__ = "deposit_event_data"
binayak9932 marked this conversation as resolved.
Show resolved Hide resolved

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


class BorrowingEventData(EventBaseModel):
"""
Database model for Borrowing event, inheriting from EventBaseModel.

This model stores details of a borrowing event, including the user address,
token, and amounts in raw and face formats.
"""

__tablename__ = "borrowing_event_data"
binayak9932 marked this conversation as resolved.
Show resolved Hide resolved

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


class WithdrawalEventData(EventBaseModel):
"""
Database model for Withdrawal event, inheriting from EventBaseModel.

This model stores details of a withdrawal event, including the user address,
token, and the amount withdrawn.
"""

__tablename__ = "withdrawal_event_data"
binayak9932 marked this conversation as resolved.
Show resolved Hide resolved

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


class CollateralEnabledDisabledEventData(EventBaseModel):
"""
Database model for CollateralEnabled/Disabled event, inheriting from EventBaseModel.

This model stores details of a collateral enabled/disabled event, including
the user address and the token involved.
"""

__tablename__ = "collateral_enabled_disabled_event_data"
binayak9932 marked this conversation as resolved.
Show resolved Hide resolved

user: Mapped[str] = mapped_column(String, nullable=False)
token: Mapped[str] = mapped_column(String, nullable=False)
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op
from sqlalchemy import inspect, text

from shared.constants import ProtocolIDs

# revision identifiers, used by Alembic.
Expand Down Expand Up @@ -90,7 +92,22 @@ def upgrade() -> None:
["event_name"],
unique=False,
)
op.drop_index("ix_notification_email", table_name="notification")

conn = op.get_bind()
result = conn.execute(
text(
"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'notification');"
)
)
table_exists = result.scalar()

if table_exists:
inspector = inspect(conn)
existing_indexes = [
index["name"] for index in inspector.get_indexes("notification")
]
if "ix_notification_email" in existing_indexes:
op.drop_index("ix_notification_email", table_name="notification")
# ### end Alembic commands ###


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"""add new event models

Revision ID: aecc94d73ee6
Revises: 71a8b872296d
Create Date: 2024-10-31 12:19:17.551380

"""

from typing import Sequence, Union

import sqlalchemy as sa
import sqlalchemy_utils
from alembic import op

from shared.constants import ProtocolIDs

# revision identifiers, used by Alembic.
revision: str = "aecc94d73ee6"
down_revision: Union[str, None] = "71a8b872296d"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"borrowing_event_data",
sa.Column("user", sa.String(), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("raw_amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("face_amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("event_name", sa.String(), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column(
"protocol_id",
sqlalchemy_utils.types.choice.ChoiceType(ProtocolIDs),
nullable=False,
),
sa.Column("id", sa.UUID(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_borrowing_event_data_block_number"),
"borrowing_event_data",
["block_number"],
unique=False,
)
op.create_index(
op.f("ix_borrowing_event_data_event_name"),
"borrowing_event_data",
["event_name"],
unique=False,
)
op.create_table(
"collateral_enabled_disabled_event_data",
sa.Column("user", sa.String(), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("event_name", sa.String(), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column(
"protocol_id",
sqlalchemy_utils.types.choice.ChoiceType(ProtocolIDs),
nullable=False,
),
sa.Column("id", sa.UUID(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_collateral_enabled_disabled_event_data_block_number"),
"collateral_enabled_disabled_event_data",
["block_number"],
unique=False,
)
op.create_index(
op.f("ix_collateral_enabled_disabled_event_data_event_name"),
"collateral_enabled_disabled_event_data",
["event_name"],
unique=False,
)
op.create_table(
"deposit_event_data",
sa.Column("user", sa.String(), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("face_amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("event_name", sa.String(), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column(
"protocol_id",
sqlalchemy_utils.types.choice.ChoiceType(ProtocolIDs),
nullable=False,
),
sa.Column("id", sa.UUID(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_deposit_event_data_block_number"),
"deposit_event_data",
["block_number"],
unique=False,
)
op.create_index(
op.f("ix_deposit_event_data_event_name"),
"deposit_event_data",
["event_name"],
unique=False,
)
op.create_table(
"repayment_event_data",
sa.Column("repayer", sa.String(), nullable=False),
sa.Column("beneficiary", sa.String(), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("raw_amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("face_amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("event_name", sa.String(), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column(
"protocol_id",
sqlalchemy_utils.types.choice.ChoiceType(ProtocolIDs),
nullable=False,
),
sa.Column("id", sa.UUID(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_repayment_event_data_block_number"),
"repayment_event_data",
["block_number"],
unique=False,
)
op.create_index(
op.f("ix_repayment_event_data_event_name"),
"repayment_event_data",
["event_name"],
unique=False,
)
op.create_table(
"withdrawal_event_data",
sa.Column("user", sa.String(), nullable=False),
sa.Column("amount", sa.Numeric(precision=38, scale=18), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("event_name", sa.String(), nullable=False),
sa.Column("block_number", sa.Integer(), nullable=False),
sa.Column(
"protocol_id",
sqlalchemy_utils.types.choice.ChoiceType(ProtocolIDs),
nullable=False,
),
sa.Column("id", sa.UUID(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
op.f("ix_withdrawal_event_data_block_number"),
"withdrawal_event_data",
["block_number"],
unique=False,
)
op.create_index(
op.f("ix_withdrawal_event_data_event_name"),
"withdrawal_event_data",
["event_name"],
unique=False,
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
op.f("ix_withdrawal_event_data_event_name"), table_name="withdrawal_event_data"
)
op.drop_index(
op.f("ix_withdrawal_event_data_block_number"),
table_name="withdrawal_event_data",
)
op.drop_table("withdrawal_event_data")
op.drop_index(
op.f("ix_repayment_event_data_event_name"), table_name="repayment_event_data"
)
op.drop_index(
op.f("ix_repayment_event_data_block_number"), table_name="repayment_event_data"
)
op.drop_table("repayment_event_data")
op.drop_index(
op.f("ix_deposit_event_data_event_name"), table_name="deposit_event_data"
)
op.drop_index(
op.f("ix_deposit_event_data_block_number"), table_name="deposit_event_data"
)
op.drop_table("deposit_event_data")
op.drop_index(
op.f("ix_collateral_enabled_disabled_event_data_event_name"),
table_name="collateral_enabled_disabled_event_data",
)
op.drop_index(
op.f("ix_collateral_enabled_disabled_event_data_block_number"),
table_name="collateral_enabled_disabled_event_data",
)
op.drop_table("collateral_enabled_disabled_event_data")
op.drop_index(
op.f("ix_borrowing_event_data_event_name"), table_name="borrowing_event_data"
)
op.drop_index(
op.f("ix_borrowing_event_data_block_number"), table_name="borrowing_event_data"
)
op.drop_table("borrowing_event_data")
# ### end Alembic commands ###
Loading