scale_rb 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,180 +0,0 @@
1
- module ScaleRb
2
- module HttpClient
3
- class << self
4
- # get_storage3 is a more ruby style function
5
- #
6
- # pallet_name and storage_name is pascal style like 'darwinia_staking'
7
- def get_storage3(url, pallet_name, storage_name, key_part1: nil, key_part2: nil, metadata: nil, at: nil)
8
- # Get metadata if not provided, and cache it.
9
- # The default dir is `metadata` dir under the caller's dir.
10
- # You can change it by setting `SCALE_RB_METADATA_DIR` env variable
11
- metadata = get_metadata_cached(url, at: at) if metadata.nil?
12
-
13
- pallet_name = to_pascal pallet_name
14
- storage_name = to_pascal storage_name
15
- ScaleRb.logger.debug "#{pallet_name}.#{storage_name}(#{[key_part1, key_part2].compact.join(', ')})"
16
-
17
- key = [key_part1, key_part2].compact.map { |part_of_key| c(part_of_key) }
18
- ScaleRb.logger.debug "converted key: #{key}"
19
-
20
- get_storage2(
21
- url,
22
- pallet_name,
23
- storage_name,
24
- key,
25
- metadata,
26
- at
27
- )
28
- end
29
-
30
- private
31
-
32
- def query_storage_at(url, storage_keys, type_id, default, registry, at = nil)
33
- result = state_queryStorageAt(url, storage_keys, at)
34
- result.map do |item|
35
- item['changes'].map do |change|
36
- storage_key = change[0]
37
- data = change[1] || default
38
- storage = data.nil? ? nil : PortableCodec.decode(type_id, data._to_bytes, registry)[0]
39
- { storage_key: storage_key, storage: storage }
40
- end
41
- end.flatten
42
- end
43
-
44
- def get_storage_keys_by_partial_key(url, partial_storage_key, start_key = nil, at = nil)
45
- storage_keys = state_getKeysPaged(url, partial_storage_key, 1000, start_key, at)
46
- if storage_keys.length == 1000
47
- storage_keys + get_storage_keys_by_partial_key(url, partial_storage_key, storage_keys.last, at)
48
- else
49
- storage_keys
50
- end
51
- end
52
-
53
- def get_storages_by_partial_key(url, partial_storage_key, type_id_of_value, default, registry, at = nil)
54
- storage_keys = get_storage_keys_by_partial_key(url, partial_storage_key, partial_storage_key, at)
55
- storage_keys.each_slice(250).map do |slice|
56
- query_storage_at(
57
- url,
58
- slice,
59
- type_id_of_value,
60
- default,
61
- registry,
62
- at
63
- )
64
- end.flatten
65
- end
66
-
67
- # 1. Plain
68
- # key: nil
69
- # value: { type: 3, modifier: 'Default', callback: '' }
70
- #
71
- # 2. Map
72
- # key: { value: value, type: 0, hashers: ['Blake2128Concat'] }
73
- # value: { type: 3, modifier: 'Default', callback: '' }
74
- #
75
- # 3. Map, but key.value is nil
76
- # key: { value: nil, type: 0, hashers: ['Blake2128Concat'] }
77
- # value: { type: 3, modifier: 'Default', callback: '' }
78
- #
79
- # example:
80
- # 'System',
81
- # 'Account',
82
- # key = {
83
- # value: [['0x724d50824542b56f422588421643c4a162b90b5416ef063f2266a1eae6651641'._to_bytes]], # [AccountId]
84
- # type: 0,
85
- # hashers: ['Blake2128Concat']
86
- # },
87
- # value = {
88
- # type: 3,
89
- # modifier: 'Default',
90
- # callback: '0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
91
- # },
92
- # ..
93
- #
94
- # TODO: part of the key is provided, but not all
95
- def get_storage(url, pallet_name, item_name, key, value, registry, at = nil)
96
- if key
97
- if key[:value].nil? || key[:value].empty?
98
- # map, but no key's value provided. get all storages under the partial storage key
99
- partial_storage_key = StorageHelper.encode_storage_key(pallet_name, item_name)._to_hex
100
- get_storages_by_partial_key(
101
- url,
102
- partial_storage_key,
103
- value[:type],
104
- value[:modifier] == 'Default' ? value[:fallback] : nil,
105
- registry,
106
- at
107
- )
108
- elsif key[:value].length != key[:hashers].length
109
- # map with multi parts, but not have all values
110
- partial_storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
111
- get_storages_by_partial_key(
112
- url,
113
- partial_storage_key,
114
- value[:type],
115
- value[:modifier] == 'Default' ? value[:fallback] : nil,
116
- registry,
117
- at
118
- )
119
- end
120
- else
121
- storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
122
- data = state_getStorage(url, storage_key, at)
123
- StorageHelper.decode_storage(data, value[:type], value[:modifier] == 'Optional', value[:fallback], registry)
124
- end
125
- end
126
-
127
- def get_storage2(url, pallet_name, item_name, value_of_key, metadata, at = nil)
128
- raise 'Metadata should not be nil' if metadata.nil?
129
-
130
- registry = Metadata.build_registry(metadata)
131
- item = Metadata.get_storage_item(
132
- pallet_name, item_name, metadata
133
- )
134
- raise "No such storage item: `#{pallet_name}`.`#{item_name}`" if item.nil?
135
-
136
- modifier = item._get(:modifier) # Default | Optional
137
- fallback = item._get(:fallback)
138
- type = item._get(:type)
139
-
140
- plain = type._get(:plain)
141
- map = type._get(:map)
142
- # debug
143
-
144
- key, value =
145
- if plain
146
- [
147
- nil,
148
- { type: plain,
149
- modifier: modifier, fallback: fallback }
150
- ]
151
- elsif map
152
- [
153
- { value: value_of_key,
154
- type: map._get(:key), hashers: map._get(:hashers) },
155
- { type: map._get(:value),
156
- modifier: modifier, fallback: fallback }
157
- ]
158
- else
159
- raise 'NoSuchStorageType'
160
- end
161
- get_storage(url, pallet_name, item_name, key, value, registry, at)
162
- end
163
-
164
- def to_pascal(str)
165
- str.split('_').collect(&:capitalize).join
166
- end
167
-
168
- # convert key to byte array
169
- def c(key)
170
- if key.start_with?('0x')
171
- key._to_bytes
172
- elsif key.to_i.to_s == key # check if key is a number
173
- key.to_i
174
- else
175
- key
176
- end
177
- end
178
- end
179
- end
180
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- module ScaleRb
6
- module RpcRequestBuilder
7
- def build_json_rpc_body(method, params, id)
8
- {
9
- 'id' => id,
10
- 'jsonrpc' => '2.0',
11
- 'method' => method,
12
- 'params' => params.reject(&:nil?)
13
- }.to_json
14
- end
15
-
16
- def respond_to_missing?(*_args)
17
- true
18
- end
19
-
20
- # example:
21
- # state_getStorage(1, '0x363a..', 563_868)
22
- #
23
- # ==
24
- #
25
- # build_json_rpc_body('state_getStorage', ['0x363a..', 563_868], 1)
26
- def method_missing(method, *args)
27
- build_json_rpc_body(method, args[1..], args[0])
28
- end
29
-
30
- ###################################
31
- # derived functions
32
- ###################################
33
- def derived_state_get_storage(rpc_id, pallet_name, item_name, key = nil, registry = nil)
34
- storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
35
- state_getStorage(rpc_id, [storage_key])
36
- end
37
-
38
- def derived_state_subscribe_storage(rpc_id, pallet_name, item_name, key = nil, registry = nil)
39
- storage_key = StorageHelper.encode_storage_key(pallet_name, item_name, key, registry)._to_hex
40
- state_subscribeStorage(rpc_id, [storage_key])
41
- end
42
-
43
- def derived_eth_call(rpc_id, to, data, at = nil)
44
- eth_call(
45
- rpc_id,
46
- [
47
- {
48
- 'from' => nil, 'to' => to, 'data' => data
49
- },
50
- at
51
- ]
52
- )
53
- end
54
- end
55
- end