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
|
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 tip(update, context):
|
|
|
|
if len(context.args) < 2:
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message('Not enough arguments passed.')
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
elif len(context.args) == 2:
|
|
|
|
message = ""
|
|
|
|
elif len(context.args) > 2:
|
|
|
|
message = context.args[2:]
|
|
|
|
|
|
|
|
# validate target user
|
|
|
|
if context.args[0].startswith('@'):
|
|
|
|
target_un = context.args[0][1:]
|
|
|
|
else:
|
|
|
|
target_un = context.args[0]
|
|
|
|
|
|
|
|
if target_un == update.message.from_user['first_name']:
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message('You cannot tip yourself!')
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if not db.User.filter(telegram_user=target_un):
|
|
|
|
reply_text = [
|
|
|
|
'That user has not registered and cannot receive tips yet.',
|
|
|
|
'If they would like to receive a tip, have them /register with the bot.'
|
|
|
|
]
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message(' '.join(reply_text))
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# validate amount
|
|
|
|
try:
|
|
|
|
amount = Decimal(context.args[1])
|
|
|
|
except:
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message(f'Bad Wownero amount specified; not a valid number.')
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
if amount < 1:
|
2021-05-10 04:40:42 +00:00
|
|
|
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()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
tipper = db.User.get(telegram_id=update.message.from_user['id'])
|
|
|
|
tipper_balances = wownero.Wallet().balances(account=tipper.account_index)
|
|
|
|
if amount >= tipper_balances[1]:
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message(f'You do not have sufficient funds to send {amount} WOW. Check your /balance')
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# get target user details
|
|
|
|
receiver = db.User.get(telegram_user=target_un)
|
|
|
|
address = wownero.Wallet().addresses(account=receiver.account_index)[0]
|
|
|
|
|
|
|
|
# transfer funds to user
|
|
|
|
try:
|
|
|
|
tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=tipper.account_index)
|
|
|
|
if 'tx_hash' in tx:
|
|
|
|
h = tx['tx_hash']
|
2020-08-07 14:46:36 +00:00
|
|
|
msg = f'Tipped @{target_un} {amount} WOW! Tx: {h}'
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message(msg)
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
else:
|
2020-08-06 08:24:39 +00:00
|
|
|
logging.error(f'Transaction failure details for {tipper.telegram_user} ({tipper.telegram_id}): {tx}')
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message(f'Failed to send a tip. Reason: "{tx["message"]}"')
|
|
|
|
update.message.delete()
|
2020-08-06 05:52:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
logging.error(f'Unable to send transfer: {e}. Debug: {update.message}')
|
2021-05-10 04:40:42 +00:00
|
|
|
update.message.from_user.send_message('Failed to send a tip. Ask for help.')
|
|
|
|
update.message.delete()
|