def donate(reddit, msg, tx_queue, failover_time): user = models.User(msg.author.name) if user.is_registered(): split_message = msg.body.lower().strip().split() donate_index = split_message.index('+donate') amount = split_message[donate_index + 1] if utils.check_amount_valid(amount) and split_message[donate_index + 2] == 'doge': crypto.tip_user(user.username.address, models.User(config.bot_name).address, amount, tx_queue, failover_time) history.add_to_history(msg.author.name, msg.author.name, config.bot_name, amount, "donate") else: bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(user.username).message('invalid amount', lang.message_invalid_amount) else: bot_logger.logger.info('user %s not registered (command : donate) ' % user.username) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=user.username))
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 test_check_amount_valid(self): self.assertEqual(True, utils.check_amount_valid(1)) self.assertEqual(True, utils.check_amount_valid(10)) self.assertEqual(False, utils.check_amount_valid(0.1)) self.assertEqual(False, utils.check_amount_valid(-1)) self.assertEqual(True, utils.check_amount_valid("1")) self.assertEqual(True, utils.check_amount_valid("10")) self.assertEqual(False, utils.check_amount_valid("0.1")) self.assertEqual(False, utils.check_amount_valid("-1"))
def withdraw_user(msg, failover_time): split_message = msg.body.strip().split() user = models.User(msg.author.name) if user.is_registered(): if utils.check_amount_valid( split_message[1]) and split_message[4] != user.address: amount = float(split_message[1]) amount = round(amount - 0.5) user_balance = user.get_balance() if amount >= float(user_balance): bot_logger.logger.info( 'user %s not have enough to withdraw this amount (%s), balance = %s' % (user.username, amount, user_balance)) msg.reply( Template(lang.message_balance_low_withdraw).render( username=user.username, user_balance=str(user_balance), amount=str(amount)) + lang.message_footer) else: receiver_address = split_message[4] tip_id = random.randint(0, 99999999) models.HistoryStorage.add_to_history(user.username, user.username, receiver_address, amount, "withdraw", "", tip_id) send = crypto.tip_user(user.address, receiver_address, amount, None, failover_time) if send: models.HistoryStorage.update_withdraw( user.username, True, send, tip_id) value_usd = utils.get_coin_value(amount) msg.reply( Template(lang.message_withdraw + lang.message_footer).render( username=user.username, receiver_address=receiver_address, amount=str(amount), value_usd=str(value_usd))) elif split_message[4] == user.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: bot_logger.logger.info('user %s not registered (command : withdraw) ' % user.username) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def withdraw_user(msg, failover_time): rpc = crypto.get_rpc() split_message = msg.body.strip().split() user = models.User(msg.author.name) if user.is_registered(): if utils.check_amount_valid( split_message[1]) and split_message[4] != user.address: amount = float(split_message[1]) amount = round(amount - 0.5) user_balance = user.get_balance_confirmed() user_spendable_balance = crypto.get_user_spendable_balance( msg.author.name) 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] history.add_to_history(msg.author.name, user.username, receiver_address, amount, "withdraw") send = crypto.tip_user(user.address, receiver_address, amount, None, failover_time) if send: history.add_to_history(msg.author.name, user.username, receiver_address, amount, "withdraw", True, send) 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] == user.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 tip_user(rpc, msg): print('An user mention detected ') split_message = msg.body.strip().split() tip_index = split_message.index('+/u/sodogetiptest') if split_message[tip_index] == '+/u/sodogetiptest' and split_message[ tip_index + 2] == 'doge': amount = split_message[tip_index + 1] if utils.check_amount_valid(amount): parent_comment = msg.parent() if user_function.user_exist(msg.author.name): # check we have enough user_balance = crypto.get_user_balance(rpc, msg.author.name) if int(amount) >= user_balance: print( 'user %s not have enough to tip this amount (%s), balance = %s' % (msg.author.name, amount, user_balance)) msg.reply('+/u/%s your balance is too low for this tip ' % msg.author.name) else: # check user have address before tip if user_function.user_exist(parent_comment.author.name): if crypto.tip_user(rpc, msg.author.name, parent_comment.author.name, amount): user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip") print '%s tip %s to %s' % ( msg.author.name, str(amount), parent_comment.author.name) msg.reply('+/u/%s tip %s to %s' % (msg.author.name, str(amount), parent_comment.author.name)) else: user_function.save_unregistered_tip( msg.author.name, parent_comment.author.name, amount) user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip", False) print('user %s not registered' % parent_comment.author.name) msg.reply( '+/u/%s need %s before can be tipped (tip saved during 3 day)' % (parent_comment.author.name, linkRegister)) else: msg.reply('You need %s before' % linkRegister) else: print('You must use valid amount') msg.reply('You must use valid amount')
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 parse_message(self, message_to_parse, rpc=None): if rpc is None: # init rpc to validate address rpc = crypto.get_rpc() p = re.compile( '(\+?\/?u\/' + config.bot_name + ')\s?(@?[0-9a-zA-Z-_\/\+]+)?\s+(\d+|[0-9a-zA-Z,.]+)\s(doge)\s?(verify)?', re.IGNORECASE) m = p.search(message_to_parse.strip()) # Group 1 is +/u/sodogetiptest # Group 2 is either blank(tip to the commentor), an address, or a user self.receiver = m.group(2) # Group 3 is the tip amount in integers(ex. 100) or a word(ex.roll) self.amount = m.group(3).replace(',', '.') # Group 4 is doge self.currency = m.group(4) # Group 5 is either blank(no verify message) or verify(verify message) self.verify = True if (m.group(5) == "verify") else False if self.receiver is not None: # to support send tip to username if '+/u/' in self.receiver: self.receiver = User(self.receiver[4:]) elif '/u/' in self.receiver: self.receiver = User(self.receiver[3:]) elif 'u/' in self.receiver: self.receiver = User(self.receiver[2:]) elif '@' in self.receiver: self.receiver = User(self.receiver[1:]) # to support send tip to an address elif len(self.receiver) == 34 and rpc.validateaddress(self.receiver)['isvalid']: address = self.receiver bot_logger.logger.info("Send an tip to address") self.receiver = User("address-" + address) self.receiver.username = ("address-" + address) self.receiver.address = address # to support any type of randomXXX amount if 'random' in self.amount and utils.check_amount_valid(self.amount[6:]): self.amount = random.randint(1, int(self.amount[6:])) # here amount is numeric, make magic to support not whole tips if utils.check_amount_valid(self.amount): self.amount = round(float(self.amount) - 0.5) # if amount is all, get balance if self.amount == 'all': # get user balance self.amount = self.sender.get_balance() bot_logger.logger.debug("isinstance self.amount = %s" % str(isinstance(self.amount, str))) bot_logger.logger.debug("type self.amount = %s" % str(type(self.amount))) if type(self.amount) is unicode or type(self.amount) is str: bot_logger.logger.debug("self.amount is str") if self.amount == "roll": self.amount = random.randint(1, 6) elif self.amount == "flip": self.amount = random.randint(1, 2) elif self.amount in config.tip_keyword.keys(): self.amount = config.tip_keyword[self.amount] bot_logger.logger.debug("self.amount = %s" % str(self.amount)) # if tip is over 1000 doge set verify if float(self.amount) >= float(1000): self.verify = True
def tip_user(reddit, msg, tx_queue, failover_time): bot_logger.logger.info('An user mention detected ') bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value))) # create an Tip tip = models.Tip() # update sender tip.set_sender(msg.author.name) # check user who use command is registered if tip.sender.is_registered() is not True: bot_logger.logger.info('user %s not registered (sender) ' % msg.author.name) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name)) return False # parse message tip.parse_message(msg.body) # set reddit message id tip.message_fullname = msg.fullname # check amount of tip if not utils.check_amount_valid(tip.amount): # invalid amount bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(msg.author.name).message('invalid amount', lang.message_invalid_amount) return False if tip.currency is None: bot_logger.logger.info(lang.message_invalid_currency) reddit.redditor(msg.author.name).message('invalid currency', lang.message_invalid_currency) return False # update receiver tip.set_receiver(msg.parent().author.name) # check user not tip self if tip.sender.username == tip.receiver.username: reddit.redditor(tip.sender.username).message( 'cannot tip self', Template(lang.message_recipient_self).render( username=tip.sender.username)) return False # check we have enough user_spendable_balance = crypto.balance_user(msg, failover_time) bot_logger.logger.debug('user_spendable_balance = %s' % user_spendable_balance) # in failover we need to use only user_balance if tip.amount >= float(user_spendable_balance): user_pending_balance = tip.sender.get_balance_unconfirmed() # not enough for tip if tip.amount < float(user_pending_balance): reddit.redditor(tip.sender.username).message( 'pending tip', Template(lang.message_balance_pending_tip).render( username=tip.sender.username)) else: bot_logger.logger.info( 'user %s not have enough to tip this amount (%s), balance = %s' % (tip.sender.username, str( tip.amount), str(user_spendable_balance))) reddit.redditor(tip.sender.username).message( 'low balance', Template(lang.message_balance_low_tip).render( username=tip.sender.username)) else: # add tip to history of sender & receiver history.add_to_history_tip(tip.sender.username, "tip send", tip) history.add_to_history_tip(tip.receiver.username, "tip receive", tip) # check user who receive tip have an account if tip.receiver.is_registered(): tip.tx_id = crypto.tip_user(tip.sender.address, tip.receiver.address, tip.amount, tx_queue, failover_time) if tip.tx_id: tip.finish = True tip.status = 'ok' bot_logger.logger.info( '%s tip %s to %s' % (msg.author.name, str(tip.amount), tip.receiver.username)) # if user have 'verify' in this command he will have confirmation if tip.verify: msg.reply( Template(lang.message_tip).render( sender=msg.author.name, receiver=tip.receiver.username, amount=str(int(tip.amount)), value_usd=str(tip.get_value_usd()), txid=tip.tx_id)) else: bot_logger.logger.info('user %s not registered (receiver)' % tip.receiver.username) tip.status = "waiting registration of receiver" # save tip user_function.save_unregistered_tip(tip) # send message to sender of tip reddit.redditor(tip.sender.username).message( 'tipped user not registered', Template(lang.message_recipient_register).render( username=tip.receiver.username)) # send message to receiver reddit.redditor(tip.receiver.username).message( Template(lang.message_recipient_need_register_title).render( amount=str(tip.amount)), Template(lang.message_recipient_need_register_message).render( username=tip.receiver.username, sender=msg.author.name, amount=str(tip.amount), value_usd=str(tip.get_value_usd()))) # update tip status history.update_tip(tip.sender.username, tip) history.update_tip(tip.receiver.username, tip)
def tip_user(rpc, reddit, msg, tx_queue, failover_time): bot_logger.logger.info('An user mention detected ') bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value))) split_message = msg.body.lower().strip().split() tip_index = split_message.index(str('+/u/' + config.bot_name)) if split_message[tip_index] == str( '+/u/' + config.bot_name) and split_message[tip_index + 2] == 'doge': amount = float(split_message[tip_index + 1]) amount = round(amount - 0.5) if utils.check_amount_valid(amount): parent_comment = msg.parent() if user_function.user_exist(msg.author.name) and ( msg.author.name != parent_comment.author.name): # check we have enough user_balance = crypto.get_user_confirmed_balance( rpc, msg.author.name) user_pending_balance = crypto.get_user_unconfirmed_balance( rpc, msg.author.name) user_spendable_balance = crypto.balance_user( rpc, msg, failover_time) bot_logger.logger.debug('user_spendable_balance = %s' % user_spendable_balance) # in failover we need to use only user_balance if amount >= float(user_spendable_balance): # not enough for tip if amount < float(user_pending_balance): reddit.redditor(msg.author.name).message( 'pending tip', Template(lang.message_balance_pending_tip).render( username=msg.author.name)) else: bot_logger.logger.info( 'user %s not have enough to tip this amount (%s), balance = %s' % (msg.author.name, str(amount), str(user_balance))) reddit.redditor(msg.author.name).message( 'low balance', Template(lang.message_balance_low_tip).render( username=msg.author.name)) else: value_usd = utils.get_coin_value(amount) # check user have address before tip if user_function.user_exist(parent_comment.author.name): txid = crypto.tip_user(rpc, msg.author.name, parent_comment.author.name, amount, tx_queue, failover_time) if txid: user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip send", txid) user_function.add_to_history( parent_comment.author.name, msg.author.name, parent_comment.author.name, amount, "tip receive", txid) bot_logger.logger.info( '%s tip %s to %s' % (msg.author.name, str(amount), parent_comment.author.name)) # if user have 'verify' in this command he will have confirmation if split_message.count( 'verify') or int(amount) >= 1000: msg.reply( Template(lang.message_tip).render( sender=msg.author.name, receiver=parent_comment.author.name, amount=str(int(amount)), value_usd=str(value_usd), txid=txid)) else: user_function.save_unregistered_tip( msg.author.name, parent_comment.author.name, amount, msg.fullname) user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip send", False) user_function.add_to_history( parent_comment.author.name, msg.author.name, parent_comment.author.name, amount, "tip receive", False) bot_logger.logger.info('user %s not registered' % parent_comment.author.name) reddit.redditor(msg.author.name).message( 'tipped user not registered', Template(lang.message_recipient_register).render( username=parent_comment.author.name)) reddit.redditor(parent_comment.author.name).message( Template(lang.message_recipient_need_register_title ).render(amount=str(amount)), Template( lang.message_recipient_need_register_message). render(username=parent_comment.author.name, sender=msg.author.name, amount=str(amount), value_usd=str(value_usd))) elif user_function.user_exist( msg.author.name) and (msg.author.name == parent_comment.author.name): reddit.redditor(msg.author.name).message( 'cannot tip self', Template(lang.message_recipient_self).render( username=msg.author.name)) else: reddit.redditor(msg.author.name).message( 'tipped user not registered', Template(lang.message_need_register).render( username=msg.author.name)) else: bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(msg.author.name).message( 'invalid amount', lang.message_invalid_amount)