abi_coder_rb 0.2.3 → 0.2.5

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: 35ffe301040ab5e55279be3bb020ba62d5ba3488f94217b055aeca82a403f229
4
- data.tar.gz: 5a2926ad367ec5fb5e4c9600dd67fd9a9550148bfdd53beb7b3d85e8b960ebff
3
+ metadata.gz: 8dc7ba678a03f7f7ba489622979497fb52912b3732dcb27890c270c3b7713e0b
4
+ data.tar.gz: 7280e400c63ba3b901e5240710673aa8d857db86197d784a19c26f7220bc5ead
5
5
  SHA512:
6
- metadata.gz: 40b222a5fbebb49b769f49ce10c0d1ec808d6c966a42338374e1a5faae5bd27378d15498f09e87de8ba3db12b724748524deba3ca02bedb5e2c6fd58a1ad724b
7
- data.tar.gz: 05b4d4f66357980d6cef05c187790374ee4c8f23b7479c90faa3c2ac84c9cf5bb5833867e2cf8ebd8cfefaceff260e042e7fdacbf81d6cd8997e738c9794d793
6
+ metadata.gz: f361c6f8d5eb3321f0f2630b7fbfa3c3e3f962ba5bb0174e9cbf99869c55838a2d15c8a6e94c929916d737b3f8960fc37b0bcec23ae9f100ca5775d5f4a98f4c
7
+ data.tar.gz: 655bc2ba8c29fa3754dca9affba12f9a0cf5625bd354abbede018a6ea21ce74ad495564237ff4c95ef448bd98b0eccc41bc6951959bdee4226e78e8ac204edb9
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.5)
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.5"
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"].underscore, fields_of(input["components"])]
92
+ elsif input["type"] == "enum"
93
+ [input["name"].underscore, "uint8"]
94
+ else
95
+ [input["name"].underscore, input["type"]]
96
+ end
97
+ end
98
+ end
99
+
81
100
  # fields:
82
101
  # [
83
102
  # ["root", "bytes32"],
@@ -103,12 +122,30 @@ 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
+ if type.is_a?(::Array)
131
+ flat_fields(type, sep: sep).map do |n, t|
132
+ ["#{name}#{sep}#{n}", t]
133
+ end
134
+ else
135
+ [[name, type]]
136
+ end
137
+ end.flatten(1)
138
+ end
139
+
140
+ # fields:
141
+ # [
142
+ # ["root", "bytes32"],
143
+ # ["message", [["channel", "address"], ["index", "uint256"], ["fromChainId", "uint256"], ["from", "address"], ["toChainId", "uint256"], ["to", "address"], ["encoded", "bytes"]]]
144
+ # ]
107
145
  # returns:
108
146
  # ["root", {"message" => ["channel", "index", "fromChainId", "from", "toChainId", "to", "gasLimit", "encoded"]}
109
147
  def fields_names(fields)
110
148
  fields.map do |name, type|
111
- name = name.underscore
112
149
  if type.is_a?(::Array)
113
150
  { name => fields_names(type) }
114
151
  elsif type.is_a?(::String)
@@ -125,9 +162,8 @@ class EventDecoder
125
162
  #
126
163
  # returns:
127
164
  # ["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: "_")
165
+ def fields_names_flatten(fields, prefix: nil, sep: ".")
129
166
  fields.map do |name, type|
130
- name = name.underscore
131
167
  if type.is_a?(::Array)
132
168
  fields_names_flatten(
133
169
  type,
@@ -140,20 +176,11 @@ class EventDecoder
140
176
  end.flatten
141
177
  end
142
178
 
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
179
+ def data_field_names(flatten: false, sep: ".")
180
+ if flatten
181
+ fields_names_flatten(@data_fields, sep: sep)
182
+ else
183
+ fields_names(@data_fields)
157
184
  end
158
185
  end
159
186
 
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.5
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: 2024-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport