Class: BlockGiven::Abi::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/block_given/abi/parameter.rb

Overview

One ABI input/output. Knows its canonical Solidity type ("(uint256,address)[]").

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, index: 0) ⇒ Parameter

Returns a new instance of Parameter.



9
10
11
12
13
14
15
16
17
18
# File 'lib/block_given/abi/parameter.rb', line 9

def initialize(definition, index: 0)
  definition = definition.transform_keys(&:to_s)
  @name = definition["name"].to_s
  @name = "arg#{index}" if @name.empty?
  @unnamed = definition["name"].to_s.empty?
  @raw_type = definition["type"].to_s
  @indexed = definition["indexed"] ? true : false
  @internal_type = definition["internalType"]
  @components = Array(definition["components"]).each_with_index.map { |c, i| Parameter.new(c, index: i) }
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



7
8
9
# File 'lib/block_given/abi/parameter.rb', line 7

def components
  @components
end

#indexedObject (readonly)

Returns the value of attribute indexed.



7
8
9
# File 'lib/block_given/abi/parameter.rb', line 7

def indexed
  @indexed
end

#internal_typeObject (readonly)

Returns the value of attribute internal_type.



7
8
9
# File 'lib/block_given/abi/parameter.rb', line 7

def internal_type
  @internal_type
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/block_given/abi/parameter.rb', line 7

def name
  @name
end

#raw_typeObject (readonly)

Returns the value of attribute raw_type.



7
8
9
# File 'lib/block_given/abi/parameter.rb', line 7

def raw_type
  @raw_type
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


25
# File 'lib/block_given/abi/parameter.rb', line 25

def array? = raw_type.end_with?("]")

#dynamic?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/block_given/abi/parameter.rb', line 44

def dynamic?
  raw_type == "string" || raw_type == "bytes" || raw_type.end_with?("[]") ||
    (tuple? && components.any?(&:dynamic?)) || (array? && element.dynamic?)
end

#elementObject

Base type without the outermost array dimension.



35
36
37
38
39
40
41
42
# File 'lib/block_given/abi/parameter.rb', line 35

def element
  return nil unless array?

  Parameter.new(
    { "name" => name, "type" => raw_type.sub(/\[\d*\]\z/, ""),
      "components" => components.map(&:to_h) }
  )
end

#indexed?Boolean

Returns:

  • (Boolean)


21
# File 'lib/block_given/abi/parameter.rb', line 21

def indexed? = indexed

#ruby_nameObject



22
# File 'lib/block_given/abi/parameter.rb', line 22

def ruby_name = Utils.snake_case(name).to_sym

#to_hObject



49
50
51
52
53
54
# File 'lib/block_given/abi/parameter.rb', line 49

def to_h
  h = { "name" => unnamed? ? "" : name, "type" => raw_type }
  h["components"] = components.map(&:to_h) if tuple?
  h["indexed"] = indexed if indexed
  h
end

#tuple?Boolean

Returns:

  • (Boolean)


24
# File 'lib/block_given/abi/parameter.rb', line 24

def tuple? = raw_type.start_with?("tuple")

#typeObject

"tuple" with components -> "(uint256,address)[]"



28
29
30
31
32
# File 'lib/block_given/abi/parameter.rb', line 28

def type
  return raw_type unless tuple?

  raw_type.sub("tuple", "(#{components.map(&:type).join(',')})")
end

#unnamed?Boolean

Returns:

  • (Boolean)


20
# File 'lib/block_given/abi/parameter.rb', line 20

def unnamed? = @unnamed