Module: Etherlite::Support::Array

Extended by:
Array
Included in:
Array
Defined in:
lib/etherlite/support/array.rb

Instance Method Summary collapse

Instance Method Details

#decode(_connection, _types, _data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/etherlite/support/array.rb', line 24

def decode(_connection, _types, _data)
  words = _data.scan(/.{64}/)
  offset = 0

  [].tap do |r|
    _types.each do |type|
      if type.dynamic?
        offset_words = Etherlite::Utils.hex_to_uint(words[offset]) / 32
        r << type.decode(_connection, words[offset_words..-1].join)
        offset += 1
      else
        size_in_words = type.size / 32 # type.size is always 32
        r << type.decode(_connection, words.slice(offset, size_in_words).join)
        offset += size_in_words
      end
    end
  end
end

#encode(_types, _values) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/etherlite/support/array.rb', line 5

def encode(_types, _values)
  head = []
  tail = []
  tail_offset = _types.sum(0) { |type| type.dynamic? ? 32 : type.size } # type.size is always 32

  _types.each_with_index do |type, i|
    content = type.encode _values[i]
    if type.dynamic?
      head << Etherlite::Utils.uint_to_hex(tail_offset)
      tail << content
      tail_offset += content.length / 2 # hex string, 2 characters per byte
    else
      head << content
    end
  end

  head.join + tail.join
end