Module: Eth::Abi::Encoder
Overview
Provides a utility module to assist encoding ABIs.
Class Method Summary collapse
-
.primitive_type(type, arg) ⇒ String
Encodes primitive types.
-
.type(type, arg) ⇒ String
Encodes a specific value, either static or dynamic.
Instance Method Summary collapse
-
#primitive_type(type, arg) ⇒ String
Encodes primitive types.
-
#type(type, arg) ⇒ String
Encodes a specific value, either static or dynamic.
Class Method Details
.primitive_type(type, arg) ⇒ String
Encodes primitive types.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/eth/abi/encoder.rb', line 60 def primitive_type(type, arg) case type.base_type when "uint" uint arg, type when "int" int arg, type when "bool" bool arg when "ureal", "ufixed" ufixed arg, type when "real", "fixed" fixed arg, type when "string", "bytes" bytes arg, type when "tuple" tuple arg, type when "hash" hash arg, type when "address" address arg else raise EncodingError, "Unhandled type: #{type.base_type} #{type.sub_type}" end end |
.type(type, arg) ⇒ String
Encodes a specific value, either static or dynamic.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/eth/abi/encoder.rb', line 34 def type(type, arg) if %w(string bytes).include? type.base_type and type.sub_type.empty? and type.dimensions.empty? raise EncodingError, "Argument must be a String" unless arg.instance_of? String arg = handle_hex_string arg, type # encodes strings and bytes size = type Type.size_type, arg.size padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size) "#{size}#{arg}#{padding}" elsif type.base_type == "tuple" && type.dimensions.empty? tuple arg, type elsif !type.dimensions.empty? encode_array type, arg else primitive_type type, arg end end |
Instance Method Details
#primitive_type(type, arg) ⇒ String
Encodes primitive types.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/eth/abi/encoder.rb', line 60 def primitive_type(type, arg) case type.base_type when "uint" uint arg, type when "int" int arg, type when "bool" bool arg when "ureal", "ufixed" ufixed arg, type when "real", "fixed" fixed arg, type when "string", "bytes" bytes arg, type when "tuple" tuple arg, type when "hash" hash arg, type when "address" address arg else raise EncodingError, "Unhandled type: #{type.base_type} #{type.sub_type}" end end |
#type(type, arg) ⇒ String
Encodes a specific value, either static or dynamic.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/eth/abi/encoder.rb', line 34 def type(type, arg) if %w(string bytes).include? type.base_type and type.sub_type.empty? and type.dimensions.empty? raise EncodingError, "Argument must be a String" unless arg.instance_of? String arg = handle_hex_string arg, type # encodes strings and bytes size = type Type.size_type, arg.size padding = Constant::BYTE_ZERO * (Util.ceil32(arg.size) - arg.size) "#{size}#{arg}#{padding}" elsif type.base_type == "tuple" && type.dimensions.empty? tuple arg, type elsif !type.dimensions.empty? encode_array type, arg else primitive_type type, arg end end |