Class: EventDecoder

Inherits:
Object
  • Object
show all
Includes:
AbiCoderRb
Defined in:
lib/periphery/event_decoder.rb

Constant Summary

Constants included from AbiCoderRb

AbiCoderRb::BYTE_EMPTY, AbiCoderRb::BYTE_ONE, AbiCoderRb::BYTE_ZERO, AbiCoderRb::INT_MAX, AbiCoderRb::INT_MIN, AbiCoderRb::UINT_MAX, AbiCoderRb::UINT_MIN, AbiCoderRb::VERSION

Instance Attribute Summary collapse

Attributes included from AbiCoderRb

#after_decoding_action, #before_encoding_action

Instance Method Summary collapse

Methods included from AbiCoderRb

#abi_to_int_signed, #after_decoding, #before_encoding, #bin_to_hex, #ceil32, #decode, #decode_array, #decode_fixed_array, #decode_primitive_type, #decode_tuple, #encode, #encode_address, #encode_array, #encode_bool, #encode_bytes, #encode_fixed_array, #encode_int, #encode_primitive_type, #encode_string, #encode_tuple, #encode_uint, #encode_uint256, #hex?, #hex_to_bin, #int_to_abi_signed, #int_to_abi_signed_256bit, #lpad, #lpad_hex, #lpad_int, #rpad

Constructor Details

#initialize(event_abi) ⇒ EventDecoder

flatten_sep: separator for flattening event name if it is nested



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/periphery/event_decoder.rb', line 14

def initialize(event_abi)
  @event_abi = event_abi
  @indexed_topic_inputs, @data_inputs = event_abi["inputs"].partition { |input| input["indexed"] }

  # indexed_topic_inputs:
  @indexed_topic_fields = fields_of(@indexed_topic_inputs)

  # data_inputs:
  @data_fields = fields_of(@data_inputs)
  @data_type_str = fields_type_str(@data_fields)

  # add after_decoding action
  after_decoding lambda { |type, value|
    if type == "address"
      "0x#{value}"
    elsif type.start_with?("bytes")
      "0x#{bin_to_hex(value)}"
    else
      value
    end
  }
end

Instance Attribute Details

#data_fieldsObject (readonly)

Returns the value of attribute data_fields.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def data_fields
  @data_fields
end

#data_inputsObject (readonly)

Returns the value of attribute data_inputs.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def data_inputs
  @data_inputs
end

#data_type_strObject (readonly)

Returns the value of attribute data_type_str.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def data_type_str
  @data_type_str
end

#event_abiObject (readonly)

Returns the value of attribute event_abi.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def event_abi
  @event_abi
end

#indexed_topic_fieldsObject (readonly)

Returns the value of attribute indexed_topic_fields.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def indexed_topic_fields
  @indexed_topic_fields
end

#indexed_topic_inputsObject (readonly)

Returns the value of attribute indexed_topic_inputs.



7
8
9
# File 'lib/periphery/event_decoder.rb', line 7

def indexed_topic_inputs
  @indexed_topic_inputs
end

Instance Method Details

#data_fields_flatten(sep: ".") ⇒ Object



37
38
39
# File 'lib/periphery/event_decoder.rb', line 37

def data_fields_flatten(sep: ".")
  flat_fields(@data_fields, sep: sep)
end

#decode_data(data, flatten: true, sep: ".", with_names: false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/periphery/event_decoder.rb', line 60

def decode_data(data, flatten: true, sep: ".", with_names: false)
  return with_names ? {} : [] if @data_type_str == "()"

  data_values = decode(@data_type_str, hex_to_bin(data))

  case flatten
  when true
    if with_names
      combine(data_field_names(flatten: true, sep: sep), data_values.flatten)
    else
      data_values.flatten
    end
  when false
    if with_names
      combine(data_field_names, data_values)
    else
      data_values
    end
  end
end

#decode_topics(topics, with_names: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/periphery/event_decoder.rb', line 41

def decode_topics(topics, with_names: false)
  topics = topics[1..] if topics.count == @indexed_topic_inputs.count + 1 && @event_abi["anonymous"] == false

  raise "topics count not match" if topics.count != @indexed_topic_inputs.count

  indexed_topic_types = @indexed_topic_inputs.map { |input| input["type"] }

  values = topics.each_with_index.map do |topic, i|
    indexed_topic_type = indexed_topic_types[i]
    decode(indexed_topic_type, hex_to_bin(topic))
  end

  if with_names
    combine(@indexed_topic_inputs.map { |input| input["name"].underscore }, values)
  else
    values
  end
end