Class: BlockGiven::Abi::Event
- Inherits:
-
Object
- Object
- BlockGiven::Abi::Event
- Defined in:
- lib/block_given/abi/event.rb
Instance Attribute Summary collapse
-
#anonymous ⇒ Object
readonly
Returns the value of attribute anonymous.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #anonymous? ⇒ Boolean
- #data_inputs ⇒ Object
-
#decode(log) ⇒ Object
Decodes a normalized log (Hash with :topics and :data) into a BlockGiven::Event.
-
#encode_topics(filters = {}) ⇒ Object
Builds the topics filter array for eth_getLogs from indexed argument values.
- #indexed_inputs ⇒ Object
-
#initialize(definition) ⇒ Event
constructor
A new instance of Event.
- #inspect ⇒ Object
- #matches?(log) ⇒ Boolean
- #ruby_name ⇒ Object
- #signature ⇒ Object
- #to_s ⇒ Object
- #topic ⇒ Object
Constructor Details
#initialize(definition) ⇒ Event
Returns a new instance of Event.
8 9 10 11 12 13 |
# File 'lib/block_given/abi/event.rb', line 8 def initialize(definition) definition = definition.transform_keys(&:to_s) @name = definition["name"].to_s @inputs = Array(definition["inputs"]).each_with_index.map { |i, idx| Parameter.new(i, index: idx) } @anonymous = !!definition["anonymous"] end |
Instance Attribute Details
#anonymous ⇒ Object (readonly)
Returns the value of attribute anonymous.
6 7 8 |
# File 'lib/block_given/abi/event.rb', line 6 def anonymous @anonymous end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
6 7 8 |
# File 'lib/block_given/abi/event.rb', line 6 def inputs @inputs end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/block_given/abi/event.rb', line 6 def name @name end |
Instance Method Details
#anonymous? ⇒ Boolean
18 |
# File 'lib/block_given/abi/event.rb', line 18 def anonymous? = anonymous |
#data_inputs ⇒ Object
21 |
# File 'lib/block_given/abi/event.rb', line 21 def data_inputs = inputs.reject(&:indexed?) |
#decode(log) ⇒ Object
Decodes a normalized log (Hash with :topics and :data) into a BlockGiven::Event.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/block_given/abi/event.rb', line 29 def decode(log) topics = Array(log[:topics] || log["topics"]) data = log[:data] || log["data"] || "0x" indexed_topics = anonymous? ? topics : topics[1..] || [] args = {} indexed_inputs.each_with_index do |param, i| args[param.ruby_name] = decode_topic(indexed_topics[i], param) end unless data_inputs.empty? Coder.decode(data_inputs, data).each_with_index do |value, i| args[data_inputs[i].ruby_name] = value end end # keep declaration order ordered = inputs.to_h { |p| [p.ruby_name, args[p.ruby_name]] } BlockGiven::Event.new(name: name, signature: signature, args: ordered, log: log) end |
#encode_topics(filters = {}) ⇒ Object
Builds the topics filter array for eth_getLogs from indexed argument values. Values can be nil (wildcard), a single value or an Array (OR).
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/block_given/abi/event.rb', line 50 def encode_topics(filters = {}) normalized = filters.transform_keys { |k| Utils.snake_case(k).to_sym } unknown = normalized.keys - indexed_inputs.map(&:ruby_name) unless unknown.empty? indexed = indexed_inputs.map(&:ruby_name).join(", ") raise InvalidArgumentError, "#{name}: #{unknown.join(', ')} is not an indexed parameter (indexed: #{indexed})" end topics = indexed_inputs.map do |param| value = normalized[param.ruby_name] next nil if value.nil? value.is_a?(Array) ? value.map { |v| encode_topic(v, param) } : encode_topic(value, param) end topics = topics.reverse.drop_while(&:nil?).reverse # trailing wildcards are implicit anonymous? ? topics : [topic, *topics] end |
#indexed_inputs ⇒ Object
20 |
# File 'lib/block_given/abi/event.rb', line 20 def indexed_inputs = inputs.select(&:indexed?) |
#inspect ⇒ Object
69 |
# File 'lib/block_given/abi/event.rb', line 69 def inspect = "#<BlockGiven::Abi::Event #{signature}>" |
#matches?(log) ⇒ Boolean
23 24 25 26 |
# File 'lib/block_given/abi/event.rb', line 23 def matches?(log) topic0 = Array(log[:topics] || log["topics"]).first !anonymous? && topic0&.downcase == topic end |
#ruby_name ⇒ Object
15 |
# File 'lib/block_given/abi/event.rb', line 15 def ruby_name = Utils.snake_case(name).to_sym |
#signature ⇒ Object
16 |
# File 'lib/block_given/abi/event.rb', line 16 def signature = "#{name}(#{inputs.map(&:type).join(',')})" |
#to_s ⇒ Object
68 |
# File 'lib/block_given/abi/event.rb', line 68 def to_s = signature |
#topic ⇒ Object
17 |
# File 'lib/block_given/abi/event.rb', line 17 def topic = @topic ||= Utils.keccak256(signature) |