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.



98
99
100
101
102
# File 'lib/web3/eth/contract.rb', line 98

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.



96
97
98
# File 'lib/web3/eth/contract.rb', line 96

def abi
  @abi
end

#constructorObject (readonly)

Returns the value of attribute constructor.



96
97
98
# File 'lib/web3/eth/contract.rb', line 96

def constructor
  @constructor
end

#eventsObject (readonly)

Returns the value of attribute events.



96
97
98
# File 'lib/web3/eth/contract.rb', line 96

def events
  @events
end

#functionsObject (readonly)

Returns the value of attribute functions.



96
97
98
# File 'lib/web3/eth/contract.rb', line 96

def functions
  @functions
end

#web3_rpcObject (readonly)

Returns the value of attribute web3_rpc.



96
97
98
# File 'lib/web3/eth/contract.rb', line 96

def web3_rpc
  @web3_rpc
end

Instance Method Details

#at(address) ⇒ Object



104
105
106
# File 'lib/web3/eth/contract.rb', line 104

def at address
  ContractInstance.new self, address
end

#call_contract(contract_address, method_name, args) ⇒ Object



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

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



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

def find_event_by_hash method_hash
  events.values.detect{|e| e.signature_hash==method_hash}
end

#find_function_by_hash(method_hash) ⇒ Object



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

def find_function_by_hash method_hash
  functions.values.detect{|e| e.signature_hash==method_hash}
end

#parse_call_args(transaction) ⇒ Object



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

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



136
137
138
# File 'lib/web3/eth/contract.rb', line 136

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

#parse_log_args(log) ⇒ Object



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

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