web3 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/generated_web3_methods.rb +291 -0
  3. data/lib/web3.rb +110 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd52176e1747819b1205cde47c8640f9d9a219d9
4
+ data.tar.gz: af3977a43e4290a6d7a5c74a9bfcf6f7bdcc205a
5
+ SHA512:
6
+ metadata.gz: 3b7047d3419cac745dc5ac96cb8e5ff3904359859186b1a960a187aeeaafb1dbc46032be158d137eaba354c3e63b03ba50d669605ea26a9137d39ffd38cf958e
7
+ data.tar.gz: 122e561162ae24b227314af2ea795a3046d341fc1a6acbf646d82bab5a26d3c06968676a4b0ab66fe74294312bc7e61f017fe0206e1434c130c68a71da5c02e8
@@ -0,0 +1,291 @@
1
+ module GeneratedWeb3Methods
2
+
3
+ def web3_clientVersion()
4
+ response = do_request("web3_clientVersion")
5
+ response["result"]
6
+ end
7
+
8
+ def web3_sha3(data)
9
+ response = do_request("web3_sha3",[data])
10
+ response["result"]
11
+ end
12
+
13
+ def net_version()
14
+ response = do_request("net_version")
15
+ response["result"]
16
+ end
17
+
18
+ def net_peerCount()
19
+ response = do_request("net_peerCount")
20
+ to_decimal response["result"]
21
+ end
22
+
23
+ def net_listening()
24
+ response = do_request("net_listening")
25
+ response["result"]
26
+ end
27
+
28
+ def eth_protocolVersion()
29
+ raise NotImplementedError.new "JSON-RPC call to eth_protocolVersion is not currently supported"
30
+ end
31
+
32
+ def eth_syncing()
33
+ response = do_request("eth_syncing")
34
+ response["result"]
35
+ end
36
+
37
+ def eth_coinbase()
38
+ response = do_request("eth_coinbase")
39
+ response["result"]
40
+ end
41
+
42
+ def eth_mining()
43
+ response = do_request("eth_mining")
44
+ response["result"]
45
+ end
46
+
47
+ def eth_hashrate()
48
+ response = do_request("eth_hashrate")
49
+ to_decimal response["result"]
50
+ end
51
+
52
+ def eth_gasPrice()
53
+ response = do_request("eth_gasPrice")
54
+ to_decimal response["result"]
55
+ end
56
+
57
+ def eth_accounts()
58
+ response = do_request("eth_accounts")
59
+ response["result"]
60
+ end
61
+
62
+ def eth_blockNumber()
63
+ response = do_request("eth_blockNumber")
64
+ to_decimal response["result"]
65
+ end
66
+
67
+ def eth_getBalance(address = @address, block = "latest")
68
+ response = do_request("eth_getBalance",[address, block])
69
+ to_decimal response["result"]
70
+ end
71
+
72
+ def eth_getStorageAt(storage_address, position, block = "latest")
73
+ response = do_request("eth_getStorageAt",[storage_address, to_hex(position), block])
74
+ response["result"]
75
+ end
76
+
77
+ def eth_getTransactionCount(address = @address, block = "latest")
78
+ response = do_request("eth_getTransactionCount",[address, block])
79
+ to_decimal response["result"]
80
+ end
81
+
82
+ def eth_getBlockTransactionCountByHash(data)
83
+ response = do_request("eth_getBlockTransactionCountByHash",[data])
84
+ to_decimal response["result"]
85
+ end
86
+
87
+ def eth_getBlockTransactionCountByNumber(block = "latest")
88
+ response = do_request("eth_getBlockTransactionCountByNumber",[block])
89
+ to_decimal response["result"]
90
+ end
91
+
92
+ def eth_getUncleCountByBlockHash(data)
93
+ response = do_request("eth_getUncleCountByBlockHash",[data])
94
+ to_decimal response["result"]
95
+ end
96
+
97
+ def eth_getUncleCountByBlockNumber(data)
98
+ response = do_request("eth_getUncleCountByBlockNumber",[data])
99
+ to_decimal response["result"]
100
+ end
101
+
102
+ def eth_getCode(address = @address, block = "latest")
103
+ response = do_request("eth_getCode",[address, block])
104
+ response["result"]
105
+ end
106
+
107
+ def eth_sign(data, address = @address)
108
+ response = do_request("eth_sign",[address, data])
109
+ response["result"]
110
+ end
111
+
112
+ def eth_sendTransaction(trans_object)
113
+ response = do_request("eth_sendTransaction",[trans_object])
114
+ response["result"]
115
+ end
116
+
117
+ def eth_sendRawTransaction(data)
118
+ response = do_request("eth_sendRawTransaction",[data])
119
+ response["result"]
120
+ end
121
+
122
+ def eth_call(trans_object,block)
123
+ response = do_request("eth_call",[trans_object, block])
124
+ response["result"]
125
+ end
126
+
127
+ def eth_estimateGas(trans_object,block)
128
+ response = do_request("eth_estimateGas",[trans_object, block])
129
+ response["result"]
130
+ end
131
+
132
+ def eth_getBlockByHash(hash, full_transactions = true)
133
+ response = do_request("eth_getBlockByHash",[hash, full_transactions])
134
+ response["result"]
135
+ end
136
+
137
+ def eth_getBlockByNumber(number, full_transactions = true)
138
+ response = do_request("eth_getBlockByNumber",[number, full_transactions])
139
+ response["result"]
140
+ end
141
+
142
+ def eth_getTransactionByHash(hash)
143
+ response = do_request("eth_getTransactionByHash",[hash])
144
+ response["result"]
145
+ end
146
+
147
+ def eth_getTransactionByBlockHashAndIndex(hash, index)
148
+ response = do_request("eth_getTransactionByBlockHashAndIndex",[hash, index])
149
+ response["result"]
150
+ end
151
+
152
+ def eth_getTransactionByBlockNumberAndIndex(number, index)
153
+ response = do_request("eth_getTransactionByBlockNumberAndIndex",[number, index])
154
+ response["result"]
155
+ end
156
+
157
+ def eth_getTransactionReceipt(hash)
158
+ response = do_request("eth_getTransactionReceipt",[hash])
159
+ response["result"]
160
+ end
161
+
162
+ def eth_getUncleByBlockHashAndIndex(hash, index)
163
+ response = do_request("eth_getUncleByBlockHashAndIndex",[hash, index])
164
+ response["result"]
165
+ end
166
+
167
+ def eth_getUncleByBlockNumberAndIndex(number, index)
168
+ response = do_request("eth_getUncleByBlockNumberAndIndex",[number, index])
169
+ response["result"]
170
+ end
171
+
172
+ def eth_getCompilers()
173
+ response = do_request("eth_getCompilers")
174
+ response["result"]
175
+ end
176
+
177
+ def eth_compileLLL(code)
178
+ response = do_request("eth_compileLLL",[code])
179
+ response["result"]
180
+ end
181
+
182
+ def eth_compileSolidity(code)
183
+ response = do_request("eth_compileSolidity",[code])
184
+ response["result"]
185
+ end
186
+
187
+ def eth_compileSerpent(code)
188
+ response = do_request("eth_compileSerpent",[code])
189
+ response["result"]
190
+ end
191
+
192
+ def eth_newFilter(fromBlock, toBlock, address, topics)
193
+ response = do_request("$CODE",[fromBlock, toBlock, address, topics])
194
+ to_decimal response["result"]
195
+ end
196
+
197
+ def eth_newBlockFilter()
198
+ response = do_request("eth_newBlockFilter")
199
+ to_decimal response["result"]
200
+ end
201
+
202
+ def eth_newPendingTransactionFilter()
203
+ response = do_request("eth_newPendingTransactionFilter")
204
+ to_decimal response["result"]
205
+ end
206
+
207
+ def eth_uninstallFilter(id)
208
+ response = do_request("eth_uninstallFilter",[id])
209
+ response["result"]
210
+ end
211
+
212
+ def eth_getFilterChanges(id)
213
+ response = do_request("eth_getFilterChanges",[id])
214
+ response["result"]
215
+ end
216
+
217
+ def eth_getFilterLogs(id)
218
+ response = do_request("eth_getFilterLogs",[id])
219
+ response["result"]
220
+ end
221
+
222
+ def eth_getLogs(filter_obj)
223
+ response = do_request("eth_getLogs",[filter_obj])
224
+ response["result"]
225
+ end
226
+
227
+ def eth_getWork()
228
+ response = do_request("eth_getWork")
229
+ response["result"]
230
+ end
231
+
232
+ def eth_submitWork(nonce, powHash, mixDigest)
233
+ response = do_request("eth_submitWork",[nonce, powHash, mixDigest])
234
+ response["result"]
235
+ end
236
+
237
+ def eth_submitHashrate(hashrate, id)
238
+ response = do_request("eth_submitHashrate",[hashrate, id])
239
+ response["result"]
240
+ end
241
+
242
+ def shh_version()
243
+ response = do_request("shh_version")
244
+ response["result"]
245
+ end
246
+
247
+ def shh_post(post_object)
248
+ response = do_request("shh_post",[post_object])
249
+ response["result"]
250
+ end
251
+
252
+ def shh_newIdentity()
253
+ response = do_request("shh_newIdentity")
254
+ response["result"]
255
+ end
256
+
257
+ def shh_hasIdentity(address)
258
+ response = do_request("shh_hasIdentity",[address])
259
+ response["result"]
260
+ end
261
+
262
+ def shh_newGroup()
263
+ response = do_request("shh_newGroup")
264
+ response["result"]
265
+ end
266
+
267
+ def shh_addToGroup(address)
268
+ response = do_request("shh_addToGroup",[address])
269
+ response["result"]
270
+ end
271
+
272
+ def shh_newFilter(filter_object)
273
+ response = do_request("shh_newFilter",[filter_object])
274
+ to_decimal response["result"]
275
+ end
276
+
277
+ def shh_uninstallFilter(id)
278
+ response = do_request("shh_uninstallFilter",[id])
279
+ response["result"]
280
+ end
281
+
282
+ def shh_getFilterChanges(id)
283
+ response = do_request("shh_getFilterChanges",[id])
284
+ response["result"]
285
+ end
286
+
287
+ def shh_getMessages(id)
288
+ response = do_request("shh_getMessages",[id])
289
+ response["result"]
290
+ end
291
+ end
@@ -0,0 +1,110 @@
1
+ # A client to Ethereum JSON-RPC https://github.com/ethereum/wiki/wiki/JSON-RPC
2
+
3
+ require "httparty"
4
+ require "json"
5
+
6
+ #@method = "eth_getBalance"
7
+ #@params = ["0x14226aD04625ED4930787D87C82c6c72a5B690a1", "latest"]
8
+
9
+ class Web3
10
+ attr_accessor :address, :endpoint, :id, :debug
11
+
12
+ @@jsonrpc = "2.0"
13
+ @@debug = ENV["ETH_DEBUG"] || false
14
+
15
+ def initialize( address = nil,
16
+ endpoint = ENV["ETH_ENDPOINT"] || "http://localhost:8545",
17
+ id = ENV["ETH_DEFAULT_CLIENT_ID"] || 999)
18
+ @address = address
19
+ @endpoint = endpoint
20
+ @id = id
21
+ end
22
+
23
+ def do_request(method, params = [], id = @id) #TODO: More error handling
24
+ request_json = { :jsonrpc => @@jsonrpc,
25
+ :method => method,
26
+ :params => params,
27
+ :id => @id
28
+ }.to_json
29
+
30
+ if @@debug
31
+ puts "Request to " + @endpoint + ": " + request_json + " ...."
32
+ end
33
+
34
+ response = HTTParty.post(@endpoint,
35
+ :body => request_json,
36
+ :headers => { 'Content-Type' => 'application/json' } )
37
+
38
+ if @@debug
39
+ puts "Response: " + response.to_s()
40
+ end
41
+
42
+ if response.bad_gateway?
43
+ raise "Unable to connect to JSON-RPC endpont" + @endpoint
44
+ end
45
+
46
+ if !response.success?
47
+ raise "JSON-RPC endpoint " + @endpoint + " returned http code " + response.code
48
+ end
49
+
50
+ if response["error"]
51
+ code = response["error"]["code"]
52
+ message = response["error"]["message"]
53
+ raise "In response to " + method + " request, JSON-RPC endpoint returned error #" + code.to_s() + ": " + message
54
+ end
55
+ response
56
+ end
57
+
58
+ # JSON RPC FUNCTION CALLS INCLUDED HERE
59
+ require_relative "generated_web3_methods.rb"
60
+ include GeneratedWeb3Methods
61
+
62
+
63
+ #Utility methods
64
+ def to_decimal(hex)
65
+ if hex == nil
66
+ return nil
67
+ end
68
+ if hex.is_a?(Integer)
69
+ return hex
70
+ end
71
+ hex.to_i(16)
72
+ end
73
+
74
+ def to_hex(decimal)
75
+ if decimal == nil
76
+ return nil
77
+ end
78
+ if decimal.is_a?(String)
79
+ return decimal
80
+ end
81
+ decimal.to_s(16)
82
+ end
83
+
84
+ def wei_to_ether(wei)
85
+ 1.0 * wei / 10**18
86
+ end
87
+
88
+ def ether_to_wei(ether)
89
+ ether * 10**18
90
+ end
91
+
92
+ end
93
+
94
+ w3 = Web3. new "0x14226aD04625ED4930787D87C82c6c72a5B690a1"
95
+ #w3.address =
96
+ #puts w3. web3_clientVersion
97
+ #puts w3. net_version
98
+ #puts w3. eth_protocolVersion
99
+ #puts "ds"
100
+ #puts w3. eth_coinbase #testme
101
+ #puts w3. eth_getBlockTransactionCountByNumber 3267552
102
+ # w3. eth_getBlockTransactionCountByHash '0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238'
103
+
104
+ #puts w3. eth_blockNumber
105
+ #puts w3. eth_mining
106
+ #puts w3. eth_getTransactionCount
107
+ #puts w3. eth_getBalance
108
+ #puts w3. eth_accounts
109
+ #puts w3. eth_gasPrice
110
+ #puts do_request(@method, @params)
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: web3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Spike Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A client interfact to connect to an Ethereum Web3 JSON RPC service
14
+ email: spikewilliams@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/generated_web3_methods.rb
20
+ - lib/web3.rb
21
+ homepage: https://github.com/spikewilliams/vtada-ethereum
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A client interfact to connect to an Ethereum Web3 JSON RPC service
45
+ test_files: []