abi_coder_rb 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35ffe301040ab5e55279be3bb020ba62d5ba3488f94217b055aeca82a403f229
4
- data.tar.gz: 5a2926ad367ec5fb5e4c9600dd67fd9a9550148bfdd53beb7b3d85e8b960ebff
3
+ metadata.gz: 5ea1c60ed8bfe0bf5e5b66501698f0cba73a8407293cea13879015c594a1a1fe
4
+ data.tar.gz: 80078bcc3870bc1b44c3655d535fc87a4092acaa18f0ca84eacacb303208bd9d
5
5
  SHA512:
6
- metadata.gz: 40b222a5fbebb49b769f49ce10c0d1ec808d6c966a42338374e1a5faae5bd27378d15498f09e87de8ba3db12b724748524deba3ca02bedb5e2c6fd58a1ad724b
7
- data.tar.gz: 05b4d4f66357980d6cef05c187790374ee4c8f23b7479c90faa3c2ac84c9cf5bb5833867e2cf8ebd8cfefaceff260e042e7fdacbf81d6cd8997e738c9794d793
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.3)
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.3"
4
+ VERSION = "0.2.4"
5
5
  end
@@ -6,26 +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
13
  # flatten_sep: separator for flattening event name if it is nested
14
- def initialize(event_abi, flatten_sep = ".")
14
+ def initialize(event_abi)
15
15
  @event_abi = event_abi
16
16
  @indexed_topic_inputs, @data_inputs = event_abi["inputs"].partition { |input| input["indexed"] }
17
17
 
18
18
  # indexed_topic_inputs:
19
- @indexed_topic_types = @indexed_topic_inputs.map { |input| input["type"] }
19
+ @indexed_topic_fields = fields_of(@indexed_topic_inputs)
20
20
 
21
21
  # data_inputs:
22
22
  @data_fields = fields_of(@data_inputs)
23
-
24
23
  @data_type_str = fields_type_str(@data_fields)
25
24
 
26
- @data_field_names = fields_names(@data_fields)
27
- @data_field_names_flattened = fields_names_flatten(@data_fields, sep: flatten_sep)
28
-
29
25
  # add after_decoding action
30
26
  after_decoding lambda { |type, value|
31
27
  if type == "address"
@@ -38,13 +34,19 @@ class EventDecoder
38
34
  }
39
35
  end
40
36
 
37
+ def data_fields_flatten(sep: ".")
38
+ flat_fields(@data_fields, sep: sep)
39
+ end
40
+
41
41
  def decode_topics(topics, with_names: false)
42
42
  topics = topics[1..] if topics.count == @indexed_topic_inputs.count + 1 && @event_abi["anonymous"] == false
43
43
 
44
44
  raise "topics count not match" if topics.count != @indexed_topic_inputs.count
45
45
 
46
+ indexed_topic_types = @indexed_topic_inputs.map { |input| input["type"] }
47
+
46
48
  values = topics.each_with_index.map do |topic, i|
47
- indexed_topic_type = @indexed_topic_types[i]
49
+ indexed_topic_type = indexed_topic_types[i]
48
50
  decode(indexed_topic_type, hex_to_bin(topic))
49
51
  end
50
52
 
@@ -55,7 +57,7 @@ class EventDecoder
55
57
  end
56
58
  end
57
59
 
58
- def decode_data(data, flatten: true, with_names: false)
60
+ def decode_data(data, flatten: true, sep: ".", with_names: false)
59
61
  return with_names ? {} : [] if @data_type_str == "()"
60
62
 
61
63
  data_values = decode(@data_type_str, hex_to_bin(data))
@@ -63,13 +65,13 @@ class EventDecoder
63
65
  case flatten
64
66
  when true
65
67
  if with_names
66
- combine(@data_field_names_flattened, data_values.flatten)
68
+ combine(data_field_names(flatten: true, sep: sep), data_values.flatten)
67
69
  else
68
70
  data_values.flatten
69
71
  end
70
72
  when false
71
73
  if with_names
72
- combine(@data_field_names, data_values)
74
+ combine(data_field_names, data_values)
73
75
  else
74
76
  data_values
75
77
  end
@@ -78,6 +80,23 @@ class EventDecoder
78
80
 
79
81
  private
80
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
+
81
100
  # fields:
82
101
  # [
83
102
  # ["root", "bytes32"],
@@ -103,7 +122,27 @@ class EventDecoder
103
122
  # ["root", "bytes32"],
104
123
  # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
105
124
  # ]
106
- #
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
+ # ]
107
146
  # returns:
108
147
  # ["root", {"message" => ["channel", "index", "fromChainId", "from", "toChainId", "to", "gasLimit", "encoded"]}
109
148
  def fields_names(fields)
@@ -125,7 +164,7 @@ class EventDecoder
125
164
  #
126
165
  # returns:
127
166
  # ["root", "message_channel", "message_index", "message_fromChainId", "message_from", "message_toChainId", "message_to", "message_gasLimit", "message_encoded"]
128
- def fields_names_flatten(fields, prefix: nil, sep: "_")
167
+ def fields_names_flatten(fields, prefix: nil, sep: ".")
129
168
  fields.map do |name, type|
130
169
  name = name.underscore
131
170
  if type.is_a?(::Array)
@@ -140,20 +179,11 @@ class EventDecoder
140
179
  end.flatten
141
180
  end
142
181
 
143
- # returns:
144
- # [
145
- # ["root", "bytes32"],
146
- # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
147
- # ]
148
- def fields_of(inputs)
149
- inputs.map do |input|
150
- if input["type"] == "tuple"
151
- [input["name"], fields_of(input["components"])]
152
- elsif input["type"] == "enum"
153
- [input["name"], "uint8"]
154
- else
155
- [input["name"], input["type"]]
156
- 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)
157
187
  end
158
188
  end
159
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.3
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-12-21 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