throttled_json_rpc_client 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +1 -0
- data/example.rb +4 -1
- data/lib/json_rpc_client/eth.rb +14 -6
- data/lib/throttled_json_rpc_client/eth.rb +6 -2
- data/lib/throttled_json_rpc_client/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 508629fc2c697fd81f0456ef75a786e1f25dcf2b75c83e7b0a43e520f8f744d4
|
4
|
+
data.tar.gz: 322e0e8a61d8e210c89fd1f36c069d956e03a7e93f329896e7791c58cef08c53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4992ab8bad7c830d766edc3e65e83716e9723b563f49084bcbfc4ada67fe9dab3772f0daf188a5c5564daedcd2a17d5e886c54048d6dcda0b4928bb80929c790
|
7
|
+
data.tar.gz: 72434e5bdc61bba92536af24d1475722b807e7f4c1acfd36edbad43921368d3127859f270ee8265a5026d101246f138cad1920f21eefdfa617927d019b86413c
|
data/README.md
CHANGED
data/example.rb
CHANGED
data/lib/json_rpc_client/eth.rb
CHANGED
@@ -3,18 +3,25 @@ module JsonRpcClient
|
|
3
3
|
class JSONRpcError < StandardError; end
|
4
4
|
|
5
5
|
class << self
|
6
|
-
def request(
|
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:
|
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(
|
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)
|
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.
|
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-
|
11
|
+
date: 2024-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redlock
|