Class: Web3::Hpb::Contract
- Inherits:
-
Object
- Object
- Web3::Hpb::Contract
- Defined in:
- lib/web3/hpb/contract.rb
Defined Under Namespace
Classes: ContractInstance, ContractMethod
Instance Attribute Summary collapse
-
#abi ⇒ Object
readonly
Returns the value of attribute abi.
-
#constructor ⇒ Object
readonly
Returns the value of attribute constructor.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#events_by_hash ⇒ Object
readonly
Returns the value of attribute events_by_hash.
-
#fucntions_by_hash ⇒ Object
readonly
Returns the value of attribute fucntions_by_hash.
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#web3_rpc ⇒ Object
readonly
Returns the value of attribute web3_rpc.
Instance Method Summary collapse
- #at(address) ⇒ Object
- #call_contract(contract_address, method_name, args) ⇒ Object
- #find_event_by_hash(method_hash) ⇒ Object
- #find_function_by_hash(method_hash) ⇒ Object
-
#initialize(abi, web_rpc = nil) ⇒ Contract
constructor
A new instance of Contract.
- #parse_call_args(transaction) ⇒ Object
- #parse_constructor_args(transaction) ⇒ Object
- #parse_log_args(log) ⇒ Object
Constructor Details
#initialize(abi, web_rpc = nil) ⇒ Contract
Returns a new instance of Contract.
100 101 102 103 104 105 |
# File 'lib/web3/hpb/contract.rb', line 100 def initialize(abi, web_rpc = nil) @web3_rpc = web_rpc # parse to json format @abi = abi.is_a?(String) ? JSON.parse(abi) : abi parse_abi(@abi) end |
Instance Attribute Details
#abi ⇒ Object (readonly)
Returns the value of attribute abi.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def abi @abi end |
#constructor ⇒ Object (readonly)
Returns the value of attribute constructor.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def constructor @constructor end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def events @events end |
#events_by_hash ⇒ Object (readonly)
Returns the value of attribute events_by_hash.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def events_by_hash @events_by_hash end |
#fucntions_by_hash ⇒ Object (readonly)
Returns the value of attribute fucntions_by_hash.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def fucntions_by_hash @fucntions_by_hash end |
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def functions @functions end |
#web3_rpc ⇒ Object (readonly)
Returns the value of attribute web3_rpc.
98 99 100 |
# File 'lib/web3/hpb/contract.rb', line 98 def web3_rpc @web3_rpc end |
Instance Method Details
#at(address) ⇒ Object
107 108 109 |
# File 'lib/web3/hpb/contract.rb', line 107 def at(address) ContractInstance.new(self, address) end |
#call_contract(contract_address, method_name, args) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/web3/hpb/contract.rb', line 111 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
118 119 120 |
# File 'lib/web3/hpb/contract.rb', line 118 def find_event_by_hash(method_hash) @events_by_hash[method_hash] end |
#find_function_by_hash(method_hash) ⇒ Object
122 123 124 |
# File 'lib/web3/hpb/contract.rb', line 122 def find_function_by_hash(method_hash) @functions_by_hash[method_hash] end |
#parse_call_args(transaction) ⇒ Object
132 133 134 135 136 |
# File 'lib/web3/hpb/contract.rb', line 132 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
138 139 140 |
# File 'lib/web3/hpb/contract.rb', line 138 def parse_constructor_args(transaction) constructor ? constructor.parse_method_args(transaction) : [] end |
#parse_log_args(log) ⇒ Object
126 127 128 129 130 |
# File 'lib/web3/hpb/contract.rb', line 126 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_method_args(log) end |