scale_rb 0.3.5 → 0.4.0
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/examples/http_client_2.rb +15 -10
- data/examples/ws_client_4.rb +12 -34
- data/lib/client/client_ext.rb +10 -17
- data/lib/client/http_client.rb +1 -1
- data/lib/client/ws_client.rb +3 -3
- data/lib/codec.rb +2 -0
- data/lib/scale_rb/version.rb +1 -1
- data/lib/storage_helper.rb +4 -2
- 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: 51cde569bc10814d0163c4b972c1d926a5ef69a044d02e88085db5bfdd9119a0
|
4
|
+
data.tar.gz: 694a2356b2626d1491c2dbb5cca8207179e614a88579c99bbd43a312c2435f9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbe20c371a268df323e64fcd9b142a8d04b87dc00e7079e1fd5e0979cbd4311de10b20687928200c427c407d839ce6ba0c804d5e08c72001a05d16b25b6faee8
|
7
|
+
data.tar.gz: 7e36ec4ef1d6e0b5584464498836fa5f19f4f7d194856c15bd25165bc6dd7f9f01e97cf0c736bc734806be83a26cc70b1fcb3b5986f95ba8fde0361dc76227b5
|
data/examples/http_client_2.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
require 'scale_rb'
|
2
2
|
|
3
|
+
def fetch_some_storages(client, block_number)
|
4
|
+
start_time = Time.now
|
5
|
+
|
6
|
+
block_hash = client.chain_getBlockHash(block_number)
|
7
|
+
metadata = client.get_metadata(block_hash)
|
8
|
+
puts "event count: #{client.get_storage('System', 'EventCount', block_hash:, metadata:)}"
|
9
|
+
puts "treasury proposal #854: #{client.get_storage('Treasury', 'Proposals', [854], block_hash:, metadata:)}"
|
10
|
+
puts "all treasury proposals: #{client.get_storage('Treasury', 'Proposals', block_hash:, metadata:)}"
|
11
|
+
puts "child bounties: #{client.get_storage('ChildBounties', 'ChildBounties', [11, 1646], block_hash:, metadata:)}"
|
12
|
+
|
13
|
+
end_time = Time.now
|
14
|
+
puts "Time taken: #{end_time - start_time} seconds"
|
15
|
+
end
|
16
|
+
|
3
17
|
ScaleRb.logger.level = Logger::DEBUG
|
4
18
|
|
5
19
|
client = ScaleRb::HttpClient.new('https://polkadot-rpc.dwellir.com')
|
6
|
-
|
7
|
-
block_hash = client.chain_getBlockHash(block_number)
|
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)
|
15
|
-
puts "block #{block_number}(#{block_hash}) has #{storage.length} events"
|
20
|
+
fetch_some_storages(client, 21711742)
|
data/examples/ws_client_4.rb
CHANGED
@@ -1,43 +1,21 @@
|
|
1
1
|
require 'scale_rb'
|
2
2
|
|
3
|
-
|
3
|
+
def fetch_some_storages(client, block_number)
|
4
|
+
start_time = Time.now
|
4
5
|
|
5
|
-
ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
6
|
-
block_number = 21711742
|
7
6
|
block_hash = client.chain_getBlockHash(block_number)
|
8
7
|
metadata = client.get_metadata(block_hash)
|
8
|
+
puts "event count: #{client.get_storage('System', 'EventCount', block_hash:, metadata:)}"
|
9
|
+
puts "treasury proposal #854: #{client.get_storage('Treasury', 'Proposals', [854], block_hash:, metadata:)}"
|
10
|
+
puts "all treasury proposals: #{client.get_storage('Treasury', 'Proposals', block_hash:, metadata:)}"
|
11
|
+
puts "child bounties: #{client.get_storage('ChildBounties', 'ChildBounties', [11, 1646], block_hash:, metadata:)}"
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
)
|
14
|
-
puts "event count: #{client.get_storage(block_hash, storage_query, metadata)}"
|
15
|
-
# event count: 48
|
16
|
-
|
17
|
-
|
18
|
-
storage_query = ScaleRb::WsClient::StorageQuery.new(
|
19
|
-
pallet_name: 'Treasury',
|
20
|
-
storage_name: 'Proposals',
|
21
|
-
key_part1: 854,
|
22
|
-
)
|
23
|
-
puts "treasury proposal #854: #{client.get_storage(block_hash, storage_query, metadata)}"
|
24
|
-
# treasury proposal #854: {:proposer=>"0xb6f0f10eec993f3e6806eb6cc4d2f13d5f5a90a17b855a7bf9847a87e07ee322", :value=>82650000000000, :beneficiary=>"0xb6f0f10eec993f3e6806eb6cc4d2f13d5f5a90a17b855a7bf9847a87e07ee322", :bond=>0}
|
25
|
-
|
26
|
-
|
27
|
-
storage_query = ScaleRb::WsClient::StorageQuery.new(
|
28
|
-
pallet_name: 'Treasury',
|
29
|
-
storage_name: 'Proposals',
|
30
|
-
)
|
31
|
-
puts "all treasury proposals: #{client.get_storage(block_hash, storage_query, metadata)}"
|
32
|
-
# all treasury proposals: [{:storage_key=>"0x89d139e01a5eb2256f222e5fc5dbe6b388c2f7188c6fdd1dffae2fa0d171f4400c1910093df9204856030000", :storage=>{:proposer=>"0xb6f0f10eec993f3e6806eb6cc4d2f13d5f5a90a17b855a7bf9847a87e07ee322", :value=>82650000000000, :beneficiary=>"0xb6f0f10eec993f3e6806eb6cc4d2f13d5f5a90a17b855a7bf9847a87e07ee322", :bond=>0}}, ...]
|
13
|
+
end_time = Time.now
|
14
|
+
puts "Time taken: #{end_time - start_time} seconds"
|
15
|
+
end
|
33
16
|
|
17
|
+
ScaleRb.logger.level = Logger::DEBUG
|
34
18
|
|
35
|
-
|
36
|
-
|
37
|
-
storage_name: 'ChildBounties',
|
38
|
-
key_part1: 11,
|
39
|
-
key_part2: 1646
|
40
|
-
)
|
41
|
-
puts "child bounties: #{client.get_storage(block_hash, storage_query, metadata)}"
|
42
|
-
# child bounties: {:parent_bounty=>11, :value=>3791150000000, :fee=>0, :curator_deposit=>0, :status=>{:PendingPayout=>{:curator=>"0xb1725c0de514e0df808b19dbfca26672019ea5f9e2eb69c0055c7f1d01b4f18a", :beneficiary=>"0xb089dedc24a15308874dc862b035d74f2f7b45cad475d6121a2d944921bbe237", :unlock_at=>21703671}}}
|
19
|
+
ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
|
20
|
+
fetch_some_storages(client, 21711742)
|
43
21
|
end
|
data/lib/client/client_ext.rb
CHANGED
@@ -2,37 +2,31 @@ module ScaleRb
|
|
2
2
|
|
3
3
|
# This module is used to add extra methods to both the ScaleRb::WsClient ScaleRb::HttpClient
|
4
4
|
module ClientExt
|
5
|
-
StorageQuery = Struct.new(:pallet_name, :storage_name, :key_part1, :key_part2, keyword_init: true) do
|
6
|
-
def initialize(pallet_name:, storage_name:, key_part1: nil, key_part2: nil)
|
7
|
-
super
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
5
|
# get decoded metadata at block_hash
|
12
|
-
def get_metadata(block_hash)
|
6
|
+
def get_metadata(block_hash = nil)
|
7
|
+
block_hash ||= chain_getHead
|
13
8
|
metadata_hex = state_getMetadata(block_hash)
|
14
9
|
ScaleRb::Metadata.decode_metadata(metadata_hex.strip._to_bytes)
|
15
10
|
end
|
16
11
|
|
17
12
|
# Get decoded storage at block_hash
|
18
|
-
def get_storage(
|
13
|
+
def get_storage(pallet_name, storage_name, params = [], block_hash: nil, metadata: nil)
|
14
|
+
block_hash ||= chain_getHead
|
19
15
|
metadata ||= get_metadata(block_hash)
|
20
16
|
|
21
17
|
# storeage item
|
22
|
-
pallet_name = convert_to_camel_case
|
23
|
-
storage_name = convert_to_camel_case
|
18
|
+
pallet_name = convert_to_camel_case pallet_name
|
19
|
+
storage_name = convert_to_camel_case storage_name
|
24
20
|
|
25
21
|
# storage param
|
26
|
-
|
27
|
-
|
28
|
-
key = key.map { |part_of_key| c(part_of_key) }
|
29
|
-
ScaleRb.logger.debug "converted key: #{key}"
|
22
|
+
ScaleRb.logger.debug "#{pallet_name}.#{storage_name}(#{params.inspect})"
|
23
|
+
params = params.map { |param| c(param) }
|
30
24
|
|
31
25
|
get_storage2(
|
32
26
|
block_hash, # at
|
33
27
|
pallet_name,
|
34
28
|
storage_name,
|
35
|
-
|
29
|
+
params,
|
36
30
|
metadata
|
37
31
|
)
|
38
32
|
end
|
@@ -102,7 +96,7 @@ module ScaleRb
|
|
102
96
|
#
|
103
97
|
# key is for the param, value is for the return
|
104
98
|
def get_storage1(block_hash, pallet_name, item_name, key, value, registry)
|
105
|
-
ScaleRb
|
99
|
+
ScaleRb.logger.debug "#{pallet_name}.#{item_name}, key: #{key.inspect}, value: #{value}"
|
106
100
|
|
107
101
|
if key
|
108
102
|
if key[:value].nil? || key[:value].empty?
|
@@ -138,7 +132,6 @@ module ScaleRb
|
|
138
132
|
end
|
139
133
|
|
140
134
|
def get_storage2(block_hash, pallet_name, item_name, params, metadata)
|
141
|
-
ScaleRb.logger.debug "get_storage2: #{pallet_name}.#{item_name} params: #{params}"
|
142
135
|
raise 'Metadata should not be nil' if metadata.nil?
|
143
136
|
|
144
137
|
registry = Metadata.build_registry(metadata)
|
data/lib/client/http_client.rb
CHANGED
data/lib/client/ws_client.rb
CHANGED
@@ -22,7 +22,7 @@ module ScaleRb
|
|
22
22
|
data = parse_message(message)
|
23
23
|
next if data.nil?
|
24
24
|
|
25
|
-
ScaleRb.logger.debug "
|
25
|
+
ScaleRb.logger.debug "Response: #{data}"
|
26
26
|
Async do
|
27
27
|
client.handle_response(data)
|
28
28
|
end
|
@@ -71,7 +71,7 @@ module ScaleRb
|
|
71
71
|
|
72
72
|
def method_missing(method, *args)
|
73
73
|
method = method.to_s
|
74
|
-
ScaleRb.logger.debug "#{method}(#{args.join(', ')})"
|
74
|
+
# ScaleRb.logger.debug "#{method}(#{args.join(', ')})"
|
75
75
|
|
76
76
|
# why not check 'rpc_methods', because there is no @supported_methods when initializing
|
77
77
|
if method != 'rpc_methods' && !@supported_methods.include?(method)
|
@@ -142,7 +142,7 @@ module ScaleRb
|
|
142
142
|
})
|
143
143
|
|
144
144
|
request = { jsonrpc: '2.0', id: @request_id, method: method, params: params }
|
145
|
-
ScaleRb.logger.debug "
|
145
|
+
ScaleRb.logger.debug "Request: #{request}"
|
146
146
|
@connection.write(request.to_json)
|
147
147
|
|
148
148
|
@request_id += 1
|
data/lib/codec.rb
CHANGED
data/lib/scale_rb/version.rb
CHANGED
data/lib/storage_helper.rb
CHANGED
@@ -20,12 +20,14 @@ module ScaleRb
|
|
20
20
|
]
|
21
21
|
else
|
22
22
|
[
|
23
|
-
registry[key[:type]]._get(:def)._get(:tuple),
|
23
|
+
registry[key[:type]]._get(:def)._get(:tuple).first(key[:value].length),
|
24
24
|
key[:value],
|
25
|
-
key[:hashers]
|
25
|
+
key[:hashers].first(key[:value].length)
|
26
26
|
]
|
27
27
|
end
|
28
28
|
|
29
|
+
ScaleRb.logger.debug "encode_storage_key: key_values: #{key_values.inspect}, key_types: #{key_types.inspect}, key_hashers: #{key_hashers.inspect}"
|
30
|
+
|
29
31
|
if key_types.class != key_values.class || key_types.length != key_values.length
|
30
32
|
raise "Key's value doesn't match key's type, key's value: #{key_values.inspect}, but key's type: #{key_types.inspect}. Please check your key's value."
|
31
33
|
end
|
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.
|
4
|
+
version: 0.4.0
|
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-08-
|
11
|
+
date: 2024-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: base58
|