Class: Eth::Contract::Event

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

Overview

Provide classes for contract event.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Event

Constructor of the Eth::Contract::Event class.

Parameters:

  • data (Hash)

    contract event data.



24
25
26
# File 'lib/eth/contract/event.rb', line 24

def initialize(data)
  @data = data
end

Instance Method Details

#addressString?

Returns the Ethereum address associated with the event.

Returns:

  • (String, nil)

    The Ethereum address, or nil if not set.



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.

Parameters:

  • topics (Array<String>)

    The list of log topics, including the event selector.

  • data (String) (defaults to: "0x")

    The log data containing non-indexed parameters.

Returns:

  • (ActiveSupport::HashWithIndifferentAccess)

    A hash of decoded event parameters.



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_stringString

Returns the event signature string.

Returns:

  • (String)

    The event signature string, generated from ABI.



52
53
54
# File 'lib/eth/contract/event.rb', line 52

def event_string
  @event_string ||= Abi::Event.signature(@data)
end

#input_typesArray<String>

Returns the input types for the event.

Returns:

  • (Array<String>)

    An array of input type names.



38
39
40
# File 'lib/eth/contract/event.rb', line 38

def input_types
  @input_types ||= @data["inputs"].map { |x| type_name(x) }
end

#inputsArray<String>

Returns the names of input parameters.

Returns:

  • (Array<String>)

    An array of input parameter names.



45
46
47
# File 'lib/eth/contract/event.rb', line 45

def inputs
  @inputs ||= @data["inputs"].map { |x| x["name"] }
end

#nameString

Returns the name of the event.

Returns:

  • (String)

    The event name.



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

Parameters:

  • address (String)

    contract address.



73
74
75
# File 'lib/eth/contract/event.rb', line 73

def set_address(address)
  @address = address ? Eth::Address.new(address).address : nil
end

#signatureString

Returns the Keccak-256 event signature hash.

Returns:

  • (String)

    The event signature hash in hexadecimal format.



59
60
61
# File 'lib/eth/contract/event.rb', line 59

def signature
  @signature ||= Digest::Keccak.hexdigest(event_string, 256)
end