Class: Ethlite::ContractMethod

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, inputs:, outputs: [], constant: true, type: 'function') ⇒ ContractMethod

Returns a new instance of ContractMethod.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ethlite/contract.rb', line 39

def initialize( name, inputs:,
                      outputs:  [],
                      constant: true,
                      type:     'function' )
  @name         = name
  @constant     = constant
  @input_types  = inputs
  @output_types = outputs

  @signature      = Abi::Utils.function_signature( @name, @input_types )
  @signature_hash = Abi::Utils.signature_hash( @signature, type=='event' ? 64 : 8)
end

Instance Attribute Details

#constantObject (readonly)

Returns the value of attribute constant.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def constant
  @constant
end

#input_typesObject (readonly)

Returns the value of attribute input_types.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def input_types
  @input_types
end

#nameObject (readonly)

Returns the value of attribute name.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def name
  @name
end

#output_typesObject (readonly)

Returns the value of attribute output_types.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def output_types
  @output_types
end

#signatureObject (readonly)

Returns the value of attribute signature.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def signature
  @signature
end

#signature_hashObject (readonly)

Returns the value of attribute signature_hash.



32
33
34
# File 'lib/ethlite/contract.rb', line 32

def signature_hash
  @signature_hash
end

Class Method Details

._parse_component_type(argument) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/ethlite/contract.rb', line 20

def self._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

.parse_abi(abi) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ethlite/contract.rb', line 4

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

  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 ) } : []
  type           = abi['type'] || 'function'

  new( name, inputs: input_types,
             outputs: output_types,
             constant: constant,
             type: type )
end

Instance Method Details

#do_call(rpc, contract_address, args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ethlite/contract.rb', line 52

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 = [Abi::Utils.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