Class: Ethlite::ContractMethod

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/ethlite/contract.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#from_hex, #hex, #remove_0x_head, #wei_to_ether

Constructor Details

#initialize(abi) ⇒ ContractMethod

Returns a new instance of ContractMethod.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ethlite/contract.rb', line 13

def initialize( abi )
  ## convenience helper -  auto-convert to json if string passed in
  abi = JSON.parse( abi ) if abi.is_a?( String )

  @abi            = abi
  @name           = abi['name']
  @constant       = !!abi['constant'] || abi['stateMutability']=='view'
  @input_types    = abi['inputs']  ? abi['inputs'].map{|a| parse_component_type a } : []
  @output_types   = abi['outputs'] ? abi['outputs'].map{|a| parse_component_type a } : nil
  @signature      = Abi::Utils.function_signature( @name, @input_types )
  @signature_hash = Abi::Utils.signature_hash( @signature, abi['type']=='event' ? 64 : 8)
end

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def abi
  @abi
end

#constantObject (readonly)

Returns the value of attribute constant.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def constant
  @constant
end

#input_typesObject (readonly)

Returns the value of attribute input_types.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def input_types
  @input_types
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def name
  @name
end

#output_typesObject (readonly)

Returns the value of attribute output_types.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def output_types
  @output_types
end

#signatureObject (readonly)

Returns the value of attribute signature.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def signature
  @signature
end

#signature_hashObject (readonly)

Returns the value of attribute signature_hash.



5
6
7
# File 'lib/ethlite/contract.rb', line 5

def signature_hash
  @signature_hash
end

Instance Method Details

#do_call(rpc, contract_address, args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ethlite/contract.rb', line 36

def do_call( rpc, contract_address, args )
  data = '0x' + @signature_hash + Abi::Utils.encode_hex(
                                   Abi::AbiCoder.encode_abi(@input_types, args) )

  method = 'eth_call'
  params = [{ to:   contract_address,
              data: data},
            'latest']
  response = rpc.request( method, params )


  puts "response:"
  pp response

  string_data = [remove_0x_head(response)].pack('H*')
  return nil if string_data.empty?

  result = Abi::AbiCoder.decode_abi( @output_types, string_data )
  puts
  puts "result decode_abi:"
  pp result


  result.length==1 ? result.first : result
end

#parse_component_type(argument) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ethlite/contract.rb', line 26

def parse_component_type( argument )
  if argument['type'] =~ /^tuple((\[[0-9]*\])*)/
    argument['components'] ? "(#{argument['components'].collect{|c| parse_component_type( c ) }.join(',')})#{$1}"
                           : "()#{$1}"
  else
    argument['type']
  end
end