Class: Klay::Abi::Type
- Inherits:
-
Object
- Object
- Klay::Abi::Type
- 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
-
#base_type ⇒ Object
readonly
The base attribute, e.g.,
string
orbytes
. -
#dimensions ⇒ Object
readonly
The dimension attribute, e.g.,
[10]
for an array of size 10. -
#sub_type ⇒ Object
readonly
The sub-type attribute, e.g.,
256
as size of an uint256.
Class Method Summary collapse
-
.size_type ⇒ Klay::Abi::Type
Creates a new uint256 type used for size.
Instance Method Summary collapse
-
#==(another_type) ⇒ Boolean
Compares two types for their attributes.
-
#initialize(base_type, sub_type, dimensions) ⇒ Klay::Abi::Type
constructor
Create a new Type object for base types, sub types, and dimensions.
-
#is_dynamic? ⇒ Boolean
Helpes to determine whether array is of dynamic size.
-
#nested_sub ⇒ Klay::Abi::Type
Types can have nested sub-types in arrays.
-
#parse(type) ⇒ Klay::Abi::Type
Attempts to parse a string containing a common Solidity type.
-
#size ⇒ Integer
Computes the size of a type if possible.
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.
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_type ⇒ Object (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 |
#dimensions ⇒ Object (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_type ⇒ Object (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_type ⇒ Klay::Abi::Type
Creates a new uint256 type used for size.
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.
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.
117 118 119 |
# File 'lib/klay/abi/type.rb', line 117 def is_dynamic? size.nil? end |
#nested_sub ⇒ Klay::Abi::Type
Types can have nested sub-types in arrays.
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).
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 |
#size ⇒ Integer
Computes the size of a type if possible.
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 |