Author Topic: Easy-to-use Python RPC API for BitShares  (Read 2350 times)

0 Members and 1 Guest are viewing this topic.

Offline abit

  • Committee member
  • Hero Member
  • *
  • Posts: 4664
    • View Profile
    • Abit's Hive Blog
  • BitShares: abit
  • GitHub: abitmore
BitShares committee member: abit
BitShares witness: in.abit

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12922
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
Download (With some custom functions)
https://github.com/xeroc/pytshares/blob/master/btsrpcapi.py

Code:
Code: [Select]
#!/usr/bin/python
import requests
import json

class btsrpc(object) :
 def __init__(self, url, user, pwd) :
     self.auth    = (user,pwd)
     self.url     = url
     self.headers = {'content-type': 'application/json'}

 def rpcexec(self,payload) :
     try:
         response = requests.post(self.url, data=json.dumps(payload), headers=self.headers, auth=self.auth)
         return json.loads(response.text)
     except:

         raise Exception("Unkown error executing RPC call!")
 def __getattr__(self, name) :
     def method(*args):
         r = self.rpcexec({
             "method": name,
             "params": args,
             "jsonrpc": "2.0",
             "id": 0
         })
         if "error" in r:
             raise Exception(r["error"])
         return r
     return method

class btsrpcapi(btsrpc) :
 def __init__(self, url, user, pwd) :
     btsrpc.__init__(self, url, user, pwd)

### Room for Custom Functions: ###
 def getassetbalance(self,name,asset) :
     balance = self.wallet_account_balance(name)
     for b in balance[ "result" ][ 0 ][ 1 ]:
       if b[ 0 ] == asset : return float(b[ 1 ])
     return -1

 def setnetwork(self,d,m) :
     return self.network_set_advanced_node_parameters({"desired_number_of_connections":d, "maximum_number_of_connections":m})

 def orderhistory(self,a,b,l) :
     return self.blockchain_market_order_history(a,b,1,l)



Quick'n Dirty Howto:

Dump all private keys (owner keys only)
Code: [Select]
#!/usr/bin/python
from btsrpcapi import *
import config
if __name__ == "__main__":
 rpc = btsrpcapi(config.url, config.user, config.passwd)
 print rpc.info()
 print rpc.wallet_open(config.wallet)
 rpc.unlock(9999,config.unlock)
 r = (rpc.wallet_list_my_accounts())
 accounts = r["result"]
 print "---------------------------------------------------------------------------------"
 for account in accounts :
  print "%20s - %s - %s" % (account["name"], account["owner_key"], rpc.wallet_dump_private_key(account["owner_key"]))
 print "---------------------------------------------------------------------------------"
 print rpc.lock()

Switch block production over to another machine:
Code: [Select]
#!/usr/bin/python
import csv
import os
from btsrpcapi import *
import config

rpc   = btsrpcapi(config.backupurl, config.backupuser, config.backuppasswd)
rpc2  = btsrpcapi(config.mainurl, config.mainuser, config.mainpasswd)

def enable( ) :
    print "checking connectivity"
    rpc2.info()
    rpc.info()

    print "enabling backup block production"
    rpc2.wallet_open("delegate")
    rpc2.wallet_delegate_set_block_production("ALL","true")
    rpc2.unlock(999999, config.mainunlock)
    rpc2.setnetwork(25,30)

    print "disabling main block production"
    rpc.wallet_open("delegate")
    rpc.lock()
    rpc.wallet_delegate_set_block_production("ALL","false")

if __name__ == "__main__":
 enable(  )


Have fun!