radiator 0.4.4 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5999a627f1c05a48b41ec3b2cef269ce3a9d9e32f71cc9f33ea8afd18a0e6fc9
4
- data.tar.gz: e0121879ce1fea0eda06f3550988d6ca9393fdcc66a19f8ac10e26f44974fd48
3
+ metadata.gz: f53430692cf228456ddec8f91bcb964b65b78aeebfcd7fa1eaab6c26e871ad7f
4
+ data.tar.gz: 44e93244d8505fc8b82fee7157fc752a0f60fadf885bfcb204e64bb83bf74653
5
5
  SHA512:
6
- metadata.gz: 5499a1b95cabf4931380818280aee212d89d320b20dc98d79cdd943d4f8d05eb59565f1f85ad73fb5f2e585a053f2f55d41ae0c1ed69a1e6b54f14bddf02e1d3
7
- data.tar.gz: 6d8e26c93dcd7176c43f04f37e11821bbebfd13163509fb94a9c6f2f61712b6ea55413c81d57024d7c677e42155325bff42d5720681a42481509ef5b75efcf6a
6
+ metadata.gz: 93187ca8f3308436391d89193e0ae555b1e3f155e98a0981073a1d74c8801d16133f0baf7a63706ed70d1edbdbe67657348fd26a1e16c0dd6de651982bb60cd8
7
+ data.tar.gz: 07df4deef02419958b07c399a399f66d1a28c7c5d7516f9c62bc117363c2c0ccd72702f6587f870c83640a70f05cbc9aa7f667ca763db022c77449f5f2c0a837
data/README.md CHANGED
@@ -10,6 +10,13 @@
10
10
 
11
11
  Radiator is an API Client for interaction with the STEEM network using Ruby.
12
12
 
13
+ #### Changes in v0.4.5
14
+
15
+ * Added support to query and stream a Steem Smart Contract backed side-chains like Steem Engine.
16
+ * [Blockchain](https://www.rubydoc.info/gems/radiator/radiator/doc/Radiator/SSC/Blockchain.html)
17
+ * [Stream](https://www.rubydoc.info/gems/radiator/radiator/doc/Radiator/SSC/Stream.html)
18
+ * [Contracts](https://www.rubydoc.info/gems/radiator/radiator/doc/Radiator/SSC/Contracts.html)
19
+
13
20
  #### Changes in v0.4.0
14
21
 
15
22
  * Gem updates
@@ -181,6 +188,63 @@ end
181
188
  "steemzine"]
182
189
  ```
183
190
 
191
+ #### Side Chain Support
192
+
193
+ Steem Smart Contract side-chains are supported by Radiator. The default side-chain is Steem Engine.
194
+
195
+ This will fetch the latest block from the side-chain ...
196
+
197
+ ```ruby
198
+ rpc = Radiator::SSC::Blockchain.new
199
+ rpc.latest_block_info
200
+ ```
201
+
202
+ This will fetch block 1 ...
203
+
204
+ ```ruby
205
+ rpc.block_info(1)
206
+ ```
207
+
208
+ Or a specific transaction ...
209
+
210
+ ```ruby
211
+ rpc.transaction_info('9d288aab2eb66064dc0d4492cb281512386e2293')
212
+ ```
213
+
214
+ You can also do contract queries. This will look up the `tokens` contract:
215
+
216
+ ```ruby
217
+ rpc = Radiator::SSC::Contracts.new
218
+ rpc.contract('tokens')
219
+ ```
220
+
221
+ This will look up a specific record in a contract result ...
222
+
223
+ ```ruby
224
+ rpc.find_one(
225
+ contract: "tokens",
226
+ table: "balances",
227
+ query: {
228
+ symbol: "STINGY",
229
+ account: "inertia"
230
+ }
231
+ )
232
+ ```
233
+
234
+ Or get multiple results ...
235
+
236
+ ```ruby
237
+ rpc.find(
238
+ contract: "tokens",
239
+ table: "balances",
240
+ query: {
241
+ symbol: "STINGY"
242
+ }
243
+ )
244
+ ```
245
+
246
+
247
+
184
248
  #### Streaming
185
249
 
186
250
  Here's an example of how to use a streaming instance to listen for votes:
@@ -372,6 +436,26 @@ Example of the output:
372
436
  .
373
437
  ```
374
438
 
439
+ #### Side-chain Streaming
440
+
441
+ Streaming side-chain transactions are supported:
442
+
443
+ ```ruby
444
+ # Default side-chain is Steem Engine.
445
+ stream = Radiator::SSC::Stream.new
446
+ stream.transactions do |tx, trx_id|
447
+ puts "[#{trx_id}] #{tx.to_json}"
448
+ end
449
+ ```
450
+
451
+ Even whole side-chain blocks:
452
+
453
+ ```ruby
454
+ stream.blocks do |bk, num|
455
+ puts "[#{num}] #{bk.to_json}"
456
+ end
457
+ ```
458
+
375
459
  #### Transaction Signing
376
460
 
377
461
  Radiator supports transaction signing, so you can use it to vote:
@@ -40,6 +40,10 @@ module Radiator
40
40
  require 'radiator/mixins/acts_as_voter'
41
41
  require 'radiator/mixins/acts_as_wallet'
42
42
  require 'radiator/chain'
43
+ require 'radiator/ssc/base_steem_smart_contract_rpc'
44
+ require 'radiator/ssc/blockchain'
45
+ require 'radiator/ssc/stream'
46
+ require 'radiator/ssc/contracts'
43
47
  require 'steem' unless defined? Steem
44
48
  extend self
45
49
  end
@@ -0,0 +1,105 @@
1
+ module Radiator
2
+ module SSC
3
+ class BaseSteemSmartContractRPC
4
+ # @private
5
+ POST_HEADERS = {
6
+ 'Content-Type' => 'application/json',
7
+ 'User-Agent' => Radiator::AGENT_ID
8
+ }
9
+
10
+ def initialize(options = {})
11
+ @root_url = options[:root_url] || 'https://api.steem-engine.com/rpc'
12
+
13
+ @self_hashie_logger = false
14
+ @hashie_logger = if options[:hashie_logger].nil?
15
+ @self_hashie_logger = true
16
+ Logger.new(nil)
17
+ else
18
+ options[:hashie_logger]
19
+ end
20
+
21
+ unless @hashie_logger.respond_to? :warn
22
+ @hashie_logger = Logger.new(@hashie_logger)
23
+ end
24
+
25
+ @reuse_ssl_sessions = if options.keys.include? :reuse_ssl_sessions
26
+ options[:reuse_ssl_sessions]
27
+ else
28
+ true
29
+ end
30
+
31
+ if defined? Net::HTTP::Persistent::DEFAULT_POOL_SIZE
32
+ @pool_size = options[:pool_size] || Net::HTTP::Persistent::DEFAULT_POOL_SIZE
33
+ end
34
+
35
+ Hashie.logger = @hashie_logger
36
+ @uri = nil
37
+ @http_id = nil
38
+ @http = nil
39
+ @max_requests = options[:max_requests] || 30
40
+ end
41
+
42
+ # Stops the persistant http connections.
43
+ #
44
+ def shutdown
45
+ @uri = nil
46
+ @http_id = nil
47
+ @http = nil
48
+ end
49
+ protected
50
+ def rpc_id
51
+ @rpc_id ||= 0
52
+ @rpc_id = @rpc_id + 1
53
+ end
54
+
55
+ def uri
56
+ @uri ||= URI.parse(@url)
57
+ end
58
+
59
+ def http_id
60
+ @http_id ||= "radiator-#{Radiator::VERSION}-ssc-blockchain-#{SecureRandom.uuid}"
61
+ end
62
+
63
+ def http
64
+ @http ||= if defined? Net::HTTP::Persistent::DEFAULT_POOL_SIZE
65
+ Net::HTTP::Persistent.new(name: http_id, pool_size: @pool_size).tap do |http|
66
+ http.keep_alive = 30
67
+ http.idle_timeout = 10
68
+ http.max_requests = @max_requests
69
+ http.retry_change_requests = true
70
+ http.reuse_ssl_sessions = @reuse_ssl_sessions
71
+ end
72
+ else
73
+ # net-http-persistent < 3.0
74
+ Net::HTTP::Persistent.new(http_id) do |http|
75
+ http = Net::HTTP.new(uri.host, uri.port)
76
+ http.use_ssl = uri.scheme == 'https'
77
+ end
78
+ end
79
+ end
80
+
81
+ def post_request
82
+ Net::HTTP::Post.new uri.request_uri, POST_HEADERS
83
+ end
84
+
85
+ def request(options)
86
+ request = post_request
87
+ request.body = JSON[options.merge(jsonrpc: '2.0', id: rpc_id)]
88
+
89
+ response = case http
90
+ when Net::HTTP::Persistent then http.request(uri, request)
91
+ when Net::HTTP then http.request(request)
92
+ else; raise ApiError, "Unsuppored scheme: #{http.inspect}"
93
+ end
94
+
95
+ response = Hashie::Mash.new(JSON[response.body])
96
+
97
+ if !!(error = response.error)
98
+ raise ApiError, "Error #{error.code}: #{error.message}"
99
+ end
100
+
101
+ response.result
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,47 @@
1
+ module Radiator
2
+ module SSC
3
+ # The "blockchain" endpoint
4
+ #
5
+ # See: https://github.com/harpagon210/steemsmartcontracts/wiki/JSON-RPC-server#1-the-blockchain-endpoint-httplocalhost5000blockchain
6
+ class Blockchain < BaseSteemSmartContractRPC
7
+ # @param options [::Hash] The attributes
8
+ # @option options [String] :url Specify the full node end-point. Default: https://api.steem-engine.com/rpc/blockchain
9
+ def initialize(options = {})
10
+ super
11
+ @url = options[:url] || "#{@root_url}/blockchain"
12
+ end
13
+
14
+ # Example using the defaults, backed by Steem Engine:
15
+ #
16
+ # rpc = Radiator::SSC::Blockchain.new
17
+ # rpc.latest_block_info
18
+ #
19
+ # @return the latest block of the sidechain
20
+ def latest_block_info
21
+ request(method: 'getLatestBlockInfo')
22
+ end
23
+
24
+ # Example using the defaults, backed by Steem Engine:
25
+ #
26
+ # rpc = Radiator::SSC::Blockchain.new
27
+ # rpc.block_info(1)
28
+ #
29
+ # @param [Integer] block_num
30
+ # @return the block with the specified block number of the sidechain
31
+ def block_info(block_num)
32
+ request(method: 'getBlockInfo', params: {blockNumber: block_num})
33
+ end
34
+
35
+ # Example using the defaults, backed by Steem Engine:
36
+ #
37
+ # rpc = Radiator::SSC::Blockchain.new
38
+ # rpc.transaction_info('9d288aab2eb66064dc0d4492cb281512386e2293')
39
+ #
40
+ # @param [String] trx_id
41
+ # @return the specified transaction info of the sidechain
42
+ def transaction_info(trx_id)
43
+ request(method: 'getTransactionInfo', params: {txid: trx_id})
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,71 @@
1
+ module Radiator
2
+ module SSC
3
+ # The "contracts" endpoint
4
+ #
5
+ # See: https://github.com/harpagon210/steemsmartcontracts/wiki/JSON-RPC-server#3-the-contracts-endpoint-httplocalhost5000contracts
6
+ class Contracts < BaseSteemSmartContractRPC
7
+ # @param options [::Hash] The attributes
8
+ # @option options [String] :url Specify the full node end-point. Default: https://api.steem-engine.com/rpc/contracts
9
+ def initialize(options = {})
10
+ super
11
+ @url = options[:url] || "#{@root_url}/contracts"
12
+ end
13
+
14
+ # Example using the defaults, backed by Steem Engine:
15
+ #
16
+ # rpc = Radiator::SSC::Contracts.new
17
+ # rpc.contract('tokens')
18
+ #
19
+ # @param [String] name
20
+ # @return the contract specified from the database
21
+ def contract(name)
22
+ request(method: 'getContract', params: {name: name})
23
+ end
24
+
25
+ # Example using the defaults, backed by Steem Engine:
26
+ #
27
+ # rpc = Radiator::SSC::Contracts.new
28
+ # rpc.find_one(
29
+ # contract: "tokens",
30
+ # table: "balances",
31
+ # query: {
32
+ # symbol: "STINGY",
33
+ # account: "inertia"
34
+ # }
35
+ # )
36
+ #
37
+ # @param options [::Hash] The attributes
38
+ # @option options [String] :contract
39
+ # @option options [String] :table
40
+ # @option options [String] :query
41
+ # @return the object that matches the query from the table of the specified contract
42
+ def find_one(options = {})
43
+ request(method: 'findOne', params: options)
44
+ end
45
+
46
+ # Example using the defaults, backed by Steem Engine:
47
+ #
48
+ # rpc = Radiator::SSC::Contracts.new
49
+ # rpc.find(
50
+ # contract: "tokens",
51
+ # table: "balances",
52
+ # query: {
53
+ # symbol: "STINGY"
54
+ # }
55
+ # )
56
+ #
57
+ # @param options [::Hash] The attributes
58
+ # @option options [String] :contract
59
+ # @option options [String] :table
60
+ # @option options [String] :query
61
+ # @option options [Integer] :limit default: 1000
62
+ # @option options [Integer] :offset default: 0
63
+ # @option options [Boolean] :descending
64
+ # @option options [::Hash] indexes default: empty, an index is an object { index: string, descending: boolean }
65
+ # @return array of objects that match the query from the table of the specified contract
66
+ def find(options = {})
67
+ request(method: 'find', params: options)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,62 @@
1
+ module Radiator
2
+ module SSC
3
+ # Streams the "blockchain" endpoint.
4
+ #
5
+ # See: https://github.com/harpagon210/steemsmartcontracts/wiki/JSON-RPC-server#3-the-contracts-endpoint-httplocalhost5000contracts
6
+ class Stream < Blockchain
7
+ # Block production on the sidechain is no faster than 3 seconds, but can
8
+ # be indefinately longer than 3 seconds if there are no pending
9
+ # transactions.
10
+ # @private
11
+ MIN_BLOCK_PRODUCTION = 3.0
12
+
13
+ # @param options [::Hash] The attributes
14
+ # @option options [String] :url Specify the full node end-point. Default: https://api.steem-engine.com/rpc/blockchain
15
+ def initialize(options = {})
16
+ super
17
+ end
18
+
19
+ # Stream each block on the side-chain.
20
+ #
21
+ # stream = Radiator::SSC::Stream.new
22
+ # stream.blocks do |block|
23
+ # puts "Block: #{block}"
24
+ # end
25
+ #
26
+ # @param options [::Hash] The attributes
27
+ # @option options [Integer] :at_block_num start stream at this block number
28
+ def blocks(options = {}, &block)
29
+ at_block_num = options[:at_block_num] || latest_block_info.blockNumber
30
+
31
+ loop do
32
+ block = block_info(at_block_num)
33
+
34
+ if block.nil?
35
+ sleep MIN_BLOCK_PRODUCTION and next
36
+ end
37
+
38
+ at_block_num += 1
39
+
40
+ yield block, block.blockNumber
41
+ end
42
+ end
43
+
44
+ # Stream each transaction on the side-chain.
45
+ #
46
+ # stream = Radiator::SSC::Stream.new
47
+ # stream.transactions do |trx|
48
+ # puts "Transaction: #{trx}"
49
+ # end
50
+ #
51
+ # @param options [::Hash] The attributes
52
+ # @option options [Integer] :at_block_num start stream at this block number
53
+ def transactions(options = {}, &block)
54
+ blocks(options) do |block, block_num|
55
+ block.transactions.each do |transaction|
56
+ yield transaction, transaction.transactionId, block_num
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,4 +1,4 @@
1
1
  module Radiator
2
- VERSION = '0.4.4'
2
+ VERSION = '0.4.5'
3
3
  AGENT_ID = "radiator/#{VERSION}"
4
4
  end
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'yard', '~> 0.9.9'
29
29
  spec.add_development_dependency 'pry', '~> 0.11', '>= 0.11.3'
30
30
  spec.add_development_dependency 'rb-readline', '~> 0.5', '>= 0.5.5'
31
+ spec.add_development_dependency 'irb', '~> 1.0', '>= 1.0.0'
31
32
 
32
33
  # net-http-persistent has an open-ended dependency because radiator directly
33
34
  # supports net-http-persistent-3.0.0 as well as net-http-persistent-2.5.2.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Martin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2019-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -212,6 +212,26 @@ dependencies:
212
212
  - - ">="
213
213
  - !ruby/object:Gem::Version
214
214
  version: 0.5.5
215
+ - !ruby/object:Gem::Dependency
216
+ name: irb
217
+ requirement: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - "~>"
220
+ - !ruby/object:Gem::Version
221
+ version: '1.0'
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: 1.0.0
225
+ type: :development
226
+ prerelease: false
227
+ version_requirements: !ruby/object:Gem::Requirement
228
+ requirements:
229
+ - - "~>"
230
+ - !ruby/object:Gem::Version
231
+ version: '1.0'
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: 1.0.0
215
235
  - !ruby/object:Gem::Dependency
216
236
  name: net-http-persistent
217
237
  requirement: !ruby/object:Gem::Requirement
@@ -388,6 +408,10 @@ files:
388
408
  - lib/radiator/operation.rb
389
409
  - lib/radiator/operation_ids.rb
390
410
  - lib/radiator/operation_types.rb
411
+ - lib/radiator/ssc/base_steem_smart_contract_rpc.rb
412
+ - lib/radiator/ssc/blockchain.rb
413
+ - lib/radiator/ssc/contracts.rb
414
+ - lib/radiator/ssc/stream.rb
391
415
  - lib/radiator/stream.rb
392
416
  - lib/radiator/tag_api.rb
393
417
  - lib/radiator/transaction.rb