Example #1
0
    def process_tip(self, amount, comment, receiving_user):
        user_table = self.db['user']
        comment_table = self.db['comments']

        # See if we have an author xrb address and a to xrb address, if not invite to register
        self.log.info("Looking for sender " + "'" + comment.author.name + "'" +
                      " in db")

        sender_user_data = util.find_user(comment.author.name, self.log,
                                          self.db)

        if sender_user_data is not None:
            self.log.info('Sender in db')
            # Author registered
            sender_user_address = sender_user_data['xrb_address']

            reply_text = None

            user_data = util.find_user(receiving_user, self.log, self.db)
            if user_data is not None:
                receiving_address = user_data['xrb_address']
            else:
                self.log.info("Receiving User " + "'" + receiving_user + "'" +
                              " Not in DB - registering")
                # Generate address
                data = {'action': 'account_create', 'wallet': self.wallet_id}
                post_body = self.rest_wallet.post_to_wallet(data, self.log)
                self.log.info("Receiving User new account: " +
                              str(post_body['account']))

                # Add to database
                record = dict(user_id=receiving_user,
                              xrb_address=post_body['account'])
                self.log.info("Inserting into db: " + str(record))
                user_table.insert(record)
                receiving_address = post_body['account']

                reply_text = str(receiving_user) \
                             + ' isn\'t registered, so I made an account for them. ' \
                             + 'They can access it by messaging the bot.'

            self.send_tip(comment, amount, sender_user_address,
                          receiving_address, receiving_user, reply_text)

        else:
            self.log.info('Sender NOT in db')
            reply_text = 'Hi /u/' + str(comment.author.name) + ', please register with the bot by sending it a' \
                         + ' private message with the text "register" in the body of the message.  \n\nGo to the [wiki]' + \
                         "(https://www.reddit.com/r/RaiBlocks_tipbot/wiki/start) for more info"

            self.comment_reply(comment, reply_text)

        # Add to db
        record = dict(comment_id=comment.fullname,
                      to=receiving_user,
                      amount=amount,
                      author=comment.author.name)
        self.log.info("Inserting into db: " + str(record))
        comment_table.insert(record)
        self.log.info('DB updated')
Example #2
0
    def transfer_funds(self, amount, item, send_address):
        try:
            user_data = util.find_user(item.author.name, self.log, self.db)
            user_address = user_data['ban_address']
            data = {'action': 'account_balance', 'account': user_address}
            parsed_json = self.rest_wallet.post_to_wallet(data, self.log)

            raw_send = str(util.banano_to_raw(float(amount)))
            # check amount left
            if int(raw_send) <= int(parsed_json['balance']):
                data = {'action': 'send', 'wallet': self.wallet_id, 'source': user_address, 'destination': send_address,
                        'amount': int(raw_send)}
                parsed_json = self.rest_wallet.post_to_wallet(data, self.log)
                reply_message = 'Successfully sent %s BANANO to %s\n\nYou can view this transaction on [BananoVault](https://vault.banano.cc/transaction/%s)' % (
                    amount, send_address, str(parsed_json['block']))
                item.reply(reply_message)
            else:
                reply_message = 'Insufficient Banano left in your account to transfer\n\n'
                item.reply(reply_message)
        except:
            reply_message = 'Invalid amount : %s' % amount
            item.reply(reply_message)
            self.log.error("Unexpected error: " + str(sys.exc_info()[0]))
            tb = traceback.format_exc()
            self.log.error(tb)
Example #3
0
 def get_balance(self, item):
     user_data = util.find_user(item.author.name, self.log, self.db)
     user_address = user_data['ban_address']
     data = {'action': 'account_balance', 'account': user_address}
     parsed_json = self.rest_wallet.post_to_wallet(data, self.log)
     ban_balance = util.raw_to_banano(int(parsed_json['balance']))
     self.log.info(str(ban_balance) + ' xrb balance')  
     #rate = util.get_price()
     #if rate is not None:
     #    usd = float(xrb_balance) * rate
     #    reply_message = 'Your balance is :\n\n %s BANANO' % xrb_balance
     #else:
     reply_message = 'Your balance is :\n\n %s BANANO' % ban_balance
     item.reply(reply_message)
Example #4
0
    def get_balance(self, item):
        user_data = util.find_user(item.author.name, self.log, self.db)
        user_address = user_data['ban_address']
        data = {'action': 'account_balance', 'account': user_address}
        parsed_json = self.rest_wallet.post_to_wallet(data, self.log)

        data = {
            'action': 'banoshi_from_raw',
            'amount': int(parsed_json['balance'])
        }
        rai_balance = self.rest_wallet.post_to_wallet(data, self.log)
        self.log.info(rai_balance['amount'])
        xrb_balance = format((float(rai_balance['amount']) / 100.0), '.2f')
        rate = util.get_price()
        if rate is not None:
            usd = float(xrb_balance) * rate
            reply_message = 'Your balance is :\n\n %s BANANO' % xrb_balance
        else:
            reply_message = 'Your balance is :\n\n %s BANANO' % xrb_balance
        item.reply(reply_message)
Example #5
0
    def get_balance(self, item):
        user_data = util.find_user(item.author.name, self.log, self.db)
        user_address = user_data['xrb_address']
        data = {'action': 'account_balance', 'account': user_address}
        parsed_json = self.rest_wallet.post_to_wallet(data, self.log)

        data = {
            'action': 'rai_from_raw',
            'amount': int(parsed_json['balance'])
        }
        rai_balance = self.rest_wallet.post_to_wallet(data, self.log)
        self.log.info(rai_balance['amount'])
        xrb_balance = format((float(rai_balance['amount']) / 1000000.0), '.6f')
        rate = util.get_price()
        if rate is not None:
            usd = float(xrb_balance) * rate
            reply_message = 'Your balance is :\n\n %s NANO or $%s USD \n\nUSD conversion rate of $%s' % \
                            (xrb_balance, str(format(float(usd), '.3f')), str(format(float(rate), '.3f')))
        else:
            reply_message = 'Your balance is :\n\n %s NANO' % xrb_balance
        item.reply(reply_message)
Example #6
0
    def transfer_funds(self, amount, item, send_address):
        try:
            user_data = util.find_user(item.author.name, self.log, self.db)
            user_address = user_data['xrb_address']
            data = {'action': 'account_balance', 'account': user_address}
            parsed_json = self.rest_wallet.post_to_wallet(data, self.log)
            data = {
                'action': 'rai_from_raw',
                'amount': int(parsed_json['balance'])
            }
            rai_balance = self.rest_wallet.post_to_wallet(data, self.log)

            rai_send = float(amount) * 1000000  # float of total send
            raw_send = str(int(rai_send)) + '000000000000000000000000'
            # check amount left
            if int(rai_send) <= int(rai_balance['amount']):
                data = {
                    'action': 'send',
                    'wallet': self.wallet_id,
                    'source': user_address,
                    'destination': send_address,
                    'amount': int(raw_send)
                }
                parsed_json = self.rest_wallet.post_to_wallet(data, self.log)
                reply_message = 'Sent %s to %s\n\n[Block Link](https://www.nanode.co/block/%s)' % (
                    amount, send_address, str(parsed_json['block']))
                item.reply(reply_message)
            else:
                reply_message = 'Not enough in your account to transfer\n\n'
                item.reply(reply_message)
        except:
            reply_message = 'Invalid amount : %s' % amount
            item.reply(reply_message)
            self.log.error("Unexpected error: " + str(sys.exc_info()[0]))
            tb = traceback.format_exc()
            self.log.error(tb)
Example #7
0
    def parse_item(self, item):
        self.log.info("\n\n")
        self.log.info("New Inbox Received")
        message_table = self.db['message']

        if message_table.find_one(message_id=item.name):
            self.log.info('Already in db, ignore')
        else:
            author_obj = item.author
            if author_obj is not None:
                author_name = author_obj.name
                if author_name is not None:
                    author = author_name.lower()
                    if author != "reddit" and author != "xrb4u" and author != "raiblocks_tipbot" and author != "giftxrb" \
                            and author != "giftnano" and author != "nano_tipbot" and author != "nano4u" and author != "automoderator":
                        user_table = self.db['user']

                        self.log.info("Item is as follows:")
                        self.log.info((vars(item)))

                        self.log.info("Attribute - Item was comment: " +
                                      str(item.was_comment))
                        if item.was_comment:
                            self.log.info("Comment subject: " +
                                          str(item.subject))
                            if item.subject == 'username mention':
                                self.process_mention(item)
                        else:
                            user_data = util.find_user(item.author.name,
                                                       self.log, self.db)
                            if user_data is not None:
                                self.log.info('Found Author ' +
                                              str(item.author.name))
                                commands = item.body.split(" ")
                                self.log.info(item.body)
                                if 'help' in item.body.lower():
                                    reply_message = 'Help\n\n Reply with the command in the body of text:\n\n  balance - get' \
                                                    + ' your balance\n\n  send <amount> <address> - send NANO to an external ' \
                                                      'address\n\naddress - get your deposit address\n\nMore info: ' \
                                                    + 'https://np.reddit.com/r/NANO_tipbot/wiki/start'
                                    item.reply(reply_message)

                                elif 'address' in item.body.lower():
                                    self.log.info(user_data['xrb_address'])
                                    reply_message = 'Your deposit address is :\n\n%s' % user_data[
                                        'xrb_address']
                                    item.reply(reply_message)

                                elif 'balance' in item.body.lower():
                                    self.log.info('Getting balance')
                                    self.get_balance(item)

                                elif 'send' in item.body.lower():
                                    self.log.info('Sending NANO')
                                    if len(commands) > 2:
                                        self.prepare_send(commands, item)
                                    else:
                                        reply_message = 'Sorry I could not parse your request.\n\nWhen making requests only put' + \
                                                        ' one command in the message body with no other text\n\nTry the "help"' + \
                                                        ' command\n\nMore info: ' \
                                                        + 'https://np.reddit.com/r/NANO_tipbot/wiki/start'
                                        item.reply(reply_message)

                                elif 'register' in item.body.lower():
                                    self.log.info("Already Registered")
                                    reply_message = 'Your account is already registered\n\nTry the "help" command\n\nMore info: ' \
                                                    + 'https://np.reddit.com/r/NANO_tipbot/wiki/start'
                                    item.reply(reply_message)

                                else:
                                    self.log.info("Bad message")
                                    reply_message = 'Sorry I could not parse your request.\n\nWhen making requests only put' + \
                                                    ' one command in the message body with no other text\n\nTry the "help"' + \
                                                    ' command\n\nMore info: ' \
                                                    + 'https://np.reddit.com/r/NANO_tipbot/wiki/start'
                                    item.reply(reply_message)
                            else:
                                self.log.info(
                                    str(item.author.name) + ' Not in DB')
                                if 'register' in item.body.lower():
                                    self.log.info('Registering account')
                                    self.register_account(item, user_table)

                                else:
                                    self.log.info("Could not parse message")
                                    reply_message = 'Your account is not registered and I could not parse your command\n\n' + \
                                                    ' Reply with "register" in the body of a private message to begin\n\n'
                                    item.reply(reply_message)
            passing = False
            author_obj = item.author
            if author_obj is not None:
                author_name = author_obj.name
                if author_name is not None:
                    passing = True

            if passing:
                record = dict(user_id=item.author.name, message_id=item.name)
            else:
                record = dict(user_id=None, message_id=item.name)

            # Add message to database
            self.log.info("Inserting into db: " + str(record))
            message_table.insert(record)