throttled_json_rpc_client 0.1.0 → 0.1.1

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: 3d75ceb289344a401badd6873381a592e0fa445a4b65baf0ca369d53bf3fd6ed
4
- data.tar.gz: 66eafc5b5be6cb4a95080f6050cc7cc9baa21e48cc3db15d4fae8d3fc942ed59
3
+ metadata.gz: 508629fc2c697fd81f0456ef75a786e1f25dcf2b75c83e7b0a43e520f8f744d4
4
+ data.tar.gz: 322e0e8a61d8e210c89fd1f36c069d956e03a7e93f329896e7791c58cef08c53
5
5
  SHA512:
6
- metadata.gz: 2c72d92f7ffca6c3f627e356d6a52a7a857e5a063a84182ce222bc25ad2c11d31ba3e138041648ebd30a2dc6074f12d49869f3da103915b040c3825b5b58fcf1
7
- data.tar.gz: cfed9cb9aa7d1a41f36e373c4cd7744a3fb2e7d0f88c830bf64050ad8e40f4540602c30efe05fdd9e30dc64a49a89154fcfc815371c7a8cf127b47beb2c78433
6
+ metadata.gz: 4992ab8bad7c830d766edc3e65e83716e9723b563f49084bcbfc4ada67fe9dab3772f0daf188a5c5564daedcd2a17d5e886c54048d6dcda0b4928bb80929c790
7
+ data.tar.gz: 72434e5bdc61bba92536af24d1475722b807e7f4c1acfd36edbad43921368d3127859f270ee8265a5026d101246f138cad1920f21eefdfa617927d019b86413c
data/README.md CHANGED
@@ -41,6 +41,7 @@ threads = []
41
41
  end
42
42
  threads.map(&:join)
43
43
  ```
44
+ see [example](./example.rb)
44
45
 
45
46
  ## Development
46
47
 
data/example.rb CHANGED
@@ -1,8 +1,11 @@
1
+ require "logger"
2
+
1
3
  rpc_url = "https://1rpc.io/eth"
2
4
 
3
5
  eth = ThrottledJsonRpcClient::Eth.new(
4
6
  rpc_url,
5
- redis_urls: ["redis://redis:6379/2"]
7
+ redis_urls: ["redis://redis:6379/2"],
8
+ logger: Logger.new($stdout, level: :debug)
6
9
  )
7
10
 
8
11
  threads = []
@@ -3,18 +3,25 @@ module JsonRpcClient
3
3
  class JSONRpcError < StandardError; end
4
4
 
5
5
  class << self
6
- def request(url, method, params)
6
+ def request(
7
+ url, method, params,
8
+ logger = Logger.new($stdout, level: :info)
9
+ )
10
+ logger.debug "rpc url: #{url}"
11
+
7
12
  uri = URI.parse(url)
8
13
  http = Net::HTTP.new(uri.host, uri.port)
9
14
  http.use_ssl = uri.scheme == "https"
10
15
 
11
16
  request = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
12
- request.body = { jsonrpc: "2.0", method: method, params: params, id: 1 }.to_json
17
+ request.body = { jsonrpc: "2.0", method: method, params: params, id: Time.now.to_i }.to_json
18
+ logger.debug "req body: #{request.body}"
13
19
 
14
20
  # https://docs.ruby-lang.org/en/master/Net/HTTPResponse.html
15
21
  response = http.request(request)
16
22
  raise HttpError, response unless response.is_a?(Net::HTTPOK)
17
23
 
24
+ logger.debug "res body: #{response.body}"
18
25
  body = JSON.parse(response.body)
19
26
  raise JSONRpcError, body["error"] if body["error"]
20
27
 
@@ -25,21 +32,22 @@ module JsonRpcClient
25
32
  class Eth
26
33
  attr_reader :url
27
34
 
28
- def initialize(url)
35
+ def initialize(url, logger: Logger.new($stdout, level: :info))
29
36
  @url = url
37
+ @logger = logger
30
38
  end
31
39
 
32
40
  def get_block_by_bumber(block_number_or_block_tag, transaction_detail_flag = false)
33
41
  rpc_method = "eth_getBlockByNumber"
34
42
  params = [block_number_or_block_tag, transaction_detail_flag]
35
- JsonRpcClient.request(url, rpc_method, params)
43
+ JsonRpcClient.request(url, rpc_method, params, @logger)
36
44
  end
37
45
 
38
46
  # == get_block_by_bumber('latest'])['number']
39
47
  def block_number
40
48
  rpc_method = "eth_blockNumber"
41
49
  params = []
42
- JsonRpcClient.request(url, rpc_method, params)
50
+ JsonRpcClient.request(url, rpc_method, params, @logger)
43
51
  end
44
52
 
45
53
  #############################
@@ -55,7 +63,7 @@ module JsonRpcClient
55
63
  words = method.to_s.split("_")
56
64
  rpc_method = "eth_#{words[0]}#{words[1..].collect(&:capitalize).join}"
57
65
  params = args
58
- JsonRpcClient.request(url, rpc_method, params)
66
+ JsonRpcClient.request(url, rpc_method, params, @logger)
59
67
  end
60
68
  end
61
69
  end
@@ -3,7 +3,11 @@ require "delegate"
3
3
  module ThrottledJsonRpcClient
4
4
  class Eth < SimpleDelegator
5
5
  # limit: #{rate} requests / #{interval} seconds
6
- def initialize(url, rate: 5, interval: 1, redis_urls: ["redis://localhost:6379/2"])
6
+ def initialize(
7
+ url,
8
+ rate: 5, interval: 1, redis_urls: ["redis://localhost:6379/2"],
9
+ logger: Logger.new($stdout, level: :info)
10
+ )
7
11
  @queue = DistributedRateQueue.new(
8
12
  redis_urls: redis_urls,
9
13
  key: "key:#{url}",
@@ -11,7 +15,7 @@ module ThrottledJsonRpcClient
11
15
  interval: interval
12
16
  )
13
17
 
14
- super(JsonRpcClient::Eth.new(url))
18
+ super(JsonRpcClient::Eth.new(url, logger: logger))
15
19
  end
16
20
 
17
21
  def method_missing(*args, **kwargs, &block)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ThrottledJsonRpcClient
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: throttled_json_rpc_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-18 00:00:00.000000000 Z
11
+ date: 2024-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redlock