Example #1
0
def task_chain_out_confirm():
    """链外转账确认"""
    with db.app.app_context():
        orders = Order.chain_out_unconfirm(COIN_NAME, 100)
        for order in orders:
            # 定时任务开关
            if not consul_clinet.get_task_switch():
                logging.warning('{}链外转账确认定时任务被手动关闭!'.format(COIN_NAME))
                return
            order_hash = order.order_hash

            # 本地节点是否链接,是否同步
            if not abs_wallet.is_connected():
                logging.error('{}节点离线!'.format(COIN_NAME))
                return
            if abs_wallet.is_syncing():
                logging.error('{}节点同步未完成!'.format(COIN_NAME))
                return

            # 订单确认详情
            order_confirm_detail = OrderDetail(
                id=str(uuid.uuid1()),
                order_id=order.id,
                act_type=4,
                query_json=json.dumps(dict(hash=order_hash)),
                status=-1
            )

            # 查询交易是否被确认
            is_confirm, is_success, msg, fee = abs_wallet.hash_get_detail(order_hash)
            print('确认结果', is_confirm, is_success, msg, fee)
            # 未确认
            if not is_confirm:
                order_confirm_detail.status = 0
                order_confirm_detail.response_json = json.dumps(msg)
                try:
                    db.session.add(order_confirm_detail)
                    db.session.commit()
                except Exception as e:
                    logging.error('chain_out_withdraw update order error:{}'.format(str(e)))
                    return
            # 已确认
            else:
                order_confirm_detail.response_json = json.dumps(msg)
                order_confirm_detail.status = 1
                # 确认交易失败
                if not is_success:
                    order.status = 0
                # 确认交易成功
                else:
                    order.status = 1
                    order.done_time = datetime.now()
                    order.fee = fee
                    order.notice_status = -2  # 待通知
                try:
                    db.session.add_all([order, order_confirm_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error('chain_out_withdraw update order error:{}'.format(str(e)))
                    return
Example #2
0
def task_chain_out_withdraw():
    """提现链外转账"""
    with db.app.app_context():

        # VPC base账户address
        vpc_base_address = consul_clinet.get_digiccy_base_account(
            COIN_NAME, is_seed=False)
        # vpc_base_seed = consul_clinet.get_digiccy_base_account(COIN_NAME)
        #
        # gas_limit, gas_price = consul_clinet.get_digiccy_collect_fee(COIN_NAME)

        # 待链外转账订单
        orders = Order.chain_out_withdraw(COIN_NAME, 100)
        for order in orders:
            # 定时任务开关
            if not consul_clinet.get_task_switch():
                logging.warning('{}链内充值定时任务被手动关闭!'.format(COIN_NAME))
                return

            # 本地节点是否链接,是否同步
            if not vpc_wallet.is_connected():
                logging.error('{}节点离线!'.format(COIN_NAME))
                return
            if vpc_wallet.is_syncing():
                logging.error('{}节点同步未完成!'.format(COIN_NAME))
                return

            # 订单收款账户,数量
            order_to = order.order_to
            amount = order.amount

            # 链外账户余额
            base_account_balance = vpc_wallet.get_balance(
                vpc_base_address)  # decimal.Decimal
            minerfee = vpc_wallet.get_minerFee(vpc_base_address)
            if minerfee < 0.0001:
                logging.error('base账户矿工费余额不足,余额{}'.format(str(minerfee)))
                return

            if amount + Decimal('0.002') > base_account_balance:
                logging.error('base账户余额不足,支付金额{}'.format(str(amount)))
                print('base账户余额不足,支付金额{}'.format(str(amount)))
                continue

            # 付款账户信息
            # 当前区块
            block_num = vpc_wallet.get_block_num()
            nonce_num = vpc_wallet.get_nonce(vpc_base_address)

            # 改变订单状态
            order.order_from = vpc_base_address
            order.status = 0

            order_detail = OrderDetail(id=str(uuid.uuid1()),
                                       order_id=order.id,
                                       act_type=2,
                                       query_json=json.dumps(
                                           dict(addrfrom=vpc_base_address,
                                                addrto=order_to,
                                                block_num=block_num,
                                                nonce_num=nonce_num)),
                                       response_json='',
                                       status=-1)
            try:
                db.session.add_all([order, order_detail])
                db.session.commit()
            except Exception as e:
                logging.error(
                    'chain_out_withdraw update order error:{}'.format(str(e)))
                return
            is_success, msg = vpc_wallet.payment(vpc_base_address, order_to,
                                                 amount)
            print('定时任务链外转账结果', is_success, msg)
            if is_success:
                order_detail.status = 1  # 订单请求成功
                order_detail.response_json = msg
                order.order_hash = msg
                order.status = 2  # 转账发起成功待确认
                try:
                    db.session.add_all([order, order_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
            else:
                order_detail.status = 0  # 订单请求失败
                order_detail.response_json = json.dumps(msg)
                try:
                    db.session.add(order_detail)
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
Example #3
0
def task_chain_in_recharge():
    """链内充值定时任务"""
    with db.app.app_context():

        # stellar base 账户
        stellar_base_seed = consul_clinet.get_stellar_base_account(COIN_NAME)

        limit_num = 100
        len_num = 0
        success_num = 0
        while True:
            orders = Order.chain_out_recharge(COIN_NAME, limit_num)
            len_num += len(orders)
            if not orders:
                print('{}链内充值处理完毕,处理条数:{},成功条数:{}'.format(
                    COIN_NAME, len_num, success_num))
                logging.info('{}链内充值处理完毕,处理条数:{},成功条数:{}'.format(
                    COIN_NAME, len_num, success_num))
                break

            for order in orders:
                if not consul_clinet.get_task_switch():
                    logging.warning('{}链内充值定时任务被手动关闭!'.format(COIN_NAME))
                    return

                destination = order.stellar_account
                amount = order.amount

                result = digiccy_base.transfer_compulsory(stellar_base_seed,
                                                          amount,
                                                          fee=0,
                                                          order_limit=False)
                if isinstance(result, CodeMsg.CM):
                    logging.error('Stellar {} base account error:{}'.format(
                        COIN_NAME, result.msg))
                    return
                sequence, stellar_kp = result
                te, stellar_hash = digiccy_base.transfer_te(sequence,
                                                            amount,
                                                            stellar_kp,
                                                            destination,
                                                            memo='r')

                record_order = Order(
                    id=str(uuid.uuid1()),
                    relate_id=order.id,
                    user_id=order.id,
                    stellar_account=destination,
                    coin_name=COIN_NAME,
                    order_type=1,
                    chain_type=1,
                    order_from=stellar_kp.address().decode('utf-8'),
                    order_to=destination,
                    amount=amount,
                    fee=0,
                    order_hash=stellar_hash,
                    status=0,
                    add_time=datetime.now(),
                    notice_status=0,
                )
                order.relate_id = record_order.id
                order.relate_order_status = 1
                record_order_detail = OrderDetail(
                    id=str(uuid.uuid1()),
                    order_id=record_order.id,
                    act_type=1,
                    query_json=json.dumps(dict(stellar_hash=stellar_hash)),
                    response_json='',
                    status=-1,
                )
                try:
                    db.session.add_all(
                        [record_order, order, record_order_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'Insert {} chain in recharge order error:{}'.format(
                            COIN_NAME, str(e)))
                    db.session.rollback()
                    print('保存出错', str(e))
                    return

                response = digiccy_base.transfer_submit(te)
                response_hash = response.get('hash')
                if response_hash:
                    record_order.status = 1
                    record_order.notice_status = -1  # 待通知
                    record_order.done_time = datetime.now()
                    record_order_detail.status = 1
                    success_num += 1
                else:
                    record_order.status = 2  # 待确认
                    record_order_detail.status = 0
                record_order_detail.response_json = json.dumps(response)
                try:
                    db.session.add_all([record_order, record_order_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'Insert {} chain in recharge order error:{}'.format(
                            COIN_NAME, str(e)))
                    db.session.rollback()
                    return
Example #4
0
def task_chain_out_confirm():
    """链外转账确认"""
    with db.app.app_context():
        # status为2的订单
        orders = Order.btc_chain_out_unconfirm(COIN_NAME, 100)
        print(orders)
        for order in orders:

            # 获取tx_id
            order_hash = order.order_hash
            # time.sleep(10)
            # 本地节点是否链接,是否同步
            print(btc_wallet.is_connected(), btc_wallet.is_sync())
            if not btc_wallet.is_connected():
                logging.error('{}节点离线123!'.format(COIN_NAME))
                return
            if not btc_wallet.is_sync():
                logging.error('{}节点同步未完成!'.format(COIN_NAME))
                return

            # 订单确认详情
            order_confirm_detail = OrderDetail(id=str(uuid.uuid1()),
                                               order_id=order.id,
                                               act_type=4,
                                               query_json=json.dumps(
                                                   dict(hash=order_hash)),
                                               status=-1)

            # 查询交易是否被确认
            is_confirm, is_success, msg, fee = btc_wallet.hash_get_detail(
                order_hash)
            print('确认结果', is_confirm, is_success, msg, fee)
            # 未确认
            if not is_confirm:
                order_confirm_detail.status = 0
                order_confirm_detail.response_json = json.dumps(dict(msg=msg))
                try:
                    db.session.add(order_confirm_detail)
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
            # 已确认
            else:
                order_confirm_detail.response_json = json.dumps(msg)
                order_confirm_detail.status = 1
                # 确认交易失败
                if not is_success:
                    order.status = 0
                # 确认交易成功
                else:
                    order.status = 1
                    order.done_time = datetime.now()
                    order.fee = fee
                    order.notice_status = -2  # 待通知
                try:
                    db.session.add_all([order, order_confirm_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
Example #5
0
def task_chain_out_withdraw():
    """提现链外转账"""
    with db.app.app_context():

        # 从consul获取BTC base账户address
        btc_base_address = consul_clinet.get_digiccy_base_account(
            COIN_NAME, is_seed=False)
        # 根据address得到BTC base账户的account
        btc_base_account = btc_wallet.get_accounts(btc_base_address)
        if btc_base_account is None:
            return
        # 查询链外status为-1的订单
        orders = Order.chain_out_withdraw(COIN_NAME, 100)
        for order in orders:
            # 定时任务开关
            if not consul_clinet.get_task_switch():
                logging.warning('{}链内充值定时任务被手动关闭!'.format(COIN_NAME))
                return

            # 节点是否链接,是否同步
            if not btc_wallet.is_connected():
                logging.error('{}节点离线!'.format(COIN_NAME))
                return
            if not btc_wallet.is_sync():
                logging.error('{}节点同步未完成!'.format(COIN_NAME))
                return

            # 链外账户的收款地址
            order_to = order.order_to
            # 转账数量
            amount = order.amount

            # 检查财务账户链外余额, 即付款账户余额
            base_account_balance = btc_wallet.get_balance_by_account(
                btc_base_account)  # decimal.Decimal
            # 链外转账手续费
            fee = btc_wallet.estimate_fee()
            if base_account_balance or fee is None:
                return
            if amount + fee > base_account_balance:
                logging.error('base账户余额不足,支付金额{}'.format(str(amount)))
                print('base账户余额不足,支付金额{}'.format(str(amount)))
                continue

            # 当前区块信息
            block_num = btc_wallet.get_block_num()

            # 改变订单状态
            order.order_from = btc_base_account
            order.status = 0

            order_detail = OrderDetail(id=str(uuid.uuid1()),
                                       order_id=order.id,
                                       act_type=2,
                                       query_json=json.dumps(
                                           dict(addrfrom=btc_base_account,
                                                addrto=order_to,
                                                block_num=block_num)),
                                       response_json='',
                                       status=-1)
            try:
                db.session.add_all([order, order_detail])
                db.session.commit()
            except Exception as e:
                logging.error(
                    'chain_out_withdraw update order error:{}'.format(str(e)))
                return

            # 链外转账
            is_success, txid = btc_wallet.payment(btc_base_account, order_to,
                                                  amount)
            print('定时任务链外转账结果', is_success, txid)
            if is_success:
                order_detail.status = 1  # 订单请求成功
                order_detail.response_json = txid
                order.order_hash = txid
                order.status = 2  # 成功发起转账,转账是否成功,待确认
                try:
                    db.session.add_all([order, order_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
            else:
                order_detail.status = 0  # 订单请求失败
                order_detail.response_json = json.dumps(txid)
                try:
                    db.session.add(order_detail)
                    db.session.commit()
                except Exception as e:
                    logging.error(
                        'chain_out_withdraw update order error:{}'.format(
                            str(e)))
                    return
Example #6
0
def task_chain_out_withdraw():
    """提现链外转账"""
    with db.app.app_context():

        # EOS base账户address
        eos_base_address = consul_clinet.get_digiccy_base_account(COIN_NAME, is_seed=False)
        privKey = consul_clinet.get_digiccy_base_privatekey(COIN_NAME)
        # privKey = digiccy_base.stellar_keypair(stellar_encrypt_seed=privKey)

        privKey = eval(decryptSeed(privKey)).get("seed")
        # privKey = "5Jb46jpSGwuXJXcom6e5PdLsaW1rMwc3MUqhZnVTApqVynMcXxA"
        # 待链外转账订单
        orders = Order.chain_out_withdraw(COIN_NAME, 100)
        for order in orders:
            # 定时任务开关
            if not consul_clinet.get_task_switch():
                logging.warning('{}链内充值定时任务被手动关闭!'.format(COIN_NAME))
                return

            # 本地节点是否链接,是否同步
            if not eos_wallet.init_consul("", ""):
                logging.error('{}节点离线!'.format(COIN_NAME))
                return
            if not eos_wallet.is_syncing():
                logging.error('{}节点同步未完成!'.format(COIN_NAME))
                return

            # 订单收款账户,数量
            order_to = order.order_to
            amount = order.amount

            # 链外账户余额
            base_account_balance = eos_wallet.get_balance(eos_base_address)  # decimal.Decimal
            if base_account_balance < amount:
                logging.error('eos base账户余额不足,支付金额{}'.format(str(amount)))

            # 当前区块
            block_num = eos_wallet.get_block_num()

            # 改变订单状态
            order.order_from = eos_base_address
            order.status = 0

            order_detail = OrderDetail(
                id=str(uuid.uuid1()),
                order_id=order.id,
                act_type=2,
                query_json=json.dumps(
                    dict(addrfrom=eos_base_address, addrto=order_to, block_num=block_num)),
                response_json='',
                status=-1
            )
            try:
                db.session.add_all([order, order_detail])
                db.session.commit()
            except Exception as e:
                logging.error('chain_out_withdraw update order error:{}'.format(str(e)))
                return
            is_success, msg = eos_wallet.payment(privKey, eos_base_address, order_to, amount)
            print('定时任务链外转账结果', is_success, msg)
            if is_success:
                order_detail.status = 1  # 订单请求成功
                order_detail.response_json = msg
                order.order_hash = msg
                order.status = 2  # 转账发起成功待确认
                try:
                    db.session.add_all([order, order_detail])
                    db.session.commit()
                except Exception as e:
                    logging.error('chain_out_withdraw update order error:{}'.format(str(e)))
                    return
            else:
                order_detail.status = 0  # 订单请求失败
                order_detail.response_json = json.dumps(msg)
                try:
                    db.session.add(order_detail)
                    db.session.commit()
                except Exception as e:
                    logging.error('chain_out_withdraw update order error:{}'.format(str(e)))
                    return