scale_rb 0.1.14 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,100 +1,103 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Metadata
4
- module MetadataV9
5
- class << self
6
- def get_module(module_name, metadata)
7
- metadata._get(:metadata)._get(:v9)._get(:modules).find do |m|
8
- m._get(:name) == module_name
3
+ module ScaleRb
4
+ module Metadata
5
+ module MetadataV9
6
+ class << self
7
+ def get_module(module_name, metadata)
8
+ metadata._get(:metadata)._get(:v9)._get(:modules).find do |m|
9
+ m._get(:name) == module_name
10
+ end
9
11
  end
10
- end
11
12
 
12
- def get_storage_item(module_name, item_name, metadata)
13
- modula = get_module(module_name, metadata)
14
- raise "Module `#{module_name}` not found" if modula.nil?
15
- modula._get(:storage)._get(:items).find do |item|
16
- item._get(:name) == item_name
13
+ def get_storage_item(module_name, item_name, metadata)
14
+ modula = get_module(module_name, metadata)
15
+ raise "Module `#{module_name}` not found" if modula.nil?
16
+
17
+ modula._get(:storage)._get(:items).find do |item|
18
+ item._get(:name) == item_name
19
+ end
17
20
  end
18
21
  end
19
- end
20
22
 
21
- TYPES = {
22
- ErrorMetadataV9: {
23
- name: 'Text',
24
- docs: 'Vec<Text>'
25
- },
26
- EventMetadataV9: {
27
- name: 'Text',
28
- args: 'Vec<Type>',
29
- docs: 'Vec<Text>'
30
- },
31
- FunctionArgumentMetadataV9: {
32
- name: 'Text',
33
- type: 'Type'
34
- },
35
- FunctionMetadataV9: {
36
- name: 'Text',
37
- args: 'Vec<FunctionArgumentMetadataV9>',
38
- docs: 'Vec<Text>'
39
- },
40
- MetadataV9: {
41
- modules: 'Vec<ModuleMetadataV9>'
42
- },
43
- ModuleConstantMetadataV9: {
44
- name: 'Text',
45
- type: 'Type',
46
- value: 'Bytes',
47
- docs: 'Vec<Text>'
48
- },
49
- ModuleMetadataV9: {
50
- name: 'Text',
51
- storage: 'Option<StorageMetadataV9>',
52
- calls: 'Option<Vec<FunctionMetadataV9>>',
53
- events: 'Option<Vec<EventMetadataV9>>',
54
- constants: 'Vec<ModuleConstantMetadataV9>',
55
- errors: 'Vec<ErrorMetadataV9>'
56
- },
57
- StorageEntryMetadataV9: {
58
- name: 'Text',
59
- modifier: 'StorageEntryModifierV9',
60
- type: 'StorageEntryTypeV9',
61
- fallback: 'Bytes',
62
- docs: 'Vec<Text>'
63
- },
64
- StorageEntryModifierV9: {
65
- _enum: %w[Optional Default Required]
66
- },
67
- StorageEntryTypeV9: {
68
- _enum: {
69
- Plain: 'Type',
70
- Map: {
71
- hasher: 'StorageHasherV9',
72
- key: 'Type',
73
- value: 'Type',
74
- linked: 'bool'
75
- },
76
- DoubleMap: {
77
- hasher: 'StorageHasherV9',
78
- key1: 'Type',
79
- key2: 'Type',
80
- value: 'Type',
81
- key2Hasher: 'StorageHasherV9'
23
+ TYPES = {
24
+ ErrorMetadataV9: {
25
+ name: 'Text',
26
+ docs: 'Vec<Text>'
27
+ },
28
+ EventMetadataV9: {
29
+ name: 'Text',
30
+ args: 'Vec<Type>',
31
+ docs: 'Vec<Text>'
32
+ },
33
+ FunctionArgumentMetadataV9: {
34
+ name: 'Text',
35
+ type: 'Type'
36
+ },
37
+ FunctionMetadataV9: {
38
+ name: 'Text',
39
+ args: 'Vec<FunctionArgumentMetadataV9>',
40
+ docs: 'Vec<Text>'
41
+ },
42
+ MetadataV9: {
43
+ modules: 'Vec<ModuleMetadataV9>'
44
+ },
45
+ ModuleConstantMetadataV9: {
46
+ name: 'Text',
47
+ type: 'Type',
48
+ value: 'Bytes',
49
+ docs: 'Vec<Text>'
50
+ },
51
+ ModuleMetadataV9: {
52
+ name: 'Text',
53
+ storage: 'Option<StorageMetadataV9>',
54
+ calls: 'Option<Vec<FunctionMetadataV9>>',
55
+ events: 'Option<Vec<EventMetadataV9>>',
56
+ constants: 'Vec<ModuleConstantMetadataV9>',
57
+ errors: 'Vec<ErrorMetadataV9>'
58
+ },
59
+ StorageEntryMetadataV9: {
60
+ name: 'Text',
61
+ modifier: 'StorageEntryModifierV9',
62
+ type: 'StorageEntryTypeV9',
63
+ fallback: 'Bytes',
64
+ docs: 'Vec<Text>'
65
+ },
66
+ StorageEntryModifierV9: {
67
+ _enum: %w[Optional Default Required]
68
+ },
69
+ StorageEntryTypeV9: {
70
+ _enum: {
71
+ Plain: 'Type',
72
+ Map: {
73
+ hasher: 'StorageHasherV9',
74
+ key: 'Type',
75
+ value: 'Type',
76
+ linked: 'bool'
77
+ },
78
+ DoubleMap: {
79
+ hasher: 'StorageHasherV9',
80
+ key1: 'Type',
81
+ key2: 'Type',
82
+ value: 'Type',
83
+ key2Hasher: 'StorageHasherV9'
84
+ }
82
85
  }
86
+ },
87
+ StorageHasherV9: {
88
+ _enum: %w[
89
+ Blake2_128
90
+ Blake2_256
91
+ Twox128
92
+ Twox256
93
+ Twox64Concat
94
+ ]
95
+ },
96
+ StorageMetadataV9: {
97
+ prefix: 'Text',
98
+ items: 'Vec<StorageEntryMetadataV9>'
83
99
  }
84
- },
85
- StorageHasherV9: {
86
- _enum: %w[
87
- Blake2_128
88
- Blake2_256
89
- Twox128
90
- Twox256
91
- Twox64Concat
92
- ]
93
- },
94
- StorageMetadataV9: {
95
- prefix: 'Text',
96
- items: 'Vec<StorageEntryMetadataV9>'
97
- }
98
- }.freeze
100
+ }.freeze
101
+ end
99
102
  end
100
103
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # https://www.rubyguides.com/2017/01/read-binary-data/
4
4
  class String
5
- def to_bytes
5
+ def _to_bytes
6
6
  data = start_with?('0x') ? self[2..] : self
7
7
  raise 'Not valid hex string' if data =~ /[^\da-f]+/i
8
8
 
@@ -10,11 +10,11 @@ class String
10
10
  data.scan(/../).map(&:hex)
11
11
  end
12
12
 
13
- def to_camel
13
+ def _to_camel
14
14
  split('_').collect(&:capitalize).join
15
15
  end
16
16
 
17
- def underscore
17
+ def _underscore
18
18
  gsub(/::/, '/')
19
19
  .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
20
20
  .gsub(/([a-z\d])([A-Z])/, '\1_\2')
@@ -24,22 +24,22 @@ class String
24
24
  end
25
25
 
26
26
  class Integer
27
- def to_bytes(bit_length = nil)
28
- return to_s(16).to_bytes unless bit_length
27
+ def _to_bytes(bit_length = nil)
28
+ return to_s(16)._to_bytes unless bit_length
29
29
 
30
30
  hex = to_s(16).rjust(bit_length / 4, '0')
31
- hex.to_bytes
31
+ hex._to_bytes
32
32
  end
33
33
 
34
34
  # unsigned to signed
35
- def to_signed(bit_length)
35
+ def _to_signed(bit_length)
36
36
  unsigned_mid = 2**(bit_length - 1)
37
37
  unsigned_ceiling = 2**bit_length
38
38
  self >= unsigned_mid ? self - unsigned_ceiling : self
39
39
  end
40
40
 
41
41
  # signed to unsigned
42
- def to_unsigned(bit_length)
42
+ def _to_unsigned(bit_length)
43
43
  unsigned_mid = 2**(bit_length - 1)
44
44
  unsigned_ceiling = 2**bit_length
45
45
  raise 'out of scope' if self >= unsigned_mid || self <= -unsigned_mid
@@ -48,48 +48,48 @@ class Integer
48
48
  end
49
49
 
50
50
  # unix timestamp to utc
51
- def to_utc
51
+ def _to_utc
52
52
  Time.at(self).utc.asctime
53
53
  end
54
54
 
55
55
  # utc to unix timestamp
56
- def from_utc(utc_asctime)
56
+ def _from_utc(utc_asctime)
57
57
  Time.parse(utc_asctime)
58
58
  end
59
59
  end
60
60
 
61
61
  class Array
62
- def to_hex
63
- raise 'Not a byte array' unless byte_array?
62
+ def _to_hex
63
+ raise 'Not a byte array' unless _byte_array?
64
64
 
65
65
  reduce('0x') { |hex, byte| hex + byte.to_s(16).rjust(2, '0') }
66
66
  end
67
67
 
68
- def to_bin
69
- raise 'Not a byte array' unless byte_array?
68
+ def _to_bin
69
+ raise 'Not a byte array' unless _byte_array?
70
70
 
71
71
  reduce('0b') { |bin, byte| bin + byte.to_s(2).rjust(8, '0') }
72
72
  end
73
73
 
74
- def to_utf8
75
- raise 'Not a byte array' unless byte_array?
74
+ def _to_utf8
75
+ raise 'Not a byte array' unless _byte_array?
76
76
 
77
77
  pack('C*').force_encoding('utf-8')
78
78
  end
79
79
 
80
- def to_uint
81
- to_hex.to_i(16)
80
+ def _to_uint
81
+ _to_hex.to_i(16)
82
82
  end
83
83
 
84
- def to_int(bit_length)
85
- to_uint.to_signed(bit_length)
84
+ def _to_int(bit_length)
85
+ _to_uint._to_signed(bit_length)
86
86
  end
87
87
 
88
- def flip
88
+ def _flip
89
89
  reverse
90
90
  end
91
91
 
92
- def byte_array?
92
+ def _byte_array?
93
93
  all? { |e| e >= 0 and e <= 255 }
94
94
  end
95
95
  end