scale.rb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,206 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV0
4
+ include SingleValue
5
+ attr_accessor :call_index, :event_index
6
+
7
+ def initialize(value)
8
+ @call_index = {}
9
+ @event_index = {}
10
+ super(value)
11
+ end
12
+
13
+ def self.decode(scale_bytes)
14
+ value = {
15
+ metadata: {
16
+ version: 0,
17
+ outerEvent: {
18
+ name: Bytes.decode(scale_bytes).value,
19
+ events: []
20
+ },
21
+ modules: [],
22
+ outerDispatch: {
23
+ name: "Call",
24
+ calls: []
25
+ }
26
+ }
27
+ }
28
+
29
+ events_modules = Scale::Types.type_of("Vec<MetadataV0EventModule>").decode(scale_bytes).value.map(&:value)
30
+ modules = Scale::Types.type_of("Vec<MetadataV0Module>").decode(scale_bytes).value.map(&:value)
31
+
32
+ Bytes.decode(scale_bytes).value
33
+
34
+ sections = Scale::Types.type_of("Vec<MetadataV0Section>").decode(scale_bytes).value.map(&:value)
35
+
36
+ value[:metadata][:outerEvent][:events] = events_modules
37
+ value[:metadata][:modules] = modules
38
+ value[:metadata][:outerDispatch][:calls] = sections
39
+
40
+ result = MetadataV0.new(value)
41
+
42
+ # call_module_index = 0
43
+ # event_module_index = 0
44
+
45
+ # modules.map(&:value).each do |m|
46
+ # if m[:calls]
47
+ # m[:calls].each_with_index do |call, index|
48
+ # call[:lookup] = "%02x%02x" % [call_module_index, index]
49
+ # result.call_index[call[:lookup]] = [m, call]
50
+ # end
51
+ # call_module_index += 1
52
+ # end
53
+
54
+ # if m[:events]
55
+ # m[:events].each_with_index do |event, index|
56
+ # event[:lookup] = "%02x%02x" % [call_module_index, index]
57
+ # result.event_index[event[:lookup]] = [m, event]
58
+ # end
59
+ # event_module_index += 1
60
+ # end
61
+ # end
62
+
63
+ result
64
+ end
65
+ end
66
+
67
+ class MetadataV0EventModule
68
+ include SingleValue
69
+
70
+ def self.decode(scale_bytes)
71
+ name = Bytes.decode(scale_bytes).value
72
+ events = Scale::Types.type_of('Vec<MetadataV0Event>').decode(scale_bytes).value.map(&:value)
73
+ MetadataV0EventModule.new([
74
+ name,
75
+ events
76
+ ])
77
+ end
78
+ end
79
+
80
+ class MetadataV0Event
81
+ include SingleValue
82
+
83
+ def self.decode(scale_bytes)
84
+ name = Bytes.decode(scale_bytes).value
85
+ args = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
86
+ documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
87
+ MetadataV0Event.new({name: name, args: args.map {|arg| arg }, documentation: documentation})
88
+ end
89
+ end
90
+
91
+ class MetadataV0Module
92
+ include SingleValue
93
+
94
+ def self.decode(scale_bytes)
95
+ prefix = Bytes.decode(scale_bytes).value
96
+ name = Bytes.decode(scale_bytes).value
97
+ call_name = Bytes.decode(scale_bytes).value
98
+
99
+ functions = Scale::Types.type_of("Vec<MetadataV0ModuleFunction>").decode(scale_bytes).value.map(&:value)
100
+
101
+ result = {
102
+ prefix: prefix,
103
+ module: {
104
+ name: name,
105
+ call: {
106
+ name: call_name,
107
+ functions: functions
108
+ }
109
+ }
110
+ }
111
+
112
+ has_storage = Bool.decode(scale_bytes).value
113
+ if has_storage
114
+ storage_prefix = Bytes.decode(scale_bytes).value
115
+ storage = Scale::Types.type_of("Vec<MetadataV0ModuleStorage>").decode(scale_bytes).value.map(&:value)
116
+ result[:storage] = {
117
+ prefix: storage_prefix,
118
+ functions: storage
119
+ }
120
+ end
121
+
122
+ MetadataV0Module.new(result)
123
+ end
124
+ end
125
+
126
+ class MetadataV0ModuleFunction
127
+ include SingleValue
128
+
129
+ def self.decode(scale_bytes)
130
+ id = U16.decode(scale_bytes).value
131
+ name = Bytes.decode(scale_bytes).value
132
+ args = Scale::Types.type_of("Vec<MetadataModuleCallArgument>").decode(scale_bytes).value.map(&:value)
133
+ documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
134
+ MetadataV0ModuleFunction.new({
135
+ id: id,
136
+ name: name,
137
+ args: args,
138
+ documentation: documentation
139
+ })
140
+ end
141
+ end
142
+
143
+ class MetadataModuleCallArgument
144
+ include SingleValue
145
+
146
+ def self.decode(scale_bytes)
147
+ name = Bytes.decode(scale_bytes).value
148
+ type = Bytes.decode(scale_bytes).value
149
+
150
+ MetadataModuleCallArgument.new({name: name, type: type})
151
+ end
152
+ end
153
+
154
+ class MetadataV0ModuleStorage
155
+ include SingleValue
156
+
157
+ def self.decode(scale_bytes)
158
+ name = Bytes.decode(scale_bytes).value
159
+ modifier = Scale::Types.type_of("Enum", ["Optional", "Default"]).decode(scale_bytes).value
160
+
161
+ is_key_value = Bool.decode(scale_bytes).value
162
+
163
+ if is_key_value
164
+ type = {
165
+ Map: {
166
+ key: Bytes.decode(scale_bytes).value,
167
+ value: Bytes.decode(scale_bytes).value
168
+ }
169
+ }
170
+ else
171
+ type = {
172
+ Plain: Bytes.decode(scale_bytes).value
173
+ }
174
+ end
175
+
176
+ fallback = Hex.decode(scale_bytes).value
177
+ documentation = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value.map(&:value)
178
+
179
+ MetadataV0ModuleStorage.new({
180
+ name: name,
181
+ modifier: modifier,
182
+ type: type,
183
+ fallback: fallback,
184
+ documentation: documentation
185
+ })
186
+ end
187
+ end
188
+
189
+ class MetadataV0Section
190
+ include SingleValue
191
+
192
+ def self.decode(scale_bytes)
193
+ name = Bytes.decode(scale_bytes).value
194
+ prefix = Bytes.decode(scale_bytes).value
195
+ id = U16.decode(scale_bytes).value
196
+
197
+ MetadataV0Section.new({
198
+ name: name,
199
+ prefix: prefix,
200
+ index: id
201
+ })
202
+ end
203
+ end
204
+
205
+ end
206
+ end
@@ -0,0 +1,120 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV1
4
+ include SingleValue
5
+ attr_accessor :call_index, :event_index
6
+
7
+ def initialize(value)
8
+ @call_index = {}
9
+ @event_index = {}
10
+ super(value)
11
+ end
12
+
13
+ def self.decode(scale_bytes)
14
+ modules = Scale::Types.type_of("Vec<MetadataV1Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 1,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV1.new(value)
25
+
26
+ call_module_index = 0
27
+ event_module_index = 0
28
+
29
+ modules.map(&:value).each do |m|
30
+ if m[:calls]
31
+ m[:calls].each_with_index do |call, index|
32
+ call[:lookup] = "%02x%02x" % [call_module_index, index]
33
+ result.call_index[call[:lookup]] = [m, call]
34
+ end
35
+ call_module_index += 1
36
+ end
37
+
38
+ if m[:events]
39
+ m[:events].each_with_index do |event, index|
40
+ event[:lookup] = "%02x%02x" % [call_module_index, index]
41
+ result.event_index[event[:lookup]] = [m, event]
42
+ end
43
+ event_module_index += 1
44
+ end
45
+ end
46
+
47
+ result
48
+ end
49
+ end
50
+
51
+ class MetadataV1Module
52
+ include SingleValue
53
+ def self.decode(scale_bytes)
54
+ name = String.decode(scale_bytes).value
55
+ prefix = String.decode(scale_bytes).value
56
+
57
+ result = {
58
+ name: name,
59
+ prefix: prefix
60
+ }
61
+
62
+ has_storage = Bool.decode(scale_bytes).value
63
+ if has_storage
64
+ storages = Scale::Types.type_of("Vec<MetadataV1ModuleStorage>").decode(scale_bytes).value
65
+ result[:storage] = storages.map(&:value)
66
+ end
67
+
68
+ has_calls = Bool.decode(scale_bytes).value
69
+ if has_calls
70
+ calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
71
+ result[:calls] = calls.map(&:value)
72
+ end
73
+
74
+ has_events = Bool.decode(scale_bytes).value
75
+ if has_events
76
+ events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
77
+ result[:events] = events.map(&:value)
78
+ end
79
+
80
+ MetadataModule.new(result)
81
+ end
82
+ end
83
+
84
+ class MetadataV1ModuleStorage
85
+ include SingleValue
86
+
87
+ def self.decode(scale_bytes)
88
+ name = Bytes.decode(scale_bytes).value
89
+ modifier = Scale::Types.type_of("Enum", ["Optional", "Default"]).decode(scale_bytes).value
90
+
91
+ is_key_value = Bool.decode(scale_bytes).value
92
+
93
+ if is_key_value
94
+ type = {
95
+ MapType: {
96
+ key: adjust(Bytes.decode(scale_bytes).value),
97
+ value: adjust(Bytes.decode(scale_bytes).value)
98
+ }
99
+ }
100
+ else
101
+ type = {
102
+ PlainType: adjust(Bytes.decode(scale_bytes).value)
103
+ }
104
+ end
105
+
106
+ fallback = Hex.decode(scale_bytes).value
107
+ docs = Scale::Types.type_of("Vec<Bytes>").decode(scale_bytes).value
108
+
109
+ MetadataV1ModuleStorage.new({
110
+ name: name,
111
+ modifier: modifier,
112
+ type: type,
113
+ default: fallback,
114
+ docs: docs
115
+ })
116
+ end
117
+ end
118
+
119
+ end
120
+ end
@@ -1,22 +1,51 @@
1
1
  module Scale
2
2
  module Types
3
-
4
3
  class MetadataV10
5
4
  include SingleValue
5
+ attr_accessor :call_index, :event_index
6
+
7
+ def initialize(value)
8
+ @call_index = {}
9
+ @event_index = {}
10
+ super(value)
11
+ end
12
+
6
13
  def self.decode(scale_bytes)
7
- modules = type_of("Vec<MetadataV8Module>").decode(scale_bytes).value;
8
- result = {
9
- magicNumber: 1635018093,
14
+ modules = Scale::Types.type_of("Vec<MetadataV8Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
10
18
  metadata: {
11
- V10: {
12
- modules: modules.map(&:value)
13
- }
19
+ version: 10,
20
+ modules: modules.map(&:value)
14
21
  }
15
22
  }
16
23
 
17
- MetadataV10.new(result)
24
+ result = MetadataV10.new(value)
25
+
26
+ call_module_index = 0
27
+ event_module_index = 0
28
+
29
+ modules.map(&:value).each do |m|
30
+ if m[:calls]
31
+ m[:calls].each_with_index do |call, index|
32
+ call[:lookup] = "%02x%02x" % [call_module_index, index]
33
+ result.call_index[call[:lookup]] = [m, call]
34
+ end
35
+ call_module_index += 1
36
+ end
37
+
38
+ if m[:events]
39
+ m[:events].each_with_index do |event, index|
40
+ event[:lookup] = "%02x%02x" % [call_module_index, index]
41
+ result.event_index[event[:lookup]] = [m, event]
42
+ end
43
+ event_module_index += 1
44
+ end
45
+ end
46
+
47
+ result
18
48
  end
19
49
  end
20
-
21
50
  end
22
51
  end
@@ -0,0 +1,51 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV11
4
+ include SingleValue
5
+ attr_accessor :call_index, :event_index
6
+
7
+ def initialize(value)
8
+ @call_index = {}
9
+ @event_index = {}
10
+ super(value)
11
+ end
12
+
13
+ def self.decode(scale_bytes)
14
+ modules = Scale::Types.type_of("Vec<MetadataV8Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 11,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV11.new(value)
25
+
26
+ call_module_index = 0
27
+ event_module_index = 0
28
+
29
+ modules.map(&:value).each do |m|
30
+ if m[:calls]
31
+ m[:calls].each_with_index do |call, index|
32
+ call[:lookup] = "%02x%02x" % [call_module_index, index]
33
+ result.call_index[call[:lookup]] = [m, call]
34
+ end
35
+ call_module_index += 1
36
+ end
37
+
38
+ if m[:events]
39
+ m[:events].each_with_index do |event, index|
40
+ event[:lookup] = "%02x%02x" % [call_module_index, index]
41
+ result.event_index[event[:lookup]] = [m, event]
42
+ end
43
+ event_module_index += 1
44
+ end
45
+ end
46
+
47
+ result
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV2
4
+ include SingleValue
5
+ attr_accessor :call_index, :event_index
6
+
7
+ def initialize(value)
8
+ @call_index = {}
9
+ @event_index = {}
10
+ super(value)
11
+ end
12
+
13
+ def self.decode(scale_bytes)
14
+ modules = Scale::Types.type_of("Vec<MetadataModule>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 2,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV2.new(value)
25
+
26
+ call_module_index = 0
27
+ event_module_index = 0
28
+
29
+ modules.map(&:value).each do |m|
30
+ if m[:calls]
31
+ m[:calls].each_with_index do |call, index|
32
+ call[:lookup] = "%02x%02x" % [call_module_index, index]
33
+ result.call_index[call[:lookup]] = [m, call]
34
+ end
35
+ call_module_index += 1
36
+ end
37
+
38
+ if m[:events]
39
+ m[:events].each_with_index do |event, index|
40
+ event[:lookup] = "%02x%02x" % [call_module_index, index]
41
+ result.event_index[event[:lookup]] = [m, event]
42
+ end
43
+ event_module_index += 1
44
+ end
45
+ end
46
+
47
+ result
48
+ end
49
+ end
50
+ end
51
+ end