Module: ScaleRb::StorageHelper

Defined in:
lib/scale_rb/storage_helper.rb

Class Method Summary collapse

Class Method Details

._encode_types_with_hashers(type_ids, values, registry, hashers) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scale_rb/storage_helper.rb', line 41

def _encode_types_with_hashers(type_ids, values, registry, hashers)
  if !hashers.nil? && hashers.length != type_ids.length
    raise "type_ids length: #{type_ids.length}, hashers length: #{hashers.length}"
  end

  type_ids
    .map.with_index { |type_id, i| ScaleRb::Codec.encode(type_id, values[i], registry) }
    .each_with_index.reduce([]) do |memo, (bytes, i)|
      memo + Hasher.apply_hasher(hashers[i], bytes)
    end
end

.decode_storage(data, type, optional, fallback, registry) ⇒ Object

data: hex string type: portable type id optional: boolean fallback: hex string returns nil or data



58
59
60
61
# File 'lib/scale_rb/storage_helper.rb', line 58

def decode_storage(data, type, optional, fallback, registry)
  data ||= (optional ? nil : fallback)
  ScaleRb::Codec.decode(type, Utils.hex_to_u8a(data), registry)[0] if data
end

.decode_storage2(data, storage_item, registry) ⇒ Object

storage_item: the storage item from metadata



64
65
66
67
68
69
# File 'lib/scale_rb/storage_helper.rb', line 64

def decode_storage2(data, storage_item, registry)
  modifier = storage_item._get(:modifier) # Default | Optional
  fallback = storage_item._get(:fallback)
  type = storage_item._get(:type, :plain) || storage_item._get(:type, :map, :value)
  decode_storage(data, type, modifier == 'Optional', fallback, registry)
end

.decode_storage3(data, pallet_name, item_name, metadata) ⇒ Object



71
72
73
74
75
# File 'lib/scale_rb/storage_helper.rb', line 71

def decode_storage3(data, pallet_name, item_name, )
  registry = Metadata.build_registry()
  storage_item = Metadata.get_storage_item(pallet_name, item_name, )
  decode_storage2(data, storage_item, registry)
end

.encode_storage_key(pallet_name, item_name, key = nil, registry = nil) ⇒ Object

key example: href="0">value=>, :type=>4, :hashers=>



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/scale_rb/storage_helper.rb', line 7

def encode_storage_key(pallet_name, item_name, key = nil, registry = nil)
  storage_key = Hasher.twox128(pallet_name) + Hasher.twox128(item_name)

  if key && registry

    key_types, key_values, key_hashers =
      if key[:hashers].length == 1
        # {:value=>[0], :type=>4, :hashers=>["Twox64Concat"]}
        # type 4 is Uint32
        [
          [key[:type]],
          key[:value],
          key[:hashers]
        ]
      else
        [
          registry[key[:type]].tuple.first(key[:value].length),
          key[:value],
          key[:hashers].first(key[:value].length)
        ]
      end

    ScaleRb.logger.debug "encode_storage_key: key_values: #{key_values.inspect}, key_types: #{key_types.inspect}, key_hashers: #{key_hashers.inspect}"

    if key_types.class != key_values.class || key_types.length != key_values.length
      raise "Key's value doesn't match key's type, key's value: #{key_values.inspect}, but key's type: #{key_types.inspect}. Please check your key's value."
    end

    storage_key + _encode_types_with_hashers(key_types, key_values, registry, key_hashers)
  else
    storage_key
  end
end