Class: Eth::Contract::Event
- Inherits:
-
Object
- Object
- Eth::Contract::Event
- Defined in:
- lib/eth/contract/event.rb
Overview
Provide classes for contract event.
Instance Method Summary collapse
-
#address ⇒ String?
Returns the Ethereum address associated with the event.
-
#decode_params(topics, data = "0x") ⇒ ActiveSupport::HashWithIndifferentAccess
Decodes event parameters from logs.
-
#event_string ⇒ String
Returns the event signature string.
-
#initialize(data) ⇒ Event
constructor
Constructor of the Event class.
-
#input_types ⇒ Array<String>
Returns the input types for the event.
-
#inputs ⇒ Array<String>
Returns the names of input parameters.
-
#name ⇒ String
Returns the name of the event.
-
#set_address(address) ⇒ Object
Set the address of the smart contract.
-
#signature ⇒ String
Returns the Keccak-256 event signature hash.
Constructor Details
#initialize(data) ⇒ Event
Constructor of the Eth::Contract::Event class.
24 25 26 |
# File 'lib/eth/contract/event.rb', line 24 def initialize(data) @data = data end |
Instance Method Details
#address ⇒ String?
Returns the Ethereum address associated with the event.
66 67 68 |
# File 'lib/eth/contract/event.rb', line 66 def address @address ||= nil end |
#decode_params(topics, data = "0x") ⇒ ActiveSupport::HashWithIndifferentAccess
Decodes event parameters from logs.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/eth/contract/event.rb', line 82 def decode_params(topics, data = "0x") inputs = @data["inputs"] indexed_inputs, non_indexed_inputs = inputs.partition { _1["indexed"] } { **indexed_inputs.each_with_index.inject({}) do |result, (input, index)| result[input["name"]] = Eth::Abi.decode([input["type"]], topics[index + 1])[0] result end, **Hash[non_indexed_inputs.map { _1["name"] }.zip( Eth::Abi.decode(non_indexed_inputs.map { |i| i["type"] }, data) )], } end |
#event_string ⇒ String
Returns the event signature string.
52 53 54 |
# File 'lib/eth/contract/event.rb', line 52 def event_string @event_string ||= Abi::Event.signature(@data) end |
#input_types ⇒ Array<String>
Returns the input types for the event.
38 39 40 |
# File 'lib/eth/contract/event.rb', line 38 def input_types @input_types ||= @data["inputs"].map { |x| type_name(x) } end |
#inputs ⇒ Array<String>
Returns the names of input parameters.
45 46 47 |
# File 'lib/eth/contract/event.rb', line 45 def inputs @inputs ||= @data["inputs"].map { |x| x["name"] } end |
#name ⇒ String
Returns the name of the event.
31 32 33 |
# File 'lib/eth/contract/event.rb', line 31 def name @data["name"] end |
#set_address(address) ⇒ Object
Set the address of the smart contract
73 74 75 |
# File 'lib/eth/contract/event.rb', line 73 def set_address(address) @address = address ? Eth::Address.new(address).address : nil end |
#signature ⇒ String
Returns the Keccak-256 event signature hash.
59 60 61 |
# File 'lib/eth/contract/event.rb', line 59 def signature @signature ||= Digest::Keccak.hexdigest(event_string, 256) end |