scale_rb 0.3.1 → 0.3.2

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: e5b8734ff8c6eda16208a8bb532ee411e15eaf04bf53f5f0f4d8465724b5f89e
4
- data.tar.gz: 4aabf928980af1c7d2c6db2857c832743a9288f4e5b7cd17007ce6c25d7def1f
3
+ metadata.gz: 6b1c3f0276dedfa00a80fed47d5647ced3d6128667e0b6e3ac38733351cb3dac
4
+ data.tar.gz: 5e137b28a94a898949937e3d10d043b80627f46232a9f5d49ba27766fbac6659
5
5
  SHA512:
6
- metadata.gz: 2645dc18deb5d1d95bb28a4ad89512d2a05e9651f19eb48c3090895ab8fa16795204d09dbef9214168ba03126f0f2e6251e654b1bfb9766f12a3d85fc8801af1
7
- data.tar.gz: 0ffb2cf53f4e46014f624f5e7d6d60cb8f92a9dabc74c97b90bf26725c3a6a80ffae96408df6aa0d4295d8ecef80b0f5520c74acb3f63a14d218084d48dd1abb
6
+ metadata.gz: 0416b944d3a0ccb254390e47c019c5692d04dc3f1c86f6033e6ceaac8e5e8a18c530e20a224ff19975b5acf8413f9e12da16cc6a24253a6f08d09972f981bdea
7
+ data.tar.gz: 74e2acc6ee10c7928c7528fa42cdf1dc8c9a778f56c77136a5d9305619b0eb386d752ce3a9902589bed61ffe9eaf74a14446b7579c622fea73d34b960693287e
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.1)
4
+ scale_rb (0.3.2)
5
5
  async
6
6
  async-http (~> 0.69.0)
7
7
  async-websocket (~> 0.26.2)
@@ -12,7 +12,7 @@ PATH
12
12
  GEM
13
13
  remote: https://rubygems.org/
14
14
  specs:
15
- async (2.14.1)
15
+ async (2.14.2)
16
16
  console (~> 1.25, >= 1.25.2)
17
17
  fiber-annotation
18
18
  io-event (~> 1.6, >= 1.6.5)
@@ -35,7 +35,7 @@ GEM
35
35
  blake2b_rs (0.1.4)
36
36
  ffi (~> 1.0)
37
37
  thermite (~> 0)
38
- console (1.25.2)
38
+ console (1.27.0)
39
39
  fiber-annotation
40
40
  fiber-local (~> 1.1)
41
41
  json
@@ -51,7 +51,7 @@ GEM
51
51
  json (2.7.2)
52
52
  minitar (0.9)
53
53
  protocol-hpack (1.4.3)
54
- protocol-http (0.27.0)
54
+ protocol-http (0.28.1)
55
55
  protocol-http1 (0.19.1)
56
56
  protocol-http (~> 0.22)
57
57
  protocol-http2 (0.18.0)
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -3,11 +3,41 @@ require 'scale_rb'
3
3
  # ScaleRb.logger.level = Logger::DEBUG
4
4
 
5
5
  ScaleRb::WsClient.start('wss://polkadot-rpc.dwellir.com') do |client|
6
- client.chain_subscribeFinalizedHeads do |head|
7
- block_number = head['number'].to_i(16)
8
- block_hash = client.chain_getBlockHash(block_number)
6
+ block_number = 21711742
7
+ block_hash = client.chain_getBlockHash(block_number)
8
+ metadata = client.get_metadata(block_hash)
9
9
 
10
- storage = client.get_storage(block_hash, 'System', 'Events')
11
- puts "block #{block_number}(#{block_hash}) has #{storage.length} events"
12
- end
10
+ storage_query = ScaleRb::WsClient::StorageQuery.new(
11
+ pallet_name: 'System',
12
+ storage_name: 'EventCount',
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}}, ...]
33
+
34
+
35
+ storage_query = ScaleRb::WsClient::StorageQuery.new(
36
+ pallet_name: 'ChildBounties',
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}}}
13
43
  end
data/lib/address.rb CHANGED
File without changes
@@ -1,21 +1,29 @@
1
1
  module ScaleRb
2
+
3
+ # This module is used to add extra methods to both the ScaleRb::WsClient ScaleRb::HttpClient
2
4
  module ClientExt
3
- def get_metadata(block_hash)
4
- dir = ENV['SCALE_RB_METADATA_DIR'] || File.join(Dir.pwd, 'metadata')
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
5
10
 
6
- get_metadata_by_block_hash(dir, block_hash)
11
+ # get decoded metadata at block_hash
12
+ def get_metadata(block_hash)
13
+ metadata_hex = state_getMetadata(block_hash)
14
+ ScaleRb::Metadata.decode_metadata(metadata_hex.strip._to_bytes)
7
15
  end
8
16
 
9
- # get storage at block_hash
10
- def get_storage(block_hash, pallet_name, storage_name, key_part1: nil, key_part2: nil)
11
- metadata = get_metadata(block_hash)
17
+ # Get decoded storage at block_hash
18
+ def get_storage(block_hash, storage_query, metadata = nil)
19
+ metadata ||= get_metadata(block_hash)
12
20
 
13
21
  # storeage item
14
- pallet_name = to_pascal pallet_name
15
- storage_name = to_pascal storage_name
22
+ pallet_name = convert_to_camel_case storage_query.pallet_name
23
+ storage_name = convert_to_camel_case storage_query.storage_name
16
24
 
17
25
  # storage param
18
- key = [key_part1, key_part2].compact
26
+ key = [storage_query.key_part1, storage_query.key_part2].compact
19
27
  ScaleRb.logger.debug "#{pallet_name}.#{storage_name}(#{key.join(', ')})"
20
28
  key = key.map { |part_of_key| c(part_of_key) }
21
29
  ScaleRb.logger.debug "converted key: #{key}"
@@ -31,41 +39,6 @@ module ScaleRb
31
39
 
32
40
  private
33
41
 
34
- def get_metadata_by_block_hash(cache_dir, block_hash)
35
- # Get metadata from cache if it exists
36
- runtime_version = state_getRuntimeVersion(block_hash)
37
- spec_name = runtime_version['specName']
38
- spec_version = runtime_version['specVersion']
39
- metadata = cached_metadata(spec_name, spec_version, cache_dir)
40
- return metadata if metadata
41
-
42
- # Get metadata from node
43
- metadata_hex = state_getMetadata(block_hash)
44
- metadata = ScaleRb::Metadata.decode_metadata(metadata_hex.strip._to_bytes)
45
-
46
- # cache it
47
- save_metadata_to_file(spec_name, spec_version, metadata, cache_dir)
48
-
49
- return metadata
50
- end
51
-
52
- def cached_metadata(spec_name, spec_version, dir)
53
- file_path = File.join(dir, "#{spec_name}-#{spec_version}.json")
54
- return unless File.exist?(file_path)
55
-
56
- JSON.parse(File.read(file_path))
57
- end
58
-
59
- def save_metadata_to_file(spec_name, spec_version, metadata, dir)
60
- FileUtils.mkdir_p(dir)
61
-
62
- File.open(File.join(dir, "#{spec_name}-#{spec_version}.json"), 'w') do |f|
63
- f.write(JSON.pretty_generate(metadata))
64
- end
65
- end
66
-
67
- ####
68
-
69
42
  def query_storage_at(block_hash, storage_keys, type_id, default, registry)
70
43
  result = state_queryStorageAt(storage_keys, block_hash)
71
44
  result.map do |item|
@@ -91,11 +64,11 @@ module ScaleRb
91
64
  storage_keys = get_storage_keys_by_partial_key(block_hash, partial_storage_key, partial_storage_key)
92
65
  storage_keys.each_slice(250).map do |slice|
93
66
  query_storage_at(
67
+ block_hash,
94
68
  slice,
95
69
  type_id_of_value,
96
70
  default,
97
- registry,
98
- block_hash
71
+ registry
99
72
  )
100
73
  end.flatten
101
74
  end
@@ -127,8 +100,10 @@ module ScaleRb
127
100
  # },
128
101
  # ..
129
102
  #
130
- # TODO: part of the key is provided, but not all
103
+ # key is for the param, value is for the return
131
104
  def get_storage1(block_hash, pallet_name, item_name, key, value, registry)
105
+ ScaleRb::logger.debug "get_storage1: #{pallet_name}.#{item_name} key: #{key} value: #{value}"
106
+
132
107
  if key
133
108
  if key[:value].nil? || key[:value].empty?
134
109
  # map, but no key's value provided. get all storages under the partial storage key
@@ -150,15 +125,20 @@ module ScaleRb
150
125
  value[:modifier] == 'Default' ? value[:fallback] : nil,
151
126
  registry
152
127
  )
128
+ else
129
+ storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
130
+ data = state_getStorage(storage_key, block_hash)
131
+ StorageHelper.decode_storage(data, value[:type], value[:modifier] == 'Optional', value[:fallback], registry)
153
132
  end
154
133
  else
155
- storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
134
+ storage_key = StorageHelper.encode_storage_key(pallet_name, item_name)._to_hex
156
135
  data = state_getStorage(storage_key, block_hash)
157
136
  StorageHelper.decode_storage(data, value[:type], value[:modifier] == 'Optional', value[:fallback], registry)
158
137
  end
159
138
  end
160
139
 
161
- def get_storage2(block_hash, pallet_name, item_name, value_of_key, metadata)
140
+ def get_storage2(block_hash, pallet_name, item_name, params, metadata)
141
+ ScaleRb.logger.debug "get_storage2: #{pallet_name}.#{item_name} params: #{params}"
162
142
  raise 'Metadata should not be nil' if metadata.nil?
163
143
 
164
144
  registry = Metadata.build_registry(metadata)
@@ -179,15 +159,12 @@ module ScaleRb
179
159
  if plain
180
160
  [
181
161
  nil,
182
- { type: plain,
183
- modifier: modifier, fallback: fallback }
162
+ { type: plain, modifier: modifier, fallback: fallback }
184
163
  ]
185
164
  elsif map
186
165
  [
187
- { value: value_of_key,
188
- type: map._get(:key), hashers: map._get(:hashers) },
189
- { type: map._get(:value),
190
- modifier: modifier, fallback: fallback }
166
+ { value: params, type: map._get(:key), hashers: map._get(:hashers) },
167
+ { type: map._get(:value), modifier: modifier, fallback: fallback }
191
168
  ]
192
169
  else
193
170
  raise 'NoSuchStorageType'
@@ -195,16 +172,17 @@ module ScaleRb
195
172
  get_storage1(block_hash, pallet_name, item_name, key, value, registry)
196
173
  end
197
174
 
198
- def to_pascal(str)
199
- str.split('_').collect(&:capitalize).join
175
+ def convert_to_camel_case(str)
176
+ words = str.split(/_|(?=[A-Z])/)
177
+ words.map(&:capitalize).join
200
178
  end
201
179
 
202
180
  # convert key to byte array
203
181
  def c(key)
204
- if key.start_with?('0x')
205
- key._to_bytes
206
- elsif key.to_i.to_s == key # check if key is a number
182
+ if key.is_a?(Integer)
207
183
  key.to_i
184
+ elsif key.is_a?(String) && key.start_with?('0x')
185
+ key._to_bytes
208
186
  else
209
187
  key
210
188
  end
data/lib/codec.rb CHANGED
File without changes
data/lib/hasher.rb CHANGED
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
data/lib/registry.rb CHANGED
File without changes
@@ -1,3 +1,3 @@
1
1
  module ScaleRb
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
@@ -3,11 +3,7 @@
3
3
  module ScaleRb
4
4
  module StorageHelper
5
5
  class << self
6
- # key: {
7
- # value: ,
8
- # type: ,
9
- # hashers: []
10
- # }
6
+ # key example: {:value=>[0], :type=>4, :hashers=>["Twox64Concat"]}
11
7
  def encode_storage_key(pallet_name, item_name, key = nil, registry = nil)
12
8
  storage_key = Hasher.twox128(pallet_name) + Hasher.twox128(item_name)
13
9
 
@@ -15,16 +11,18 @@ module ScaleRb
15
11
 
16
12
  key_types, key_values, key_hashers =
17
13
  if key[:hashers].length == 1
14
+ # {:value=>[0], :type=>4, :hashers=>["Twox64Concat"]}
15
+ # type 4 is Uint32
18
16
  [
19
17
  [key[:type]],
20
- [key[:value]],
18
+ key[:value],
21
19
  key[:hashers]
22
20
  ]
23
21
  else
24
22
  [
25
- registry[key[:type]]._get(:def)._get(:tuple).first(key[:value].length),
23
+ registry[key[:type]]._get(:def)._get(:tuple),
26
24
  key[:value],
27
- key[:hashers].first(key[:value].length)
25
+ key[:hashers]
28
26
  ]
29
27
  end
30
28
 
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.1
4
+ version: 0.3.2
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-16 00:00:00.000000000 Z
11
+ date: 2024-07-19 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.4.19
168
- signing_key:
167
+ rubygems_version: 3.3.7
168
+ signing_key:
169
169
  specification_version: 4
170
170
  summary: A Ruby SCALE Codec Library, and, Substrate RPC Client
171
171
  test_files: []