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) ⇒ ContractMethod

Returns a new instance of ContractMethod.



37
38
39
40
41
42
43
44
45
46
# File 'lib/ethlite/contract.rb', line 37

def initialize( name, inputs:,
                      outputs:  [],
                      constant: true )
  @name         = name
  @constant     = constant
  @input_types  = inputs
  @output_types = outputs
  @signature      = "#{@name}(#{@input_types.join(',')})"
  @signature_hash = Utils.signature_hash( @signature, 8 )
end

Instance Attribute Details

#constantObject (readonly)

Returns the value of attribute constant.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def constant
  @constant
end

#input_typesObject (readonly)

Returns the value of attribute input_types.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def input_types
  @input_types
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def name
  @name
end

#output_typesObject (readonly)

Returns the value of attribute output_types.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def output_types
  @output_types
end

#signatureObject (readonly)

Returns the value of attribute signature.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def signature
  @signature
end

#signature_hashObject (readonly)

Returns the value of attribute signature_hash.



30
31
32
# File 'lib/ethlite/contract.rb', line 30

def signature_hash
  @signature_hash
end

Class Method Details

._parse_component_type(argument) ⇒ Object



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

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
# 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 ) } : []

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

Instance Method Details

#do_call(rpc, contract_address, args) ⇒ Object



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

def do_call( rpc, contract_address, args )
  data = '0x' + @signature_hash + Utils.encode_hex(
                                   Abi.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 = Utils.decode_hex(
                   Utils.remove_0x_head(response))
  return nil if string_data.empty?

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


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