Exemple #1
0
def balance_user(rpc, msg):
    if user_function.user_exist(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
        bot_logger.logger.info('user %s balance = %s' %
                               (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_balance + 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)))

        user_function.add_to_history(msg.author.name, "", "", balance,
                                     "balance")
    else:
        bot_logger.logger.info('user %s not registered ' % msg.author.name)
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Exemple #2
0
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))
Exemple #3
0
def store_user_buy(user, quantity, tx_id):
    db = TinyDB(config.DATA_PATH + 'reddit_gold.json')
    db.insert({
        "user_buyer":
        user.username,
        "quantity":
        quantity,
        "price":
        config.price_reddit_gold,
        "currency":
        'doge',
        "amount":
        config.price_reddit_gold * quantity,
        "usd_price":
        utils.get_coin_value(1, 8),
        "total_price":
        utils.get_coin_value(config.price_reddit_gold * quantity, 2),
        'tx_id':
        tx_id,
        'status':
        "buy",
        'time':
        datetime.datetime.now().isoformat(),
    })
    db.close()
Exemple #4
0
def register_user(msg):
    user = models.User(msg.author.name)
    if not user.is_registered():
        user.get_new_address()
        if user.address:
            content_reply = Template(lang.message_register_success + lang.message_footer).render(
                username=user.username,
                address=user.address)
            tittle_reply = 'you are registered'

            user.register()
            models.HistoryStorage.add_to_history(msg.author.name, "", "", "", "register")

            # create a backup of wallet
            crypto.backup_wallet()
        else:
            bot_logger.logger.warning('Error during register !')
    else:
        bot_logger.logger.info('%s are already registered ' % user.username)

        # pending_tips is balance of tip send to unregistered users
        pending_tips = user.get_balance_pending_tip()
        unconfirmed_balance = user.get_balance_unconfirmed()
        spendable_balance = user.get_balance()

        bot_logger.logger.info('user %s spendable_balance = %s' % (user.username, spendable_balance))

        unconfirmed_value_usd = utils.get_coin_value(unconfirmed_balance)
        spendable_value_usd = utils.get_coin_value(spendable_balance)
        pending_tips_value_usd = utils.get_coin_value(pending_tips)

        content_reply = Template(
            lang.message_already_registered + lang.message_account_details + lang.message_footer).render(
            username=msg.author.name,
            address=user.address,
            spendable_balance=str(spendable_balance),
            spendable_value_usd=str(spendable_value_usd),
            unconfirmed_balance=str(unconfirmed_balance),
            unconfirmed_value_usd=str(unconfirmed_value_usd),
            pending_tips=str(pending_tips),
            pending_tips_value_usd=str(pending_tips_value_usd)
        )
        tittle_reply = 'you are already registered'

    # send PM so just reply
    if type(msg) is Message:
        msg.reply(content_reply)

    # we have just comment so send info in PM
    if type(msg) is Comment:
        user.send_private_message(tittle_reply, content_reply)
Exemple #5
0
def register_user(msg, reddit):
    rpc = crypto.get_rpc()

    user = models.User(msg.author.name)
    if not user.is_registered():
        user.get_new_address()
        if user.address:
            content_reply = Template(lang.message_register_success +
                                     lang.message_footer).render(
                                         username=user.username,
                                         address=user.address)
            tittle_reply = 'you are registered'

            user_function.add_user(msg.author.name, user.address)

            history.add_to_history(msg.author.name, "", "", "", "register")

            # create a backup of wallet
            crypto.backup_wallet()
        else:
            bot_logger.logger.warning('Error during register !')
    else:
        bot_logger.logger.info('%s are already registered ' % msg.author.name)

        balance = user.get_balance_confirmed()
        pending_balance = user.get_balance_unconfirmed()
        spendable_balance = crypto.get_user_spendable_balance(
            msg.author.name) + balance
        pending_value_usd = utils.get_coin_value(pending_balance)
        spendable_value_usd = utils.get_coin_value(spendable_balance)
        content_reply = Template(
            lang.message_already_registered + lang.message_account_details +
            lang.message_footer).render(
                username=msg.author.name,
                address=user.address,
                pending_balance=str(pending_balance),
                pending_value_usd=str(pending_value_usd),
                spendable_balance=str(spendable_balance),
                spendable_value_usd=str(spendable_value_usd))
        tittle_reply = 'you are already registered'

    # send PM so just reply
    if type(msg) is Message:
        msg.reply(content_reply)

    # we have just comment so send info in PM
    if type(msg) is Comment:
        reddit.redditor(msg.author.name).message(tittle_reply, content_reply)
Exemple #6
0
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))
Exemple #7
0
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))
Exemple #8
0
def balance_user(rpc, msg):
    if user_function.user_exist(msg.author.name):
        balance = crypto.get_user_balance(rpc, msg.author.name)
        print('user %s balance = %s' % (msg.author.name, balance))
        value_usd = utils.get_coin_value(balance)
        msg.reply('Your balance : ' + str(balance) + ' ( ' + str(value_usd) +
                  '$ ) ')
    else:
        print('user %s not registered ' % (msg.author.name))
        msg.reply('You need %s before' % linkRegister)
Exemple #9
0
def info_user(msg):
    rpc = crypto.get_rpc()

    user = models.User(msg.author.name)
    if user.is_registered():
        balance = user.get_balance_confirmed()

        # pending_tips is balance of tip send to unregistered users
        pending_tips = user.get_balance_unregistered_tip()

        pending_balance = user.get_balance_unconfirmed()
        spendable_balance = crypto.get_user_spendable_balance(
            msg.author.name) + balance

        bot_logger.logger.info('user %s balance = %s' %
                               (msg.author.name, balance))
        bot_logger.logger.info('user %s spendable_balance = %s' %
                               (msg.author.name, spendable_balance))

        pending_value_usd = utils.get_coin_value(pending_balance)
        spendable_value_usd = utils.get_coin_value(spendable_balance)
        pending_tips_value_usd = utils.get_coin_value(pending_tips)

        msg.reply(
            Template(lang.message_account_details +
                     lang.message_footer).render(
                         username=msg.author.name,
                         spendable_balance=str(spendable_balance),
                         spendable_value_usd=str(spendable_value_usd),
                         pending_balance=str(pending_balance),
                         pending_value_usd=str(pending_value_usd),
                         pending_tips=str(pending_tips),
                         pending_tips_value_usd=str(pending_tips_value_usd),
                         address=user.address))

        history.add_to_history(msg.author.name, "", "", spendable_balance,
                               "info")
    else:
        bot_logger.logger.info('user %s not registered (command : info) ' %
                               msg.author.name)
        msg.reply(
            Template(lang.message_need_register +
                     lang.message_footer).render(username=msg.author.name))
Exemple #10
0
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))
Exemple #11
0
def replay_remove_pending_tip(rpc, reddit, tx_queue, failover_time):
    # check if it's not too old & replay tipping
    limit_date = datetime.datetime.now() - datetime.timedelta(days=3)

    # check if user have pending tips
    list_tips = user_function.get_unregistered_tip()

    if list_tips:
        for tip in list_tips:
            bot_logger.logger.info("replay tipping check for %s" %
                                   str(tip['id']))
            if (datetime.datetime.strptime(tip['time'], '%Y-%m-%dT%H:%M:%S.%f')
                    > limit_date):
                if (user_function.user_exist(tip['receiver'])):
                    bot_logger.logger.info(
                        "replay tipping %s - %s send %s to %s  " %
                        (str(tip['id']), tip['sender'], tip['amount'],
                         tip['receiver']))
                    txid = crypto.tip_user(rpc, tip['sender'], tip['receiver'],
                                           tip['amount'], tx_queue,
                                           failover_time)
                    user_function.remove_pending_tip(tip['id'])

                    value_usd = utils.get_coin_value(tip['amount'])

                    if 'message_fullname' in tip.keys():
                        msg_id = re.sub(r't\d+_(?P<id>\w+)', r'\g<id>',
                                        tip['message_fullname'])
                        msg = Comment(reddit, msg_id)
                        msg.reply(
                            Template(lang.message_tip).render(
                                sender=tip['sender'],
                                receiver=tip['receiver'],
                                amount=str(tip['amount']),
                                value_usd=str(value_usd),
                                txid=txid))

                else:
                    bot_logger.logger.info(
                        "replay check for %s - user %s not registered " %
                        (str(tip['id']), tip['receiver']))
            else:
                bot_logger.logger.info(
                    "delete old tipping - %s send %s for %s  " %
                    (tip['sender'], tip['amount'], tip['receiver']))
                user_function.remove_pending_tip(tip['id'])
    else:
        bot_logger.logger.info("no pending tipping")
Exemple #12
0
 def get_value_usd(self):
     return utils.get_coin_value(self.amount, self.currency)
Exemple #13
0
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)
Exemple #14
0
 def get_value_usd(self):
     return utils.get_coin_value(self.amount)