Class: EthereumContractABI::Contract

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

Instance Method Summary collapse

Constructor Details

#initialize(functions, events) ⇒ Contract

Returns a new instance of Contract.



10
11
12
13
14
15
16
17
# File 'lib/ethereum-contract-abi/contract.rb', line 10

def initialize(functions, events)
  @functions = functions.to_h { |func| [func.name, func]}
  @events = events

  functions.each do |f|
    self.class.send(:define_method, f.name) { function(f.name) }
  end
end

Instance Method Details

#function(name) ⇒ Object



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

def function(name)
  @functions.dig(name)
end

#function_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ethereum-contract-abi/contract.rb', line 27

def function_exists?(name)
  @functions.key? name
end

#functionsObject



19
20
21
# File 'lib/ethereum-contract-abi/contract.rb', line 19

def functions
  @functions.values
end

#has_function?(func) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ethereum-contract-abi/contract.rb', line 44

def has_function?(func)
  return false unless function_exists?(func.name)
  contract_func = function(func.name)
  return false unless contract_func.inputs.size == func.inputs.size
  return false unless contract_func.outputs.size == func.outputs.size

  input_type_strings = contract_func.inputs.map {|i| i.type.to_s }
  expected_input_type_strings = func.inputs.map {|i| i.type.to_s }
  output_type_string = contract_func.outputs.map {|i| i.type.to_s }
  expected_output_type_string = func.outputs.map {|i| i.type.to_s }

  Set.new(expected_input_type_strings) == Set.new(input_type_strings) &&
    Set.new(expected_output_type_string) == Set.new(output_type_string)
end

#implements_interface(identifier) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ethereum-contract-abi/contract.rb', line 31

def implements_interface(identifier)
  case identifier
  when EIP::
    EIP::ERC721MetadataInterface.is_implemented_by?(self)
  when EIP::ERC721_ENUMERABLE_ID
    EIP::ERC721EnumerableInterface.is_implemented_by?(self)
  when EIP::
    EIP::ERC1155MetadataInterface.is_implemented_by?(self)
  else
    raise ArgumentError.new('Unknown interface identifier')
  end
end