class Withdrawal(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False) withdrawal_id = db.Column(db.Integer, primary_key=True, nullable=False) address = db.Column(db.String(100), nullable=False) amount = db.Column(db.BigInteger, nullable=False) withdrawal_token = db.Column(db.String(100), nullable=False) cancelled = db.Column(db.Boolean, nullable=False) transaction_id = db.Column(db.String(100), nullable=False) created_date = db.Column(db.DateTime(), nullable=False) confirmed_date = db.Column(db.DateTime()) sent_date = db.Column(db.DateTime()) def to_dic(self): return { "userId": self.user_id, "withdrawalId": self.withdrawal_id, "address": self.address, "amount": self.amount, "cancelled": self.cancelled, "transactionId": self.transaction_id, "createdDate": self.created_date, "confirmedDate": self.confirmed_date, "sentDate": self.sent_date } def clone(self): return copy.copy(self) def copy_values(self, item): self.__dict__.update(item.__dict__)
class ChangeEmail(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False) change_email_token = db.Column(db.String(100), primary_key=True, nullable=False) new_email = db.Column(db.String(200), nullable=False) created_date = db.Column(db.DateTime(), nullable=False) def __init__(self): return def __init__(self, dic): self.from_dic(dic) def is_valid(self): if self.user_id < 0: return False if self.new_email.strip() == '': return False if not validate_email(self.new_email): return False return True def from_dic(self, dic): self.change_email_token = uuid.uuid4() try: self.user_id = int(dic.get("userId", "")) except: self.user_id = -1 self.new_email = dic.get("newEmail", "").strip().lower() self.created_date = datetime.datetime.utcnow()
class Email(db.Model): email_id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) address = db.Column(db.String(2000), nullable=False) sent_date = db.Column(db.DateTime(), nullable=False, index=True) subject = db.Column(db.String(2000), nullable=False) body = db.Column(db.Text, nullable=False)
class Currency(db.Model): currency_id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) symbol = db.Column(db.String(5), unique=True, nullable=False) name = db.Column(db.String(100), nullable=False) price_decimal_point = db.Column(db.Integer, nullable=False) def __init__(self): self.price = 0
class UserRegister(db.Model): registration_token = db.Column(db.String(100), primary_key=True, nullable=False) username = db.Column(db.String(100), index=True, nullable=False) email = db.Column(db.String(200), index=True, nullable=False) password = db.Column(db.String(200), nullable=False) registration_date = db.Column(db.DateTime(), nullable=False) def __init__(self): return def __init__(self, dic): self.from_dic(dic) def is_valid(self): if not re.match('^[a-zA-Z0-9_\-]{6,}$', self.username): return False if self.email.strip() == '': return False if not validate_email(self.email): return False if len(self.password) < 8: return False return True def set_password(self, password): self.password = hashlib.sha512( (password + app.config['SALT']).encode('utf-8')).hexdigest() def from_dic(self, dic): self.registration_token = uuid.uuid4() self.username = dic.get("username", "").strip().lower() self.email = dic.get("email", "").strip().lower() self.password = dic.get("password", "") self.registration_date = datetime.datetime.utcnow() def to_dic(self): return { "registrationToken": self.registration_token, "username": self.username, "email": self.email, "registrationDate": self.registration_date }
class DepositAddress(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False) address_id = db.Column(db.Integer, primary_key=True, nullable=False) address = db.Column(db.String(100), nullable=False) created_date = db.Column(db.DateTime(), nullable=False) def to_dic(self): return { "userId": self.user_id, "addressId": self.address_id, "address": self.address, "createdDate": self.created_date } def clone(self): return copy.copy(self) def copy_values(self, item): self.__dict__.update(item.__dict__)
class ForgotPassword(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False) forgot_password_token = db.Column(db.String(100), primary_key=True, nullable=False) created_date = db.Column(db.DateTime(), nullable=False)
class Equity(db.Model): PERCENT_MULTIPLIER = 10000 equity_id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) symbol = db.Column(db.String(100), nullable=False) name = db.Column(db.String(100), nullable=False) broker = db.Column(db.String(100), nullable=False) base_currency_index = db.Column(db.Integer, nullable=False) decimal_points_price = db.Column(db.Integer, nullable=False) decimal_points_quantity = db.Column(db.Integer, nullable=False) smallest_quantity = db.Column(db.Integer, nullable=False) api_index = db.Column(db.Integer, nullable=False) api_symbol = db.Column(db.String(100), nullable=False) tradable_requirement = db.Column(db.Integer, nullable=False) margin_requirement = db.Column(db.Integer, nullable=False) def __init__(self): self.current_price = -1 return def __init__(self, dic): self.current_price = -1 self.from_dic(dic) def clone(self): return copy.copy(self) def copy_values(self, item): self.__dict__.update(item.__dict__) def get_price(self): return self.current_price / (self.decimal_points_price + 0.0) def from_dic(self, dic): self.equity_id = dic.get("equityId", None) self.symbol = dic.get("symbol", "") self.name = dic.get("name", "") self.broker = dic.get("broker", "") self.base_currency_index = dic.get("baseCurrencyIndex", 0) self.decimal_points_price = dic.get("decimalPointsPrice", 1) self.decimal_points_quantity = dic.get("decimalPointsQuantity", 1) self.smallest_quantity = dic.get("smallestQuantity", "") self.api_index = dic.get("apiIndex", "") self.api_symbol = dic.get("apiSymbol", "") self.tradable_requirement = dic.get("tradableRequirement", "") self.margin_requirement = dic.get("marginRequirement", "") def to_dic(self): return { "equityId": self.equity_id, "symbol": self.symbol, "name": self.name, "broker": self.broker, "baseCurrencyIndex": self.base_currency_index, "decimalPointsPrice": self.decimal_points_price, "decimalPointsQuantity": self.decimal_points_quantity, "smallestQuantity": self.smallest_quantity, "apiIndex": self.api_index, "apiSymbol": self.api_symbol, "tradableRequirement": self.tradable_requirement, "marginRequirement": self.margin_requirement }
class User(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) username = db.Column(db.String(100), unique=True, nullable=False) email = db.Column(db.String(200), unique=True, nullable=False) two_f_a_token = db.Column(db.String(100), nullable=False) two_f_a_enabled = db.Column(db.Boolean, nullable=False) password = db.Column(db.String(200), nullable=False) balance = db.Column(db.BigInteger, nullable=False) is_margin_called = db.Column(db.Boolean, nullable=False) is_admin = db.Column(db.Boolean, nullable=False) registration_date = db.Column(db.DateTime(), nullable=False) def __init__(self): self.margin_used = 0 self.margin_used_percent = 0.0 self.margin_used_orders = 0 self.margin_used_orders_percent = 0.0 def __init__(self, user_register): self.margin_used = 0 self.margin_used_percent = 0.0 self.margin_used_orders = 0 self.margin_used_orders_percent = 0.0 self.from_user_register(user_register) def clone(self): return copy.copy(self) def copy_values(self, item): self.__dict__.update(item.__dict__) def init_calculated_field(self): self.margin_used = 0 self.margin_used_percent = 0.0 self.margin_used_orders = 0 self.margin_used_orders_percent = 0.0 def from_user_register(self, user_register): self.username = user_register.username self.email = user_register.email self.two_f_a_token = "" self.two_f_a_enabled = False self.password = user_register.password self.balance = 0.0 self.is_margin_called = False self.is_admin = False self.registration_date = datetime.datetime.utcnow() def to_dic(self): return { "userId": self.user_id, "username": self.username, "email": self.email, "twoFactorEnabled": self.two_f_a_enabled, "balance": int(self.balance), "isMarginCalled": self.is_margin_called, "isAdmin": self.is_admin, "registrationDate": self.registration_date } @staticmethod def margin_used_percent_comparer_dec(item1, item2): comp = 1 if item1.margin_used_percent > item2.margin_used_percent else -1 if item1.margin_used_percent < item2.margin_used_percent else 0 if comp != 0: return comp return -1 if item1.user_id > item2.user_id else 1 if item1.user_id < item2.user_id else 0 @staticmethod def margin_used_orders_percent_comparer_dec(item1, item2): comp = 1 if item1.margin_used_orders_percent > item2.margin_used_orders_percent else -1 if item1.margin_used_orders_percent < item2.margin_used_orders_percent else 0 if comp != 0: return comp return -1 if item1.user_id > item2.user_id else 1 if item1.user_id < item2.user_id else 0 def check_password(self, password): return hashlib.sha512( (password + app.config['SALT']).encode('utf-8')).hexdigest() == self.password def check_token(self, token): if not self.two_f_a_enabled: return True totp = pyotp.TOTP(self.two_f_a_token) return totp.verify(token) def set_password(self, password): self.password = hashlib.sha512( (password + app.config['SALT']).encode('utf-8')).hexdigest() def can_place_order(self, order): pass def can_execute_order(self, order, next_price): pass def add_to_balance_and_margin(self, balance, margin, margin_orders, bitcoin_price): self.balance += math.floor(balance) self.margin_used += math.ceil(margin) self.margin_used_orders += math.ceil(margin_orders) self.margin_used_percent = self.margin_used / (self.balance + 0.0) self.margin_used_orders_percent = self.margin_used_orders / ( self.balance + 0.0) if self.is_margin_called and self.margin_used_percent / bitcoin_price < 1.0: self.is_margin_called = False
class Participant(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) def __repr__(self): return '<Participant %s>' % self.name
class SessionToken(db.Model): user_id = db.Column(db.Integer, primary_key=True, nullable=False) session_token = db.Column(db.String(100), primary_key=True, nullable=False) ip_address = db.Column(db.String(100), nullable=False) issued_date = db.Column(db.DateTime(), nullable=False) expiry_date = db.Column(db.DateTime(), nullable=False)