bitsharesapi.websocket module

class bitsharesapi.websocket.BitSharesWebsocket(urls, user='', password='', *args, accounts=None, markets=None, objects=None, on_tx=None, on_object=None, on_block=None, on_account=None, on_market=None, keep_alive=25, num_retries=-1, **kwargs)[source]

Bases: events.events.Events

Create a websocket connection and request push notifications.

Parameters:
  • urls (str) – Either a single Websocket URL, or a list of URLs
  • user (str) – Username for Authentication
  • password (str) – Password for Authentication
  • accounts (list) – list of account names or ids to get push notifications for
  • markets (list) – list of asset_ids, e.g. [['1.3.0', '1.3.121']]
  • objects (list) – list of objects id’s you’d like to be notified when changing
  • keep_alive (int) – seconds between a ping to the backend (defaults to 25seconds)

After instanciating this class, you can add event slots for:

  • on_tx
  • on_object
  • on_block
  • on_account
  • on_market

which will be called accordingly with the notification message received from the BitShares node:

ws = BitSharesWebsocket(
    "wss://node.testnet.bitshares.eu",
    objects=["2.0.x", "2.1.x", "1.3.x"]
)
ws.on_object += print
ws.run_forever()

Notices:

  • on_account:

    {'id': '2.6.29',
     'lifetime_fees_paid': '44257768405',
     'most_recent_op': '2.9.1195638',
     'owner': '1.2.29',
     'pending_fees': 0,
     'pending_vested_fees': 100,
     'total_core_in_orders': '6788960277634',
     'total_ops': 505865}
    
  • on_block:

    '0062f19df70ecf3a478a84b4607d9ad8b3e3b607'
    
  • on_tx:

    {'expiration': '2017-02-23T09:33:22',
     'extensions': [],
     'operations': [[0,
                     {'amount': {'amount': 100000, 'asset_id': '1.3.0'},
                      'extensions': [],
                      'fee': {'amount': 100, 'asset_id': '1.3.0'},
                      'from': '1.2.29',
                      'to': '1.2.17'}]],
     'ref_block_num': 62001,
     'ref_block_prefix': 390951726,
     'signatures': ['20784246dc1064ed5f87dbbb9aaff3fcce052135269a8653fb500da46e7068bec56e85ea997b8d250a9cc926777c700eed41e34ba1cabe65940965ebe133ff9098']}
    
  • on_market:

    ['1.7.68612']
    
cancel_subscriptions()[source]
close(*args, **kwargs)[source]

Closes the websocket connection and waits for the ping thread to close.

get_request_id()[source]
on_close(*args, **kwargs)[source]

Called when websocket connection is closed.

on_error(error, *args, **kwargs)[source]

Called on websocket errors.

on_message(reply, *args, **kwargs)[source]

This method is called by the websocket connection on every message that is received.

If we receive a notice, we hand over post-processing and signalling of events to process_notice.

on_open(*args, **kwargs)[source]

This method will be called once the websocket connection is established. It will.

  • login,
  • register to the database api, and
  • subscribe to the objects defined if there is a callback/slot available for callbacks
process_notice(notice)[source]

This method is called on notices that need processing.

Here, we call on_object and on_account slots.

reset_subscriptions(accounts=None, markets=None, objects=None)[source]
rpcexec(payload)[source]

Execute a call by sending the payload.

Parameters:

payload (dict) – Payload data

Raises:
  • ValueError – if the server does not respond in proper JSON format
  • RPCError – if the server returns an error
run_forever(*args, **kwargs)[source]

This method is used to run the websocket app continuously.

It will execute callbacks as defined and try to stay connected with the provided APIs