Module: Eth::Abi::Packed::Encoder

Extended by:
Encoder
Included in:
Encoder
Defined in:
lib/eth/abi/packed/encoder.rb

Overview

Provides a utility module to assist encoding ABIs.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.type(type, arg) ⇒ String

Encodes a specific value, either static or dynamic in non-standard packed encoding mode.

Parameters:

  • type (Eth::Abi::Type)

    type to be encoded.

  • arg (String|Number)

    value to be encoded.

Returns:

  • (String)

    the packed encoded type.

Raises:

  • (EncodingError)

    if value does not match type.

  • (ArgumentError)

    if encoding fails for type.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eth/abi/packed/encoder.rb', line 38

def type(type, arg)
  case type
  when /^uint(\d+)$/
    uint(arg, $1.to_i / 8)
  when /^int(\d+)$/
    int(arg, $1.to_i / 8)
  when "bool"
    bool(arg)
  when /^ureal(\d+)x(\d+)$/, /^ufixed(\d+)x(\d+)$/
    ufixed(arg, $1.to_i / 8, $2.to_i)
  when /^real(\d+)x(\d+)$/, /^fixed(\d+)x(\d+)$/
    fixed(arg, $1.to_i / 8, $2.to_i)
  when "string"
    string(arg)
  when /^bytes(\d+)$/
    bytes(arg, $1.to_i)
  when "bytes"
    string(arg)
  when /^tuple\((.+)\)$/
    tuple($1.split(","), arg)
  when /^hash(\d+)$/
    hash(arg, $1.to_i / 8)
  when "address"
    address(arg)
  when /^(.+)\[\]$/
    array($1, arg)
  when /^(.+)\[(\d+)\]$/
    fixed_array($1, arg, $2.to_i)
  else
    raise EncodingError, "Unhandled type: #{type}"
  end
end

Instance Method Details

#type(type, arg) ⇒ String

Encodes a specific value, either static or dynamic in non-standard packed encoding mode.

Parameters:

  • type (Eth::Abi::Type)

    type to be encoded.

  • arg (String|Number)

    value to be encoded.

Returns:

  • (String)

    the packed encoded type.

Raises:

  • (EncodingError)

    if value does not match type.

  • (ArgumentError)

    if encoding fails for type.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eth/abi/packed/encoder.rb', line 38

def type(type, arg)
  case type
  when /^uint(\d+)$/
    uint(arg, $1.to_i / 8)
  when /^int(\d+)$/
    int(arg, $1.to_i / 8)
  when "bool"
    bool(arg)
  when /^ureal(\d+)x(\d+)$/, /^ufixed(\d+)x(\d+)$/
    ufixed(arg, $1.to_i / 8, $2.to_i)
  when /^real(\d+)x(\d+)$/, /^fixed(\d+)x(\d+)$/
    fixed(arg, $1.to_i / 8, $2.to_i)
  when "string"
    string(arg)
  when /^bytes(\d+)$/
    bytes(arg, $1.to_i)
  when "bytes"
    string(arg)
  when /^tuple\((.+)\)$/
    tuple($1.split(","), arg)
  when /^hash(\d+)$/
    hash(arg, $1.to_i / 8)
  when "address"
    address(arg)
  when /^(.+)\[\]$/
    array($1, arg)
  when /^(.+)\[(\d+)\]$/
    fixed_array($1, arg, $2.to_i)
  else
    raise EncodingError, "Unhandled type: #{type}"
  end
end