-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathmodels.py
225 lines (180 loc) · 6.15 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""
This module contains SQLAlchemy models for the application, including
User, Position, AirDrop, and TelegramUser. Each model represents a
table in the database and defines the structure and relationships
between the data entities.
"""
from datetime import datetime
from enum import Enum as PyEnum
from uuid import uuid4
from sqlalchemy import (
DECIMAL,
NUMERIC,
Boolean,
Column,
DateTime,
Enum,
Float,
ForeignKey,
String,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
from web_app.db.database import Base
class Status(PyEnum):
"""
Enum for the position status.
"""
PENDING = "pending"
OPENED = "opened"
CLOSED = "closed"
@classmethod
def choices(cls):
"""
Returns the list of status choices.
"""
return [status.value for status in cls]
class User(Base):
"""
SQLAlchemy model for the user table.
"""
__tablename__ = "user"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
is_contract_deployed = Column(Boolean, default=False)
wallet_id = Column(String, nullable=False, unique=True, index=True)
contract_address = Column(String)
class Referal(Base):
"""
SQLAlchemy model for the referal table.
"""
__tablename__ = "referal"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
user_id = Column(
UUID(as_uuid=True), ForeignKey("user.id"), index=True, nullable=False
)
referal_id = Column(String(16), nullable=False, unique=True, index=True)
created_at = Column(DateTime, nullable=False, default=func.now())
class Position(Base):
"""
SQLAlchemy model for the position table.
"""
__tablename__ = "position"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
user_id = Column(
UUID(as_uuid=True), ForeignKey("user.id"), index=True, nullable=False
)
token_symbol = Column(String, nullable=False)
amount = Column(String, nullable=False)
multiplier = Column(NUMERIC, nullable=False)
created_at = Column(DateTime, nullable=False, default=func.now())
closed_at = Column(DateTime, nullable=True)
status = Column(
Enum(
Status, name="status_enum", values_callable=lambda x: [e.value for e in x]
),
nullable=True,
default="pending",
)
start_price = Column(DECIMAL, nullable=False)
is_protection = Column(Boolean, default=False)
liquidation_bonus = Column(Float, default=0.0)
is_liquidated = Column(Boolean, default=False)
datetime_liquidation = Column(DateTime, nullable=True)
class AirDrop(Base):
"""
SQLAlchemy model for the airdrop table.
"""
__tablename__ = "airdrop"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
user_id = Column(
UUID(as_uuid=True), ForeignKey("user.id"), index=True, nullable=False
)
created_at = Column(DateTime, nullable=False, default=func.now())
amount = Column(DECIMAL, nullable=True)
is_claimed = Column(Boolean, default=False, index=True)
claimed_at = Column(DateTime, nullable=True)
class TelegramUser(Base):
"""
SQLAlchemy model for the telegram_user table.
"""
__tablename__ = "telegram_user"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
telegram_id = Column(String, nullable=False, unique=True, index=True)
username = Column(String)
first_name = Column(String)
last_name = Column(String)
wallet_id = Column(String, ForeignKey("user.wallet_id"))
photo_url = Column(String)
is_allowed_notification = Column(Boolean, default=False)
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
DateTime, nullable=False, default=func.now(), onupdate=func.now()
)
class Vault(Base):
"""
SQLAlchemy model for the vault table.
"""
__tablename__ = "vault"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
user_id = Column(
UUID(as_uuid=True), ForeignKey("user.id"), index=True, nullable=False
)
symbol = Column(String)
amount = Column(String)
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
DateTime, nullable=False, default=func.now(), onupdate=func.now()
)
class TransactionStatus(PyEnum):
"""
Enum for the transaction status.
"""
OPENED = "opened"
EXTRA_DEPOSIT = "extra_deposit"
CLOSED = "closed"
@classmethod
def choices(cls):
"""
Returns the list of transaction status choices.
"""
return [status.value for status in cls]
class Transaction(Base):
"""
SQLAlchemy model for the transaction table.
Stores transaction information related to positions.
"""
__tablename__ = "transaction"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
position_id = Column(
UUID(as_uuid=True), ForeignKey("position.id"), index=True, nullable=False
)
status = Column(
Enum(
TransactionStatus,
name="transaction_status_enum",
values_callable=lambda x: [e.value for e in x],
),
nullable=False,
default="opened",
)
transaction_hash = Column(String, nullable=False, unique=True, index=True)
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(
DateTime, nullable=False, default=func.now(), onupdate=func.now()
)
class ExtraDeposit(Base):
"""
SQLAlchemy model for extra deposits associated with positions.
Tracks additional deposits made to existing positions with unique
constraints on token symbol and position combinations.
"""
__tablename__ = "extra_deposits"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
token_symbol = Column(String, nullable=False)
amount = Column(String, nullable=False)
added_at = Column(DateTime, default=datetime.utcnow)
position_id = Column(UUID(as_uuid=True), ForeignKey("position.id"))
__table_args__ = (
UniqueConstraint("position_id", "token_symbol", name="_position_token_uc"),
)