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

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

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

Constructor Details

#initialize(abi) ⇒ ContractMethod

Returns a new instance of ContractMethod.



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

def initialize abi
  @abi = abi
  @name = abi['name']
  @constant = !!abi['constant']
  @input_types = abi['inputs'].map{|a| a['type']}
  @output_types = abi['outputs'].map{|a| a['type']} if abi['outputs']
  @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.



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

def abi
  @abi
end

#constantObject (readonly)

Returns the value of attribute constant.



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

def constant
  @constant
end

#input_typesObject (readonly)

Returns the value of attribute input_types.



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

def input_types
  @input_types
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#output_typesObject (readonly)

Returns the value of attribute output_types.



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

def output_types
  @output_types
end

#signatureObject (readonly)

Returns the value of attribute signature.



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

def signature
  @signature
end

#signature_hashObject (readonly)

Returns the value of attribute signature_hash.



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

def signature_hash
  @signature_hash
end

Instance Method Details

#do_call(web3_rpc, contract_address, args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/web3ethereum/contract.rb', line 84

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

  response = web3_rpc.request "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_event_args(log) ⇒ Object



44
45
46
47
48
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
75
76
# File 'lib/web3ethereum/contract.rb', line 44

def parse_event_args log

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

  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| a['type']}
    [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



79
80
81
82
# File 'lib/web3ethereum/contract.rb', line 79

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