Author Topic: Account Balances in Human Readable Format - Python  (Read 1146 times)

0 Members and 1 Guest are viewing this topic.

Offline litepresence

get named balances returns an asset id without its human readable name....
and an amount - without the location of its decimal point!

this is kind of thing makes neckbeard itchy

Code: [Select]
["database","get_named_account_balances",["abc123", [] ]]
-----------------------------------------------------------
[{'asset_id': '1.3.0', 'amount': 12019967}, {'asset_id': '1.3.1382', 'amount': 100}, {'asset_id': '1.3.1578', 'amount': 100000000}, {'asset_id': '1.3.2419', 'amount': 500000}, {'asset_id': '1.3.2841', 'amount': 12000}, {'asset_id': '1.3.2931', 'amount': 10000}, {'asset_id': '1.3.3248', 'amount': 2580000}, {'asset_id': '1.3.3261', 'amount': 1000}, {'asset_id': '1.3.3279', 'amount': 10000}, {'asset_id': '1.3.3389', 'amount': 1000000000}, {'asset_id': '1.3.3431', 'amount': 1000}, {'asset_id': '1.3.3530', 'amount': 100000000}, {'asset_id': '1.3.3540', 'amount': 100000000}, {'asset_id': '1.3.3830', 'amount': '25000000000'}]

we can fix this... so get asset details

which is another api call and gives us "precision"; location of decimal, and the asset "symbol"; ie OPEN.BTC or BTS


but then we have to merge those dictionaries and they do not contain a common key; one has "asset_id" the other "id"

grrr

every call for what the asset is called and where its decimal point goes also contains bit of spam for every shitcoin donated to your account, on every bot tick, which brings in a huge page of asset banner ads like this silliness:


Code: [Select]
'{"main":"WARNING, DANGER, RED-ALERT, PANIC! PANIC! PANIC!\\n\\nYOU HAVE BEEN INFECTED,\\n\\nZOMBIES HAVE BEEN RELEASED! \\n\\nZombies are going to appear from nowhere & infest your portfolio,\\nSome zombies will come from your friends and family,\\nAND YES there are even some sickos out there that will want to buy Zombies too :s\\n\\n\\nThese VILE undead human corpses have risen, The Mission, BURN Zombies :)\\nHelp FILL THE POOL, Zombies are BURNED when there sent to the pool.\\n\\n\\nDisclaimer: NO REAL ZOMBIES WILL BE HURT IN THE CREATION OF THIS TOKEN ;)","short_name":"Zombies",

be thankful you're not on data plan!

nonetheless, hocus pocus account balances:

Code: [Select]
>>>
balances
{'TURION': 0.1, 'FREECOIN': 10000.0, 'SOLOMON': 0.1, 'BIRMINGHAMGOLD': 100.0, 'ZOMBIES': 12.0, 'DECENTRALIZED': 10.0, 'ANONYMOUS': 1.0, 'BTS': 120.19967, 'BTSJON': 1000.0, 'BADCOIN': 10000.0, 'HERTZ': 0.01, 'URTHONA': 50.0, 'SEED': 258.0, 'UNIVERSAL': 25000.0}


Code: [Select]
'litepresence 2018'

import websocket  #pip install websocket-client
websocket.enableTrace(False)
from ast import literal_eval as literal

def database_call(node, call):

    while 1:
        try:
            call = call.replace("'",'"') # never use single quotes
            ws = websocket.create_connection(node)
            print('')
            print((call.split(',"params":')[1]).rstrip('}'))
            print('-----------------------------------------------------------')
            ws.send(call)
            # 'result' key of literally evaluated
            # string representation of dictionary from websocket
            ret = literal(ws.recv())['result']
            print (ret)
            ws.close()
            return ret
        except Exception as e:
            print (e.args)
            pass

def account_balances(node, account_name):

    Z = '{"id":1,"method":"call","params":["database",'
    # make call for raw account balances as returned by api
    get_named_account_balances = Z + '"get_named_account_balances",["%s", [] ]]}' % (account_name)
    raw_balances = database_call(node, get_named_account_balances)
    # make list of asset_id's in raw account balances
    asset_ids = []
    for i in range(len(raw_balances)):
        asset_ids.append(raw_balances[i]['asset_id'])
    # make a second api request for additional data about each asset
    get_assets = Z + '"get_assets",[%s]]}' % asset_ids
    raw_assets = database_call(node, get_assets)
    # create a common key "asset_id" for both list of dicts
    # also extract the symbol and precision
    id_sym_prec = []
    for i in range(len(raw_assets)):
        id_sym_prec.append({'asset_id':raw_assets[i]['id'],
                            'symbol':raw_assets[i]['symbol'],
                            'precision':raw_assets[i]['precision'], })
    # merge the two list of dicts with common key "asset_id"
    data = {}
    lists = [raw_balances, id_sym_prec]
    for each_list in lists:
       for each_dict in each_list:
           data.setdefault(each_dict['asset_id'], {}).update(each_dict)
    # convert back to list
    data = list(data.values())
    # create a new dictionary containing only the symbol and quantity
    ret = {}
    for i in range(len(data)):
        qty = float(data[i]['amount'])/10**float(data[i]['precision'])
        ret[data[i]['symbol']] = qty
    return raw_balances, ret

#node = 'wss://bts-seoul.clockwork.gr' # websocket address
node = 'wss://api.bts.mobi/wss'
account_name = 'abc123' # string
raw_balances, balances = account_balances(node, account_name)


print('')
print('balances')
print(balances)

I'll also host this on my github

https://github.com/litepresence/extinction-event
https://github.com/litepresence/extinction-event/blob/master/tools/bitshares-account_balances.py
« Last Edit: April 27, 2018, 08:04:11 pm by litepresence »