Class: Web3::Eth::Contract::ContractMethod

Inherits:
Object
  • Object
show all
Includes:
Abi::AbiCoder, Abi::Utils, Utility
Defined in:
lib/web3/eth/contract.rb

Direct Known Subclasses

ContractConstructor

Constant Summary

Constants included from Abi::Constant

Abi::Constant::BYTE_EMPTY, Abi::Constant::BYTE_ONE, Abi::Constant::BYTE_ZERO, Abi::Constant::CONTRACT_CODE_SIZE_LIMIT, Abi::Constant::HASH_ZERO, Abi::Constant::INT_MAX, Abi::Constant::INT_MIN, Abi::Constant::PRIVKEY_ZERO, Abi::Constant::PRIVKEY_ZERO_HEX, Abi::Constant::PUBKEY_ZERO, Abi::Constant::TT160, Abi::Constant::TT256, Abi::Constant::TT32, Abi::Constant::TT40, Abi::Constant::TT64M1, Abi::Constant::UINT_MAX, Abi::Constant::UINT_MIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#from_hex, #hex, #remove_0x_head, #wei_to_ether

Methods included from Abi::Utils

#base58_check_to_bytes, #big_endian_to_int, #bytearray_to_int, #bytes_to_base58_check, #bytes_to_int_array, #ceil32, #coerce_addr_to_hex, #coerce_to_bytes, #coerce_to_int, #decode_hex, #decode_int, #double_sha256, #encode_hex, #encode_int, #function_signature, #hash160, #hash160_hex, #int_array_to_bytes, #int_to_addr, #int_to_big_endian, #keccak256, #keccak256_rlp, #keccak512, #lpad, #mk_contract_address, #mk_metropolis_contract_address, #mod_exp, #mod_mul, #normalize_address, #normalize_hex_without_prefix, #parse_int_or_hex, #ripemd160, #rpad, #sha256, #to_signed, #zpad, #zpad_hex, #zpad_int, #zunpad

Methods included from Abi::AbiCoder

#decode_abi, #decode_primitive_type, #decode_type, #decode_typed_data, #encode_abi, #encode_primitive_type, #encode_type, #min_data_size, #zero_padding

Constructor Details

#initialize(abi) ⇒ ContractMethod

Returns a new instance of ContractMethod.



34
35
36
37
38
39
40
41
42
# File 'lib/web3/eth/contract.rb', line 34

def initialize abi
  @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'].map{|a| parse_component_type a } if abi['outputs']
  @signature = Abi::Utils.function_signature @name, @input_types
  @signature_hash = Abi::Utils.signature_hash @signature, (abi['type'].try(:downcase)=='event' ? 64 : 8)
end

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



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

def abi
  @abi
end

#constantObject (readonly)

Returns the value of attribute constant.



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

def constant
  @constant
end

#input_typesObject (readonly)

Returns the value of attribute input_types.



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

def input_types
  @input_types
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#output_typesObject (readonly)

Returns the value of attribute output_types.



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

def output_types
  @output_types
end

#signatureObject (readonly)

Returns the value of attribute signature.



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

def signature
  @signature
end

#signature_hashObject (readonly)

Returns the value of attribute signature_hash.



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

def signature_hash
  @signature_hash
end

Instance Method Details

#do_call(web3_rpc, contract_address, args) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/web3/eth/contract.rb', line 92

def do_call web3_rpc, contract_address, args
  data = '0x' + signature_hash + encode_hex(encode_abi(input_types, args) )

  response = web3_rpc.eth.call [{ to: contract_address, data: data}, 'latest']

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

  result = decode_abi output_types, string_data
  result.length==1 ? result.first : result
end

#parse_component_type(argument) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/web3/eth/contract.rb', line 44

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

#parse_event_args(log) ⇒ 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
77
78
79
80
81
82
83
84
# File 'lib/web3/eth/contract.rb', line 52

def parse_event_args log

  log_data = remove_0x_head log.raw_data['data']
  indexed_types = abi['inputs'].select{|a| a['indexed']}.collect{|a| parse_component_type a }
  not_indexed_types = abi['inputs'].select{|a| !a['indexed']}.collect{|a| parse_component_type a }

  indexed_args = log.indexed_args

  if indexed_args.size==indexed_types.size

    indexed_values = [indexed_types, indexed_args].transpose.collect{|arg|
      decode_typed_data( arg.first, [arg.second].pack('H*') )
    }

    not_indexed_values = not_indexed_types.empty? ? [] :
                             decode_abi(not_indexed_types, [log_data].pack('H*') )

    i = j = 0

    abi['inputs'].collect{|input|
      input['indexed'] ? (i+=1; indexed_values[i-1]) : (j+=1;not_indexed_values[j-1])
    }

  elsif !indexed_args.empty? || !log_data.empty?
    all_types = abi['inputs'].collect{|a| parse_component_type a }
    [all_types[0...indexed_args.size], indexed_args].transpose.collect{|arg|
      decode_typed_data( arg.first, [arg.second].pack('H*') )
    } + decode_abi(all_types[indexed_args.size..-1], [log_data].pack('H*') )
  else
    []
  end

end

#parse_method_args(transaction) ⇒ Object



87
88
89
90
# File 'lib/web3/eth/contract.rb', line 87

def parse_method_args transaction
  d = transaction.call_input_data
  (!d || d.empty?) ? [] : decode_abi(input_types, [d].pack('H*'))
end