ruby-client 1.1.4 → 1.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f6475996f60be83ea786daa66d456dbf6e31184fa185e493d2c9cdd576af9aa
4
- data.tar.gz: ad61ee287cd26dab228cb099747e0aab833da2cbac0e2fee117168e33cf53b00
3
+ metadata.gz: 4711b6ed9140348c52a5e03c44e1b11d62e57215388e2132caaf2accf6645b64
4
+ data.tar.gz: d97fe8bb83867f89123ed94985ef0dcc0a1edd1cce46fba8d9933c4958189a83
5
5
  SHA512:
6
- metadata.gz: dfa4928e4860a1cdca26674831215110e8a199512b65a64a03540c20cb85932d0faea538afb044ba8694776af8aa3dc73ec5702acda0ef2ce8bbd44954544d44
7
- data.tar.gz: 6a0dbcfada7f7d0f1fa1e8a64bc727b9e29d208838394a4bfe120b326b5d23c398688514c4352165c97eff85965be95997fe742b71be118f27b81375850df197
6
+ metadata.gz: 8965bb208ef3de7dd5dd638e4c0e7e75a52bcda0effe8640e33f4a4bc6fbebdaf9431b358361bc7b15e2b1785884577c94d4bd802e2d0ba16fdd19c4da08fd56
7
+ data.tar.gz: bb0874200c64eb473b20dde0b0afe04db15ae0ab228b3dbec460e5b93fbef69fa708786700c6c7c301795ddb22267cf8312307eeab4903ed7e7823bd4e1dfb13
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "nimiq"
4
+
5
+ require "irb"
6
+ IRB.start(__FILE__)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,397 @@
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 "sorbet-runtime"
18
+ require_relative "types"
19
+
20
+ module Api
21
+ attr_accessor :rpc
22
+ extend T::Sig
23
+
24
+ # accounts - Returns a list of addresses owned by client.
25
+ sig { returns(T::Array[Types::Account]) }
26
+
27
+ def accounts
28
+ result = @rpc.request("accounts")
29
+ return result
30
+ end
31
+
32
+ # block_number - Returns the height of most recent block.
33
+ sig { returns(Integer) }
34
+
35
+ def block_number
36
+ result = @rpc.request("blockNumber")
37
+ return result
38
+ end
39
+
40
+ # consensus - Returns information on the current consensus state.
41
+ sig { returns(String) }
42
+
43
+ def consensus
44
+ result = @rpc.request("consensus")
45
+ return result
46
+ end
47
+
48
+ # constant - Returns or overrides a constant value.
49
+ # When no parameter is given, it returns the value of the constant.
50
+ # When giving a value as parameter, it sets the constant to the given value.
51
+ # To reset the constant to the default value, the parameter "reset" should be used.
52
+ # - @param [String] name - The class and name of the constant. (format should be Class.CONSTANT)
53
+ # - @param [Integer] value - The new value of the constant or "reset". (optional)
54
+ sig { params(name: String, value: T.nilable(Integer)).returns(Integer) }
55
+
56
+ def constant(name, value = nil)
57
+ if value
58
+ result = @rpc.request("constant", name, value)
59
+ else
60
+ result = @rpc.request("constant", name)
61
+ end
62
+ return result
63
+ end
64
+
65
+ # create_account - Creates a new account and stores its private key in the client store.
66
+ sig { returns(Types::Wallet) }
67
+
68
+ def create_account
69
+ result = @rpc.request("createAccount")
70
+ return result
71
+ end
72
+
73
+ # create_raw_transaction - Creates and signs a transaction without sending it.
74
+ # The transaction can then be send via `send_raw_transaction` without accidentally replaying it.
75
+ # - @param [TransactionObject] transaction - The transaction object.
76
+ sig { params(transaction: Types::TransactionOutgoing).returns(String) }
77
+
78
+ def create_raw_transaction(transaction)
79
+ result = @rpc.request("createRawTransaction", transaction)
80
+ return result
81
+ end
82
+
83
+ # NEW
84
+ # get_raw_transaction_info - Checks signed_transaction raw information.
85
+ # - @param [String] signed_transaction - The hex encoded signed transaction.
86
+ sig { params(signed_transaction: String).returns(Object) }
87
+
88
+ def get_raw_transaction_info(signed_transaction)
89
+ result = @rpc.request("getRawTransactionInfo", signed_transaction)
90
+ return result
91
+ end
92
+
93
+ # get_account - Returns details for the account of given address.
94
+ # - @param [String] address - Address to get account details.
95
+ sig { params(address: String).returns(Types::Account) }
96
+
97
+ def get_account(address)
98
+ result = @rpc.request("getAccount", address)
99
+ return result
100
+ end
101
+
102
+ # get_balance - Returns the balance of the account of given address.
103
+ # - @param [String] address - Address to check for balance.
104
+ sig { params(address: String).returns(Integer) }
105
+
106
+ def get_balance(address)
107
+ result = @rpc.request("getBalance", address)
108
+ return result
109
+ end
110
+
111
+ # get_block_by_hash - Returns information about a block by hash.
112
+ # - @param [String] block_hash - Hash of the block to gather information on.
113
+ # - @param [Boolean] full_transactions (optional) - If true it returns the full transaction objects, if false only the hashes of the transactions. (default false)
114
+ sig { params(block_hash: String, full_transactions: T.nilable(T::Boolean)).returns(Types::Block) }
115
+
116
+ def get_block_by_hash(block_hash, full_transactions = nil)
117
+ if full_transactions
118
+ result = @rpc.request("getBlockByHash", block_hash, full_transactions)
119
+ else
120
+ result = @rpc.request("getBlockByHash", block_hash)
121
+ end
122
+ return result
123
+ end
124
+
125
+ # get_block_by_number - Returns information about a block by block number.
126
+ # - @param [Integer] block_number - The height of the block to gather information on.
127
+ # - @param [Boolean] full_transactions (optional) - If true it returns the full transaction objects, if false only the hashes of the transactions. (default false)
128
+ sig { params(block_number: Integer, full_transactions: T.nilable(T::Boolean)).returns(Types::Block) }
129
+
130
+ def get_block_by_number(block_number, full_transactions = nil)
131
+ if full_transactions
132
+ result = @rpc.request("getBlockByNumber", block_number, full_transactions)
133
+ else
134
+ result = @rpc.request("getBlockByNumber", block_number)
135
+ end
136
+ return result
137
+ end
138
+
139
+ # get_block_template - Returns a template to build the next block for mining.
140
+ # This will consider pool instructions when connected to a pool.
141
+ # - @param [String] address (optional) - Address to use as a miner for this block. This overrides the address provided during startup or from the pool.
142
+ # - @param [String] data_field (optional) - Hex-encoded value for the extra data field. This overrides the address provided during startup or from the pool.
143
+ sig { params(address: T.nilable(Integer), data_field: T.nilable(String)).returns(Types::BlockTemplate) }
144
+
145
+ def get_block_template(address = nil, data_field = nil)
146
+ result = @rpc.request("getBlockTemplate")
147
+ return result
148
+ end
149
+
150
+ # get_block_transaction_count_by_hash - Returns the number of transactions in a block from a block matching the given block hash.
151
+ # - @param [String] block_hash - Hash of the block.
152
+ sig { params(block_hash: String).returns(Integer) }
153
+
154
+ def get_block_transaction_count_by_hash(block_hash)
155
+ result = @rpc.request("getBlockTransactionCountByHash", block_hash)
156
+ return result
157
+ end
158
+
159
+ # get_block_transaction_count_by_number - Returns the number of transactions in a block matching the given block number.
160
+ # - @param [Integer] block_number - Height of the block.
161
+ sig { params(block_number: Integer).returns(Integer) }
162
+
163
+ def get_block_transaction_count_by_number(block_number)
164
+ result = @rpc.request("getBlockTransactionCountByNumber", block_number)
165
+ return result
166
+ end
167
+
168
+ # get_transaction_by_block_hash_and_index - Returns information about a transaction by block hash and transaction index position.
169
+ # - @param [String] block_hash - Hash of the block containing the transaction.
170
+ # - @param [Integer] transaction_index - Index of the transaction in the block.
171
+ sig { params(block_hash: String, transaction_index: Integer).returns(T.nilable(Types::Transaction)) }
172
+
173
+ def get_transaction_by_block_hash_and_index(block_hash, transaction_index)
174
+ result = @rpc.request("getTransactionByBlockHashAndIndex", block_hash, transaction_index)
175
+ return result
176
+ end
177
+
178
+ # get_transaction_by_block_number_and_index - Returns information about a transaction by block number and transaction index position.
179
+ # - @param [Integer] block_hash_index - Height of the block containing the transaction.
180
+ # - @param [Integer] transaction_index - Index of the transaction in the block.
181
+ sig { params(block_hash_index: Integer, transaction_index: Integer).returns(T.nilable(Types::Transaction)) }
182
+
183
+ def get_transaction_by_block_number_and_index(block_hash_index, transaction_index)
184
+ result = @rpc.request("getTransactionByBlockNumberAndIndex", block_hash_index, transaction_index)
185
+ return result
186
+ end
187
+
188
+ # get_transaction_by_hash - Returns the information about a transaction requested by transaction hash.
189
+ # - @param [String] transaction_hash - Hash of a transaction.
190
+ sig { params(transaction_hash: String).returns(T.nilable(Types::Transaction)) }
191
+
192
+ def get_transaction_by_hash(transaction_hash)
193
+ result = @rpc.request("getTransactionByHash", transaction_hash)
194
+ return result
195
+ end
196
+
197
+ # get_transaction_receipt - Returns the receipt of a transaction by transaction hash.
198
+ # Note That the receipt is not available for pending transactions.
199
+ # - @param [String] transaction_hash - Hash of a transaction.
200
+ sig { params(transaction_hash: String).returns(T.nilable(Types::TransactionReceipt)) }
201
+
202
+ def get_transaction_receipt(transaction_hash)
203
+ result = @rpc.request("getTransactionReceipt", transaction_hash)
204
+ return result
205
+ end
206
+
207
+ # get_transactions_by_address - Returns the latest transactions successfully performed by or for an address.
208
+ # - @param [String] address - Address of which transactions should be gathered.
209
+ # - @param [Integer] transactions_number (optional) - Number of transactions that shall be returned. (default 1000)
210
+ sig { params(address: String, transactions_number: T.nilable(Integer)).returns(T.nilable(T::Array[Types::Transaction])) }
211
+
212
+ def get_transactions_by_address(address, transactions_number = nil)
213
+ if transactions_number
214
+ result = @rpc.request("getTransactionsByAddress", address, transactions_number)
215
+ else
216
+ result = @rpc.request("getTransactionsByAddress", address)
217
+ end
218
+ return result
219
+ end
220
+
221
+ # get_work - Returns instructions to mine the next block. This will consider pool instructions when connected to a pool.
222
+ # - @param [String] address (optional) - Address to use as a miner for this block. This overrides the address provided during startup or from the pool.
223
+ # - @param [String] data_field (optional) - Hex-encoded value for the extra data field. This overrides the address provided during startup or from the pool.
224
+ sig { params(address: T.nilable(String), data_field: T.nilable(String)).returns(Types::MiningWork) }
225
+
226
+ def get_work(address = nil, data_field = nil)
227
+ result = @rpc.request("getWork", address, data_field)
228
+ return result
229
+ end
230
+
231
+ # hashrate - Returns the number of hashes per second that the node is mining with.
232
+ sig { returns(T.any(Integer, Float)) }
233
+
234
+ def hashrate
235
+ result = @rpc.request("hashrate")
236
+ return result
237
+ end
238
+
239
+ # log - Sets the log level of the node.
240
+ # - @param [String] tag - If the tag is '*' the log level will be set globally, otherwise the log level is applied only on this tag.
241
+ # - @param [String] log_level - Log levels valid options: `trace`, `verbose`, `debug`, `info`, `warn`, `error`, `assert`.
242
+ sig { params(tag: String, log_level: String).returns(T::Boolean) }
243
+
244
+ def log(tag, log_level)
245
+ result = @rpc.request("log", tag, log_level)
246
+ return result
247
+ end
248
+
249
+ # 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).
250
+ sig { returns(Object) }
251
+
252
+ def mempool
253
+ result = @rpc.request("mempool")
254
+ return result
255
+ end
256
+
257
+ # mempool_content - Returns transactions that are currently in the mempool.
258
+ sig { params(include_full_transactions: T.nilable(T::Boolean)).returns(T.any(T.nilable(T::Array[Types::Transaction]), T.nilable(T::Array))) }
259
+
260
+ def mempool_content(include_full_transactions = nil)
261
+ result = @rpc.request("mempoolContent")
262
+ return result
263
+ end
264
+
265
+ # miner_address - Returns the miner address.
266
+ sig { returns(String) }
267
+
268
+ def miner_address
269
+ result = @rpc.request("minerAddress")
270
+ return result
271
+ end
272
+
273
+ # miner_threads - Returns or sets the number of CPU threads for the miner.
274
+ # - @param [Integer] set_threads (optional) - The number of threads to allocate for mining.
275
+ sig { params(set_threads: T.nilable(Integer)).returns(Integer) }
276
+
277
+ def miner_threads(set_threads = nil)
278
+ if set_threads
279
+ result = @rpc.request("minerThreads", set_threads)
280
+ else
281
+ result = @rpc.request("minerThreads")
282
+ end
283
+ return result
284
+ end
285
+
286
+ # min_fee_per_byte - Returns or sets the minimum fee per byte.
287
+ # - @param [Integer] set_min_fee (optional) - The new minimum fee per byte.
288
+ sig { params(set_min_fee: T.nilable(Integer)).returns(Integer) }
289
+
290
+ def min_fee_per_byte(set_min_fee = nil)
291
+ if set_min_fee
292
+ result = @rpc.request("minFeePerByte", set_min_fee)
293
+ else
294
+ result = @rpc.request("minFeePerByte")
295
+ end
296
+ return result
297
+ end
298
+
299
+ # mining - Returns `true` if client is actively mining new blocks.
300
+ sig { returns(T::Boolean) }
301
+
302
+ def mining
303
+ result = @rpc.request("mining")
304
+ return result
305
+ end
306
+
307
+ # peer_count - Returns number of peers currently connected to the client.
308
+ sig { returns(Integer) }
309
+
310
+ def peer_count
311
+ result = @rpc.request("peerCount")
312
+ return result
313
+ end
314
+
315
+ # peer_list - Returns list of peers known to the client.
316
+ sig { returns(T::Array[Types::Peer]) }
317
+
318
+ def peer_list
319
+ result = @rpc.request("peerList")
320
+ return result
321
+ end
322
+
323
+ # peer_state - Returns the state of the peer.
324
+ # - @param [String] peer_address - The address of the peer.
325
+ sig { params(peer_address: String).returns(Types::Peer) }
326
+
327
+ def peer_state(peer_address)
328
+ result = @rpc.request("peerState", peer_address)
329
+ return result
330
+ end
331
+
332
+ # pool - Returns or sets the mining pool.
333
+ # 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.
334
+ # - @param [String/Boolean] pool_address (optional) - The mining pool connection string (url:port) or boolean to enable/disable pool mining.
335
+ sig { params(pool_address_or_action: T.nilable(T.any(String, T::Boolean))).returns(T.nilable(String)) }
336
+
337
+ def pool(pool_address_or_action = nil)
338
+ if pool_address_or_action
339
+ result = @rpc.request("pool", pool_address_or_action)
340
+ else
341
+ result = @rpc.request("pool")
342
+ end
343
+ return result
344
+ end
345
+
346
+ # pool_confirmed_balance - Returns the confirmed mining pool balance.
347
+ sig { returns(Integer) }
348
+
349
+ def pool_confirmed_balance
350
+ result = @rpc.request("poolConfirmedBalance")
351
+ return result
352
+ end
353
+
354
+ # pool_connection_state - Returns the connection state to mining pool.
355
+ sig { returns(Integer) }
356
+
357
+ def pool_connection_state
358
+ result = @rpc.request("poolConnectionState")
359
+ return result
360
+ end
361
+
362
+ # send_raw_transaction - Sends a signed message call transaction or a contract creation, if the data field contains code.
363
+ # - @param [String] signed_transaction - The hex encoded signed transaction.
364
+ sig { params(signed_transaction: String).returns(Object) }
365
+
366
+ def send_raw_transaction(signed_transaction)
367
+ result = @rpc.request("sendRawTransaction", signed_transaction)
368
+ return result
369
+ end
370
+
371
+ # send_transaction - Creates new message call transaction or a contract creation, if the data field contains code.
372
+ # - @param [TransactionObject] transaction - The transaction object.
373
+ sig { params(transaction: Types::TransactionOutgoing).returns(String) }
374
+
375
+ def send_transaction(transaction)
376
+ result = @rpc.request("sendTransaction", transaction)
377
+ return result
378
+ end
379
+
380
+ # submit_block - Submits a block to the node. When the block is valid, the node will forward it to other nodes in the network.
381
+ # When submitting work from getWork, remember to include the suffix.
382
+ # - @param [String] block - Hex-encoded full block (including header, interlink and body).
383
+ sig { params(block: String) }
384
+
385
+ def submit_block(block)
386
+ result = @rpc.request("submitBlock", block)
387
+ return result
388
+ end
389
+
390
+ # syncing - Returns an object with data about the sync status or `false`.
391
+ sig { returns(T.any(Object, T::Boolean)) }
392
+
393
+ def syncing
394
+ result = @rpc.request("syncing")
395
+ return result
396
+ end
397
+ end
@@ -0,0 +1,3 @@
1
+ module Nimiq # :nodoc: all
2
+ VERSION = "1.2.2"
3
+ end
@@ -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
@@ -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 "rpc"
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
@@ -0,0 +1,110 @@
1
+ require "sorbet-runtime"
2
+
3
+ module Types
4
+ extend T::Sig
5
+
6
+ Block = {
7
+ number: Integer,
8
+ hash: String,
9
+ pow: String,
10
+ parentHash: String,
11
+ nonce: Integer,
12
+ bodyHash: String,
13
+ accountsHash: String,
14
+ miner: String,
15
+ minerAddress: String,
16
+ difficulty: String,
17
+ extraData: String,
18
+ size: Integer,
19
+ timestamp: Integer,
20
+ transactions: Array,
21
+ confirmations: T.nilable(Integer),
22
+ }
23
+
24
+ BlockTemplate = {
25
+ header: {
26
+ version: Integer,
27
+ prevHash: String,
28
+ interlinkHash: String,
29
+ accountsHash: String,
30
+ nBits: Integer,
31
+ height: Integer,
32
+ },
33
+ interlink: String,
34
+ body: {
35
+ hash: String,
36
+ minerAddr: String,
37
+ extraData: String,
38
+ transactions: T::Array[String],
39
+ prunedAccounts: T::Array[String],
40
+ merkleHashes: T::Array[String],
41
+ },
42
+ target: Integer,
43
+ }
44
+
45
+ Wallet = {
46
+ id: String,
47
+ address: String,
48
+ publicKey: String,
49
+ }
50
+
51
+ Account = {
52
+ id: String,
53
+ address: String,
54
+ balance: Integer,
55
+ type: Integer,
56
+ }
57
+
58
+ Transaction = {
59
+ hash: String,
60
+ blockHash: T.nilable(String),
61
+ blockNumber: T.nilable(Integer),
62
+ timestamp: T.nilable(Integer),
63
+ confirmations: T.nilable(Integer),
64
+ transactionIndex: T.nilable(Integer),
65
+ from: String,
66
+ fromAddress: String,
67
+ to: String,
68
+ toAddress: String,
69
+ value: Integer,
70
+ fee: Integer,
71
+ data: T.nilable(String),
72
+ flags: Integer,
73
+ }
74
+
75
+ TransactionOutgoing = {
76
+ from: String,
77
+ to: String,
78
+ value: Integer,
79
+ fee: Integer,
80
+ }
81
+
82
+ TransactionReceipt = {
83
+ transactionHash: String,
84
+ transactionIndex: Integer,
85
+ blockNumber: Integer,
86
+ blockHash: String,
87
+ confirmations: Integer,
88
+ timestamp: Integer,
89
+ }
90
+
91
+ MiningWork = {
92
+ data: String,
93
+ suffix: String,
94
+ target: Integer,
95
+ algorithm: String,
96
+ }
97
+
98
+ Peer = {
99
+ id: String,
100
+ address: String,
101
+ addressState: Integer,
102
+ connectionState: T.nilable(Integer),
103
+ version: T.nilable(Integer),
104
+ timeOffset: T.nilable(Integer),
105
+ headHash: T.nilable(String),
106
+ latency: T.nilable(Integer),
107
+ rx: T.nilable(Integer),
108
+ tx: T.nilable(Integer),
109
+ }
110
+ end
@@ -0,0 +1,304 @@
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
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
18
+ require "ruby-client"
19
+
20
+ describe "Nimiq", type: :request do
21
+
22
+ # Initialize
23
+ before(:all) do
24
+ options = {
25
+ # host: "https://rpc.nimiq.observer/",
26
+ # port: 433,
27
+
28
+ host: "localhost",
29
+ port: 8005,
30
+ user: "user",
31
+ pass: "pass",
32
+ # dev: true,
33
+ }
34
+ @main_addr = "NQ70 46LN 1SKC KGFN VV8U G92N XC4X 9VFB SBVJ" # Node address
35
+ @seco_addr = "NQ27 B9ED 98G5 3VH7 FY8D BFP0 XNF4 BD8L TN4B" # Some address
36
+ @signed_transaction = "000000.......1A84FA23" # get this by running Makes @signed_transaction
37
+ @nimiq = Nimiq::Client.new(options)
38
+ end
39
+
40
+ # Test suite for client
41
+ describe "RPC Client" do
42
+
43
+ # Test suite for client version
44
+ it "Must have a version." do
45
+ expect(::Nimiq::VERSION).not_to eq(nil)
46
+ end
47
+
48
+ # Test suite for client connection
49
+ it "Must be able to connect to the Nimiq node." do
50
+ expect(@nimiq.ping).to eq(200)
51
+ end
52
+
53
+ # Test suite for client to send raw requests
54
+ it "Must be able to send raw requests." do
55
+ expect(@nimiq.request("consensus")).to be_a_kind_of(String)
56
+ end
57
+ end
58
+
59
+ # Test suite for API
60
+ describe "Api" do
61
+
62
+ # Test suite for client to retrieve the addresses owned by client.
63
+ it "Must be able to retrieve addresses owned by client." do
64
+ expect(@nimiq.accounts()).to be_a_kind_of(Object)
65
+ end
66
+
67
+ # Test suite for client to retrieve height of most recent block.
68
+ it "Must be able to retrieve height of most recent block." do
69
+ expect(@nimiq.block_number()).to be_a_kind_of(Object)
70
+ end
71
+
72
+ # Test suite for client to retrieve the consensus status.
73
+ it "Must be able to retrieve the consensus status." do
74
+ expect(@nimiq.consensus()).to be_a_kind_of(String)
75
+ end
76
+
77
+ # Test suite for client to override a constant value.
78
+ it "Must be able to override a constant value." do
79
+ constant_value = 10
80
+ expect(@nimiq.constant("BaseConsensus.MAX_ATTEMPTS_TO_FETCH", constant_value)).to eq(constant_value)
81
+ end
82
+
83
+ # Test suite for client to retrieve a constant value.
84
+ it "Must be able to retrieve a constant value." do
85
+ expect(@nimiq.constant("BaseConsensus.MAX_ATTEMPTS_TO_FETCH")).to eq(10)
86
+ end
87
+
88
+ # Test suite for client to create a new account.
89
+ it "Must be able to create a new account." do
90
+ expect(@nimiq.create_account()).to be_a_kind_of(Object)
91
+ end
92
+
93
+ # Test suite for client to retrieve information related to an account.
94
+ it "Must be able to retrieve information related to an account." do
95
+ expect(@nimiq.get_account(@seco_addr)).to be_a_kind_of(Object)
96
+ end
97
+
98
+ # Test suite for client to retrieve balance of account.
99
+ it "Must be able to retrieve balance of account." do
100
+ expect(@nimiq.get_balance(@main_addr)).to be_a_kind_of(Integer)
101
+ end
102
+
103
+ # Test suite for client to retrieve block by hash.
104
+ it "Must be able to retrieve block by hash." do
105
+ expect(@nimiq.get_block_by_hash("14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87", false)).to be_a_kind_of(Object)
106
+ end
107
+
108
+ # Test suite for client to retrieve block by hash, full transactions included.
109
+ it "Must be able to retrieve block by hash, full transactions included." do
110
+ expect(@nimiq.get_block_by_hash("14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87", true)).to be_a_kind_of(Object)
111
+ end
112
+
113
+ # Test suite for client to retrieve block by number.
114
+ it "Must be able to retrieve block by number." do
115
+ expect(@nimiq.get_block_by_number(76415, false)).to be_a_kind_of(Object)
116
+ end
117
+
118
+ # Test suite for client to retrieve block by number, full transactions included.
119
+ it "Must be able to retrieve block by number, full transactions included." do
120
+ expect(@nimiq.get_block_by_number(76415, true)).to be_a_kind_of(Object)
121
+ end
122
+
123
+ # Test suite for client to retrieve block template.
124
+ it "Must be able to retrieve block template." do
125
+ expect(@nimiq.get_block_template()).to be_a_kind_of(Object)
126
+ end
127
+
128
+ # Test suite for client to retrieve the number of transactions in a block by block hash.
129
+ it "Must be able to retrieve the number of transactions in a block by block hash." do
130
+ expect(@nimiq.get_block_transaction_count_by_hash("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f")).to be_a_kind_of(Integer)
131
+ end
132
+
133
+ # Test suite for client to retrieve the number of transactions in a block by block number.
134
+ it "Must be able to retrieve the number of transactions in a block by block number." do
135
+ expect(@nimiq.get_block_transaction_count_by_number(76415)).to be_a_kind_of(Integer)
136
+ end
137
+
138
+ # Test suite for client to retrieve information about a transaction by block hash and transaction index position.
139
+ it "Must be able to retrieve information about a transaction by block hash and transaction index position." do
140
+ expect(@nimiq.get_transaction_by_block_hash_and_index("dfe7d166f2c86bd10fa4b1f29cd06c13228f893167ce9826137c85758645572f", 20)).to be_a_kind_of(Object)
141
+ end
142
+
143
+ # Test suite for client to retrieve information about a transaction by block number and transaction index position.
144
+ it "Must be able to retrieve information about a transaction by block number and transaction index position." do
145
+ expect(@nimiq.get_transaction_by_block_number_and_index(76415, 20)).to be_a_kind_of(Object)
146
+ end
147
+
148
+ # Test suite for client to retrieve the information about a transaction requested by transaction hash.
149
+ it "Must be able to retrieve the information about a transaction requested by transaction hash." do
150
+ expect(@nimiq.get_transaction_by_hash("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554")).to be_a_kind_of(Object)
151
+ end
152
+
153
+ # Test suite for client to retrieve the receipt of a transaction by transaction hash.
154
+ it "Must be able to retrieve the receipt of a transaction by transaction hash." do
155
+ expect(@nimiq.get_transaction_receipt("465a63b73aa0b9b54b777be9a585ea00b367a17898ad520e1f22cb2c986ff554")).to be_a_kind_of(Object)
156
+ end
157
+
158
+ # Test suite for client to retrieve the latest transactions successfully performed by or for an address.
159
+ it "Must be able to retrieve the latest transactions successfully performed by or for an address." do
160
+ expect(@nimiq.get_transactions_by_address("NQ699A4AMB83HXDQ4J46BH5R4JFFQMA9C3GN", 100)).to be_a_kind_of(Object)
161
+ end
162
+
163
+ # Test suite for client to retrieve instructions to mine the next block.
164
+ it "Must be able to retrieve instructions to mine the next block." do
165
+ expect(@nimiq.get_work()).to be_a_kind_of(Object)
166
+ end
167
+
168
+ # Test suite for client to retrieve the number of hashes per second that the node is mining with.
169
+ it "Must be able to retrieve the number of hashes per second that the node is mining with." do
170
+ expect(@nimiq.hashrate()).to be_a_kind_of(Integer)
171
+ end
172
+
173
+ # Test suite for client to set the log level of the node.
174
+ it "Must be able to set the log level of the node." do
175
+ expect(@nimiq.log("BaseConsensus", "debug")).to be(true).or be(false)
176
+ end
177
+
178
+ # Test suite for client to retrieve information on the current mempool situation.
179
+ it "Must be able to retrieve information on the current mempool situation." do
180
+ expect(@nimiq.mempool()).to be_a_kind_of(Object)
181
+ end
182
+
183
+ # Test suite for client to retrieve transactions that are currently in the mempool.
184
+ it "Must be able to retrieve transactions that are currently in the mempool." do
185
+ expect(@nimiq.mempool_content()).to be_a_kind_of(Object)
186
+ end
187
+
188
+ # Test suite for client to retrieve miner address.
189
+ it "Must be able to retrieve miner address." do
190
+ expect(@nimiq.miner_address()).to be_a_kind_of(String)
191
+ end
192
+
193
+ # Test suite for client to set the number of CPU threads for the miner.
194
+ it "Must be able to set the number of CPU threads for the miner." do
195
+ expect(@nimiq.miner_threads(2)).to be_a_kind_of(Integer)
196
+ end
197
+
198
+ # Test suite for client to retrieve the number of CPU threads for the miner.
199
+ it "Must be able to retrieve the number of CPU threads for the miner." do
200
+ expect(@nimiq.miner_threads()).to be_a_kind_of(Integer)
201
+ end
202
+
203
+ # Test suite for client to set the minimum fee per byte.
204
+ it "Must be able to set the minimum fee per byte." do
205
+ expect(@nimiq.min_fee_per_byte(1)).to be_a_kind_of(Integer)
206
+ end
207
+
208
+ # Test suite for client to retrieve the minimum fee per byte.
209
+ it "Must be able to retrieve the minimum fee per byte." do
210
+ expect(@nimiq.min_fee_per_byte()).to be_a_kind_of(Integer)
211
+ end
212
+
213
+ # Test suite for client to retrieve if client is actively mining new blocks.
214
+ it "Must be able to retrieve if client is actively mining new blocks." do
215
+ expect(@nimiq.mining()).to be(true).or be(false)
216
+ end
217
+
218
+ # Test suite for client to retrieve the number of peers currently connected to the client.
219
+ it "Must be able to retrieve the number of peers currently connected to the client." do
220
+ expect(@nimiq.peer_count()).to be_a_kind_of(Integer)
221
+ end
222
+
223
+ # Test suite for client to retrieve the list of peers known to the client.
224
+ it "Must be able to retrieve the list of peers known to the client." do
225
+ expect(@nimiq.peer_list()).to be_a_kind_of(Object)
226
+ end
227
+
228
+ # Test suite for client to retrieve the state of the peer.
229
+ it "Must be able to retrieve the state of the peer." do
230
+ expect(@nimiq.peer_state("wss://seed-1.nimiq.com:8443/dfd55fe967d6c0ca707d3a73ec31e4ac")).to be_a_kind_of(Object)
231
+ end
232
+
233
+ # Test suite for client to set the mining pool.
234
+ it "Must be able to set the mining pool." do
235
+ expect(@nimiq.pool("eu.nimpool.io:8444")).to be_a_kind_of(String).or be(nil)
236
+ end
237
+
238
+ # Test suite for client to retrieve the mining pool.
239
+ it "Must be able to retrieve the mining pool." do
240
+ expect(@nimiq.pool()).to be_a_kind_of(String).or be(nil)
241
+ end
242
+
243
+ # Test suite for client to retrieve the confirmed mining pool balance.
244
+ it "Must be able to retrieve the confirmed mining pool balance." do
245
+ expect(@nimiq.pool_confirmed_balance()).to be_a_kind_of(Integer)
246
+ end
247
+
248
+ # Test suite for client to retrieve the connection state to mining pool.
249
+ it "Must be able to retrieve the connection state to mining pool." do
250
+ expect(@nimiq.pool_connection_state()).to be_a_kind_of(Integer)
251
+ end
252
+
253
+ # Makes @signed_transaction
254
+ # Test suite for client to create a transaction without sending it.
255
+ it "Must be able to create a transaction without sending it." do
256
+ transaction = {
257
+ "from": @main_addr,
258
+ "to": @seco_addr,
259
+ "value": 100,
260
+ "fee": 0,
261
+ }
262
+ @signed_transaction = @nimiq.create_raw_transaction(transaction)
263
+ expect(@signed_transaction).to be_a_kind_of(Object)
264
+ end
265
+
266
+ # NEW
267
+ # Test suite for client to retrieve a transaction information.
268
+ it "Must be able to retrieve a transaction information." do
269
+ # expect(@nimiq.get_raw_transaction_info(@signed_transaction)).to be_a_kind_of(Object)
270
+ expect(@nimiq.get_raw_transaction_info("009de5aeefe9fa3b4017cbf460e863831808d121b614ae034337a69cdabbeeb4185a5cd4a2051f6277fd0d5bee0f59e45b514dd88b000000000000006400000000000000000007f8642afa9861e3dd9c708318aba517f0d40882af99579841cc78afd395927de3913b88c87990659a6c55b2ee28c073559520fe685c051f2daa7cc63ffb9caa9ac9e20f")).to be_a_kind_of(Object)
271
+ end
272
+
273
+ # Test suite for client to retrieve an object with data about the sync status.
274
+ it "Must be able to retrieve an object with data about the sync status." do
275
+ expect(@nimiq.syncing()[:startingBlock]).to be > 0
276
+ end
277
+
278
+ # # Must have at least some NIM to be able to send it to another address
279
+ # # Test suite for client to send a signed message call transaction or a contract creation, if the data field contains code.
280
+ # it "Must be able to send a signed message call transaction or a contract creation, if the data field contains code." do
281
+ # expect(@nimiq.send_raw_transaction(@signed_transaction)).to be_a_kind_of(Object)
282
+ # end
283
+
284
+ # # Must have at least some NIM to be able to send it to another address
285
+ # # Test suite for client to create new message call transaction or a contract creation, if the data field contains code.
286
+ # it "Must be able to create new message call transaction or a contract creation, if the data field contains code." do
287
+ # transaction = {
288
+ # "from": @main_addr,
289
+ # "to": @seco_addr,
290
+ # "value": 100,
291
+ # "fee": 0,
292
+ # }
293
+ # expect(@nimiq.send_transaction(transaction)).to be_a_kind_of(Object)
294
+ # end
295
+
296
+ # # Must have a valid generated block for the blockchain to accept, returns nothing if block is valid
297
+ # # Test suite for client to submit a block to the node.
298
+ # it "Must be able to submit a block to the node." do
299
+ # block = "14c91f6d6f3a0b62271e546bb09461231ab7e4d1ddc2c3e1b93de52d48a1da87"
300
+ # expect(@nimiq.submit_block(block)).to be("")
301
+ # end
302
+
303
+ end
304
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.4
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jxdv
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-20 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -24,13 +24,21 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.15'
27
- description:
27
+ description: Ruby implementation of the Nimiq RPC client specification.
28
28
  email:
29
29
  - root@localhost
30
30
  executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
- files: []
33
+ files:
34
+ - bin/console
35
+ - bin/setup
36
+ - lib/api.rb
37
+ - lib/nimiq/version.rb
38
+ - lib/rpc.rb
39
+ - lib/ruby-client.rb
40
+ - lib/types.rb
41
+ - spec/client_spec.rb
34
42
  homepage: https://github.com/jxdv/ruby-client
35
43
  licenses:
36
44
  - Apache-2.0
@@ -56,5 +64,5 @@ requirements: []
56
64
  rubygems_version: 3.1.4
57
65
  signing_key:
58
66
  specification_version: 4
59
- summary: Ruby implementation of the Nimiq RPC client specifications.
67
+ summary: Ruby implementation of the Nimiq RPC client specification.
60
68
  test_files: []