Class: EthereumContractABI::ContractInterface::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/ethereum-contract-abi/contract/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, inputs, outputs) ⇒ Function

Returns a new instance of Function.



15
16
17
18
19
20
21
# File 'lib/ethereum-contract-abi/contract/function.rb', line 15

def initialize(name, inputs, outputs)
  @name = name
  @inputs = inputs
  @outputs = outputs
  @decoder = FunctionDecoder.new(outputs)
  @encoder = FunctionEncoder.new(inputs)
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



13
14
15
# File 'lib/ethereum-contract-abi/contract/function.rb', line 13

def inputs
  @inputs
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/ethereum-contract-abi/contract/function.rb', line 13

def name
  @name
end

#outputsObject (readonly)

Returns the value of attribute outputs.



13
14
15
# File 'lib/ethereum-contract-abi/contract/function.rb', line 13

def outputs
  @outputs
end

Instance Method Details

#decode_output(output_string) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/ethereum-contract-abi/contract/function.rb', line 48

def decode_output(output_string)
  output_hex_string = EthereumContractABI::Util.remove_hex_prefix(output_string)
  if has_any_dynamic_outputs
    @decoder.decode_dynamic_output(output_hex_string)
  else
    @decoder.decode_static_output(output_hex_string)
  end
end

#encode_call(*args) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
# File 'lib/ethereum-contract-abi/contract/function.rb', line 38

def encode_call(*args)
  raise ArgumentError.new("Invalid function arguments") unless valid_args?(args)
  if has_any_dynamic_inputs
    encoded_input = @encoder.encode_dynamic_input(args)
  else
    encoded_input = @encoder.encode_static_input(args)
  end
  EthereumContractABI::Util.fromHexByteString(method_id + encoded_input)
end

#method_idObject



28
29
30
31
# File 'lib/ethereum-contract-abi/contract/function.rb', line 28

def method_id
  hash = [Digest::Keccak256.new.hexdigest(signature)].pack("H*")
  hash.slice(0..3)
end

#signatureObject



23
24
25
26
# File 'lib/ethereum-contract-abi/contract/function.rb', line 23

def signature
  params = @inputs.map { |i| i.type.to_s }
  "#{@name}(#{params.join(",")})"
end

#valid_args?(args) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/ethereum-contract-abi/contract/function.rb', line 33

def valid_args?(args)
  return false unless args.size === @inputs.size
  true
end