gui.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. require "fox16"
  2. class MainWindow < Fox::FXMainWindow
  3. include Fox
  4. attr_reader :key
  5. def initialize app, cheapeth
  6. super app, "CheapEth - EthRegistry", :width=>450, :height=>200
  7. @cheapeth = cheapeth
  8. @reg = EthRegistry.new @cheapeth.rpc_client, $contract_addr, nil
  9. @key = nil
  10. @main_frame = FXVerticalFrame.new self, :opts => LAYOUT_FILL
  11. @chain_frame = FXVerticalFrame.new @main_frame, :opts => LAYOUT_FILL
  12. @account_frame = FXVerticalFrame.new @main_frame, :opts => LAYOUT_FILL_X
  13. @reg_frame = FXHorizontalFrame.new @main_frame, :opts => LAYOUT_FILL_X
  14. @block_label = FXLabel.new @chain_frame, "Current block: #{cheapeth.get_current_block}", :padBottom => 0
  15. @gas_label = FXLabel.new @chain_frame, "Gas price: #{cheapeth.gas_price}", :padTop => 0
  16. @account_button = FXButton.new @account_frame, "Load account"
  17. @address_label = FXLabel.new @account_frame, "Address: 0x", :padBottom => 0
  18. @balance_label = FXLabel.new @account_frame, "Balance: 0.0", :padTop => 0, :padBottom => 0
  19. @nonce_label = FXLabel.new @account_frame, "Nonce: 0.0", :padTop => 0
  20. @account_button.connect(SEL_COMMAND) {
  21. load_key
  22. }
  23. @reg_button = FXButton.new @reg_frame, "Register"
  24. @get_button = FXButton.new @reg_frame, "Get entry"
  25. @get_own_button = FXButton.new @reg_frame, "Get own entry"
  26. @reg_button.connect(SEL_COMMAND) {
  27. register
  28. }
  29. @get_button.connect(SEL_COMMAND) {
  30. get
  31. }
  32. @get_own_button.connect(SEL_COMMAND) {
  33. get_own
  34. }
  35. end
  36. def create
  37. super
  38. show(Fox::PLACEMENT_SCREEN)
  39. end
  40. protected
  41. def output_entry entry
  42. if entry != ""
  43. FXMessageBox.information self, MBOX_OK, "Result", entry
  44. else
  45. FXMessageBox.error self, MBOX_OK, "Not found", "Entry not found or empty"
  46. end
  47. end
  48. def get
  49. addr_dialog = FXInputDialog.new self, "Input address", "Address:"
  50. res = addr_dialog.execute
  51. if res == 0
  52. FXMessageBox.warning self, MBOX_OK, "Error", "Error: No input"
  53. return false
  54. end
  55. key_dialog = FXInputDialog.new self, "Input key", "Key:"
  56. res = key_dialog.execute
  57. if res == 0
  58. FXMessageBox.warning self, MBOX_OK, "Error", "Error: No input"
  59. return false
  60. end
  61. output_entry @reg.get(addr_dialog.text, key_dialog.text)
  62. end
  63. def get_own
  64. if ! @key
  65. FXMessageBox.warning self, MBOX_OK, "Error", "Error: Load account first"
  66. else
  67. key_dialog = FXInputDialog.new self, "Input key", "Key:"
  68. res = key_dialog.execute
  69. if res == 0
  70. FXMessageBox.warning self, MBOX_OK, "Error", "Error: No input"
  71. return false
  72. end
  73. output_entry @reg.get(@key.address, key_dialog.text)
  74. end
  75. end
  76. def register
  77. if ! @key
  78. FXMessageBox.warning self, MBOX_OK, "Error", "Error: Load account first"
  79. else
  80. key_dialog = FXInputDialog.new self, "Input key", "Key:"
  81. key_dialog.execute
  82. value_dialog = FXInputDialog.new self, "Input value", "Value:"
  83. value_dialog.execute
  84. tx = @reg.register key_dialog.text, value_dialog.text
  85. puts "txid: #{tx.id}"
  86. end
  87. end
  88. def load_key
  89. dialog = FXInputDialog.new self, "Private key", "Private key:"
  90. dialog.execute
  91. priv_key = dialog.text
  92. if priv_key != ""
  93. Thread.new {
  94. @account_button.disable
  95. @key = Eth::Key.new priv: priv_key
  96. puts "load account #{@key.address}"
  97. @reg = EthRegistry.new @cheapeth.rpc_client, $contract_addr, @key
  98. @address_label.text = "Address: #{@key.address}"
  99. @balance_label.text = "Balance: #{@cheapeth.get_balance_of @key.address}"
  100. @nonce_label.text = "Nonce: #{@cheapeth.get_nonce_of @key.address}"
  101. @account_button.enable
  102. }
  103. end
  104. end
  105. end