flush.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:two1-python 作者: 21dotco 项目源码 文件源码
def _flush(client, wallet, machine_auth, amount=None, payout_address=None, silent=False,
           to_primary=False):
    """ Flushes current off-chain buffer to the blockchain.

    Args:
        client (two1.server.rest_client.TwentyOneRestClient) an object for
            sending authenticated requests to the TwentyOne backend.
        wallet (two1.wallet.Wallet): a user's wallet instance.
        amount (int): The amount to be flushed. Should be more than 10k.
        payout_address (string): The address to flush the Bitcoins to.
        silent (boolean): If True, disables the confirmation prompt.
        to_primary (boolean): If True, flushes to the primary wallet.

    Raises:
        ServerRequestError: if server returns an error code other than 401.
    """

    # check the payout address
    if payout_address:
        try:
            base58.b58decode_check(payout_address)
        except ValueError:
            logger.error(uxstring.UxString.flush_invalid_address)
            return

    # select the wallet and the associated payout address for the flush
    all_wallets = client.list_wallets()
    wallet_payout_address, wallet_name = _select_flush_wallet(client, machine_auth, payout_address,
                                                              all_wallets, to_primary)

    # ask user for confirmation
    if not silent:
        # if the user has not specified a payout_address then the buffer will be flushed to the
        # primary wallet.
        is_local = payout_address is None
        should_continue = _show_confirmation(machine_auth, amount, all_wallets,
                                             wallet_payout_address, wallet_name, to_primary,
                                             is_local)
        if not should_continue:
            return

    # perform flush
    try:
        response = client.flush_earnings(amount=amount, payout_address=wallet_payout_address)
        if response.ok:
            success_msg = uxstring.UxString.flush_success.format(wallet_payout_address)
            logger.info(success_msg)
    except exceptions.ServerRequestError as ex:
        if ex.status_code == 401:
            logger.info(ex.message)
        elif ex.status_code == 400 and ex.data.get("error") == "TO500":
            logger.info(uxstring.UxString.flush_not_enough_earnings.format(amount), fg="red")
        elif ex.status_code == 403 and ex.data.get("error") == "social_account_required_to_flush":
            logger.info("You must connect a social account with your 21.co account before you can flush.", fg="red")
        else:
            raise ex
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号