2020-08-06 05:52:13 +00:00
|
|
|
import logging
|
|
|
|
from decimal import Decimal
|
|
|
|
from tipbot import wownero
|
|
|
|
from tipbot import db
|
2020-08-06 20:40:03 +00:00
|
|
|
from tipbot.helpers.decorators import wallet_rpc_required, log_event, registration_required, check_debug
|
2021-05-10 20:09:55 +00:00
|
|
|
from tipbot.helpers.utils import reply_user
|
2020-08-06 05:52:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@wallet_rpc_required
|
|
|
|
@registration_required
|
|
|
|
@log_event
|
2020-08-06 20:40:03 +00:00
|
|
|
@check_debug
|
2020-08-06 05:52:13 +00:00
|
|
|
def withdraw(update, context):
|
|
|
|
if len(context.args) < 2:
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = 'Not enough arguments passed.'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# validate address
|
|
|
|
if len(context.args[0]) in [97, 108]:
|
|
|
|
address = context.args[0]
|
|
|
|
else:
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = 'This does not look like a valid Wownero address. Try again.'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# validate amount
|
|
|
|
try:
|
|
|
|
amount = Decimal(context.args[1])
|
|
|
|
except:
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = 'Bad Wownero amount specified; not a valid number.'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if amount < 1:
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = 'Bad Wownero amount specified. Provide only positive integers or decimals greater than or equal to 1.'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
2021-05-10 20:09:55 +00:00
|
|
|
sender = db.User.get(telegram_id=update.message.from_user.id)
|
2020-08-06 05:52:13 +00:00
|
|
|
sender_balances = wownero.Wallet().balances(account=sender.account_index)
|
|
|
|
if amount > sender_balances[1]:
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = f'You do not have sufficient funds to withdraw {amount} WOW. Check your /balance'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
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']
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = f'Sent {amount} WOW! Tx: {h}'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
else:
|
2020-08-06 08:24:39 +00:00
|
|
|
logging.error(f'Transaction failure details for {sender.telegram_user} ({sender.telegram_id}): {tx}')
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = f'Failed to withdraw Wownero. Reason: "{tx["message"]}"'
|
|
|
|
reply_user(update.message, context, _m)
|
2020-08-06 05:52:13 +00:00
|
|
|
except Exception as e:
|
2020-08-09 07:55:42 +00:00
|
|
|
logging.error(f'Unable to withdraw transfer: {e}. Debug: {update.message}')
|
2021-05-10 20:09:55 +00:00
|
|
|
_m = 'Failed to withdraw Wownero. Ask for help.'
|
|
|
|
reply_user(update.message, context, _m)
|