def balance(bot, update): """ Fetches and returns the balance (in USD) """ if not checks.check_username(update): return create_user(update.message.from_user.username) key = Key(get_wif(update.message.from_user.username)) balance = key.get_balance("usd") return update.message.reply_text("You have: $" + str(balance))
class Wallet: def __init__(self, testing=False, unittest_prkey=''): if testing == True: self.testing = testing self.private_key = read_drive(testing) self.key = PrivateKeyTestnet(self.private_key) elif unittest_prkey != '': self.testing = testing self.private_key = unittest_prkey self.key = Key(self.private_key) else: self.testing = testing self.private_key = read_drive(testing) self.key = Key(self.private_key) def bch_address(self): return self.key.address def bch_segwit_address(self): return self.key.segwit_address def bch_balance(self, currency="bch"): spinner.start() balance = self.key.get_balance(currency) spinner.stop() return balance def bch_history(self): spinner.start() transactions = self.key.get_transactions() spinner.stop() print("") print(colorama.Fore.GREEN + str(len(transactions)) + " transactions:") print("") for transaction in transactions: print("[ " + transaction + " ]") # perform a transaction def transaction(self, recipient_addr, amount, currency): cashaddr = re.match( '^((bitcoincash|bchreg|bchtest):)?(q|p)[a-z0-9]{41}$', recipient_addr) legacyaddr = re.match('^([13][a-km-zA-HJ-NP-Z1-9]{25,34})', recipient_addr) caplegacyaddr = re.match('^((BITCOINCASH:)?(Q|P)[A-Z0-9]{41})$', recipient_addr) if not any([cashaddr, legacyaddr, caplegacyaddr]): print(colorama.Fore.RED + "Transaction aborted: Invalid bitcoin cash address.") exit() print('You are about to send {} {} to {}'.format( str(amount), currency.upper(), recipient_addr)) amount_left = float(self.bch_balance(currency)) - amount if amount_left <= 0: print(colorama.Fore.RED + 'You don\'t have enough funds to perform the transaction.') exit() if currency == 'usd': print("You will have ${} left on your wallet.".format( str(amount_left))) elif currency == 'bch': print("You will have {} BCH left on your wallet.".format( str(amount_left))) elif currency == 'eur': print("You will have {}€ left on your wallet.".format( str(amount_left))) elif currency == 'gbp': print("You will have £{} left on your wallet.".format( str(amount_left))) elif currency == 'jpy': print("You will have ¥{} left on your wallet.".format( str(amount_left))) print("") decision = input( 'Are you sure you want to perform this transaction? yes | no\n') if decision != 'yes': print("") print(colorama.Fore.RED + 'Transaction aborted') exit() else: try: spinner.start() transaction_id = self.key.send([(recipient_addr, amount, currency)]) spinner.stop() print(colorama.Fore.GREEN + 'Success: ' + colorama.Fore.WHITE + transaction_id) except InsufficientFunds: print( colorama.Fore.RED + 'You don\'t have enough funds to perform the transaction.') def bch_tx_fee(self): fee = get_fee() return fee
def tip(bot, update, args): """ Sends Bitcoin Cash on-chain """ if not checks.check_username(update): return if len(args) != 2 and not update.message.reply_to_message: return update.message.reply_text("Usage: /tip [amount] [username]") if "@" in args[0]: # this swaps args[0] and args[1] in case user input username before # amount (e.g. /tip @merc1er $1) - the latter will still work tmp = args[1] args[1] = args[0] args[0] = tmp amount = args[0].replace("$", "") if not checks.amount_is_valid(amount): return update.message.reply_text(amount + " is not a valid amount.") if update.message.reply_to_message: recipient_username = update.message.reply_to_message.from_user.username if not recipient_username: return update.message.reply_text( "You cannot tip someone who has not set a username.") else: recipient_username = args[1] if not checks.username_is_valid(recipient_username): return update.message.reply_text(recipient_username + " is not a valid username.") recipient_username = recipient_username.replace("@", "") sender_username = update.message.from_user.username if recipient_username == sender_username: return update.message.reply_text("You cannot send money to yourself.") create_user(recipient_username) # IMPROVE recipient_address = get_address(recipient_username) sender_wif = get_wif(sender_username) key = Key(sender_wif) balance = key.get_balance("usd") # checks the balance if float(amount) > float(balance): return update.message.reply_text("You don't have enough funds! " + "Type /deposit to add funds!!") fee = float(amount) * FEE_PERCENTAGE sent_amount = float(amount) - 0.01 if fee < 0.01: outputs = [ (recipient_address, sent_amount, "usd"), ] else: sent_amount -= fee # deducts fee outputs = [ (recipient_address, sent_amount, "usd"), (FEE_ADDRESS, fee, "usd"), ] try: key.send(outputs, fee=1) except Exception: return bot.send_message( chat_id=update.effective_chat.id, text="Transaction failed!", parse_mode=ParseMode.MARKDOWN, ) return bot.send_message( chat_id=update.effective_chat.id, text="You sent $" + amount + " to " + recipient_username, )