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

0 Members and 1 Guest are viewing this topic.

Offline xeroc

  • Board Moderator
  • Hero Member
  • *****
  • Posts: 12552
  • ChainSquad GmbH
    • View Profile
    • ChainSquad GmbH
  • BitShares: xeroc
  • GitHub: xeroc
Easy-to-use Python RPC API for BitShares
« on: January 23, 2015, 02:47:03 pm »
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!
Give BitShares a try! Use the http://testnet.bitshares.eu provided by http://bitshares.eu powered by ChainSquad GmbH

Offline abit

  • Committee member
  • Hero Member
  • *
  • Posts: 3141
    • View Profile
    • Steemit Blog
  • BitShares: abit
  • GitHub: abitmore
Re: Easy-to-use Python RPC API for BitShares
« Reply #1 on: January 23, 2015, 02:56:25 pm »
Interesting!  +5%
BTS account: abit
BTS committee member: abit
BTS witness: in.abit