Class: Ethlite::ContractMethod
- Inherits:
-
Object
- Object
- Ethlite::ContractMethod
- Defined in:
- lib/ethlite/contract.rb
Instance Attribute Summary collapse
-
#constant ⇒ Object
readonly
Returns the value of attribute constant.
-
#input_types ⇒ Object
readonly
Returns the value of attribute input_types.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_types ⇒ Object
readonly
Returns the value of attribute output_types.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
-
#signature_hash ⇒ Object
readonly
Returns the value of attribute signature_hash.
Instance Method Summary collapse
-
#debug? ⇒ Boolean
private helpers.
- #do_call(rpc, contract_address, args) ⇒ Object
-
#initialize(name, inputs: [], outputs: [], constant: true) ⇒ ContractMethod
constructor
A new instance of ContractMethod.
Constructor Details
#initialize(name, inputs: [], outputs: [], constant: true) ⇒ ContractMethod
Returns a new instance of ContractMethod.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ethlite/contract.rb', line 12 def initialize( name, inputs: [], outputs: [], constant: true ) @name = name @constant = constant ## parse inputs & outputs into types @input_types = inputs.map { |str| ABI::Type.parse( str ) } @output_types = outputs.map { |str| ABI::Type.parse( str ) } types = @input_types.map {|type| type.format }.join(',') @signature = "#{@name}(#{types})" @signature_hash = Utils.encode_hex( Utils.keccak256( @signature)[0,4] ) end |
Instance Attribute Details
#constant ⇒ Object (readonly)
Returns the value of attribute constant.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def constant @constant end |
#input_types ⇒ Object (readonly)
Returns the value of attribute input_types.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def input_types @input_types end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def name @name end |
#output_types ⇒ Object (readonly)
Returns the value of attribute output_types.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def output_types @output_types end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def signature @signature end |
#signature_hash ⇒ Object (readonly)
Returns the value of attribute signature_hash.
5 6 7 |
# File 'lib/ethlite/contract.rb', line 5 def signature_hash @signature_hash end |
Instance Method Details
#debug? ⇒ Boolean
private helpers
60 |
# File 'lib/ethlite/contract.rb', line 60 def debug?() Ethlite.debug?; end |
#do_call(rpc, contract_address, args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ethlite/contract.rb', line 28 def do_call( rpc, contract_address, args ) data = '0x' + @signature_hash + Utils.encode_hex( ABI.encode(@input_types, args) ) method = 'eth_call' params = [{ to: contract_address, data: data}, 'latest'] response = rpc.request( method, params ) if debug? puts "response:" pp response end bin = Utils.decode_hex( response ) return nil if bin.empty? result = ABI.decode( @output_types, bin ) if debug? puts puts "result decode_abi:" pp result end result.length==1 ? result.first : result end |