Sha256: a32bf2e73d1c01982b714001b4c1680786995391e0ed331120d0c12b2030aced

Contents?: true

Size: 1.17 KB

Versions: 20

Compression:

Stored size: 1.17 KB

Contents

module Etherlite::Support
  module Array
    extend self

    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

    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
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
etherlite-0.6.0 lib/etherlite/support/array.rb
etherlite-0.5.3 lib/etherlite/support/array.rb
etherlite-0.5.2 lib/etherlite/support/array.rb
etherlite-0.5.1 lib/etherlite/support/array.rb
etherlite-0.5.0 lib/etherlite/support/array.rb
etherlite-0.4.2 lib/etherlite/support/array.rb
etherlite-0.4.1 lib/etherlite/support/array.rb
etherlite-0.4.0 lib/etherlite/support/array.rb
etherlite-0.3.0 lib/etherlite/support/array.rb
etherlite-0.2.6 lib/etherlite/support/array.rb
etherlite-0.2.5 lib/etherlite/support/array.rb
etherlite-0.2.4 lib/etherlite/support/array.rb
etherlite-0.2.3 lib/etherlite/support/array.rb
etherlite-0.2.2 lib/etherlite/support/array.rb
etherlite-0.2.1 lib/etherlite/support/array.rb
etherlite-0.2.0 lib/etherlite/support/array.rb
etherlite-0.1.8 lib/etherlite/support/array.rb
etherlite-0.1.7 lib/etherlite/support/array.rb
etherlite-0.1.6 lib/etherlite/support/array.rb
etherlite-0.1.5 lib/etherlite/support/array.rb