abi_coder_rb 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f20aa23f421459866cdbd1cd485027ec5fec4b52af891adc9c4025f247b42a9
4
- data.tar.gz: 439e3e33e16d8cd9b20267f6e35218adb3c542f7aa6e80482c8d9aafee220bd3
3
+ metadata.gz: 5ea1c60ed8bfe0bf5e5b66501698f0cba73a8407293cea13879015c594a1a1fe
4
+ data.tar.gz: 80078bcc3870bc1b44c3655d535fc87a4092acaa18f0ca84eacacb303208bd9d
5
5
  SHA512:
6
- metadata.gz: 6c423bbce7507e4dc2d79d73355c65b84c8ceff0bc4c9e89045e23a461084898c9b97e35225e6b0b8e0173ddb05d1dfd13c3df5e7071cfe05960254037e2a3f9
7
- data.tar.gz: d2977279469c49bc63d67c360d12b281bb2442856dddc166ef784b1311198afb0dabbe19604160c53edd5c54047b031113a6ae18e5ef60b93611b68fa9f8b657
6
+ metadata.gz: c2ec394e57941ac7a6d6d1ee765e0ade1f1a449d4e1bdcd221c3e16cb496249dcfe449712d826eb43335556d176c17949d5865be6e85c12f88c53cb8678e38d7
7
+ data.tar.gz: 3c5aebd1602663598241c823759bf5a30a508386e93c6624221d80646503db1cfde5365d97f13b35fb98a87cc7f46c5e388bc6c423de180e08eaf0c83dffba14
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- abi_coder_rb (0.2.2)
4
+ abi_coder_rb (0.2.4)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AbiCoderRb
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -6,25 +6,22 @@ class EventDecoder
6
6
 
7
7
  attr_reader :event_abi,
8
8
  # indexed_topic_inputs
9
- :indexed_topic_inputs, :indexed_topic_types,
9
+ :indexed_topic_inputs, :indexed_topic_fields,
10
10
  # data_inputs
11
- :data_inputs, :data_type_str, :data_field_names, :data_field_names_flattened
11
+ :data_inputs, :data_fields, :data_type_str
12
12
 
13
+ # flatten_sep: separator for flattening event name if it is nested
13
14
  def initialize(event_abi)
14
15
  @event_abi = event_abi
15
16
  @indexed_topic_inputs, @data_inputs = event_abi["inputs"].partition { |input| input["indexed"] }
16
17
 
17
18
  # indexed_topic_inputs:
18
- @indexed_topic_types = @indexed_topic_inputs.map { |input| input["type"] }
19
+ @indexed_topic_fields = fields_of(@indexed_topic_inputs)
19
20
 
20
21
  # data_inputs:
21
22
  @data_fields = fields_of(@data_inputs)
22
-
23
23
  @data_type_str = fields_type_str(@data_fields)
24
24
 
25
- @data_field_names = fields_names(@data_fields)
26
- @data_field_names_flattened = fields_names_flatten(@data_fields)
27
-
28
25
  # add after_decoding action
29
26
  after_decoding lambda { |type, value|
30
27
  if type == "address"
@@ -37,13 +34,19 @@ class EventDecoder
37
34
  }
38
35
  end
39
36
 
37
+ def data_fields_flatten(sep: ".")
38
+ flat_fields(@data_fields, sep: sep)
39
+ end
40
+
40
41
  def decode_topics(topics, with_names: false)
41
42
  topics = topics[1..] if topics.count == @indexed_topic_inputs.count + 1 && @event_abi["anonymous"] == false
42
43
 
43
44
  raise "topics count not match" if topics.count != @indexed_topic_inputs.count
44
45
 
46
+ indexed_topic_types = @indexed_topic_inputs.map { |input| input["type"] }
47
+
45
48
  values = topics.each_with_index.map do |topic, i|
46
- indexed_topic_type = @indexed_topic_types[i]
49
+ indexed_topic_type = indexed_topic_types[i]
47
50
  decode(indexed_topic_type, hex_to_bin(topic))
48
51
  end
49
52
 
@@ -54,7 +57,7 @@ class EventDecoder
54
57
  end
55
58
  end
56
59
 
57
- def decode_data(data, flatten: true, with_names: false)
60
+ def decode_data(data, flatten: true, sep: ".", with_names: false)
58
61
  return with_names ? {} : [] if @data_type_str == "()"
59
62
 
60
63
  data_values = decode(@data_type_str, hex_to_bin(data))
@@ -62,13 +65,13 @@ class EventDecoder
62
65
  case flatten
63
66
  when true
64
67
  if with_names
65
- combine(@data_field_names_flattened, data_values.flatten)
68
+ combine(data_field_names(flatten: true, sep: sep), data_values.flatten)
66
69
  else
67
70
  data_values.flatten
68
71
  end
69
72
  when false
70
73
  if with_names
71
- combine(@data_field_names, data_values)
74
+ combine(data_field_names, data_values)
72
75
  else
73
76
  data_values
74
77
  end
@@ -77,6 +80,23 @@ class EventDecoder
77
80
 
78
81
  private
79
82
 
83
+ # returns:
84
+ # [
85
+ # ["root", "bytes32"],
86
+ # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
87
+ # ]
88
+ def fields_of(inputs)
89
+ inputs.map do |input|
90
+ if input["type"] == "tuple"
91
+ [input["name"], fields_of(input["components"])]
92
+ elsif input["type"] == "enum"
93
+ [input["name"], "uint8"]
94
+ else
95
+ [input["name"], input["type"]]
96
+ end
97
+ end
98
+ end
99
+
80
100
  # fields:
81
101
  # [
82
102
  # ["root", "bytes32"],
@@ -102,7 +122,27 @@ class EventDecoder
102
122
  # ["root", "bytes32"],
103
123
  # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
104
124
  # ]
105
- #
125
+ # returns:
126
+ # [["root", "bytes32"], ["message.channel", "address"], ["message.index", "uint256"], ["message.from_chain_id", "uint256"], ["message.from", "address"], ["message.to_chain_id", "uint256"], ["message.to", "address"], ["message.gas_limit", "uint256"], ["message.encoded", "bytes"]]
127
+
128
+ def flat_fields(fields, sep: ".")
129
+ fields.map do |name, type|
130
+ name = name.underscore
131
+ if type.is_a?(::Array)
132
+ flat_fields(type, sep: sep).map do |n, t|
133
+ ["#{name}#{sep}#{n}", t]
134
+ end
135
+ else
136
+ [[name, type]]
137
+ end
138
+ end.flatten(1)
139
+ end
140
+
141
+ # fields:
142
+ # [
143
+ # ["root", "bytes32"],
144
+ # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
145
+ # ]
106
146
  # returns:
107
147
  # ["root", {"message" => ["channel", "index", "fromChainId", "from", "toChainId", "to", "gasLimit", "encoded"]}
108
148
  def fields_names(fields)
@@ -124,34 +164,26 @@ class EventDecoder
124
164
  #
125
165
  # returns:
126
166
  # ["root", "message_channel", "message_index", "message_fromChainId", "message_from", "message_toChainId", "message_to", "message_gasLimit", "message_encoded"]
127
- def fields_names_flatten(fields, prefix = nil)
167
+ def fields_names_flatten(fields, prefix: nil, sep: ".")
128
168
  fields.map do |name, type|
129
169
  name = name.underscore
130
170
  if type.is_a?(::Array)
131
171
  fields_names_flatten(
132
172
  type,
133
- prefix.nil? ? name : "#{prefix}.#{name}"
173
+ prefix: prefix.nil? ? name : "#{prefix}#{sep}#{name}",
174
+ sep: sep
134
175
  )
135
176
  elsif type.is_a?(::String)
136
- prefix.nil? ? name : "#{prefix}.#{name}"
177
+ prefix.nil? ? name : "#{prefix}#{sep}#{name}"
137
178
  end
138
179
  end.flatten
139
180
  end
140
181
 
141
- # returns:
142
- # [
143
- # ["root", "bytes32"],
144
- # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
145
- # ]
146
- def fields_of(inputs)
147
- inputs.map do |input|
148
- if input["type"] == "tuple"
149
- [input["name"], fields_of(input["components"])]
150
- elsif input["type"] == "enum"
151
- [input["name"], "uint8"]
152
- else
153
- [input["name"], input["type"]]
154
- end
182
+ def data_field_names(flatten: false, sep: ".")
183
+ if flatten
184
+ fields_names_flatten(@data_fields, sep: sep)
185
+ else
186
+ fields_names(@data_fields)
155
187
  end
156
188
  end
157
189
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abi_coder_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aki Wu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-30 00:00:00.000000000 Z
11
+ date: 2023-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport