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