Class: Web3::Eth::Abi::Tuple

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

Instance Attribute Summary collapse

Attributes inherited from Type

#base, #dims, #sub

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#dynamic?, size_type

Constructor Details

#initialize(types, dims) ⇒ Tuple

Returns a new instance of Tuple.



160
161
162
163
164
# File 'lib/web3/eth/abi/type.rb', line 160

def initialize types, dims
  super('tuple', '', dims)
  @types = types
  @parsed_types = types.map{|t| Type.parse t}
end

Instance Attribute Details

#parsed_typesObject (readonly)

Returns the value of attribute parsed_types.



159
160
161
# File 'lib/web3/eth/abi/type.rb', line 159

def parsed_types
  @parsed_types
end

#typesObject (readonly)

Returns the value of attribute types.



159
160
161
# File 'lib/web3/eth/abi/type.rb', line 159

def types
  @types
end

Class Method Details

.parse(types, dims) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/web3/eth/abi/type.rb', line 127

def self.parse types, dims

  depth = 0
  collected = []
  current = ''

  types.split('').each do |c|
    case c
      when ',' then
        if depth==0
          collected << current
          current = ''
        else
          current += c
        end
      when '(' then
        depth += 1
        current += c
      when ')' then
        depth -= 1
        current += c
      else
        current += c
    end

  end
  collected << current unless current.empty?

  Tuple.new collected, dims.map {|x| x[1...-1].to_i}

end

Instance Method Details

#==(another_type) ⇒ Object



166
167
168
169
170
# File 'lib/web3/eth/abi/type.rb', line 166

def ==(another_type)
  another_type.kind_of?(Tuple) &&
      another_type.types == types &&
      another_type.dims == dims
end

#calculate_sizeObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/web3/eth/abi/type.rb', line 176

def calculate_size
  if dims.empty?
    s = 0
    parsed_types.each do |type|
      ts = type.size
      return nil if ts.nil?
      s += ts
    end
    s
  else
    if dims.last == 0 # 0 for dynamic array []
      nil
    else
      subtype.dynamic? ? nil : dims.last * subtype.size
    end
  end


end

#sizeObject



172
173
174
# File 'lib/web3/eth/abi/type.rb', line 172

def size
  @size ||= calculate_size
end

#subtypeObject



196
197
198
# File 'lib/web3/eth/abi/type.rb', line 196

def subtype
  @subtype ||= Tuple.new(types, dims[0...-1])
end