scale_rb 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/.rubocop.yml +0 -0
- data/.travis.yml +0 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/Gemfile +0 -0
- data/Gemfile.lock +2 -2
- data/LICENSE.txt +0 -0
- data/README.md +3 -1
- data/Rakefile +0 -0
- data/examples/http_client_1.rb +0 -0
- data/examples/http_client_2.rb +8 -2
- data/examples/ws_client_1.rb +3 -3
- data/examples/ws_client_2.rb +8 -3
- data/examples/ws_client_3.rb +3 -2
- data/lib/address.rb +0 -0
- data/lib/client/client_ext.rb +1 -1
- data/lib/client/http_client.rb +6 -5
- data/lib/client/ws_client.rb +41 -90
- data/lib/codec.rb +0 -0
- data/lib/hasher.rb +0 -0
- data/lib/metadata/metadata.rb +0 -0
- data/lib/metadata/metadata_v10.rb +0 -0
- data/lib/metadata/metadata_v11.rb +0 -0
- data/lib/metadata/metadata_v12.rb +0 -0
- data/lib/metadata/metadata_v13.rb +0 -0
- data/lib/metadata/metadata_v14.rb +0 -0
- data/lib/metadata/metadata_v9.rb +0 -0
- data/lib/monkey_patching.rb +0 -0
- data/lib/portable_codec.rb +0 -0
- data/lib/registry.rb +0 -0
- data/lib/scale_rb/version.rb +1 -1
- data/lib/scale_rb.rb +0 -0
- data/scale_rb.gemspec +0 -0
- data/tea.yaml +0 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6df0775bbed0fb5886ee7aba23efa8660dfde37a1f437d1d83958a6cc3f82c1f
|
4
|
+
data.tar.gz: a6ad3ab392fdaa8b9a3f431f98a6d1e7b099b5ce43cb966e5055267d5515322d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd7a0fbc2f605c3fef4a0b88e7c8475db9ac7e63223130536fddb786040fc4af79e9306f101697e2ae990f04d913755086df2965e8b922638c7c8498455c0fc8
|
7
|
+
data.tar.gz: 378a219709b0cce2ea277b3e717bd2b0653984519c7593e5525a841155a47b1478c6a10cebf29480cc862cac78b33287e34c1a9a7f433679a8d9e66cfe0ce356
|
data/.gitignore
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/.rubocop.yml
CHANGED
File without changes
|
data/.travis.yml
CHANGED
File without changes
|
data/CODE_OF_CONDUCT.md
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
scale_rb (0.3.
|
4
|
+
scale_rb (0.3.3)
|
5
5
|
async
|
6
6
|
async-http (~> 0.69.0)
|
7
7
|
async-websocket (~> 0.26.2)
|
@@ -50,7 +50,7 @@ GEM
|
|
50
50
|
io-stream (0.4.0)
|
51
51
|
json (2.7.2)
|
52
52
|
minitar (0.9)
|
53
|
-
protocol-hpack (1.
|
53
|
+
protocol-hpack (1.5.0)
|
54
54
|
protocol-http (0.28.1)
|
55
55
|
protocol-http1 (0.19.1)
|
56
56
|
protocol-http (~> 0.22)
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
data/Rakefile
CHANGED
File without changes
|
data/examples/http_client_1.rb
CHANGED
File without changes
|
data/examples/http_client_2.rb
CHANGED
@@ -3,7 +3,13 @@ require 'scale_rb'
|
|
3
3
|
ScaleRb.logger.level = Logger::DEBUG
|
4
4
|
|
5
5
|
client = ScaleRb::HttpClient.new('https://polkadot-rpc.dwellir.com')
|
6
|
-
block_number =
|
6
|
+
block_number = 21711742
|
7
7
|
block_hash = client.chain_getBlockHash(block_number)
|
8
|
-
|
8
|
+
metadata = client.get_metadata(block_hash)
|
9
|
+
|
10
|
+
storage_query = ScaleRb::WsClient::StorageQuery.new(
|
11
|
+
pallet_name: 'System',
|
12
|
+
storage_name: 'Events',
|
13
|
+
)
|
14
|
+
storage = client.get_storage(block_hash, storage_query, metadata)
|
9
15
|
puts "block #{block_number}(#{block_hash}) has #{storage.length} events"
|
data/examples/ws_client_1.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'scale_rb'
|
2
2
|
|
3
|
-
|
3
|
+
ScaleRb.logger.level = Logger::DEBUG
|
4
4
|
|
5
5
|
ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
6
6
|
block_hash = client.chain_getBlockHash(21585684)
|
7
7
|
runtime_version = client.state_getRuntimeVersion(block_hash)
|
8
|
-
puts runtime_version[
|
9
|
-
puts runtime_version[
|
8
|
+
puts runtime_version[:specName]
|
9
|
+
puts runtime_version[:specVersion]
|
10
10
|
end
|
data/examples/ws_client_2.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
require 'scale_rb'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
# You can have multiple subscriptions at the same time
|
5
4
|
ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
6
5
|
client.chain_subscribeNewHead do |head|
|
7
|
-
puts "Received new head at height: #{head[
|
6
|
+
puts "Received new head at height: #{head[:number].to_i(16)}"
|
7
|
+
end
|
8
|
+
|
9
|
+
client.state_subscribeStorage do |storage|
|
10
|
+
block_hash = storage[:block]
|
11
|
+
changes = storage[:changes]
|
12
|
+
puts "Received #{changes.size} storage changes at block: #{block_hash}"
|
8
13
|
end
|
9
14
|
end
|
data/examples/ws_client_3.rb
CHANGED
@@ -2,6 +2,7 @@ require 'scale_rb'
|
|
2
2
|
|
3
3
|
# ScaleRb.logger.level = Logger::DEBUG
|
4
4
|
|
5
|
+
# Unsubscribe after receiving 5 new heads
|
5
6
|
ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
6
7
|
count = 0
|
7
8
|
|
@@ -9,12 +10,12 @@ ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
|
9
10
|
count = count + 1
|
10
11
|
|
11
12
|
if count < 5
|
12
|
-
block_number = head[
|
13
|
+
block_number = head[:number].to_i(16)
|
13
14
|
block_hash = client.chain_getBlockHash(block_number)
|
14
15
|
puts "Received new head at height: #{block_number}, block hash: #{block_hash}"
|
15
16
|
else
|
16
17
|
unsub_result = client.chain_unsubscribeNewHead(subscription_id)
|
17
|
-
puts "
|
18
|
+
puts "Unsubscribe result: #{unsub_result}"
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
data/lib/address.rb
CHANGED
File without changes
|
data/lib/client/client_ext.rb
CHANGED
@@ -42,7 +42,7 @@ module ScaleRb
|
|
42
42
|
def query_storage_at(block_hash, storage_keys, type_id, default, registry)
|
43
43
|
result = state_queryStorageAt(storage_keys, block_hash)
|
44
44
|
result.map do |item|
|
45
|
-
item[
|
45
|
+
item[:changes].map do |change|
|
46
46
|
storage_key = change[0]
|
47
47
|
data = change[1] || default
|
48
48
|
storage = data.nil? ? nil : PortableCodec.decode(type_id, data._to_bytes, registry)[0]
|
data/lib/client/http_client.rb
CHANGED
@@ -17,7 +17,7 @@ module ScaleRb
|
|
17
17
|
raise 'url format is not correct' unless url.match?(url_regex)
|
18
18
|
|
19
19
|
@uri = URI.parse(url)
|
20
|
-
@supported_methods = request('rpc_methods', [])[
|
20
|
+
@supported_methods = request('rpc_methods', [])[:methods]
|
21
21
|
end
|
22
22
|
|
23
23
|
def request(method, params = [])
|
@@ -37,11 +37,12 @@ module ScaleRb
|
|
37
37
|
response = http.request(request)
|
38
38
|
raise response unless response.is_a?(Net::HTTPOK)
|
39
39
|
|
40
|
-
|
40
|
+
# parse response, make key symbol
|
41
|
+
body = JSON.parse(response.body, symbolize_names: true)
|
41
42
|
ScaleRb.logger.debug "Response: #{body}"
|
42
|
-
raise body[
|
43
|
+
raise body[:error] if body[:error]
|
43
44
|
|
44
|
-
body[
|
45
|
+
body[:result]
|
45
46
|
end
|
46
47
|
|
47
48
|
def respond_to_missing?(*_args)
|
@@ -54,4 +55,4 @@ module ScaleRb
|
|
54
55
|
request(method.to_s, args)
|
55
56
|
end
|
56
57
|
end
|
57
|
-
end
|
58
|
+
end
|
data/lib/client/ws_client.rb
CHANGED
@@ -9,50 +9,34 @@ require_relative 'client_ext'
|
|
9
9
|
module ScaleRb
|
10
10
|
class WsClient
|
11
11
|
def self.start(url)
|
12
|
-
|
12
|
+
|
13
|
+
Sync do |task|
|
13
14
|
endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
|
14
|
-
client = WsClient.new
|
15
|
-
|
16
|
-
task.async do
|
17
|
-
Async::WebSocket::Client.connect(endpoint) do |connection|
|
18
|
-
Async do
|
19
|
-
while request = client.next_request
|
20
|
-
ScaleRb.logger.debug "Sending request: #{request.to_json}"
|
21
|
-
connection.write(request.to_json)
|
22
|
-
end
|
23
|
-
end
|
24
15
|
|
25
|
-
|
16
|
+
Async::WebSocket::Client.connect(endpoint) do |connection|
|
17
|
+
client = WsClient.new(connection)
|
18
|
+
|
19
|
+
recv_task = task.async do
|
26
20
|
while message = connection.read
|
27
|
-
data =
|
21
|
+
data = message.parse
|
28
22
|
ScaleRb.logger.debug "Received message: #{data}"
|
29
23
|
|
30
|
-
|
24
|
+
task.async do
|
31
25
|
client.handle_response(data)
|
32
|
-
rescue => e
|
33
|
-
ScaleRb.logger.error "#{e.class}: #{e.message}"
|
34
|
-
ScaleRb.logger.error e.backtrace.join("\n")
|
35
|
-
task.stop
|
36
26
|
end
|
37
27
|
end
|
38
|
-
rescue => e
|
39
|
-
ScaleRb.logger.error "#{e.class}: #{e.message}"
|
40
|
-
ScaleRb.logger.error e.backtrace.join("\n")
|
41
|
-
ensure
|
42
|
-
task.stop
|
43
28
|
end
|
44
|
-
end
|
45
29
|
|
46
|
-
|
47
|
-
client.supported_methods = client.rpc_methods()['methods']
|
30
|
+
client.supported_methods = client.rpc_methods()[:methods]
|
48
31
|
yield client
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
32
|
+
|
33
|
+
recv_task.wait
|
34
|
+
ensure
|
35
|
+
recv_task&.stop
|
53
36
|
end
|
54
|
-
end
|
55
|
-
|
37
|
+
end # Sync
|
38
|
+
|
39
|
+
end # start
|
56
40
|
end
|
57
41
|
end
|
58
42
|
|
@@ -61,8 +45,8 @@ module ScaleRb
|
|
61
45
|
include ClientExt
|
62
46
|
attr_accessor :supported_methods
|
63
47
|
|
64
|
-
def initialize
|
65
|
-
@
|
48
|
+
def initialize(connection)
|
49
|
+
@connection = connection
|
66
50
|
@response_handler = ResponseHandler.new
|
67
51
|
@subscription_handler = SubscriptionHandler.new
|
68
52
|
@request_id = 1
|
@@ -87,7 +71,7 @@ module ScaleRb
|
|
87
71
|
raise "A subscribe method needs a block" unless block_given?
|
88
72
|
|
89
73
|
subscribe(method, args) do |notification|
|
90
|
-
yield notification[
|
74
|
+
yield notification[:params][:result]
|
91
75
|
end
|
92
76
|
else
|
93
77
|
request(method, args)
|
@@ -111,14 +95,10 @@ module ScaleRb
|
|
111
95
|
end
|
112
96
|
end
|
113
97
|
|
114
|
-
def next_request
|
115
|
-
@queue.dequeue
|
116
|
-
end
|
117
|
-
|
118
98
|
def handle_response(response)
|
119
|
-
if response.key?(
|
99
|
+
if response.key?(:id)
|
120
100
|
@response_handler.handle(response)
|
121
|
-
elsif response.key?(
|
101
|
+
elsif response.key?(:method)
|
122
102
|
@subscription_handler.handle(response)
|
123
103
|
else
|
124
104
|
puts "Received an unknown message: #{response}"
|
@@ -131,53 +111,34 @@ module ScaleRb
|
|
131
111
|
response_future = Async::Notification.new
|
132
112
|
|
133
113
|
@response_handler.register(@request_id, proc { |response|
|
134
|
-
|
135
|
-
response_future.signal(response['result'])
|
114
|
+
response_future.signal(response[:result])
|
136
115
|
})
|
137
116
|
|
138
|
-
request =
|
139
|
-
|
117
|
+
request = { jsonrpc: '2.0', id: @request_id, method: method, params: params }
|
118
|
+
ScaleRb.logger.debug "Sending request: #{request}"
|
119
|
+
@connection.write(request.to_json)
|
140
120
|
|
141
121
|
@request_id += 1
|
142
|
-
|
143
122
|
response_future.wait
|
144
123
|
end
|
145
124
|
end
|
146
125
|
|
147
|
-
class JsonRpcRequest
|
148
|
-
attr_reader :id, :method, :params
|
149
|
-
|
150
|
-
def initialize(id, method, params = {})
|
151
|
-
@id = id
|
152
|
-
@method = method
|
153
|
-
@params = params
|
154
|
-
end
|
155
|
-
|
156
|
-
def to_json(*_args)
|
157
|
-
{ jsonrpc: '2.0', id: @id, method: @method, params: @params }.to_json
|
158
|
-
end
|
159
|
-
|
160
|
-
# def to_s
|
161
|
-
# to_json
|
162
|
-
# end
|
163
|
-
end
|
164
|
-
|
165
126
|
class ResponseHandler
|
166
127
|
def initialize
|
167
|
-
@
|
128
|
+
@callbacks = {}
|
168
129
|
end
|
169
130
|
|
170
|
-
#
|
171
|
-
def register(id,
|
172
|
-
@
|
131
|
+
# callback: a proc with response data as param
|
132
|
+
def register(id, callback)
|
133
|
+
@callbacks[id] = callback
|
173
134
|
end
|
174
135
|
|
175
136
|
def handle(response)
|
176
|
-
id = response[
|
177
|
-
if @
|
178
|
-
|
179
|
-
|
180
|
-
@
|
137
|
+
id = response[:id]
|
138
|
+
if @callbacks.key?(id)
|
139
|
+
callback = @callbacks[id]
|
140
|
+
callback.call(response)
|
141
|
+
@callbacks.delete(id)
|
181
142
|
else
|
182
143
|
ScaleRb.logger.debug "Received a message with unknown id: #{response}"
|
183
144
|
end
|
@@ -186,33 +147,23 @@ module ScaleRb
|
|
186
147
|
|
187
148
|
class SubscriptionHandler
|
188
149
|
def initialize
|
189
|
-
@
|
150
|
+
@callbacks = {}
|
190
151
|
end
|
191
152
|
|
192
|
-
def subscribe(subscription_id,
|
193
|
-
@
|
153
|
+
def subscribe(subscription_id, callback)
|
154
|
+
@callbacks[subscription_id] = callback
|
194
155
|
end
|
195
156
|
|
196
157
|
def unsubscribe(subscription_id)
|
197
|
-
@
|
158
|
+
@callbacks.delete(subscription_id)
|
198
159
|
end
|
199
160
|
|
200
161
|
def handle(notification)
|
201
|
-
subscription_id = notification.dig(
|
162
|
+
subscription_id = notification.dig(:params, :subscription)
|
202
163
|
return if subscription_id.nil?
|
203
164
|
|
204
|
-
if @
|
205
|
-
@
|
206
|
-
else
|
207
|
-
# the subscription_id may be not registered.
|
208
|
-
# in client.subscribe function,
|
209
|
-
# ...
|
210
|
-
# subscription_id = request(method, params)
|
211
|
-
# @subscription_handler.subscribe(subscription_id, block)
|
212
|
-
# ...
|
213
|
-
# the request(method, params) may be slow, so the subscription_id may be not registered when the first notification comes.
|
214
|
-
sleep 0.01
|
215
|
-
handle(notification)
|
165
|
+
if @callbacks.key?(subscription_id)
|
166
|
+
@callbacks[subscription_id].call(notification)
|
216
167
|
end
|
217
168
|
end
|
218
169
|
end
|
data/lib/codec.rb
CHANGED
File without changes
|
data/lib/hasher.rb
CHANGED
File without changes
|
data/lib/metadata/metadata.rb
CHANGED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
data/lib/metadata/metadata_v9.rb
CHANGED
File without changes
|
data/lib/monkey_patching.rb
CHANGED
File without changes
|
data/lib/portable_codec.rb
CHANGED
File without changes
|
data/lib/registry.rb
CHANGED
File without changes
|
data/lib/scale_rb/version.rb
CHANGED
data/lib/scale_rb.rb
CHANGED
File without changes
|
data/scale_rb.gemspec
CHANGED
File without changes
|
data/tea.yaml
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scale_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aki Wu
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: base58
|
@@ -149,7 +149,7 @@ licenses:
|
|
149
149
|
metadata:
|
150
150
|
bug_tracker_uri: https://github.com/wuminzhe/scale_rb/issues/
|
151
151
|
source_code_uri: https://github.com/wuminzhe/scale_rb.git
|
152
|
-
post_install_message:
|
152
|
+
post_install_message:
|
153
153
|
rdoc_options: []
|
154
154
|
require_paths:
|
155
155
|
- lib
|
@@ -164,8 +164,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
requirements: []
|
167
|
-
rubygems_version: 3.
|
168
|
-
signing_key:
|
167
|
+
rubygems_version: 3.4.19
|
168
|
+
signing_key:
|
169
169
|
specification_version: 4
|
170
170
|
summary: A Ruby SCALE Codec Library, and, Substrate RPC Client
|
171
171
|
test_files: []
|