class Settlement(db.Model): id = db.Column(db.Integer, primary_key=True) ext_account_id_to_settle = db.Column(db.String(256)) sender_withdrawal_amount = db.Column(db.Float) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id_to_settle, sender_withdrawal_amount): self.ext_account_id_to_settle = ext_account_id_to_settle self.sender_withdrawal_amount = sender_withdrawal_amount def __repr__(self): return '<Settlement: %r>' % self.ext_account_id @classmethod def find_by_ext_account_id(cls, ext_account_id): records = None try: records = cls.query.filter_by(ext_account_id_to_settle=ext_account_id). \ order_by(Settlement.time_created).First() except: records = None return records def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Client(db.Model): __tablename__ = 'client' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String()) created = db.Column(db.DateTime(), default=func.now()) last_connection = db.Column(db.DateTime(), default=func.now(), onupdate=datetime.now) def __repr__(self): return f'<Client(id, {self.name})>'
class BCMonitor(db.Model): __tablename__ = 'monitor' id = db.Column(db.Integer, primary_key=True) latest_block = db.Column(db.Integer) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, latest_block): self.latest_block = latest_block def __repr__(self): return '<bc monitor: %r>' % self.latest_block # @classmethod # def find_by_last_block_by_number(cls, latest_block): # try: # return cls.query.filter_by(latest_block=latest_block).first() # except Exception as e: # print('Failed to find_by_last_block: ' + str(e)) # db.session.rollback() @classmethod def find_by_last_block(cls): try: return cls.query.order_by(None).first() except Exception as e: print('Failed to find_by_last_block: ' + str(e)) db.session.rollback() @classmethod def find_latest_block_by_number(cls, latest_block): try: return cls.query.filter_by(latest_block=latest_block).one() except: db.session.rollback() return None def update_db(self): db.session.merge(self) db.session.flush() db.session.commit() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Account(BaseModel, db.Model): __tablename__ = 'accounts' id = db.Column(db.Integer, primary_key=True) ext_account_id = db.Column(db.String(256)) user_public_key = db.Column(db.String(66)) pk_sender = db.Column(db.String(256)) token_address = db.Column(db.String(256)) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id, user_public_key, token_address, pk_sender=None): self.ext_account_id = ext_account_id self.user_public_key = user_public_key self.token_address = token_address self.pk_sender = pk_sender @classmethod def find_by_ext_account_id(cls, ext_account_id): records = None try: records = cls.query.filter_by(ext_account_id=ext_account_id).one() except: records = None return records @classmethod def find_by_user_public_key(cls, user_public_key): return cls.query.filter_by(user_public_key=user_public_key).all() @classmethod def get_all_accounts(cls): try: return cls.query.order_by(Account.time_created).all() except: return None def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Token(db.Model): __tablename__ = 'tokens' id = db.Column(db.Integer, primary_key=True) token_address = db.Column(db.String(256)) token_decimal = db.Column(db.Integer) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, token_address, token_decimal): self.token_address = token_address self.token_decimal = token_decimal def __repr__(self): return '<bc monitor: %r>' % self.latest_block @classmethod def find_by_token_address(cls, token_address): try: return cls.query.filter_by(token_address=token_address).one() except: return None @classmethod def get_all_tokens(cls): try: return cls.query.order_by(Token.time_created).all() except: return None def update_db(self): db.session.no_autoflush() db.session.merge(self) db.session.flush() db.session.commit() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class CurrentChannelState(db.Model): # CCS.TotalPaidToReceiver CCS.Nonce __tablename__ = 'ccs' id = db.Column(db.Integer, primary_key=True) ext_account_id = db.Column(db.String(256)) total_paid_to_receiver = db.Column(db.Float) nonce = db.Column(db.Integer) receiver_signature = db.Column(db.String(256)) sender_signature = db.Column(db.String(256)) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id, nonce, total_paid_to_receiver, receiver_signature, sender_signature): self.ext_account_id = ext_account_id self.nonce = nonce self.total_paid_to_receiver = total_paid_to_receiver self.receiver_signature = receiver_signature self.sender_signature = sender_signature def __repr__(self): return '<ccs: %r>' % self.ext_account_id @classmethod def find_by_ext_account_id(cls, ext_account_id): records = None try: records = cls.query.filter_by( ext_account_id=ext_account_id).order_by( CurrentChannelState.time_created.desc()).first() except: records = None return records def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Withdraw(db.Model): id = db.Column(db.Integer, primary_key=True) ext_account_id = db.Column(db.String(256)) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id): self.ext_account_id = ext_account_id def __repr__(self): return '<Account: %r>' % self.ext_account_id def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()
class Command(db.Model): __tablename__ = 'commands' id = db.Column(db.Integer, primary_key=True) command_name = db.Column(db.String(100)) user_public_key = db.Column(db.String(66)) command_args = db.Column(db.JSON) # 1 - to do , 2 - ready, 3- failed , 4 - in process command_status = db.Column(db.Integer) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, command_name, user_public_key, command_status, command_args): self.command_name = command_name self.command_args = command_args self.command_status = command_status self.user_public_key = user_public_key @classmethod def find_by_command_status(cls, command_status): return cls.query.filter_by(command_status=command_status).all() def save_to_db(self): db.session.add(self) db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit() def update_db(self): db.session.merge(self) db.session.flush() db.session.commit()
class User(db.Model): """ Telegram user. """ __tablename__ = "users" # Columns id = db.Column(db.Integer, primary_key=True) create_date = db.Column(db.DateTime(timezone=False), server_default=func.now(), nullable=False) last_update_date = db.Column(db.DateTime(timezone=False), server_default=func.now(), onupdate=func.now(), nullable=False) telegram_id = db.Column( db.Integer, unique=True, nullable=False, comment="Unique ID to identificate user in Telegram") is_bot = db.Column(db.Boolean, nullable=False, comment="User is bot in Telegram") language = db.Column(db.Enum(SupportedLanguages), default=SupportedLanguages.EN, nullable=False, comment="Preferred language of user") group = db.Column(db.Enum(UserGroup), default=UserGroup.USER, nullable=False, comment="User rights group") # Relationships chats = db.relationship("Chat", back_populates="user", uselist=True) yandex_disk_token = db.relationship("YandexDiskToken", back_populates="user", uselist=False) def __repr__(self): return f"<User {self.id}>" @staticmethod def create_fake(): """ Creates fake user. """ from faker import Faker fake = Faker() random_number = fake.pyint(min_value=1, max_value=20, step=1) result = User() result.create_date = fake.date_time_between(start_date="-2y", end_date="now", tzinfo=None) result.last_update_date = fake.date_time_between_dates( datetime_start=result.create_date, tzinfo=None) result.telegram_id = fake.pyint(min_value=100000, max_value=10000000, step=1) result.is_bot = (fake.pyint() % 121 == 0) result.language = fake.random_element(list(SupportedLanguages)) result.group = (fake.random_element(list(UserGroup)) if (random_number % 20 == 0) else UserGroup.USER) return result
class Channel(db.Model): __tablename__ = 'channels' # Balance, Sender Withdrawable, Receiver Withdrawable, TotalWithdrawedByReceiver, id = db.Column(db.Integer, primary_key=True) ext_account_id = db.Column(db.String(256)) user_public_key = db.Column(db.String(66)) channel_address = db.Column(db.String(66)) channel_balance = db.Column(db.Float) channel_capacity = db.Column(db.Float) last_settled_nonce = db.Column(db.Integer) channel_status = db.Column(db.String(50)) sender_withdrawable = db.Column(db.Float) receiver_withdrawable = db.Column(db.Float) total_withdrawed_by_receiver = db.Column(db.Float) time_created = db.Column(db.DateTime(timezone=True), server_default=func.now()) time_updated = db.Column(db.DateTime(timezone=True), onupdate=func.now()) def __init__(self, ext_account_id, user_public_key, channel_balance, channel_status, total_withdrawed_by_receiver, sender_withdrawable, receiver_withdrawable, channel_address, channel_capacity, last_settled_nonce): self.ext_account_id = ext_account_id self.user_public_key = user_public_key self.channel_balance = channel_balance self.channel_status = channel_status self.total_withdrawed_by_receiver = total_withdrawed_by_receiver self.sender_withdrawable = sender_withdrawable self.receiver_withdrawable = receiver_withdrawable self.channel_address = channel_address self.channel_capacity = channel_capacity self.last_settled_nonce = last_settled_nonce def json(self): return { 'ext_account_id': self.ext_account_id, 'user_public_key': self.user_public_key, 'channel_address': self.channel_address, 'channel_balance': self.channel_balance, # 'channel_capacity': self.channel_capacity, 'channel_status': self.channel_status, 'total_withdrawed_by_receiver': self.total_withdrawed_by_receiver, 'sender_withdrawable': self.sender_withdrawable, 'receiver_withdrawable': self.receiver_withdrawable } def __repr__(self): return '<Channel: %r>' % self.id def channel_snapshot_json(self): return { 'ext_account_id': self.ext_account_id, 'channel_contract_ethereum_address': self.channel_contract_ethereum_address, 'channel_address': self.channel_address, 'channel_balance': self.channel_balance, 'sender_withdrawable': self.sender_withdrawable, 'receiver_withdrawable': self.receiver_withdrawable, 'channel_status': self.channel_status, 'total_withdrawed_by_receiver': self.total_withdrawed_by_receiver } @classmethod def find_by_user_public_key(cls, user_public_key): return cls.query.filter_by(user_public_key=user_public_key).all() @classmethod def find_by_ext_account_id(cls, ext_account_id): return cls.query.filter_by(ext_account_id=ext_account_id).first() @classmethod def find_by_channel_address(cls, channel_address): try: return cls.query.filter_by(channel_address=channel_address).one() except: return None def save_to_db(self): db.session.add(self) db.session.commit() def update_db(self): db.session.merge(self) db.session.flush() db.session.commit() def delete_from_db(self): db.session.delete(self) db.session.commit()