Class: Web3::Eth::Abi::Type
- Inherits:
-
Object
- Object
- Web3::Eth::Abi::Type
- Defined in:
- lib/web3/eth/abi/type.rb
Direct Known Subclasses
Defined Under Namespace
Classes: ParseError
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#dims ⇒ Object
readonly
Returns the value of attribute dims.
-
#sub ⇒ Object
readonly
Returns the value of attribute sub.
Class Method Summary collapse
-
.parse(type) ⇒ Object
Crazy regexp to seperate out base type component (eg. uint), size (eg. 256, 128x128, nil), array component (eg. [], [45], nil).
- .size_type ⇒ Object
Instance Method Summary collapse
- #==(another_type) ⇒ Object
- #dynamic? ⇒ Boolean
-
#initialize(base, sub, dims) ⇒ Type
constructor
A new instance of Type.
-
#size ⇒ Integer, NilClass
Get the static size of a type, or nil if dynamic.
-
#subtype ⇒ Ethereum::ABI::Type
Type with one dimension lesser.
Constructor Details
#initialize(base, sub, dims) ⇒ Type
Returns a new instance of Type.
73 74 75 76 77 |
# File 'lib/web3/eth/abi/type.rb', line 73 def initialize(base, sub, dims) @base = base @sub = sub @dims = dims end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
65 66 67 |
# File 'lib/web3/eth/abi/type.rb', line 65 def base @base end |
#dims ⇒ Object (readonly)
Returns the value of attribute dims.
65 66 67 |
# File 'lib/web3/eth/abi/type.rb', line 65 def dims @dims end |
#sub ⇒ Object (readonly)
Returns the value of attribute sub.
65 66 67 |
# File 'lib/web3/eth/abi/type.rb', line 65 def sub @sub end |
Class Method Details
.parse(type) ⇒ Object
Crazy regexp to seperate out base type component (eg. uint), size (eg. 256, 128x128, nil), array component (eg. [], [45], nil)
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 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/web3/eth/abi/type.rb', line 14 def parse(type) return parse('uint256') if type=='trcToken' if type =~ /^\((.*)\)((\[[0-9]*\])*)/ return Tuple.parse $1, $2.scan(/\[[0-9]*\]/) end _, 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 '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 '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 size must be multiple of 8" unless high % 8 == 0 raise ParseError, "Low sizes must be 0 to 80" unless low>0 && low<=80 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_type ⇒ Object
60 61 62 |
# File 'lib/web3/eth/abi/type.rb', line 60 def size_type @size_type ||= new('uint', 256, []) end |
Instance Method Details
#==(another_type) ⇒ Object
79 80 81 82 83 |
# File 'lib/web3/eth/abi/type.rb', line 79 def ==(another_type) base == another_type.base && sub == another_type.sub && dims == another_type.dims end |
#dynamic? ⇒ Boolean
107 108 109 |
# File 'lib/web3/eth/abi/type.rb', line 107 def dynamic? size.nil? end |
#size ⇒ Integer, NilClass
Get the static size of a type, or nil if dynamic.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/web3/eth/abi/type.rb', line 91 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 |
#subtype ⇒ Ethereum::ABI::Type
Type with one dimension lesser.
119 120 121 |
# File 'lib/web3/eth/abi/type.rb', line 119 def subtype @subtype ||= self.class.new(base, sub, dims[0...-1]) end |