ruby-client 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +59 -0
- data/.travis.yml +6 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +91 -0
- data/LICENSE +177 -0
- data/README.md +49 -0
- data/Rakefile +14 -0
- data/bin/console +6 -0
- data/bin/setup +6 -0
- data/doc/Api.html +1592 -0
- data/doc/ClientRPC.html +99 -0
- data/doc/ClientRPC/Connect.html +106 -0
- data/doc/ClientRPC/Connect/Error.html +106 -0
- data/doc/Gemfile.html +101 -0
- data/doc/Gemfile_lock.html +193 -0
- data/doc/LICENSE.html +177 -0
- data/doc/LICENSE_txt.html +105 -0
- data/doc/Nimiq.html +99 -0
- data/doc/Nimiq/Client.html +248 -0
- data/doc/Nimiq/Error.html +108 -0
- data/doc/Nimiq/Exceptions.html +163 -0
- data/doc/Object.html +125 -0
- data/doc/README_md.html +162 -0
- data/doc/Rakefile.html +108 -0
- data/doc/Ruby.html +99 -0
- data/doc/Ruby/ClientTest.html +106 -0
- data/doc/bin/setup.html +99 -0
- data/doc/classes/Api.html +1506 -0
- data/doc/classes/ClientRPC.html +120 -0
- data/doc/classes/ClientRPC/Connect.html +120 -0
- data/doc/classes/ClientRPC/Connect/Error.html +111 -0
- data/doc/classes/Nimiq.html +145 -0
- data/doc/classes/Nimiq/Client.html +237 -0
- data/doc/classes/Nimiq/Error.html +113 -0
- data/doc/classes/Nimiq/Exceptions.html +152 -0
- data/doc/classes/index.html +80 -0
- data/doc/created.rid +13 -0
- data/doc/css/application.css +64 -0
- data/doc/css/bootstrap.min.css +868 -0
- data/doc/css/coderay.css +141 -0
- data/doc/css/fonts.css +167 -0
- data/doc/css/rdoc.css +668 -0
- data/doc/files/index.html +82 -0
- data/doc/files/lib/api_rb.html +112 -0
- data/doc/files/lib/client_rb.html +118 -0
- data/doc/files/lib/nimiq/version_rb.html +105 -0
- data/doc/files/lib/rpcclient_rb.html +119 -0
- data/doc/fonts/Lato-Light.ttf +0 -0
- data/doc/fonts/Lato-LightItalic.ttf +0 -0
- data/doc/fonts/Lato-Regular.ttf +0 -0
- data/doc/fonts/Lato-RegularItalic.ttf +0 -0
- data/doc/fonts/SourceCodePro-Bold.ttf +0 -0
- data/doc/fonts/SourceCodePro-Regular.ttf +0 -0
- data/doc/images/add.png +0 -0
- data/doc/images/arrow_up.png +0 -0
- data/doc/images/brick.png +0 -0
- data/doc/images/brick_link.png +0 -0
- data/doc/images/bug.png +0 -0
- data/doc/images/bullet_black.png +0 -0
- data/doc/images/bullet_toggle_minus.png +0 -0
- data/doc/images/bullet_toggle_plus.png +0 -0
- data/doc/images/date.png +0 -0
- data/doc/images/delete.png +0 -0
- data/doc/images/find.png +0 -0
- data/doc/images/loadingAnimation.gif +0 -0
- data/doc/images/macFFBgHack.png +0 -0
- data/doc/images/package.png +0 -0
- data/doc/images/page_green.png +0 -0
- data/doc/images/page_white_text.png +0 -0
- data/doc/images/page_white_width.png +0 -0
- data/doc/images/plugin.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tag_blue.png +0 -0
- data/doc/images/tag_green.png +0 -0
- data/doc/images/transparent.png +0 -0
- data/doc/images/wrench.png +0 -0
- data/doc/images/wrench_orange.png +0 -0
- data/doc/images/zoom.png +0 -0
- data/doc/img/glyphicons-halflings-white.png +0 -0
- data/doc/img/glyphicons-halflings.png +0 -0
- data/doc/index.html +110 -0
- data/doc/js/application.js +50 -0
- data/doc/js/bootstrap.min.js +7 -0
- data/doc/js/darkfish.js +84 -0
- data/doc/js/index.js +1 -0
- data/doc/js/jquery.js +4 -0
- data/doc/js/navigation.js +105 -0
- data/doc/js/navigation.js.gz +0 -0
- data/doc/js/search.js +110 -0
- data/doc/js/search_index.js +1 -0
- data/doc/js/search_index.js.gz +0 -0
- data/doc/js/searcher.js +229 -0
- data/doc/js/searcher.js.gz +0 -0
- data/doc/method/index.html +122 -0
- data/doc/table_of_contents.html +318 -0
- data/lib/api.rb +317 -0
- data/lib/client.rb +46 -0
- data/lib/nimiq/version.rb +3 -0
- data/lib/rpcclient.rb +108 -0
- data/ruby-client.gemspec +28 -0
- metadata +160 -0
data/lib/api.rb
ADDED
@@ -0,0 +1,317 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2020 Nimiq community.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
=end
|
16
|
+
|
17
|
+
module Api
|
18
|
+
attr_accessor :rpc
|
19
|
+
|
20
|
+
# accounts - Returns a list of addresses owned by client.
|
21
|
+
def accounts
|
22
|
+
result = @rpc.request("accounts")
|
23
|
+
return result
|
24
|
+
end
|
25
|
+
|
26
|
+
# block_number - Returns the height of most recent block.
|
27
|
+
def block_number
|
28
|
+
result = @rpc.request("blockNumber")
|
29
|
+
return result
|
30
|
+
end
|
31
|
+
|
32
|
+
# consensus - Returns information on the current consensus state.
|
33
|
+
def consensus
|
34
|
+
result = @rpc.request("consensus")
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
|
38
|
+
# constant - Returns or overrides a constant value.
|
39
|
+
# When no parameter is given, it returns the value of the constant.
|
40
|
+
# When giving a value as parameter, it sets the constant to the given value.
|
41
|
+
# To reset the constant to the default value, the parameter "reset" should be used.
|
42
|
+
# - @param [String] name - The class and name of the constant. (format should be Class.CONSTANT)
|
43
|
+
# - @param [Integer] value - The new value of the constant or "reset". (optional)
|
44
|
+
def constant(name, value = nil)
|
45
|
+
if value
|
46
|
+
result = @rpc.request("constant", name, value)
|
47
|
+
else
|
48
|
+
result = @rpc.request("constant", name)
|
49
|
+
end
|
50
|
+
return result
|
51
|
+
end
|
52
|
+
|
53
|
+
# create_account - Creates a new account and stores its private key in the client store.
|
54
|
+
def create_account
|
55
|
+
result = @rpc.request("createAccount")
|
56
|
+
return result
|
57
|
+
end
|
58
|
+
|
59
|
+
# create_raw_transaction - Creates and signs a transaction without sending it.
|
60
|
+
# The transaction can then be send via `send_raw_transaction` without accidentally replaying it.
|
61
|
+
# - @param [TransactionObject] transaction - The transaction object.
|
62
|
+
def create_raw_transaction(transaction)
|
63
|
+
result = @rpc.request("createRawTransaction", transaction)
|
64
|
+
return result
|
65
|
+
end
|
66
|
+
|
67
|
+
# # new: update with more info
|
68
|
+
# # get_raw_transaction_info - Checks signed_transaction raw information.
|
69
|
+
# # - @param [String] signed_transaction - The hex encoded signed transaction.
|
70
|
+
# def get_raw_transaction_info(signed_transaction)
|
71
|
+
# result = @rpc.request("getRawTransactionInfo", signed_transaction)
|
72
|
+
# return result
|
73
|
+
# end
|
74
|
+
|
75
|
+
# get_account - Returns details for the account of given address.
|
76
|
+
# - @param [String] address - Address to get account details.
|
77
|
+
def get_account(address)
|
78
|
+
result = @rpc.request("getAccount", address)
|
79
|
+
return result
|
80
|
+
end
|
81
|
+
|
82
|
+
# get_balance - Returns the balance of the account of given address.
|
83
|
+
# - @param [String] address - Address to check for balance.
|
84
|
+
def get_balance(address)
|
85
|
+
result = @rpc.request("getBalance", address)
|
86
|
+
return result
|
87
|
+
end
|
88
|
+
|
89
|
+
# get_block_by_hash - Returns information about a block by hash.
|
90
|
+
# - @param [String] block_hash - Hash of the block to gather information on.
|
91
|
+
# - @param [Boolean] full_transactions (optional) - If true it returns the full transaction objects, if false only the hashes of the transactions. (default false)
|
92
|
+
def get_block_by_hash(block_hash, full_transactions = nil)
|
93
|
+
if full_transactions
|
94
|
+
result = @rpc.request("getBlockByHash", block_hash, full_transactions)
|
95
|
+
else
|
96
|
+
result = @rpc.request("getBlockByHash", block_hash)
|
97
|
+
end
|
98
|
+
return result
|
99
|
+
end
|
100
|
+
|
101
|
+
# get_block_by_number - Returns information about a block by block number.
|
102
|
+
# - @param [Integer] block_number - The height of the block to gather information on.
|
103
|
+
# - @param [Boolean] full_transactions (optional) - If true it returns the full transaction objects, if false only the hashes of the transactions. (default false)
|
104
|
+
def get_block_by_number(block_number, full_transactions = nil)
|
105
|
+
if full_transactions
|
106
|
+
result = @rpc.request("getBlockByNumber", block_number, full_transactions)
|
107
|
+
else
|
108
|
+
result = @rpc.request("getBlockByNumber", block_number)
|
109
|
+
end
|
110
|
+
return result
|
111
|
+
end
|
112
|
+
|
113
|
+
# get_block_template - Returns a template to build the next block for mining.
|
114
|
+
# This will consider pool instructions when connected to a pool.
|
115
|
+
# - @param [String] address (optional) - Address to use as a miner for this block. This overrides the address provided during startup or from the pool.
|
116
|
+
# - @param [String] dada_field (optional) - Hex-encoded value for the extra data field. This overrides the address provided during startup or from the pool.
|
117
|
+
def get_block_template(address = nil, dada_field = nil)
|
118
|
+
result = @rpc.request("getBlockTemplate")
|
119
|
+
return result
|
120
|
+
end
|
121
|
+
|
122
|
+
# get_block_transaction_count_by_hash - Returns the number of transactions in a block from a block matching the given block hash.
|
123
|
+
# - @param [String] block_hash - Hash of the block.
|
124
|
+
def get_block_transaction_count_by_hash(block_hash)
|
125
|
+
result = @rpc.request("getBlockTransactionCountByHash", block_hash)
|
126
|
+
return result
|
127
|
+
end
|
128
|
+
|
129
|
+
# get_block_transaction_count_by_number - Returns the number of transactions in a block matching the given block number.
|
130
|
+
# - @param [Integer] block_number - Height of the block.
|
131
|
+
def get_block_transaction_count_by_number(block_number)
|
132
|
+
result = @rpc.request("getBlockTransactionCountByNumber", block_number)
|
133
|
+
return result
|
134
|
+
end
|
135
|
+
|
136
|
+
# get_transaction_by_block_hash_and_index - Returns information about a transaction by block hash and transaction index position.
|
137
|
+
# - @param [Integer] block_hash - Hash of the block containing the transaction.
|
138
|
+
# - @param [Integer] transaction_index - Index of the transaction in the block.
|
139
|
+
def get_transaction_by_block_hash_and_index(block_hash, transaction_index)
|
140
|
+
result = @rpc.request("getTransactionByBlockHashAndIndex", block_hash, transaction_index)
|
141
|
+
return result
|
142
|
+
end
|
143
|
+
|
144
|
+
# get_transaction_by_block_number_and_index - Returns information about a transaction by block number and transaction index position.
|
145
|
+
# - @param [Integer] block_height - Height of the block containing the transaction.
|
146
|
+
# - @param [Integer] transaction_index - Index of the transaction in the block.
|
147
|
+
def get_transaction_by_block_number_and_index(block_height, transaction_index)
|
148
|
+
result = @rpc.request("getTransactionByBlockNumberAndIndex", block_height, transaction_index)
|
149
|
+
return result
|
150
|
+
end
|
151
|
+
|
152
|
+
# get_transaction_by_hash - Returns the information about a transaction requested by transaction hash.
|
153
|
+
# - @param [String] transaction_hash - Hash of a transaction.
|
154
|
+
def get_transaction_by_hash(transaction_hash)
|
155
|
+
result = @rpc.request("getTransactionByHash", transaction_hash)
|
156
|
+
return result
|
157
|
+
end
|
158
|
+
|
159
|
+
# get_transaction_receipt - Returns the receipt of a transaction by transaction hash.
|
160
|
+
# Note That the receipt is not available for pending transactions.
|
161
|
+
# - @param [String] transaction_hash - Hash of a transaction.
|
162
|
+
def get_transaction_receipt(transaction_hash)
|
163
|
+
result = @rpc.request("getTransactionReceipt", transaction_hash)
|
164
|
+
return result
|
165
|
+
end
|
166
|
+
|
167
|
+
# get_transactions_by_address - Returns the latest transactions successfully performed by or for an address.
|
168
|
+
# - @param [String] address - Address of which transactions should be gathered.
|
169
|
+
# - @param [Integer] transactions_number (optional) - Number of transactions that shall be returned. (default 1000)
|
170
|
+
def get_transactions_by_address(address, transactions_number = nil)
|
171
|
+
if transactions_number
|
172
|
+
result = @rpc.request("getTransactionsByAddress", address, transactions_number)
|
173
|
+
else
|
174
|
+
result = @rpc.request("getTransactionsByAddress", address)
|
175
|
+
end
|
176
|
+
return result
|
177
|
+
end
|
178
|
+
|
179
|
+
# get_work - Returns instructions to mine the next block. This will consider pool instructions when connected to a pool.
|
180
|
+
# - @param [String] address (optional) - Address to use as a miner for this block. This overrides the address provided during startup or from the pool.
|
181
|
+
# - @param [String] dada_field (optional) - Hex-encoded value for the extra data field. This overrides the address provided during startup or from the pool.
|
182
|
+
def get_work(address = nil, dada_field = nil)
|
183
|
+
result = @rpc.request("getWork", address, dada_field)
|
184
|
+
return result
|
185
|
+
end
|
186
|
+
|
187
|
+
# hashrate - Returns the number of hashes per second that the node is mining with.
|
188
|
+
def hashrate
|
189
|
+
result = @rpc.request("hashrate")
|
190
|
+
return result
|
191
|
+
end
|
192
|
+
|
193
|
+
# log - Sets the log level of the node.
|
194
|
+
# - @param [String] tag - If the tag is '*' the log level will be set globally, otherwise the log level is applied only on this tag.
|
195
|
+
# - @param [String] log_level - Log levels valid options: `trace`, `verbose`, `debug`, `info`, `warn`, `error`, `assert`.
|
196
|
+
def log(tag, log_level)
|
197
|
+
result = @rpc.request("log", tag, log_level)
|
198
|
+
return result
|
199
|
+
end
|
200
|
+
|
201
|
+
# mempool - Returns information on the current mempool situation. This will provide an overview of the number of transactions sorted into buckets based on their fee per byte (in smallest unit).
|
202
|
+
def mempool
|
203
|
+
result = @rpc.request("mempool")
|
204
|
+
return result
|
205
|
+
end
|
206
|
+
|
207
|
+
# mempool_content - Returns transactions that are currently in the mempool.
|
208
|
+
def mempool_content
|
209
|
+
result = @rpc.request("mempoolContent")
|
210
|
+
return result
|
211
|
+
end
|
212
|
+
|
213
|
+
# miner_address - Returns the miner address.
|
214
|
+
def miner_address
|
215
|
+
result = @rpc.request("minerAddress")
|
216
|
+
return result
|
217
|
+
end
|
218
|
+
|
219
|
+
# miner_threads - Returns or sets the number of CPU threads for the miner.
|
220
|
+
# - @param [Integer] set_threads (optional) - The number of threads to allocate for mining.
|
221
|
+
def miner_threads(set_threads = nil)
|
222
|
+
if set_threads
|
223
|
+
result = @rpc.request("minerThreads", set_threads)
|
224
|
+
else
|
225
|
+
result = @rpc.request("minerThreads")
|
226
|
+
end
|
227
|
+
return result
|
228
|
+
end
|
229
|
+
|
230
|
+
# min_fee_per_byte - Returns or sets the minimum fee per byte.
|
231
|
+
# - @param [Integer] set_min_fee (optional) - The new minimum fee per byte.
|
232
|
+
def min_fee_per_byte(set_min_fee = nil)
|
233
|
+
if set_min_fee
|
234
|
+
result = @rpc.request("minFeePerByte", set_min_fee)
|
235
|
+
else
|
236
|
+
result = @rpc.request("minFeePerByte")
|
237
|
+
end
|
238
|
+
return result
|
239
|
+
end
|
240
|
+
|
241
|
+
# mining - Returns `true` if client is actively mining new blocks.
|
242
|
+
def mining
|
243
|
+
result = @rpc.request("mining")
|
244
|
+
return result
|
245
|
+
end
|
246
|
+
|
247
|
+
# peer_count - Returns number of peers currently connected to the client.
|
248
|
+
def peer_count
|
249
|
+
result = @rpc.request("peerCount")
|
250
|
+
return result
|
251
|
+
end
|
252
|
+
|
253
|
+
# peer_list - Returns list of peers known to the client.
|
254
|
+
def peer_list
|
255
|
+
result = @rpc.request("peerList")
|
256
|
+
return result
|
257
|
+
end
|
258
|
+
|
259
|
+
# peer_state - Returns the state of the peer.
|
260
|
+
# - @param [String] peer_address - The address of the peer.
|
261
|
+
def peer_state(peer_address)
|
262
|
+
result = @rpc.request("peerState", peer_address)
|
263
|
+
return result
|
264
|
+
end
|
265
|
+
|
266
|
+
# pool - Returns or sets the mining pool.
|
267
|
+
# When no parameter is given, it returns the current mining pool. When a value is given as parameter, it sets the mining pool to that value.
|
268
|
+
# - @param [String/Boolean] pool_address (optional) - The mining pool connection string (url:port) or boolean to enable/disable pool mining.
|
269
|
+
def pool(pool_address = nil)
|
270
|
+
if pool_address
|
271
|
+
result = @rpc.request("pool", pool_address)
|
272
|
+
else
|
273
|
+
result = @rpc.request("pool")
|
274
|
+
end
|
275
|
+
return result
|
276
|
+
end
|
277
|
+
|
278
|
+
# pool_confirmed_balance - Returns the confirmed mining pool balance.
|
279
|
+
def pool_confirmed_balance
|
280
|
+
result = @rpc.request("poolConfirmedBalance")
|
281
|
+
return result
|
282
|
+
end
|
283
|
+
|
284
|
+
# pool_connection_state - Returns the connection state to mining pool.
|
285
|
+
def pool_connection_state
|
286
|
+
result = @rpc.request("poolConnectionState")
|
287
|
+
return result
|
288
|
+
end
|
289
|
+
|
290
|
+
# send_raw_transaction - Sends a signed message call transaction or a contract creation, if the data field contains code.
|
291
|
+
# - @param [String] signed_transaction - The hex encoded signed transaction.
|
292
|
+
def send_raw_transaction(signed_transaction)
|
293
|
+
result = @rpc.request("sendRawTransaction", signed_transaction)
|
294
|
+
return result
|
295
|
+
end
|
296
|
+
|
297
|
+
# send_transaction - Creates new message call transaction or a contract creation, if the data field contains code.
|
298
|
+
# - @param [TransactionObject] transaction - The transaction object.
|
299
|
+
def send_transaction(transaction)
|
300
|
+
result = @rpc.request("sendTransaction", transaction)
|
301
|
+
return result
|
302
|
+
end
|
303
|
+
|
304
|
+
# submit_block - Submits a block to the node. When the block is valid, the node will forward it to other nodes in the network.
|
305
|
+
# When submitting work from getWork, remember to include the suffix.
|
306
|
+
# - @param [String] block - Hex-encoded full block (including header, interlink and body).
|
307
|
+
def submit_block(block)
|
308
|
+
result = @rpc.request("submitBlock", block)
|
309
|
+
return result
|
310
|
+
end
|
311
|
+
|
312
|
+
# syncing - Returns an object with data about the sync status or `false`.
|
313
|
+
def syncing
|
314
|
+
result = @rpc.request("syncing")
|
315
|
+
return result
|
316
|
+
end
|
317
|
+
end
|
data/lib/client.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2020 Nimiq community.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
=end
|
16
|
+
|
17
|
+
require_relative "nimiq/version"
|
18
|
+
require_relative "rpcclient"
|
19
|
+
require_relative "api"
|
20
|
+
require "json"
|
21
|
+
|
22
|
+
module Nimiq
|
23
|
+
class Error < StandardError; end # :nodoc: all
|
24
|
+
|
25
|
+
attr_reader :rpc # :nodoc: all
|
26
|
+
|
27
|
+
class Client
|
28
|
+
include Api
|
29
|
+
|
30
|
+
def initialize(opts)
|
31
|
+
return @rpc = ClientRPC::Connect.new(opts)
|
32
|
+
puts "Connecting to #{@opts}"
|
33
|
+
end
|
34
|
+
|
35
|
+
# Sends a raw request to the Nimiq node.
|
36
|
+
def request(name, params = nil)
|
37
|
+
return @rpc.request(name, params)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Ping the Nimiq node.
|
41
|
+
def ping
|
42
|
+
@rpc.ping_node
|
43
|
+
@pingres = @rpc.instance_variable_get(:@pingres)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/rpcclient.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2020 Nimiq community.
|
3
|
+
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
you may not use this file except in compliance with the License.
|
6
|
+
You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
See the License for the specific language governing permissions and
|
14
|
+
limitations under the License.
|
15
|
+
=end
|
16
|
+
|
17
|
+
require "oj"
|
18
|
+
require "net/http"
|
19
|
+
|
20
|
+
# :nodoc: all
|
21
|
+
module ClientRPC
|
22
|
+
class Connect
|
23
|
+
attr_accessor :options, :uri
|
24
|
+
|
25
|
+
DEFAULTS = {
|
26
|
+
host: "localhost",
|
27
|
+
port: 18332,
|
28
|
+
# dev: true,
|
29
|
+
}.freeze
|
30
|
+
|
31
|
+
def initialize(host)
|
32
|
+
@options = DEFAULTS.dup.merge(host.dup)
|
33
|
+
@uri = @options[:uri] ? URI(@options[:uri]) : URI(uri_check())
|
34
|
+
end
|
35
|
+
|
36
|
+
def request(name, *args)
|
37
|
+
puts "\n> #{name}: #{args.join(",")}" if options[:dev]
|
38
|
+
response = request_http_post(name, args)
|
39
|
+
puts "< #{response.code} #{response.message}" if options[:dev]
|
40
|
+
raise Error, response.message unless (200...300).cover?(response.code.to_i)
|
41
|
+
begin
|
42
|
+
response = Oj.load(response.body, symbol_keys: true, bigdecimal_load: true)
|
43
|
+
rescue StandardError => e
|
44
|
+
puts "WARN < Failed to parse JSON response: #{e}" if options[:dev]
|
45
|
+
raise
|
46
|
+
end
|
47
|
+
puts "\n> #{name}: #{args.join(",")}" if options[:dev]
|
48
|
+
puts response[:result] if options[:dev]
|
49
|
+
raise Error, response[:error] if response[:error]
|
50
|
+
response[:result]
|
51
|
+
end
|
52
|
+
|
53
|
+
def ping_node
|
54
|
+
user = uri.user
|
55
|
+
pass = uri.password
|
56
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
57
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
58
|
+
request.basic_auth(user, pass)
|
59
|
+
request.body = request_body("", nil)
|
60
|
+
request["Content-Type"] = "application/json".freeze
|
61
|
+
response = http.request(request)
|
62
|
+
@pingres = (response.code).to_i.dup
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def request_http_post(name, params)
|
68
|
+
user = uri.user
|
69
|
+
pass = uri.password
|
70
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
71
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
72
|
+
request.basic_auth(user, pass)
|
73
|
+
request.body = request_body(name, params)
|
74
|
+
request["Content-Type"] = "application/json".freeze
|
75
|
+
http.request(request)
|
76
|
+
end
|
77
|
+
|
78
|
+
def request_body(name, params)
|
79
|
+
Oj.dump({
|
80
|
+
id: "rpc",
|
81
|
+
jsonrpc: "2.0",
|
82
|
+
method: name,
|
83
|
+
params: params,
|
84
|
+
}, mode: :compat)
|
85
|
+
end
|
86
|
+
|
87
|
+
def uri_check
|
88
|
+
if (options[:host].include? "http://" or options[:host].include? "https://")
|
89
|
+
host = options[:host]
|
90
|
+
newhost = url_strip(host.dup)
|
91
|
+
if (options[:host].include? "https")
|
92
|
+
return "https://#{options[:user]}:#{options[:pass]}@#{newhost}:#{options[:port]}"
|
93
|
+
else
|
94
|
+
return "http://#{options[:user]}:#{options[:pass]}@#{newhost}:#{options[:port]}"
|
95
|
+
end
|
96
|
+
else
|
97
|
+
return "http://#{options[:user]}:#{options[:pass]}@#{options[:host]}:#{options[:port]}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def url_strip(url)
|
102
|
+
return url.to_s.sub!(/https?(\:)?(\/)?(\/)?(www\.)?/, "") if url.include?("http")
|
103
|
+
url.to_s.sub!(/(www\.)?/, "") if url.include?("www")
|
104
|
+
end
|
105
|
+
|
106
|
+
class Error < RuntimeError; end
|
107
|
+
end
|
108
|
+
end
|