class Web3::Eth::Contract

Attributes

abi[R]
constructor[R]
events[R]
events_by_hash[R]
functions[R]
functions_by_hash[R]
web3_rpc[R]

Public Class Methods

new(abi, web_rpc = nil) click to toggle source
# File lib/web3/eth/contract.rb, line 154
def initialize abi, web_rpc = nil
  @web3_rpc = web_rpc
  @abi = abi.kind_of?(String) ? JSON.parse(abi) : abi
  parse_abi @abi
end

Public Instance Methods

at(address) click to toggle source
# File lib/web3/eth/contract.rb, line 160
def at address
  ContractInstance.new self, address
end
call_contract(contract_address, method_name, args) click to toggle source
# File lib/web3/eth/contract.rb, line 164
def call_contract contract_address, method_name, args
  function = functions[method_name]
  raise "No method found in ABI: #{method_name}" unless function
  raise "Function #{method_name} is not constant: #{method_name}, requires to sign transaction" unless function.constant
  function.do_call web3_rpc, contract_address, args
end
find_event_by_hash(method_hash) click to toggle source
# File lib/web3/eth/contract.rb, line 171
def find_event_by_hash method_hash
  @events_by_hash[method_hash]
end
find_function_by_hash(method_hash) click to toggle source
# File lib/web3/eth/contract.rb, line 175
def find_function_by_hash method_hash
  @functions_by_hash[method_hash]
end
parse_call_args(transaction) click to toggle source
# File lib/web3/eth/contract.rb, line 185
def parse_call_args transaction
  function = find_function_by_hash transaction.method_hash
  raise "No function found by hash #{transaction.method_hash}, probably ABI is not related to call" unless function
  function.parse_method_args transaction
end
parse_constructor_args(transaction) click to toggle source
# File lib/web3/eth/contract.rb, line 192
def parse_constructor_args transaction
  constructor ? constructor.parse_method_args(transaction) : []
end
parse_log_args(log) click to toggle source
# File lib/web3/eth/contract.rb, line 179
def parse_log_args log
  event = find_event_by_hash log.method_hash
  raise "No event found by hash #{log.method_hash}, probably ABI is not related to log event" unless event
  event.parse_event_args log
end

Private Instance Methods

parse_abi(abi) click to toggle source
# File lib/web3/eth/contract.rb, line 198
def parse_abi abi
  @functions = {}
  @events = {}

  @functions_by_hash = {}
  @events_by_hash = {}

  abi.each{|a|

    case a['type'].try(:downcase)
      when 'function'
        method = ContractMethod.new(a)
        @functions[method.name] = method
        @functions_by_hash[method.signature_hash] = method
      when 'event'
        method = ContractMethod.new(a)
        @events[method.name] = method
        @events_by_hash[method.signature_hash] = method
      when 'constructor'
        @constructor = ContractConstructor.new(a)
    end
  }
end