class DeadlineFractionMixin(object): __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) account_key = db.Column(db.String(64), index=True) plotter_id = db.Column(db.String(64), index=True) miner_name = db.Column(db.String(64), index=True) fraction = db.Column(db.DECIMAL(32, 16)) capacity = db.Column(db.DECIMAL(32, 16)) height = db.Column(db.INTEGER) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) deadline = db.Column(db.BigInteger) def to_dict(self): dl_fraction_dict = { "account_key": self.account_key, "plotter_id": self.plotter_id, "miner_name": self.miner_name, "fraction": self.fraction, "capacity": self.capacity, "height": self.height, "create_time": str(self.create_time), "deadline": self.deadline, } return dl_fraction_dict
class IncomeMixin(BaseModel): __abstract__ = True __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) # 用户id account_key = db.Column(db.String(36)) # 区块收益bhd数量 amount = db.Column(db.DECIMAL(32, 16)) # 实际获取数量 actual_amount = db.Column(db.DECIMAL(32, 16)) # 挖出区块高度 height = db.Column(db.INTEGER) # 矿工抵押率 mortgage_rate = db.Column(db.DECIMAL(32, 16)) # 类型 type = db.Column(db.INTEGER) # 容量 capacity = db.Column(db.DECIMAL(32, 16)) # 是否已经计算收益 is_add_asset = db.Column(db.SmallInteger) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) update_time = db.Column(db.TIMESTAMP(), default=datetime.now) def __init__(self, account_key, amount, income_type, is_add_asset=0, capacity=0): self.account_key = account_key self.amount = amount self.actual_amount = amount self.mortgage_rate = 1 self.type = income_type self.is_add_asset = is_add_asset self.capacity = capacity def __setattr__(self, key, value): super().__setattr__(key, value) if key != "update_time": self.update_time = datetime.now() def to_dict(self): income_record_dict = { "account_key": self.account_key, "amount": self.amount, "actual_amount": self.actual_amount, "height": self.height, "mortgage_rate": self.mortgage_rate, "create_time": str(self.create_time), "is_add_asset": self.is_add_asset, "capacity": self.capacity, "type": self.type, } return income_record_dict
class WithdrawalTransaction(BaseModel): __tablename__ = 'pool_withdrawal_transaction' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) account_key = db.Column(db.String(64), index=True) to_address = db.Column(db.String(128)) amount = db.Column(db.DECIMAL(32, 16)) actual_amount = db.Column(db.DECIMAL(32, 16)) coin_name = db.Column(db.String(32)) txid = db.Column(db.String(256), default='') status = db.Column(db.SmallInteger, default='1') create_time = db.Column(db.TIMESTAMP(), default=datetime.now) def __init__(self, account_key, amount, to_address, status=WITHDRAWAL_APPLY, coin_name=BHD_COIN_NAME): self.account_key = account_key self.amount = amount self.coin_name = coin_name self.to_address = to_address if coin_name == DISK_NAME: if amount < 50: raise ValueError elif amount >= 50 and amount <= 500: self.actual_amount = (amount - Decimal("5")).quantize( Decimal('.00000000'), rounding=ROUND_DOWN) elif amount > 500: self.actual_amount = (amount * DISK_WITHDRAWAL_ACTUAL).quantize( Decimal('.00000000'), rounding=ROUND_DOWN) else: if amount < 100: self.actual_amount = (amount - Decimal("0.1")).quantize( Decimal('.00000000'), rounding=ROUND_DOWN) else: self.actual_amount = (amount * WITHDRAWAL_ACTUAL).quantize( Decimal('.00000000'), rounding=ROUND_DOWN) self.status = status def to_dict(self): withdrawal_transaction_dict = { "id": self.id, "account_key": self.account_key, "to_address": self.to_address, "amount": self.amount, "coin_name": self.coin_name, "txid": self.txid, "create_time": str(self.create_time), "status": self.status, } return withdrawal_transaction_dict
class AssetTransfer(db.Model): __tablename__ = 'pool_asset_transfer' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) account_key = db.Column(db.String(64), index=True) amount = db.Column(db.DECIMAL(32, 16)) direction = db.Column(db.BOOLEAN, comment="True:抵押到余额,False:余额到抵押") create_time = db.Column(db.TIMESTAMP(), default=datetime.now) coin_name = db.Column(db.String(32), default='0') def __init__(self, account_key, amount, direction, coin_name): self.account_key = account_key self.amount = amount self.direction = direction self.coin_name = coin_name def to_dict(self): transfer_dict = { "account_key": self.account_key, "amount": self.amount, "coin_name": self.coin_name, "direction": self.direction, "create_time": str(self.create_time), } return transfer_dict
class Billings(db.Model): """ 平台账单,记录所有资产转移记录。 目前包括type: 充提 1、充值, 2、提现, 划转 3、余额->抵押 4、抵押->余额 挖矿收益 5、合作 6、非合作 7、活动奖励 8、汇聚 """ __tablename__ = 'pool_billings' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) account_key = db.Column(db.String(64), index=True) to_address = db.Column(db.String(128), default='') from_address = db.Column(db.String(128), default='') amount = db.Column(db.DECIMAL(32, 16)) txid = db.Column(db.String(256), default='') tag = db.Column(db.String(256), default='') type = db.Column(db.SmallInteger) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) def __init__(self, account_key, amount, from_address, to_address, type, txid='', tag='', create_time=datetime.now()): self.account_key = account_key self.amount = amount self.from_address = from_address self.to_address = to_address self.type = type self.txid = txid self.tag = tag self.create_time = create_time def to_dict(self): billing_dict = { "account_key": self.account_key, "from_address": self.from_address, "to_address": self.to_address, "amount": self.amount, "txid": self.txid, "create_time": str(self.create_time), "type": self.type, } return billing_dict
class LHDTeamWorkActivity(BaseModel): __tablename__ = 'pool_lhd_team_work_activity' __table_args__ = {'extend_existing': True} # id id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(64)) total_amount = db.Column(db.DECIMAL(32, 16)) distribution = db.Column(db.DECIMAL(32, 16)) available_amount = db.Column(db.DECIMAL(32, 16)) deadline = db.Column(db.DECIMAL(32, 16)) min_amount = db.Column(db.DECIMAL(32, 16)) type = db.Column(db.INTEGER) rate = db.Column(db.DECIMAL(32, 16)) create_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) update_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) end_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) def to_dict(self): tr_dict = { "title": self.title, "total_amount": self.total_amount, "distribution": self.distribution, "available_amount": self.available_amount, "deadline": self.deadline, "min_amount": self.min_amount, "type": self.type, "rate": self.rate, "create_time": str(self.create_time), "end_time": str(self.end_time), } return tr_dict
class LHDTeamWorkRecordActivity(BaseModel): __tablename__ = 'pool_lhd_team_work_record_activity' __table_args__ = {'extend_existing': True} # id id = db.Column(db.Integer, primary_key=True) account_key = db.Column(db.String(64), index=True) order_id = db.Column(db.BigInteger) income = db.Column(db.DECIMAL(32, 16)) coo_amount = db.Column(db.DECIMAL(32, 16)) # 1进行中,0已结束 status = db.Column(db.Integer) is_foul = db.Column(db.Integer) cooperation_id = db.Column(db.Integer) type = db.Column(db.INTEGER) release_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) begin_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) end_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) create_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) update_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) def to_dict(self): tr_dict = { "account_key": self.account_key, "order_id": self.order_id, "income": self.income, "coo_amount": self.coo_amount, "status": self.status, "cooperation_id": self.cooperation_id, "is_foul": self.is_foul, "type": self.type, "create_time": str(self.create_time), } return tr_dict
class Produto(db.Model): __tablename__ = 'produtos' id = db.Column(db.Integer, primary_key=True) nome = db.Column(db.String(250), nullable=False) preco_unitario = db.Column(db.DECIMAL(18, 2), nullable=False) multiplo = db.Column(db.Integer, nullable=True, server_default=db.text('1')) def __init__(self, nome, preco_unitario, multiplo): self.nome = nome self.preco_unitario = preco_unitario self.multiplo = multiplo
class ItensPedido(db.Model): __tablename__ = 'itens_pedido' id = db.Column(db.Integer, primary_key=True) produto_id = db.Column(db.Integer, db.ForeignKey('produtos.id', ondelete='CASCADE'), nullable=False) produto = db.relationship('Produto', backref=db.backref('produtos', lazy='dynamic')) preco_unitario = db.Column(db.DECIMAL(18, 2), nullable=False) quantidade = db.Column(db.Integer, nullable=False) rentabilidade = db.Column(db.String, nullable=False) pedidos_id = db.Column(db.Integer, db.ForeignKey('pedidos.id', ondelete='CASCADE'), nullable=False) pedido = db.relationship('Pedido', backref=db.backref('pedidos', lazy='dynamic')) def __init__(self, produto_id, preco_unitario, quantidade, rentabilidade, pedidos_id): self.produto_id = produto_id self.preco_unitario = preco_unitario self.quantidade = quantidade self.rentabilidade = rentabilidade self.pedidos_id = pedidos_id
class LHDRemotePledgeTransaction(BaseModel): __tablename__ = 'pool_lhd_remote_pledge_transaction' __table_args__ = {'extend_existing': True} # id id = db.Column(db.Integer, primary_key=True) # 用户account_key account_key = db.Column(db.String(64), index=True) # 用户远程抵押地址 pledge_address = db.Column(db.String(64), index=True) # 抵押数量 pledge_amount = db.Column(db.DECIMAL(32, 16)) # 抵押高度 pledge_height = db.Column(db.Integer) # 抵押交易id pledge_txid = db.Column(db.String(64), unique=True) # 抵押状态, 抵押状态。1,抵押撤销,0 status = db.Column(db.Integer) create_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) update_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) def __init__(self, account_key, pledge_address, pledge_amount, pledge_height, pledge_txid): self.account_key = account_key self.pledge_address = pledge_address self.pledge_amount = pledge_amount self.pledge_height = pledge_height self.pledge_txid = pledge_txid self.status = DEBITING def to_dict(self): remote_pledge_tr_dict = { "account_key": self.account_key, "pledge_address": self.pledge_address, "pledge_amount": self.pledge_amount, "pledge_height": self.pledge_height, "pledge_txid": self.pledge_txid, "status": self.status, "create_time": str(self.create_time), } return remote_pledge_tr_dict
class ActivityReward(BaseModel): __tablename__ = 'pool_capacity_winning_record_activity' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer) account_key = db.Column(db.String(64), index=True) email = db.Column(db.String(128)) amount = db.Column(db.DECIMAL(32, 16)) grade = db.Column(db.String(32)) update_time = db.Column(db.TIMESTAMP(), default=datetime.now) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) is_add_asset = db.Column(db.SmallInteger) def to_dict(self): activity_reward_dict = { "account_key": self.account_key, "amount": self.amount, "is_add_asset": self.is_add_asset, "create_time": self.create_time, } return activity_reward_dict
class DepositTranscation(BaseModel): __tablename__ = 'pool_deposit_transaction' id = db.Column(db.Integer, primary_key=True) # 用户id account_key = db.Column(db.String(64), index=True) # 交易所在区块高度 block_number = db.Column(db.Integer) # 确认数 confirmed = db.Column(db.SmallInteger) # 需要确认数 need_confirm_num = db.Column(db.SmallInteger) # 推送过的确认数 pushed_confirm = db.Column(db.SmallInteger, server_default='0') # 币种名 coin_name = db.Column(db.String(32)) # 交易id tx_id = db.Column(db.String(255), index=True) # 充值数量 amount = db.Column(db.DECIMAL(32, 16)) # 交易状态,1 确认中, 2 成功, 3 失败 status = db.Column(db.SmallInteger) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) update_time = db.Column(db.TIMESTAMP(), default=datetime.now) # 是否发送到汇聚地址, 0, 未发送, 1, 发送 is_pushed = db.Column(db.SmallInteger) __table_args__ = (db.UniqueConstraint('account_key', 'tx_id', name='account_key_unite_tx_id'), { 'extend_existing': True }) def __init__(self, account_key, amount, coin_name, tx_id, block_number, need_confirm_num, confirmed=0): self.account_key = account_key self.amount = float(amount) self.coin_name = coin_name self.tx_id = tx_id self.status = DEPOSIT_CONFIRMING self.is_pushed = 0 self.block_number = block_number self.need_confirm_num = need_confirm_num self.confirmed = confirmed self.pushed_confirm = -1 def __setattr__(self, key, value): super(DepositTranscation, self).__setattr__(key, value) if key != "update_time": self.update_time = datetime.now() def to_dict(self): transaction_dict = { "account_key": self.account_key, "amount": self.amount, "coin_name": self.coin_name, "tx_id": self.tx_id, "block_number": self.block_number, "need_confirm_num": self.need_confirm_num, "confirmed": self.confirmed, "status": self.status, "create_time": str(self.create_time), "update_time": str(self.update_time), } return transaction_dict
class UserAsset(BaseModel): __tablename__ = 'pool_user_asset' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key=True) # 用户account_key account_key = db.Column(db.String(64), index=True) # 用户id user_id = db.Column(db.INTEGER) # 资产id asset_id = db.Column(db.INTEGER) # 币种名 coin_name = db.Column(db.String(32), default='0') # 抵押资产 pledge_asset = db.Column(db.DECIMAL(32, 16), default='0') # 可用资产, 可用来提现,交易,远程抵押保证金 available_asset = db.Column(db.DECIMAL(32, 16), default='0') # 提现冻结资产 frozen_asset = db.Column(db.DECIMAL(32, 16), default='0') # 交易冻结资产 trading_asset = db.Column(db.DECIMAL(32, 16), default='0') # 合作挖矿冻结资产 coop_freeze_asset = db.Column(db.DECIMAL(32, 16), default='0') # 远程抵押资产总额 remote_freeze_asset = db.Column(db.DECIMAL(32, 16), default='0') # 远程抵押资产-抵押金额 remote_4pledge_asset = db.Column(db.DECIMAL(32, 16), default='0') # 远程抵押资产-合作金额 remote_4coop_asset = db.Column(db.DECIMAL(32, 16), default='0') # 总资产 = 可用+抵押+交易冻结+提现冻结+合作冻结中不包含远程合作 total_asset = db.Column(db.DECIMAL(32, 16), default='0') # 理财冻结 coop_freeze_asset_deadlock = db.Column(db.DECIMAL(32, 16), default='0') # 理论抵押 # theory_asset = db.Column(db.BigInteger) # 收益率 # yield_rate = db.Column(db.DECIMAL(32, 16)) # 抵押率 # pledge_rate = db.Column(db.DECIMAL(32, 16)) create_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) update_time = db.Column(db.TIMESTAMP(), server_default=func.current_timestamp()) def __setattr__(self, key, value): super(UserAsset, self).__setattr__(key, value) if key != "update_time": self.update_time = datetime.now() def to_dict(self): user_asset_dict = { "account_key": self.account_key, "user_id": self.user_id, "coin_name": self.coin_name, "asset_id": self.asset_id, "available_asset": self.available_asset, "total_asset": self.total_asset, "frozen_asset": self.frozen_asset, "trading_asset": self.trading_asset, "create_time": str(self.create_time), } if self.coin_name in (BHD_COIN_NAME, LHD_NAME): user_asset_dict.update({ # 矿池抵押 "pledge_asset": self.get_pledge_amount(), # 指向抵押 "remote_freeze_asset": self.remote_freeze_asset, # 合作冻结 "coop_freeze_asset": self.coop_freeze_asset, # 指向用于抵押 "remote_4pledge_asset": self.remote_4pledge_asset, # 指向用于合作 "remote_4coop_asset": self.remote_4coop_asset, # 可用抵押资产 "available_pledge_asset": self.get_available_pledge_amount(), # 可用保证金 "available_margin_asset": self.get_available_margin_amount(), # 理财冻结 "coop_freeze_asset_deadlock": self.coop_freeze_asset_deadlock, }) return user_asset_dict # 保证金 def get_available_margin_amount(self): # 充值+收益=可用+抵押+合作冻结-远程借贷合作部分 amount = self.available_asset + self.pledge_asset + self.coop_freeze_asset - self.remote_4coop_asset # 抵押金 if self.coin_name == LHD_NAME: margin_amount = self.remote_freeze_asset * 3 / 7 else: margin_amount = self.remote_freeze_asset / 4 available_margin_asset = amount - margin_amount return available_margin_asset def get_available_pledge_amount(self): return self.available_asset + self.remote_freeze_asset - self.remote_4pledge_asset - self.remote_4coop_asset def get_pledge_amount(self): return self.pledge_asset + self.remote_4pledge_asset def get_can_deduct(self): return self.coop_freeze_asset - self.remote_4coop_asset def get_remote_avai_amount(self): return self.remote_freeze_asset - self.remote_4pledge_asset - self.remote_4coop_asset def get_total_available_pledge_amount(self): return self.get_remote_avai_amount() + self.available_asset def get_available_withdrawal_amount(self): available_asset = self.available_asset + self.get_remote_avai_amount() total_asset = available_asset + self.coop_freeze_asset + self.get_pledge_amount( ) if self.coin_name == LHD_NAME: available_withdrawal_asset = total_asset - self.remote_freeze_asset / 7 * 10 else: available_withdrawal_asset = total_asset - self.remote_freeze_asset / 4 * 5 return available_withdrawal_asset def get_local_in_coop(self): return self.coop_freeze_asset - self.remote_4coop_asset def withdrawal_asset(self, amount): # todo 自动合并可提现资产, available_amount = self.get_available_withdrawal_amount() if available_amount < amount: raise Exception("available_amount: %s, withdrawal_amount:%s " % (available_amount, amount))