diff --git a/tipbot/commands.py b/tipbot/commands.py index e1e389d..cb13a53 100644 --- a/tipbot/commands.py +++ b/tipbot/commands.py @@ -11,7 +11,7 @@ def log_event(f): @wraps(f) def decorated_function(*args, **kwargs): msg = args[0].message - logging.info(f'"{f.__name__}" invoked from {msg.from_user["username"]} - Full command: "{msg.text}"') + logging.info(f'"{f.__name__}" invoked from {msg.from_user["id"]} ({msg.from_user["first_name"]}) - Full command: "{msg.text}"') return f(*args, **kwargs) return decorated_function @@ -52,12 +52,25 @@ def help(update, context): @log_event def register(update, context): uid = update.message.from_user['id'] - un = update.message.from_user['username'] + un = update.message.from_user['first_name'] + if db.User.filter(telegram_id=uid): - update.message.reply_text('You are already registered.') + if db.User.filter(telegram_id=uid, telegram_user=un): + update.message.reply_text('You are already registered. Use /help to see available bot commands.') + else: + update.message.reply_text('Your ID exists in the database already but your `first_name` attribute has changed. Updating.') + try: + u = db.User.get(telegram_id=uid) + u.telegram_user = un + u.save() + update.message.reply_text(f'You have been registered again as Telegram ID {uid} but with username {un}.') + except Exception as e: + logging.error(f'Unable to update user in DB: {e}. Debug: {update.message}') + update.message.reply_text('Unable to update your existing account. Ask for help.') + return False else: - wallet = wownero.Wallet() try: + wallet = wownero.Wallet() account_index = wallet.new_account(label=un) except Exception as e: logging.error(f'Unable to create a new account in wallet RPC: {e}. Debug: {update.message}') @@ -71,8 +84,8 @@ def register(update, context): ) u.save() reply_text = [ - 'You have been registered and can now send and receive tips.', - 'Ask for /help to see all available bot commands.' + f'You have been registered as Telegram ID {uid} and username {un} and can now send and receive tips.', + 'Ask for /help to see all available bot commands. Maybe start with /deposit to get your deposit address.' ] update.message.reply_text(' '.join(reply_text)) except Exception as e: @@ -98,7 +111,7 @@ def tip(update, context): else: target_un = context.args[0] - if target_un == update.message.from_user['username']: + if target_un == update.message.from_user['first_name']: update.message.reply_text('You cannot tip yourself!') return False @@ -123,19 +136,21 @@ def tip(update, context): 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]: + if amount >= tipper_balances[1]: update.message.reply_text(f'You do not have sufficient funds to send {amount} WOW. Check your /balance') return False # get target user details - u = db.User.get(telegram_user=target_un) - address = wownero.Wallet().addresses(account=u.account_index)[0] + 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=amount, priority=2, account=u.account_index) - print(tx) - update.message.reply_text(f'@{update.message.from_user["username"]} has tipped @{target_un} {amount} WOW!') + tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=tipper.account_index) + if 'tx_hash' in tx: + update.message.reply_text(f'Tipped @{target_un} {wownero.from_atomic(tx["amount"])} WOW! Here is the TX ID: {tx["tx_hash"]}') + else: + update.message.reply_text('Failed to send a tip. Ask for help.') except Exception as e: logging.error(f'Unable to send transfer: {e}. Debug: {update.message}') update.message.reply_text('Failed to send a tip. Ask for help.') @@ -176,8 +191,11 @@ def send(update, context): # transfer funds to given address try: - tx = wownero.Wallet().transfer(dest_address=address, amount=amount, priority=2, account=sender.account_index) - update.message.reply_text(f'Sent {amount} WOW! Tx: {tx}') + tx = wownero.Wallet().transfer(dest_address=address, amount=wownero.as_wownero(amount), priority=2, account=sender.account_index) + if 'tx_hash' in tx: + update.message.reply_text(f'Sent {wownero.from_atomic(tx["amount"])} WOW! Here is the TX ID: {tx["tx_hash"]}') + else: + update.message.reply_text('Failed to send Wownero. Ask for help.') except Exception as e: logging.error(f'Unable to send transfer: {e}. Debug: {update.message}') update.message.reply_text('Failed to send Wownero. Ask for help.') @@ -188,7 +206,7 @@ def send(update, context): def balance(update, context): u = db.User.get(telegram_id=update.message.from_user['id']) balances = wownero.Wallet().balances(account=u.account_index) - update.message.reply_text(f'Available balance for {update.message.from_user["username"]}: {float(balances[1])} WOW ({float(balances[0])} WOW locked)') + update.message.reply_text(f'Available balance for {u.telegram_user}: {float(balances[1])} WOW ({float(balances[0])} WOW locked)') @wallet_rpc_required @registration_required @@ -196,26 +214,13 @@ def balance(update, context): def deposit(update, context): u = db.User.get(telegram_id=update.message.from_user['id']) address = wownero.Wallet().addresses(account=u.account_index)[0] - update.message.reply_text(f'Deposit address for {update.message.from_user["username"]}: {address}') + update.message.reply_text(f'Deposit address for {u.telegram_user}: {address}') @wallet_rpc_required @log_event def debug(update, context): if is_tg_admin(update.message.from_user['id']): - # tx = wownero.Wallet().transfer( - # dest_address='WW2vmEGV68ZFeQWwPEJda3UcdWCPfWBnDK1Y6MB9Uojx9adBhCxfx9F51TomRjmD3z7Gyogie3mfVQEkRQjLxqbs1KMzaozDw', - # amount=Decimal(2), - # priority=2, - # account=0 - # ) - # update.message.reply_text(str(tx)) - # balances = wownero.Wallet().balances(account=0) - # addresses = wownero.Wallet().addresses(account=0) - # accounts = wownero.Wallet().accounts() - # a = [] - # for i in accounts: - # a.append(str(wownero.Wallet().balances(account=i)[1])) - update.message.reply_text('sup lza') + pass else: update.message.reply_text('you cant do that.') diff --git a/tipbot/wownero.py b/tipbot/wownero.py index e42e05f..bc85970 100644 --- a/tipbot/wownero.py +++ b/tipbot/wownero.py @@ -107,7 +107,7 @@ def to_atomic(amount): if not isinstance(amount, (Decimal, float) + six.integer_types): raise ValueError("Amount '{}' doesn't have numeric type. Only Decimal, int, long and " "float (not recommended) are accepted as amounts.") - return int(amount * 10**12) + return int(amount * 10**11) def from_atomic(amount): return (Decimal(amount) * PICOWOW).quantize(PICOWOW)