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.
@@ -1,22 +1,51 @@
1
1
  module Scale
2
2
  module Types
3
-
4
3
  class MetadataV3
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<MetadataModule>").decode(scale_bytes).value;
8
- result = {
9
- magicNumber: 1635018093,
14
+ modules = Scale::Types.type_of("Vec<MetadataModule>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
10
18
  metadata: {
11
- V3: {
12
- modules: modules.map(&:value)
13
- }
19
+ version: 3,
20
+ modules: modules.map(&:value)
14
21
  }
15
22
  }
16
23
 
17
- MetadataV3.new(result)
24
+ result = MetadataV3.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,126 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV4
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<MetadataV4Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 4,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV4.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 MetadataV4Module
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<MetadataV4ModuleStorage>").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
+ MetadataV4Module.new(result)
81
+ end
82
+ end
83
+
84
+ class MetadataV4ModuleStorage
85
+ include SingleValue
86
+ def self.decode(scale_bytes)
87
+ result = {
88
+ name: String.decode(scale_bytes).value,
89
+ modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
90
+ }
91
+
92
+ storage_function_type = Scale::Types.type_of("Enum", %w[Plain Map DoubleMap]).decode(scale_bytes).value
93
+ if storage_function_type == "Plain"
94
+ result[:type] = {
95
+ Plain: adjust(String.decode(scale_bytes).value)
96
+ }
97
+ elsif storage_function_type == "Map"
98
+ result[:type] = {
99
+ Map: {
100
+ hasher: StorageHasher.decode(scale_bytes).value,
101
+ key: adjust(String.decode(scale_bytes).value),
102
+ value: adjust(String.decode(scale_bytes).value),
103
+ linked: Bool.decode(scale_bytes).value
104
+ }
105
+ }
106
+ elsif storage_function_type == "DoubleMap"
107
+ result[:type] = {
108
+ DoubleMap: {
109
+ hasher: StorageHasher.decode(scale_bytes).value,
110
+ key1: adjust(String.decode(scale_bytes).value),
111
+ key2: adjust(String.decode(scale_bytes).value),
112
+ value: adjust(String.decode(scale_bytes).value),
113
+ key2Hasher: StorageHasher.decode(scale_bytes).value
114
+ }
115
+ }
116
+ end
117
+
118
+ result[:fallback] = Hex.decode(scale_bytes).value
119
+ result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
120
+
121
+ MetadataV4ModuleStorage.new(result)
122
+ end
123
+ end
124
+
125
+ end
126
+ end
@@ -0,0 +1,126 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV5
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<MetadataV5Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 5,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV5.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 MetadataV5Module
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<MetadataV5ModuleStorage>").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
+ MetadataV5Module.new(result)
81
+ end
82
+ end
83
+
84
+ class MetadataV5ModuleStorage
85
+ include SingleValue
86
+ def self.decode(scale_bytes)
87
+ result = {
88
+ name: String.decode(scale_bytes).value,
89
+ modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
90
+ }
91
+
92
+ storage_function_type = Scale::Types.type_of("Enum", %w[Plain Map DoubleMap]).decode(scale_bytes).value
93
+ if storage_function_type == "Plain"
94
+ result[:type] = {
95
+ Plain: adjust(String.decode(scale_bytes).value)
96
+ }
97
+ elsif storage_function_type == "Map"
98
+ result[:type] = {
99
+ Map: {
100
+ hasher: StorageHasher.decode(scale_bytes).value,
101
+ key: adjust(String.decode(scale_bytes).value),
102
+ value: adjust(String.decode(scale_bytes).value),
103
+ linked: Bool.decode(scale_bytes).value
104
+ }
105
+ }
106
+ elsif storage_function_type == "DoubleMap"
107
+ result[:type] = {
108
+ DoubleMap: {
109
+ hasher: StorageHasher.decode(scale_bytes).value,
110
+ key1: adjust(String.decode(scale_bytes).value),
111
+ key2: adjust(String.decode(scale_bytes).value),
112
+ value: adjust(String.decode(scale_bytes).value),
113
+ key2Hasher: StorageHasher.decode(scale_bytes).value
114
+ }
115
+ }
116
+ end
117
+
118
+ result[:fallback] = Hex.decode(scale_bytes).value
119
+ result[:documentation] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
120
+
121
+ MetadataV5ModuleStorage.new(result)
122
+ end
123
+ end
124
+
125
+ end
126
+ end
@@ -0,0 +1,101 @@
1
+ module Scale
2
+ module Types
3
+ class MetadataV6
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<MetadataV6Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
18
+ metadata: {
19
+ version: 6,
20
+ modules: modules.map(&:value)
21
+ }
22
+ }
23
+
24
+ result = MetadataV6.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 MetadataV6Module
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<MetadataV6ModuleStorage>").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
+ result[:constants] = Scale::Types.type_of("Vec<MetadataV6ModuleConstants>").decode(scale_bytes).value.map(&:value)
81
+
82
+ MetadataV6Module.new(result)
83
+ end
84
+ end
85
+
86
+ class MetadataV6ModuleStorage < MetadataV5ModuleStorage; end
87
+
88
+ class MetadataV6ModuleConstants
89
+ include SingleValue
90
+ def self.decode(scale_bytes)
91
+ result = {
92
+ name: String.decode(scale_bytes).value,
93
+ type: String.decode(scale_bytes).value, # convert
94
+ value: Hex.decode(scale_bytes).value,
95
+ docs: Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
96
+ }
97
+ MetadataV6ModuleConstants.new result
98
+ end
99
+ end
100
+ end
101
+ end
@@ -1,20 +1,50 @@
1
1
  module Scale
2
2
  module Types
3
-
4
3
  class MetadataV7
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<MetadataV7Module>").decode(scale_bytes).value;
8
- result = {
9
- magicNumber: 1635018093,
14
+ modules = Scale::Types.type_of("Vec<MetadataV7Module>").decode(scale_bytes).value
15
+
16
+ value = {
17
+ magicNumber: 1_635_018_093,
10
18
  metadata: {
11
- V7: {
12
- modules: modules.map(&:value)
13
- }
19
+ version: 7,
20
+ modules: modules.map(&:value)
14
21
  }
15
22
  }
16
23
 
17
- MetadataV7.new(result)
24
+ result = MetadataV7.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
50
 
@@ -24,7 +54,7 @@ module Scale
24
54
  name = String.decode(scale_bytes).value
25
55
 
26
56
  result = {
27
- name: name,
57
+ name: name
28
58
  }
29
59
 
30
60
  has_storage = Bool.decode(scale_bytes).value
@@ -36,17 +66,17 @@ module Scale
36
66
 
37
67
  has_calls = Bool.decode(scale_bytes).value
38
68
  if has_calls
39
- calls = type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
69
+ calls = Scale::Types.type_of("Vec<MetadataModuleCall>").decode(scale_bytes).value
40
70
  result[:calls] = calls.map(&:value)
41
71
  end
42
72
 
43
73
  has_events = Bool.decode(scale_bytes).value
44
74
  if has_events
45
- events = type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
75
+ events = Scale::Types.type_of("Vec<MetadataModuleEvent>").decode(scale_bytes).value
46
76
  result[:events] = events.map(&:value)
47
77
  end
48
78
 
49
- result[:constants] = type_of("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
79
+ result[:constants] = Scale::Types.type_of("Vec<MetadataV7ModuleConstants>").decode(scale_bytes).value.map(&:value)
50
80
 
51
81
  MetadataV7Module.new(result)
52
82
  end
@@ -56,7 +86,7 @@ module Scale
56
86
  include SingleValue
57
87
  def self.decode(scale_bytes)
58
88
  prefix = String.decode(scale_bytes).value
59
- items = type_of("Vec<MetadataV7ModuleStorageEntry>").decode(scale_bytes).value.map(&:value)
89
+ items = Scale::Types.type_of("Vec<MetadataV7ModuleStorageEntry>").decode(scale_bytes).value.map(&:value)
60
90
  result = {
61
91
  prefix: prefix,
62
92
  items: items
@@ -71,9 +101,9 @@ module Scale
71
101
  def self.decode(scale_bytes)
72
102
  result = {
73
103
  name: String.decode(scale_bytes).value,
74
- modifier: type_of("Enum", enum_values: ["Optional", "Default"]).decode(scale_bytes).value
104
+ modifier: Scale::Types.type_of("Enum", %w[Optional Default]).decode(scale_bytes).value
75
105
  }
76
- storage_function_type = type_of("Enum", enum_values: ["Plain", "Map", "DoubleMap"]).decode(scale_bytes).value
106
+ storage_function_type = Scale::Types.type_of("Enum", %w[Plain Map DoubleMap]).decode(scale_bytes).value
77
107
  if storage_function_type == "Plain"
78
108
  result[:type] = {
79
109
  Plain: adjust(String.decode(scale_bytes).value)
@@ -100,7 +130,7 @@ module Scale
100
130
  end
101
131
 
102
132
  result[:fallback] = Hex.decode(scale_bytes).value
103
- result[:docs] = type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
133
+ result[:docs] = Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
104
134
 
105
135
  MetadataV7ModuleStorageEntry.new(result)
106
136
  end
@@ -113,11 +143,10 @@ module Scale
113
143
  name: String.decode(scale_bytes).value,
114
144
  type: String.decode(scale_bytes).value, # convert
115
145
  value: Hex.decode(scale_bytes).value,
116
- docs: type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
146
+ docs: Scale::Types.type_of("Vec<String>").decode(scale_bytes).value.map(&:value)
117
147
  }
118
148
  MetadataV7ModuleConstants.new result
119
149
  end
120
150
  end
121
-
122
151
  end
123
152
  end