github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/cmd/lit-qt/rpcui.py (about)

     1  #!/usr/bin/env python
     2  
     3  from PyQt4 import QtCore, QtGui
     4  
     5  #Extend the library search path to our `qt_files` directory
     6  import sys
     7  sys.path.append("qt_files") 
     8  
     9  import socket
    10  import json
    11  
    12  #ui file import 
    13  import rpcui_ui
    14  
    15  #Handles rpc communications and conjugate response handler functions
    16  class rpcCom():
    17      def __init__(self, addr, port):
    18  
    19          #Open up the socket connection
    20          self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    21          self.conn.connect((addr, port))
    22  
    23      def getBal(self):
    24          rpcCmd = {
    25              "method": "LitRPC.Bal",
    26              "params": [{
    27              }]
    28          }
    29          #TODO: What is the purpose of this `id`
    30          rpcCmd.update({"jsonrpc": "2.0", "id": "93"})
    31  
    32          #print json.dumps(rpcCmd)
    33          self.conn.sendall(json.dumps(rpcCmd))
    34  
    35          r = json.loads(self.conn.recv(8000000))
    36          #print r
    37  
    38          return r["result"]["TxoTotal"]
    39  
    40      def getAdr(self):
    41          rpcCmd = {
    42  		   "method": "LitRPC.Address",
    43  		   "params": [{"NumToMake": 0}]
    44  	}
    45  
    46  	rpcCmd.update({"jsonrpc": "2.0", "id": "94"})
    47  
    48  	#print json.dumps(rpcCmd)
    49  	self.conn.sendall(json.dumps(rpcCmd))
    50  
    51  	r = json.loads(self.conn.recv(8000000))
    52  	#print r
    53  
    54  	return r["result"]["Addresses"][-1]
    55  
    56      def prSend(self, adr, amt):
    57  	rpcCmd = {
    58  		   "method": "LitRPC.Send",
    59  		   "params": [{"DestAddrs": [adr], "Amts": [amt]}]
    60  	}
    61  
    62  	rpcCmd.update({"jsonrpc": "2.0", "id": "95"})
    63  
    64  	#print json.dumps(rpcCmd)
    65  	self.conn.sendall(json.dumps(rpcCmd))
    66  
    67  	r = json.loads(self.conn.recv(8000000))
    68  	#print r
    69  
    70  	if r["error"] != None:
    71              raise RuntimeError(r["error"])
    72  
    73  	return "Sent. TXID: " + r["result"]["Txids"][0]
    74  
    75  class mainWindow(QtGui.QMainWindow, rpcui_ui.Ui_MainWindow):
    76      def __init__(self, parent):
    77          #Set up calls to get QT working
    78          QtGui.QMainWindow.__init__(self, parent)
    79          self.setupUi(self)
    80  
    81          #There is no need for a hint button
    82          self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint)
    83  
    84          #Set up the RPC communication object
    85          self.rpcCom = rpcCom("127.0.0.1", 9750)
    86  
    87          #Setup the connections to their triggers
    88          self.setup_connections()
    89  
    90      #Sets the text value for the balance label. Make this its own function to 
    91      # be used as a callback for the "Refresh" button
    92      def set_bal_label(self):
    93          bal = self.rpcCom.getBal()
    94          self.bal_label.setText(str(bal))
    95  
    96      #The trigger for the send button being clicked
    97      def send_button_clicked(self):
    98          #TODO: Implement address validity verification
    99          to_addr = str(self.send_addr_line_edit.text())
   100          amt = self.send_amt_spin_box.value()
   101          
   102          try:
   103              #TODO: Make this display something to the user that their input is poor
   104              if amt == 0:
   105                  raise RuntimeError("Invalid input send amount")
   106  
   107              self.rpcCom.prSend(to_addr, amt)
   108          except RuntimeError as rterror:
   109              print "Error: " + str(rterror)
   110  
   111      def setup_connections(self):
   112          #Populate the address label
   113          addr = self.rpcCom.getAdr()
   114          self.addr_label.setText(addr);
   115  
   116          #Populate the balance label
   117          self.set_bal_label()
   118  
   119          #Connect the trigger for the "Refresh" button
   120          self.bal_refresh_button.clicked.connect(self.set_bal_label)
   121  
   122          #Connect the trigger for the "Send" button
   123          self.send_button.clicked.connect(self.send_button_clicked)
   124  
   125  
   126  def main(args):
   127      app = QtGui.QApplication(args)
   128      window = mainWindow(None)
   129  
   130      window.show()
   131  
   132      sys.exit(app.exec_())
   133  
   134  if __name__ == '__main__':
   135      main(sys.argv)