Module: ScaleRb::Encode

Extended by:
TypeEnforcer
Includes:
Types
Included in:
Codec
Defined in:
lib/scale_rb/encode.rb

Constant Summary

Constants included from Types

Types::DecodeResult, Types::HashMap, Types::Hex, Types::PortableType, Types::Primitive, Types::Registry, Types::Ti, Types::TypedArray, Types::U8, Types::U8Array, Types::UnsignedInteger, Types::VariantKind

Instance Method Summary collapse

Methods included from TypeEnforcer

__, extended, method_added

Instance Method Details

#encode(id, value, registry) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scale_rb/encode.rb', line 10

def encode(id, value, registry)
  ScaleRb.logger.debug("Encoding #{id}, value: #{value}")
  type = registry[id]
  raise Codec::TypeNotFound, "id: #{id}" if type.nil?

  case type # type: PortableType
  when PrimitiveType then encode_primitive(type, value)
  when CompactType then encode_compact(value)
  when ArrayType then encode_array(type, value, registry)
  when SequenceType then encode_sequence(type, value, registry)
  when TupleType then encode_tuple(type, value, registry)
  when StructType then encode_struct(type, value, registry)
  when UnitType then []
  when VariantType then encode_variant(type, value, registry)
  else raise Codec::TypeNotImplemented, "encoding id: #{id}, type: #{type}"
  end
end

#encode_array(array_type, value, registry) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/scale_rb/encode.rb', line 49

def encode_array(array_type, value, registry)
  ScaleRb.logger.debug("Encoding array: #{array_type}, value: #{value}")

  len = array_type.len
  inner_type_id = array_type.type

  _encode_types([inner_type_id] * len, value, registry)
end

#encode_compact(value) ⇒ Object



42
43
44
45
46
# File 'lib/scale_rb/encode.rb', line 42

def encode_compact(value)
  ScaleRb.logger.debug("Encoding compact: #{value}")

  ScaleRb::CodecUtils.encode_compact(value)
end

#encode_primitive(type, value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/scale_rb/encode.rb', line 29

def encode_primitive(type, value)
  primitive = type.primitive.to_s
  ScaleRb.logger.debug("Encoding primitive: #{primitive}, value: #{value}")

  return ScaleRb::CodecUtils.encode_uint(primitive, value) if primitive.start_with?('U')
  return ScaleRb::CodecUtils.encode_int(primitive, value) if primitive.start_with?('I')
  return ScaleRb::CodecUtils.encode_string(value) if primitive == 'Str'
  return ScaleRb::CodecUtils.encode_boolean(value) if primitive == 'Bool'

  raise Codec::TypeNotImplemented, "encoding primitive: #{primitive}"
end

#encode_sequence(sequence_type, value, registry) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/scale_rb/encode.rb', line 59

def encode_sequence(sequence_type, value, registry)
  ScaleRb.logger.debug("Encoding sequence: #{sequence_type}, value: #{value}")

  len = value.length
  inner_type_id = sequence_type.type

  encode_compact(len) + _encode_types([inner_type_id] * len, value, registry)
end

#encode_struct(struct_type, value, registry) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/scale_rb/encode.rb', line 82

def encode_struct(struct_type, value, registry)
  ScaleRb.logger.debug("Encoding struct: #{struct_type}, value: #{value}")

  fields = struct_type.fields

  type_ids = fields.map(&:type)
  _encode_types(type_ids, value.values, registry)
end

#encode_tuple(tuple_type, value, registry) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scale_rb/encode.rb', line 69

def encode_tuple(tuple_type, value, registry)
  ScaleRb.logger.debug("Encoding tuple: #{tuple_type}, value: #{value}")

  type_ids = tuple_type.tuple

  # For example: if the tuple type is (AccountId32), the value can be a AccountId32
  # TODO: Check if this is correct
  value = [value] if type_ids.length == 1

  _encode_types(type_ids, value, registry)
end

#encode_variant(variant_type, value, registry) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/scale_rb/encode.rb', line 92

def encode_variant(variant_type, value, registry)
  ScaleRb.logger.debug("Encoding variant: #{variant_type}, value: #{value}")

  if variant_type.option?
    if value.nil?
      name = :None
    else
      name = :Some
    end
  else
    name = value.is_a?(::Symbol) ? value : value.keys.first
  end

  variant = variant_type.variants.find { |v| v.name == name }
  raise Codec::VariantItemNotFound, "type: #{variant_type}, name: #{value}" if variant.nil?

  case variant
  when SimpleVariant
    ScaleRb::CodecUtils.encode_uint('U8', variant.index)
  when TupleVariant
    # value example1: {:X2=>[{:Parachain=>12}, {:PalletInstance=>34}]}
    # value.values.first: [[{:Parachain=>12}, {:PalletInstance=>34}]]
    #
    # value example2: {:Parachain=>12}
    # value.values.first: 12
    ScaleRb::CodecUtils.encode_uint('U8', variant.index) + encode_tuple(variant.tuple, value.values.first, registry)
  when StructVariant
    # value example: {
    #   :Transact=>{
    #     :origin_type=>:SovereignAccount, 
    #     :require_weight_at_most=>5000000000,
    #     :call=>...
    #   }
    # }
    # value.values.first: {
    #   :origin_type=>:SovereignAccount,
    #   :require_weight_at_most=>5000000000,
    #   :call=>...
    # }
    ScaleRb::CodecUtils.encode_uint('U8', variant.index) + encode_struct(variant.struct, value.values.first, registry)
  end
end