class Web3::Eth::Contract::ContractMethod

Attributes

abi[R]
constant[R]
input_types[R]
name[R]
output_types[R]
signature[R]
signature_hash[R]

Public Class Methods

new(abi) click to toggle source
# 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

Public Instance Methods

do_call(web3_rpc, contract_address, args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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