Class: Web3::Hpb::Abi::Type

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

Defined Under Namespace

Classes: ParseError

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.



55
56
57
58
59
# File 'lib/web3/hpb/abi/type.rb', line 55

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



53
54
55
# File 'lib/web3/hpb/abi/type.rb', line 53

def base
  @base
end

#dimsObject (readonly)

Returns the value of attribute dims.



53
54
55
# File 'lib/web3/hpb/abi/type.rb', line 53

def dims
  @dims
end

#subObject (readonly)

Returns the value of attribute sub.



53
54
55
# File 'lib/web3/hpb/abi/type.rb', line 53

def sub
  @sub
end

Class Method Details

.parse(type) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/web3/hpb/abi/type.rb', line 11

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

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

  case base
  when ''
    return parse 'address'
  when 'string'
    raise ParseError, "String type must have no suffix or numerical suffix" unless sub.empty?
  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 'fixed', 'ufixed'
    raise ParseError, "Fixed type must have suffix of form <high>x<low>, e.g. 128x128" unless sub =~ /\A[0-9]+x[0-9]+\z/

    high, low = sub.split('x').map(&:to_i)
    total = high + low
    raise ParseError, "Fixed size out of bounds (max 32 bytes)" unless total >= 8 && total <= 256
    raise ParseError, "Fixed high/low sizes must be multiples of 8" unless high % 8 == 0 && low % 8 == 0
  when 'hash'
    raise ParseError, "Hash type must have numerical suffix" unless sub =~ /\A[0-9]+\z/
  when 'address'
    raise ParseError, "Address cannot have suffix" unless sub.empty?
  when 'bool'
    raise ParseError, "Bool cannot have suffix" unless sub.empty?
  else
    raise ParseError, "Unrecognized type base: #{base}"
  end

  new(base, sub, dims.map{ |x| x[1...-1].to_i })
end

.size_typeObject



48
49
50
# File 'lib/web3/hpb/abi/type.rb', line 48

def size_type
  @size_type ||= new('uint', 256, [])
end

Instance Method Details

#==(another_type) ⇒ Object



61
62
63
# File 'lib/web3/hpb/abi/type.rb', line 61

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

#dynamic?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/web3/hpb/abi/type.rb', line 82

def dynamic?
  size.nil?
end

#sizeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/web3/hpb/abi/type.rb', line 65

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

end

#subtypeObject



86
87
88
# File 'lib/web3/hpb/abi/type.rb', line 86

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