Class: ABI::Type

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

Direct Known Subclasses

Tuple

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

BASE_TYPE_RX =

Crazy regexp to seperate out base type component (eg. uint), size (eg. 256, 128, nil), array component (eg. [], [45], nil)

/([a-z]*)
 ([0-9]*)
 ((\[[0-9]*\])*)
/x
TUPLE_TYPE_RX =
/^\((.*)\)
  ((\[[0-9]*\])*)
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, sub, dims = []) ⇒ Type

Returns a new instance of Type.

Parameters:

  • base (String)

    base name of type, e.g. uint for uint256

  • sub (String)

    subscript of type, e.g. 256 for uint256

  • dims (Array[Integer]) (defaults to: [])

    dimensions of array type, e.g. [1,2,0] for uint256[2][], [] for non-array type



80
81
82
83
84
# File 'lib/abicoder/type.rb', line 80

def initialize( base, sub, dims=[] )
  @base = base
  @sub  = sub
  @dims = dims
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



72
73
74
# File 'lib/abicoder/type.rb', line 72

def base
  @base
end

#dimsObject (readonly)

Returns the value of attribute dims.



72
73
74
# File 'lib/abicoder/type.rb', line 72

def dims
  @dims
end

#subObject (readonly)

Returns the value of attribute sub.



72
73
74
# File 'lib/abicoder/type.rb', line 72

def sub
  @sub
end

Class Method Details

._parse_base_type(str) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/abicoder/type.rb', line 15

def self._parse_base_type( str )
  _, base, sub, dimension = BASE_TYPE_RX.match( str ).to_a

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

  dims = dims.map {|x| x[1...-1].to_i }

  [base, sub, dims]
end

.parse(type) ⇒ Object



34
35
36
37
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
# File 'lib/abicoder/type.rb', line 34

def self.parse( type )
  if type =~ TUPLE_TYPE_RX
        types = $1
        dims  = $2.scan( /\[[0-9]*\]/ )
        dims  = dims.map {|x| x[1...-1].to_i}
    return Tuple._parse( types, dims )
  end


 base, sub, dims = _parse_base_type( type )

  case base
    when 'bytes', 'string'
      raise ParseError, "Maximum 32 bytes for fixed-length string or bytes" unless sub.empty? || sub.to_i <= 32
    when 'uint', 'int'
      raise ParseError, "Integer type must have numerical suffix" unless sub =~ /\A[0-9]+\z/

      size = sub.to_i
      raise ParseError, "Integer size out of bounds" unless size >= 8 && size <= 256
      raise ParseError, "Integer size must be multiple of 8" unless size % 8 == 0
    when 'address'
      raise ParseError, "Address cannot have suffix" unless sub.empty?
    when 'bool'
      raise ParseError, "Bool cannot have suffix" unless sub.empty?
    else
      puts "  type: >#{type}<"
      raise ParseError, "Unrecognized type base: #{base}"
  end

  new( base, sub, dims )
end

.size_typeObject



67
68
69
# File 'lib/abicoder/type.rb', line 67

def self.size_type
  @size_type ||= new( 'uint', '256' )
end

Instance Method Details

#==(another_type) ⇒ Object



86
87
88
89
90
# File 'lib/abicoder/type.rb', line 86

def ==(another_type)
  @base == another_type.base &&
      @sub == another_type.sub &&
      @dims == another_type.dims
end

#calculate_sizeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/abicoder/type.rb', line 101

def calculate_size
           if @dims.empty?
              if ['string','bytes'].include?( @base ) && @sub.empty?
                nil
              else
                32
              end
            else
              if @dims.last == 0    # note: 0 used for dynamic array []
                nil
              else
                subtype.dynamic? ? nil : @dims.last * subtype.size
              end
            end
end

#dynamic?Boolean

Returns:

  • (Boolean)


117
# File 'lib/abicoder/type.rb', line 117

def dynamic?() size.nil?; end

#formatObject



133
134
135
136
137
138
139
# File 'lib/abicoder/type.rb', line 133

def format
  ## rebuild minimal type string
  buf = "#{@base}"
  buf << @sub  unless @sub.empty?
  buf << (@dims.map {|dim| dim==0 ? '[]' : "[#{dim}]"}.join)   unless @dims.empty?
  buf
end

#sizeInteger, NilClass

Get the static size of a type, or nil if dynamic.

Returns:

  • (Integer, NilClass)

    size of static type, or nil for dynamic type



97
98
99
# File 'lib/abicoder/type.rb', line 97

def size
   @size ||= calculate_size
end

#subtypeABI::Type

Type with one dimension lesser.

Examples:

Type.parse("uint256[2][]").subtype # => Type.new('uint', '256', [2])

Returns:



128
129
130
# File 'lib/abicoder/type.rb', line 128

def subtype
  @subtype ||= Type.new( @base, @sub, @dims[0...-1] )
end