def withdraw(bot, update, args): """ Withdraws BCH to user's wallet """ if not checks.check_username(update): return if update.message.chat.type != "private": # check if in DM return bot.send_message( chat_id=update.effective_chat.id, text="Private message me to withdraw your money", ) if len(args) != 2: message = ("Usage: /withdraw [amount] [address]\n\n" "You may also withdraw everything at once using:" " /withdraw all [address]") return update.message.reply_text(message) address = checks.check_address(update, args[1]) if not address: # does not send anything if address is False return wif = get_wif(update.message.from_user.username) key = Key(wif) if args[0] == "all": sent_amount = 1000 currency = "satoshi" else: amount = args[0].replace("$", "") if not checks.amount_is_valid(amount): return update.message.reply_text(amount + " is not a valid amount") currency = "usd" sent_amount = float(amount) - 0.01 # after 1 cent fee outputs = [ (address, sent_amount, currency), ] key.get_unspents() try: if args[0] == "all": tx_id = key.send(outputs, fee=1, leftover=address) else: tx_id = key.send(outputs, fee=1) except Exception: return update.message.reply_text("Transaction failed!") return update.message.reply_text("Sent! Transaction ID: " + tx_id)
def withdraw(bot, update, args): """ Withdraws BCH to user's wallet """ if not checks.check_username(update): return if update.message.chat.type != 'private': # check if in DM return bot.send_message( chat_id=update.effective_chat.id, text='Private message me to withdraw your money') if len(args) != 2: message = 'Usage: /withdraw [amount] [address]\n\n'\ 'You may also withdraw everything at once using:'\ ' /withdraw all [address]' return update.message.reply_text(message) address = checks.check_address(update, args[1]) if not address: # does not send anything if address is False return wif = get_wif(update.message.from_user.username) key = Key(wif) if args[0] == 'all': sent_amount = 1000 currency = 'satoshi' else: amount = args[0].replace('$', '') if not checks.amount_is_valid(amount): return update.message.reply_text(amount + ' is not a valid amount') currency = 'usd' sent_amount = float(amount) - 0.01 # after 1 cent fee outputs = [ (address, sent_amount, currency), ] key.get_unspents() try: if args[0] == 'all': tx_id = key.send(outputs, fee=1, leftover=address) else: tx_id = key.send(outputs, fee=1) except Exception: return update.message.reply_text('Transaction failed!') return update.message.reply_text('Sent! Transaction ID: ' + tx_id)
def send_bitcash_message(message): if len(message.encode('utf-8')) > 213: raise ValueError(message + "超长,长度是:" + len(message.encode('utf-8'))) else: config = get_systemconfigs() key = Key(config.private_key) upspents = key.get_unspents() logger.info(upspents) logger.info(key.address) outputs = [ (config.outaddress, 10056, 'satoshi'), ] try: txhash, txid = key.send(outputs, message=message, fee=config.default_fee) logging.info('{m}交易成功,txid:{txid}'.format(m=message, txid=txid)) return (txhash, txid) except Exception as e: logging.error('{m}交易失败,异常:{e}'.format(m=message, e=str(e))) raise e