import logging from decimal import Decimal from tipbot import wownero from tipbot import db from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug @wallet_rpc_required @registration_required @log_event @check_debug def withdraw(update, context): if len(context.args) < 2: update.message.from_user.send_message('Not enough arguments passed.') update.message.delete() return False # validate address if len(context.args[0]) in [97, 108]: address = context.args[0] else: update.message.from_user.send_message('This does not look like a valid Wownero address. Try again.') update.message.delete() return False # validate amount try: amount = Decimal(context.args[1]) except: update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.') update.message.delete() return False if amount < 1: update.message.from_user.send_message('Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.') update.message.delete() return False sender = db.User.get(telegram_id=update.message.from_user['id']) sender_balances = wownero.Wallet().balances(account=sender.account_index) if amount > sender_balances[1]: update.message.from_user.send_message(f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance') update.message.delete() return False # transfer funds to given address try: tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=sender.account_index) if 'tx_hash' in tx: h = tx['tx_hash'] msg = f'Sent {amount} WOW! Tx: {h}' update.message.from_user.send_message(msg) update.message.delete() else: logging.error(f'Transaction failure details for {sender.telegram_user} ({sender.telegram_id}): {tx}') update.message.from_user.send_message(f'Failed to withdraw Wownero. Reason: "{tx["message"]}"') update.message.delete() except Exception as e: logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}') update.message.from_user.send_message('Failed to withdraw Wownero. Ask for help.') update.message.delete()