def tip_user(rpc, sender_user, receiver_user, amount_tip): sender_address = user_function.get_user_address(sender_user) receiver_address = user_function.get_user_address(receiver_user) try: return send_to(rpc, sender_address, receiver_address, amount_tip) except: traceback.print_exc()
def get_user_spendable_balance(user, rpc=None): if rpc is None: rpc = get_rpc() # spendable_balance is the confirmed balance and the unconfirmed balance of # transactions that the bot has generated, but not the unconfirmed balance of # transactions originating from a wallet address that does not belong to the bot unspent_amounts = [] address = user_function.get_user_address(user) list_unspent = rpc.listunspent(0, 0, [address]) # in case of no un-spent transaction if len(list_unspent) == 0: return 0 for i in range(0, len(list_unspent), 1): trans = rpc.decoderawtransaction(rpc.getrawtransaction(list_unspent[i]['txid'])) # for v_in in range(0,len(trans['vin']),1): vin = rpc.decoderawtransaction(rpc.getrawtransaction(trans['vin'][0]['txid'])) if vin['vout'][0]['scriptPubKey']['addresses'][0] in user_function.get_users().values(): unspent_amounts.append(list_unspent[i]['amount']) bot_logger.logger.debug("unspent_amounts %s" % (str(sum(unspent_amounts)))) # check if user have pending tips pending_tips = user_function.get_balance_unregistered_tip(user) bot_logger.logger.debug("pending_tips %s" % (str(pending_tips))) return int(sum(unspent_amounts) - int(pending_tips))
def get_user_confirmed_balance(rpc, user): unspent_amounts = [] address = user_function.get_user_address(user) list_unspent = rpc.listunspent(1, 99999999999, [address]) # in case of no un-spent transaction if len(list_unspent) == 0: return 0 for i in range(0, len(list_unspent), 1): unspent_amounts.append(list_unspent[i]['amount']) bot_logger.logger.debug("unspent_amounts %s" % (str(sum(unspent_amounts)))) current_balance = rpc.getbalance("reddit-%s" % user) bot_logger.logger.debug("current_balance %s" % (str(int(current_balance)))) if int(current_balance) != int(sum(unspent_amounts)): bot_logger.logger.warn("maybe an error !") # check if user have pending tips pending_tips = user_function.get_balance_unregistered_tip(user) bot_logger.logger.debug("pending_tips %s" % (str(pending_tips))) return int(sum(unspent_amounts) - int(pending_tips))
def withdraw_user(rpc, msg): split_message = msg.body.strip().split() if user_function.user_exist(msg.author.name): sender_address = user_function.get_user_address(msg.author.name) amount = split_message[1] user_balance = crypto.get_user_balance(rpc, msg.author.name) if int(amount) >= user_balance: print( 'user %s not have enough to withdraw this amount (%s), balance = %s' % (msg.author.name, amount, user_balance)) msg.reply('+/u/%s your balance is too low for this withdraw ' % msg.author.name) else: if utils.check_amount_valid(amount): receiver_address = split_message[4] try: if crypto.send_to(rpc, sender_address, receiver_address, amount, True): user_function.add_to_history(msg.author.name, sender_address, receiver_address, amount, "withdraw") msg.reply('Withdraw : ' + str(amount) + ' to ' + receiver_address) except: traceback.print_exc() else: print('You must use valid amount') msg.reply('You must use valid amount') else: msg.reply('You need %s before' % linkRegister)
def info_user(rpc, msg): if user_function.user_exist(msg.author.name): address = user_function.get_user_address(msg.author.name) balance = crypto.get_user_confirmed_balance(rpc, msg.author.name) pending_balance = crypto.get_user_unconfirmed_balance( rpc, msg.author.name) spendable_balance = crypto.get_user_spendable_balance( rpc, msg.author.name) + balance balance_value_usd = utils.get_coin_value(balance) pending_value_usd = utils.get_coin_value(pending_balance) spendable_value_usd = utils.get_coin_value(spendable_balance) msg.reply( Template(lang.message_account_details + lang.message_footer).render( username=msg.author.name, balance=str(balance), balance_value_usd=str(balance_value_usd), pendingbalance=str(pending_balance), pending_value_usd=str(pending_value_usd), spendablebalance=str(spendable_balance), spendable_value_usd=str(spendable_value_usd), address=address)) else: msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def register_user(rpc, msg): if not user_function.user_exist(msg.author.name): address = rpc.getnewaddress("reddit-%s" % msg.author.name) if address: msg.reply( Template(lang.message_register_success + lang.message_footer).render(username=msg.author.name, address=address)) user_function.add_user(msg.author.name, address) user_function.add_to_history(msg.author.name, "", "", "", "register") # create a backup of wallet rpc.backupwallet( config.backup_wallet_path + "backup_" + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + ".dat") else: bot_logger.logger.warning('Error during register !') else: bot_logger.logger.info('%s are already registered ' % msg.author.name) balance = crypto.get_user_confirmed_balance(rpc, msg.author.name) address = user_function.get_user_address(msg.author.name) msg.reply( Template(lang.message_already_registered + lang.message_account_details + lang.message_footer).render(username=msg.author.name, address=address, balance=str(balance)))
def tip_user(rpc, sender_user, receiver_user, amount_tip, tx_queue, failover_time): bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value))) sender_address = user_function.get_user_address(sender_user) receiver_address = user_function.get_user_address(receiver_user) if time.time() > int(failover_time.value) + 86400: bot_logger.logger.info("tip send in normal mode") try: return send_to(rpc, sender_address, receiver_address, amount_tip, False, tx_queue) except: traceback.print_exc() else: bot_logger.logger.info("tip send in safe mode") try: return send_to_failover(rpc, sender_address, receiver_address, amount_tip, False, tx_queue) except: traceback.print_exc()
def help_user(rpc, msg): if user_function.user_exist(msg.author.name): address = user_function.get_user_address(msg.author.name) msg.reply( Template(lang.message_help + lang.message_footer).render( username=msg.author.name, address=address)) else: msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def withdraw_user(rpc, msg, failover_time): split_message = msg.body.strip().split() if user_function.user_exist(msg.author.name): sender_address = user_function.get_user_address(msg.author.name) amount = float(split_message[1]) amount = round(amount - 0.5) print(amount) user_balance = crypto.get_user_confirmed_balance(rpc, msg.author.name) user_spendable_balance = crypto.get_user_spendable_balance( rpc, msg.author.name) if utils.check_amount_valid( amount) and split_message[4] != sender_address: if amount >= float(user_balance) + float(user_spendable_balance): bot_logger.logger.info( 'user %s not have enough to withdraw this amount (%s), balance = %s' % (msg.author.name, amount, user_balance)) msg.reply( Template(lang.message_balance_low_withdraw).render( username=msg.author.name, user_balance=str(user_balance), amount=str(amount)) + lang.message_footer) else: receiver_address = split_message[4] if time.time() > int(failover_time.value) + 86400: send = crypto.send_to(rpc, sender_address, receiver_address, amount) else: send = crypto.send_to_failover(rpc, sender_address, receiver_address, amount) if send: user_function.add_to_history(msg.author.name, sender_address, receiver_address, amount, "withdraw") value_usd = utils.get_coin_value(amount) msg.reply( Template(lang.message_withdraw + lang.message_footer).render( username=msg.author.name, receiver_address=receiver_address, amount=str(amount), value_usd=str(value_usd))) elif split_message[4] == sender_address: msg.reply(lang.message_withdraw_to_self + lang.message_footer) else: bot_logger.logger.info(lang.message_invalid_amount) msg.reply(lang.message_invalid_amount + lang.message_footer) else: msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def get_user_balance(rpc, user): pending_tips = [] unspent_amounts = [] address = user_function.get_user_address(user) list_unspent = rpc.listunspent(0, 99999999999, [address]) for i in range(0, len(list_unspent), 1): unspent_amounts.append(list_unspent[i]['amount']) # check if user have pending tips list_tip_unregistered = user_function.get_unregistered_tip() for list_tip in list_tip_unregistered.values(): for tip in list_tip: if tip['sender'] == user: pending_tips.append(int(tip['amount'])) return int(sum(unspent_amounts) - sum(pending_tips))
def get_user_spendable_balance(rpc, user): # spendable_balance is the confirmed balance and the unconfirmed balance of # transactions that the bot has generated, but not the unconfirmed balance of # transactions originating from a wallet address that does not belong to the bot unspent_amounts = [] address = user_function.get_user_address(user) list_unspent = rpc.listunspent(0, 0, [address]) # in case of no un-spent transaction if len(list_unspent) == 0: return 0 for i in range(0, len(list_unspent), 1): trans = rpc.decoderawtransaction(rpc.getrawtransaction(list_unspent[i]['txid'])) # for v_in in range(0,len(trans['vin']),1): vin = rpc.decoderawtransaction(rpc.getrawtransaction(trans['vin'][0]['txid'])) if vin['vout'][0]['scriptPubKey']['addresses'][0] in user_function.get_users().values(): unspent_amounts.append(list_unspent[i]['amount']) # for item in range(0,len(vin['vout']),1): # for addr in range(0,len(vin['vout'][item]['scriptPubKey']['addresses']),1): # if vin['vout'][item]['scriptPubKey']['addresses'][addr] in user_function.get_users().values(): # continue # else: # list_unspent = rpc.listunspent(1, 99999999999, [address]) # continue # for i in range(0, len(list_unspent), 1): # unspent_amounts.append(list_unspent[i]['amount']) bot_logger.logger.debug("unspent_amounts %s" % (str(sum(unspent_amounts)))) current_balance = rpc.getbalance("reddit-%s" % user) bot_logger.logger.debug("current_balance %s" % (str(int(current_balance)))) if int(current_balance) != int(sum(unspent_amounts)): bot_logger.logger.warn("maybe an error !") # check if user have pending tips pending_tips = user_function.get_balance_unregistered_tip(user) bot_logger.logger.debug("pending_tips %s" % (str(pending_tips))) return int(sum(unspent_amounts) - int(pending_tips))
def get_user_unconfirmed_balance(rpc, user): unspent_amounts = [] address = user_function.get_user_address(user) list_unspent = rpc.listunspent(0, 0, [address]) # in case of no unconfirmed transactions if len(list_unspent) == 0: return 0 for i in range(0, len(list_unspent), 1): unspent_amounts.append(list_unspent[i]['amount']) bot_logger.logger.debug("unconfirmed_amounts %s" % (str(sum(unspent_amounts)))) unconfirmed_balance = rpc.getbalance("reddit-%s" % user) bot_logger.logger.debug("unconfirmed_balance %s" % (str(int(unconfirmed_balance)))) return int(sum(unspent_amounts))
def __init__(self, user): self.username = user self.address = None if user_function.user_exist(self.username): self.address = user_function.get_user_address(self.username)