Beispiel #1
0
def balances_bit_shares(config, secrets_json, source, scopeTickers):
    printSource = '' # this is cleaner than passing printSource as a param.
    if not config.VERBOSE: printSource = source+"\n  " # VERBOSE already printed it.

    from bitshares import BitShares
    amounts = BitShares(config, secrets_json['account'])
    for key, value in amounts.items():
        if not scopeTickers or key in scopeTickers:
            printBalance(printSource, key, value)
            printSource = '  '

    try: # bitshares.account will work under Python3, but throw exception if Python2
        from bitshares.account import Account
        account = Account(secrets_json['account'])
        print (account.json())
        # We add the balances and open-orders amounts, since we still own unfilled open-orders
        amounts = { }
        for balance in account.balances:
            ticker = balance.symbol.replace('BRIDGE.','')
            if ticker not in amounts: amounts[ticker] = 0
            amounts[ticker] += balance.amount
        for openorder in account.openorders:
            order = openorder['base']
            ticker = order.symbol.replace('BRIDGE.','')
            if ticker not in amounts: amounts[ticker] = 0
            amounts[ticker] += order.amount
        for key, value in amounts.items():
            if not scopeTickers or key in scopeTickers:
                printBalance(printSource, key, value)
                printSource = '  '
        return ''
    except KeyboardInterrupt as ex:
        raise KeyboardInterrupt()
    except:
        ex = sys.exc_info()[0]
        if config.VERBOSE: print( "Exception in balances.balances_bit_shares(): "+str(ex), file=sys.stderr )

    proc = subprocess.Popen(['/opt/mining/mining/balances/bit-shares.py', source], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    out, err = proc.communicate(None)
    if err:
        print(err,file=sys.stderr)
        return
    out = out.decode('utf-8')

    lines = out.splitlines()
    # "[0.00001017 BRIDGE.BTC, 162.1362 BRIDGE.RVN]"
    line = lines[0].replace('[','').replace(']','')
    vals = line.split(", ")
    for val in vals:
        val_coin = val.split(' ')
        value = val_coin[0]
        ticker = val_coin[1].replace('BRIDGE.','')
        if not scopeTickers or ticker in scopeTickers:
            #print('  '+ticker + " " + value)
            printBalance(printSource, ticker, float(value.replace(',','')))
            printSource = '  '