Class: Web3::Eth::Contract

Inherits:
Object
  • Object
show all
Defined in:
lib/web3/eth/contract.rb

Defined Under Namespace

Classes: ContractInstance, ContractMethod

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(abi, web_rpc = nil) ⇒ Contract

Returns a new instance of Contract.



108
109
110
111
112
# File 'lib/web3/eth/contract.rb', line 108

def initialize abi, web_rpc = nil
  @web3_rpc = web_rpc
  @abi = abi.kind_of?(String) ? JSON.parse(abi) : abi
  parse_abi @abi
end

Instance Attribute Details

#abiObject (readonly)

Returns the value of attribute abi.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def abi
  @abi
end

#constructorObject (readonly)

Returns the value of attribute constructor.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def constructor
  @constructor
end

#eventsObject (readonly)

Returns the value of attribute events.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def events
  @events
end

#events_by_hashObject (readonly)

Returns the value of attribute events_by_hash.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def events_by_hash
  @events_by_hash
end

#functionsObject (readonly)

Returns the value of attribute functions.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def functions
  @functions
end

#functions_by_hashObject (readonly)

Returns the value of attribute functions_by_hash.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def functions_by_hash
  @functions_by_hash
end

#web3_rpcObject (readonly)

Returns the value of attribute web3_rpc.



106
107
108
# File 'lib/web3/eth/contract.rb', line 106

def web3_rpc
  @web3_rpc
end

Instance Method Details

#at(address) ⇒ Object



114
115
116
# File 'lib/web3/eth/contract.rb', line 114

def at address
  ContractInstance.new self, address
end

#call_contract(contract_address, method_name, args) ⇒ Object



118
119
120
121
122
123
# File 'lib/web3/eth/contract.rb', line 118

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) ⇒ Object



125
126
127
# File 'lib/web3/eth/contract.rb', line 125

def find_event_by_hash method_hash
  @events_by_hash[method_hash]
end

#find_function_by_hash(method_hash) ⇒ Object



129
130
131
# File 'lib/web3/eth/contract.rb', line 129

def find_function_by_hash method_hash
  @functions_by_hash[method_hash]
end

#parse_call_args(transaction) ⇒ Object



139
140
141
142
143
# File 'lib/web3/eth/contract.rb', line 139

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) ⇒ Object



146
147
148
# File 'lib/web3/eth/contract.rb', line 146

def parse_constructor_args transaction
  constructor ? constructor.parse_method_args(transaction) : []
end

#parse_log_args(log) ⇒ Object



133
134
135
136
137
# File 'lib/web3/eth/contract.rb', line 133

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