Class: Klay::Abi::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/klay/abi/type.rb

Overview

Provides a class to handle and parse common ABI types.

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_type, sub_type, dimensions) ⇒ Klay::Abi::Type

Create a new Type object for base types, sub types, and dimensions. Should not be used; use #parse instead.

Parameters:

  • base_type (String)

    the base-type attribute.

  • sub_type (String)

    the sub-type attribute.

  • dimensions (Array)

    the dimension attribute.



45
46
47
48
49
50
# File 'lib/klay/abi/type.rb', line 45

def initialize(base_type, sub_type, dimensions)
  sub_type = sub_type.to_s
  @base_type = base_type
  @sub_type = sub_type
  @dimensions = dimensions
end

Instance Attribute Details

#base_typeObject (readonly)

The base attribute, e.g., string or bytes.



30
31
32
# File 'lib/klay/abi/type.rb', line 30

def base_type
  @base_type
end

#dimensionsObject (readonly)

The dimension attribute, e.g., [10] for an array of size 10.



36
37
38
# File 'lib/klay/abi/type.rb', line 36

def dimensions
  @dimensions
end

#sub_typeObject (readonly)

The sub-type attribute, e.g., 256 as size of an uint256.



33
34
35
# File 'lib/klay/abi/type.rb', line 33

def sub_type
  @sub_type
end

Class Method Details

.size_typeKlay::Abi::Type

Creates a new uint256 type used for size.

Returns:



81
82
83
# File 'lib/klay/abi/type.rb', line 81

def self.size_type
  @size_type ||= new("uint", 256, [])
end

Instance Method Details

#==(another_type) ⇒ Boolean

Compares two types for their attributes.

Parameters:

Returns:

  • (Boolean)

    true if all attributes match.



89
90
91
92
93
# File 'lib/klay/abi/type.rb', line 89

def ==(another_type)
  base_type == another_type.base_type and
    sub_type == another_type.sub_type and
    dimensions == another_type.dimensions
end

#is_dynamic?Boolean

Helpes to determine whether array is of dynamic size.

Returns:

  • (Boolean)

    true if array is of dynamic size.



117
118
119
# File 'lib/klay/abi/type.rb', line 117

def is_dynamic?
  size.nil?
end

#nested_subKlay::Abi::Type

Types can have nested sub-types in arrays.

Returns:



124
125
126
# File 'lib/klay/abi/type.rb', line 124

def nested_sub
  @nested_sub ||= self.class.new(base_type, sub_type, dimensions[0...-1])
end

#parse(type) ⇒ Klay::Abi::Type

Attempts to parse a string containing a common Solidity type. Creates a new Type upon success (using konstructor).

Parameters:

  • type (String)

    a common Solidity type.

Returns:

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/klay/abi/type.rb', line 61

def parse(type)
  _, base_type, sub_type, dimension = /([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)/.match(type).to_a

  # type dimension can only be numeric
  dims = dimension.scan(/\[[0-9]*\]/)
  raise ParseError, "Unknown characters found in array declaration" if dims.join != dimension

  # enforce base types
  validate_base_type base_type, sub_type

  # return a new Type (using konstructor)
  sub_type = sub_type.to_s
  @base_type = base_type
  @sub_type = sub_type
  @dimensions = dims.map { |x| x[1...-1].to_i }
end

#sizeInteger

Computes the size of a type if possible.

Returns:

  • (Integer)

    the size of the type; or nil if not available.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/klay/abi/type.rb', line 98

def size
  s = nil
  if dimensions.empty?
    unless ["string", "bytes"].include?(base_type) and sub_type.empty?
      s = 32
    end
  else
    unless dimensions.last == 0
      unless nested_sub.is_dynamic?
        s = dimensions.last * nested_sub.size
      end
    end
  end
  @size ||= s
end