EthRegistry.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. require "eth"
  2. class EthRegistry
  3. def initialize rpc_client, contract_addr, key
  4. @rpc_client = rpc_client
  5. abi = <<ABI
  6. [
  7. {
  8. "anonymous": false,
  9. "inputs": [
  10. {
  11. "indexed": false,
  12. "internalType": "address",
  13. "name": "addr",
  14. "type": "address"
  15. },
  16. {
  17. "indexed": false,
  18. "internalType": "bytes32",
  19. "name": "key",
  20. "type": "bytes32"
  21. },
  22. {
  23. "indexed": false,
  24. "internalType": "string",
  25. "name": "value",
  26. "type": "string"
  27. }
  28. ],
  29. "name": "LogRegister",
  30. "type": "event"
  31. },
  32. {
  33. "inputs": [
  34. {
  35. "internalType": "address",
  36. "name": "addr",
  37. "type": "address"
  38. },
  39. {
  40. "internalType": "bytes32",
  41. "name": "key",
  42. "type": "bytes32"
  43. }
  44. ],
  45. "name": "get",
  46. "outputs": [
  47. {
  48. "internalType": "string",
  49. "name": "",
  50. "type": "string"
  51. }
  52. ],
  53. "stateMutability": "view",
  54. "type": "function"
  55. },
  56. {
  57. "inputs": [
  58. {
  59. "internalType": "bytes32",
  60. "name": "key",
  61. "type": "bytes32"
  62. }
  63. ],
  64. "name": "get",
  65. "outputs": [
  66. {
  67. "internalType": "string",
  68. "name": "",
  69. "type": "string"
  70. }
  71. ],
  72. "stateMutability": "view",
  73. "type": "function"
  74. },
  75. {
  76. "inputs": [
  77. {
  78. "internalType": "bytes32",
  79. "name": "key",
  80. "type": "bytes32"
  81. },
  82. {
  83. "internalType": "string",
  84. "name": "value",
  85. "type": "string"
  86. }
  87. ],
  88. "name": "register",
  89. "outputs": [],
  90. "stateMutability": "nonpayable",
  91. "type": "function"
  92. },
  93. {
  94. "inputs": [
  95. {
  96. "internalType": "address",
  97. "name": "",
  98. "type": "address"
  99. },
  100. {
  101. "internalType": "bytes32",
  102. "name": "",
  103. "type": "bytes32"
  104. }
  105. ],
  106. "name": "registry",
  107. "outputs": [
  108. {
  109. "internalType": "string",
  110. "name": "",
  111. "type": "string"
  112. }
  113. ],
  114. "stateMutability": "view",
  115. "type": "function"
  116. }
  117. ]
  118. ABI
  119. @contract = Ethereum::Contract.create(client: rpc_client, name: "EthRegistry", address: contract_addr, abi: abi)
  120. if key != nil
  121. @contract.key = key
  122. @addr = key.address
  123. end
  124. end
  125. def get_own_entry key
  126. @contract.call.get__address__bytes32(@addr, key)
  127. end
  128. def get addr, key
  129. @contract.call.get__address__bytes32(addr, key)
  130. end
  131. def register key, content
  132. return false if ! @addr
  133. @contract.transact.register(key, content)
  134. end
  135. end