Class: BlockGiven::Abi::Function
- Inherits:
-
Object
- Object
- BlockGiven::Abi::Function
- Defined in:
- lib/block_given/abi/function.rb
Instance Attribute Summary collapse
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
-
#state_mutability ⇒ Object
readonly
Returns the value of attribute state_mutability.
Instance Method Summary collapse
-
#decode_output(hex) ⇒ Object
Single output -> value; several -> Array.
-
#encode(args = [], kwargs = {}) ⇒ Object
Positional args or keyword args (matched on snake_cased input names).
-
#initialize(definition) ⇒ Function
constructor
A new instance of Function.
- #input_names ⇒ Object
- #inspect ⇒ Object
- #payable? ⇒ Boolean
- #read? ⇒ Boolean
- #resolve_args(args, kwargs) ⇒ Object
- #ruby_name ⇒ Object
- #selector ⇒ Object
- #signature ⇒ Object
- #to_s ⇒ Object
- #write? ⇒ Boolean
Constructor Details
#initialize(definition) ⇒ Function
Returns a new instance of Function.
8 9 10 11 12 13 14 |
# File 'lib/block_given/abi/function.rb', line 8 def initialize(definition) definition = definition.transform_keys(&:to_s) @name = definition["name"].to_s @inputs = Array(definition["inputs"]).each_with_index.map { |i, idx| Parameter.new(i, index: idx) } @outputs = Array(definition["outputs"]).each_with_index.map { |o, idx| Parameter.new(o, index: idx) } @state_mutability = (definition["stateMutability"] || legacy_mutability(definition)).to_s end |
Instance Attribute Details
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
6 7 8 |
# File 'lib/block_given/abi/function.rb', line 6 def inputs @inputs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/block_given/abi/function.rb', line 6 def name @name end |
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
6 7 8 |
# File 'lib/block_given/abi/function.rb', line 6 def outputs @outputs end |
#state_mutability ⇒ Object (readonly)
Returns the value of attribute state_mutability.
6 7 8 |
# File 'lib/block_given/abi/function.rb', line 6 def state_mutability @state_mutability end |
Instance Method Details
#decode_output(hex) ⇒ Object
Single output -> value; several -> Array.
33 34 35 36 |
# File 'lib/block_given/abi/function.rb', line 33 def decode_output(hex) values = Coder.decode(outputs, hex) outputs.size == 1 ? values.first : values end |
#encode(args = [], kwargs = {}) ⇒ Object
Positional args or keyword args (matched on snake_cased input names).
27 28 29 30 |
# File 'lib/block_given/abi/function.rb', line 27 def encode(args = [], kwargs = {}) values = resolve_args(args, kwargs) selector + Utils.strip_hex(Coder.encode(inputs, values)) end |
#input_names ⇒ Object
24 |
# File 'lib/block_given/abi/function.rb', line 24 def input_names = inputs.map(&:ruby_name) |
#inspect ⇒ Object
60 |
# File 'lib/block_given/abi/function.rb', line 60 def inspect = "#<BlockGiven::Abi::Function #{signature} #{state_mutability}>" |
#payable? ⇒ Boolean
22 |
# File 'lib/block_given/abi/function.rb', line 22 def payable? = state_mutability == "payable" |
#read? ⇒ Boolean
20 |
# File 'lib/block_given/abi/function.rb', line 20 def read? = %w[view pure].include?(state_mutability) |
#resolve_args(args, kwargs) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/block_given/abi/function.rb', line 38 def resolve_args(args, kwargs) raise InvalidArgumentError, "#{name}: mix of positional and keyword arguments" if !args.empty? && !kwargs.empty? return args if kwargs.empty? if inputs.any?(&:unnamed?) raise InvalidArgumentError, "#{name}: inputs are unnamed in the ABI, use positional arguments" end normalized = kwargs.transform_keys { |k| Utils.snake_case(k).to_sym } unknown = normalized.keys - input_names unless unknown.empty? raise InvalidArgumentError, "#{name}: unknown argument(s) #{unknown.join(', ')} (expected #{input_names.join(', ')})" end missing = input_names - normalized.keys raise InvalidArgumentError, "#{name}: missing argument(s) #{missing.join(', ')}" unless missing.empty? input_names.map { |n| normalized[n] } end |
#ruby_name ⇒ Object
16 |
# File 'lib/block_given/abi/function.rb', line 16 def ruby_name = Utils.snake_case(name).to_sym |
#selector ⇒ Object
18 |
# File 'lib/block_given/abi/function.rb', line 18 def selector = @selector ||= Utils.keccak256(signature)[0, 10] |
#signature ⇒ Object
17 |
# File 'lib/block_given/abi/function.rb', line 17 def signature = "#{name}(#{inputs.map(&:type).join(',')})" |
#to_s ⇒ Object
59 |
# File 'lib/block_given/abi/function.rb', line 59 def to_s = signature |
#write? ⇒ Boolean
21 |
# File 'lib/block_given/abi/function.rb', line 21 def write? = !read? |