Class: Eth::Contract::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/contract/function.rb

Overview

Provides the methods for smart contract function.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Function

Constructor of the Function class.

Parameters:

  • data (Hash)

    function input and output data.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eth/contract/function.rb', line 27

def initialize(data)
  @name = data["name"]
  @constant = data["constant"]
  @inputs = data["inputs"].map do |input|
    Eth::Contract::FunctionInput.new(input)
  end
  @outputs = data["outputs"].collect do |output|
    Eth::Contract::FunctionOutput.new(output)
  end
  @function_string = self.class.calc_signature(@name, @inputs)
  @signature = self.class.encoded_function_signature(@function_string)
end

Instance Attribute Details

#constantObject

Returns the value of attribute constant.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def constant
  @constant
end

#function_stringObject

Returns the value of attribute function_string.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def function_string
  @function_string
end

#inputsObject

Returns the value of attribute inputs.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def inputs
  @inputs
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def name
  @name
end

#outputsObject

Returns the value of attribute outputs.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def outputs
  @outputs
end

#signatureObject

Returns the value of attribute signature.



22
23
24
# File 'lib/eth/contract/function.rb', line 22

def signature
  @signature
end

Class Method Details

.calc_signature(name, inputs) ⇒ String

Creates function strings.

Parameters:

Returns:

  • (String)

    function string.



45
46
47
# File 'lib/eth/contract/function.rb', line 45

def self.calc_signature(name, inputs)
  "#{name}(#{inputs.map { |x| x.parsed_type.to_s }.join(",")})"
end

.encoded_function_signature(signature) ⇒ String

Encodes a function signature.

Parameters:

  • signature (String)

    function signature.

Returns:

  • (String)

    encoded function signature string.



53
54
55
# File 'lib/eth/contract/function.rb', line 53

def self.encoded_function_signature(signature)
  Util.bin_to_hex Util.keccak256(signature)[0..3]
end

Instance Method Details

#decode_call_result(data) ⇒ Array

Decodes a function call result

Parameters:

  • data (String)

    eth_call result in hex format

Returns:

  • (Array)


71
72
73
74
75
76
# File 'lib/eth/contract/function.rb', line 71

def decode_call_result(data)
  return nil if data == "0x"

  types = outputs.map(&:parsed_type)
  Eth::Abi.decode(types, data)
end

#encode_call(*args) ⇒ String

Encodes a function call arguments

Parameters:

  • args (Array)

    function arguments

Returns:

  • (String)

    encoded function call data



61
62
63
64
65
# File 'lib/eth/contract/function.rb', line 61

def encode_call(*args)
  types = inputs.map(&:parsed_type)
  encoded_str = Util.bin_to_hex(Eth::Abi.encode(types, args))
  Util.prefix_hex(signature + (encoded_str.empty? ? "0" * 64 : encoded_str))
end