Esempio n. 1
0
def liquidity_info(app, log, totals):
    """
    Calculate the current amount of liquidity in ranks 1 and 2 and submit them to Nu
    :param totals:
    :param log:
    :param rpc:
    :param app:
    :return:
    """
    rpc = get_rpc(app, log)
    if rpc is None:
        return
    for exchange in app.config['exchanges']:
        for unit in app.config['{}.units'.format(exchange)]:
            for rank in app.config['{}.{}.bid.ranks'.format(exchange, unit)]:
                identifier = "1:{}:{}:{}.{}".format(
                    'NBT{}'.format(unit.upper()),
                    exchange,
                    app.config['pool.name'],
                    rank
                )
                if rpc is not None:
                    try:
                        # get a connection to the nud rpc interface

                        rpc.liquidityinfo(
                            'B',
                            totals[exchange][unit]['bid'][rank],
                            totals[exchange][unit]['ask'][rank],
                            app.config['pool.grant_address'],
                            identifier
                        )
                    except JSONRPCException as e:
                        log.error('Sending liquidity info failed: {}'.format(e.message))
                    log.info(
                        'sent liquidity info for %s: ask=%s, bid=%s',
                        identifier,
                        totals[exchange][unit]['ask'][rank],
                        totals[exchange][unit]['bid'][rank]
                    )
Esempio n. 2
0
def pay(app, log):
    """
    Pay all users who have a balance greater than the minimum payout
    :param log:
    :param rpc:
    :param app:
    :return:
    """
    log.info('payout started')
    # get the credit details from the database
    conn = database.get_db(app)
    db = conn.cursor()
    db.execute("SELECT c.id,c.key,c.reward,u.address FROM credits AS c INNER JOIN "
               "users AS u on u.key=c.key WHERE c.paid=0")
    rewards = db.fetchall()
    # Calculate the total credit for each unique address
    user_rewards = {}
    for reward in rewards:
        if reward[3] not in user_rewards:
            user_rewards[reward[3]] = 0.00
        user_rewards[reward[3]] += float(reward[2])
    # remove those which don't meet the minimum payout threshold
    # and round to 6dp
    user_payouts = user_rewards.copy()
    for address in user_rewards:
        if user_rewards[address] < float(app.config['pool.minimum_payout']):
            del(user_payouts[address])
            continue
        user_payouts[address] = round(float(user_payouts[address]), 6)
    if not user_payouts:
        log.info('no-one to payout to: %s', user_rewards)
        timer_time = 86400.0
    else:
        # SendMany from nud. Report any error to log output
        try:
            # get an rpc connection
            rpc = get_rpc(app, log)
            rpc.sendmany("", user_payouts)
            log.info('payout successful: \'%s\'', json.dumps(user_payouts))
            # mark credits to paid addresses as paid
            for reward in rewards:
                if reward[3] in user_payouts:
                    db.execute('UPDATE credits SET paid=1 WHERE id=%s', (reward[0],))
            # set the timer for the next payout
            timer_time = 86400.0
        except JSONRPCException as e:
            log.error('Payout failed - %s: \'%s\'', e.message, json.dumps(user_payouts))
            timer_time = 120.0
        except (socket.error, CannotSendRequest, ValueError):
            log.error('Payout failed - no connection with nud: \'%s\'', json.dumps(
                    user_payouts))
            timer_time = 120.0
    # reset timer
    payout_timer = Timer(timer_time, pay,
                         kwargs={'app': app, 'log': log})
    payout_timer.name = 'payout_timer'
    payout_timer.daemon = True
    payout_timer.start()
    # update the next payout time
    db.execute('UPDATE info SET value=%s WHERE key=%s', (int(time.time() + timer_time),
                                                         'next_payout_time'))
    conn.commit()
    conn.close()