-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcontract_helpers.ex
More file actions
439 lines (355 loc) · 12.1 KB
/
contract_helpers.ex
File metadata and controls
439 lines (355 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
defmodule Ethers.ContractHelpers do
@moduledoc false
require Logger
@spec read_abi(Keyword.t()) :: {abi :: [...], file_path :: String.t() | nil}
def read_abi(opts) do
case Keyword.take(opts, [:abi, :abi_file]) do
[{type, data}] ->
do_read_abi(type, data, nil)
_ ->
raise ArgumentError, "Invalid arguments. Specify either `:abi` or `:abi_file` option"
end
end
@spec maybe_read_contract_binary(Keyword.t()) :: binary() | nil
def maybe_read_contract_binary(opts) do
case Keyword.take(opts, [:abi, :abi_file]) do
[{type, data}] ->
maybe_read_contract_binary(type, data)
_ ->
raise ArgumentError, "Invalid options"
end
end
def document_types(types, names \\ []) do
if length(types) <= length(names) do
Enum.zip(types, names)
else
types
end
|> Enum.map_join("\n", fn
{type, ""} ->
" - `#{inspect(type)}`"
{type, name} when is_binary(name) or is_atom(name) ->
" - #{name}: `#{inspect(type)}`"
type ->
" - `#{inspect(type)}`"
end)
end
def document_help_message(selectors) do
selectors
|> Enum.map(& &1.state_mutability)
|> Enum.uniq()
|> do_document_help_message()
end
defp do_document_help_message([state_mutability]) do
message =
case state_mutability do
sm when sm in [:pure, :view] ->
"""
This function should only be called for result and never in a transaction on its own. (Use `Ethers.call/2`)
"""
:non_payable ->
"""
This function can be used for a transaction or additionally called for results (Use `Ethers.send_transaction/2`).
No amount of Ether can be sent with this function.
"""
:payable ->
"""
This function can be used for a transaction or additionally called for results (Use `Ethers.send_transaction/2`).
It also supports receiving ether from the transaction origin.
"""
end
"""
#{message}
State mutability: #{document_state_mutabilities([state_mutability])}
"""
end
defp do_document_help_message(state_mutabilities) do
"""
This function has multiple state mutabilities based on the overload that you use.
You may use the correct function (`Ethers.call/2` or `Ethers.send_transaction/2`) to interact with this function
based on the overload you choose.
State mutabilities: #{document_state_mutabilities(state_mutabilities)}
"""
end
def document_parameters([%{types: []}]), do: ""
def document_parameters([%{type: :event} | _] = selectors) do
parameters_docs =
Enum.map_join(selectors, "\n\n### OR\n", fn selector ->
{types, names} =
Enum.zip(selector.types, selector.input_names)
|> Enum.zip(selector.inputs_indexed)
|> Enum.filter(&elem(&1, 1))
|> Enum.map(&elem(&1, 0))
|> Enum.unzip()
document_types(types, names)
end)
"""
## Parameter Types (Event indexed topics)
#{parameters_docs}
"""
end
def document_parameters(selectors) do
parameters_docs =
Enum.map_join(selectors, "\n\n### OR\n", &document_types(&1.types, &1.input_names))
"""
## Function Parameter Types
#{parameters_docs}
"""
end
def document_returns([%{type: :event} | _] = selectors) do
return_type_docs =
selectors
|> Enum.map(fn selector ->
Enum.zip([selector.types, selector.input_names, selector.inputs_indexed])
|> Enum.reject(&elem(&1, 2))
|> Enum.map(fn {type, name, false} -> {type, name} end)
|> Enum.unzip()
end)
|> Enum.uniq()
|> Enum.map_join("\n\n### OR\n", fn
{[], _input_names} ->
"This event does not contain any values!"
{types, input_names} ->
document_types(types, input_names)
end)
"""
## Event `data` Types (when called with `Ethers.get_logs/2`)
These are non-indexed topics (often referred to as data) of the event log.
#{return_type_docs}
"""
end
def document_returns(selectors) when is_list(selectors) do
return_type_docs =
selectors
|> Enum.uniq_by(& &1.returns)
|> Enum.map_join("\n\n### OR\n", fn selector ->
if Enum.empty?(selector.returns) do
"This function does not return any values!"
else
document_types(selector.returns, selector.return_names)
end
end)
"""
## Return Types (when called with `Ethers.call/2`)
#{return_type_docs}
"""
end
defp document_state_mutabilities(state_mutabilities) do
Enum.map_join(state_mutabilities, " OR ", &"`#{&1}`")
end
def human_signature(%ABI.FunctionSelector{
input_names: names,
types: types,
function: function
}) do
args =
if is_list(names) and length(types) == length(names) do
Enum.zip(types, names)
else
types
end
|> Enum.map_join(", ", fn
{type, name} when is_binary(name) ->
String.trim("#{ABI.FunctionSelector.encode_type(type)} #{name}")
type ->
"#{ABI.FunctionSelector.encode_type(type)}"
end)
"#{function}(#{args})"
end
def human_signature(selectors) when is_list(selectors) do
Enum.map_join(selectors, " OR ", &human_signature/1)
end
def generate_error_arguments(mod, arity, names) do
generate_arguments(mod, arity, names)
|> Enum.map(fn {arg, _ctx, _mod} -> arg end)
end
def generate_arguments(mod, arity, names) when is_integer(arity) do
args = Macro.generate_arguments(arity, mod)
if length(names) >= length(args) do
args
|> Enum.zip(names)
|> Enum.map(&get_argument_name_ast/1)
else
args
end
end
def generate_typespecs(selectors) do
Enum.map(selectors, & &1.types)
|> do_generate_typescpecs()
end
def generate_event_typespecs(selectors, arity) do
Enum.map(selectors, &Enum.take(&1.types, arity))
|> do_generate_typescpecs(true)
end
def generate_struct_typespecs(args, selector) do
types = Enum.map(selector.types, &Ethers.Types.to_elixir_type/1)
# quoted expression of %__MODULE__{key: type(), ...}
{:%, [], [{:__MODULE__, [], Elixir}, {:%{}, [], Enum.zip(args, types)}]}
end
defp do_generate_typescpecs(types, optional? \\ false) do
Enum.zip_with(types, & &1)
|> Enum.map(fn type_group ->
type_group
|> Enum.map(&Ethers.Types.to_elixir_type/1)
|> Enum.uniq()
|> then(&if(optional?, do: [nil | &1], else: &1))
|> Enum.reduce(fn type, acc ->
quote do
unquote(type) | unquote(acc)
end
end)
end)
end
def find_selector!(selectors, args) do
filtered_selectors = Enum.filter(selectors, &selector_match?(&1, args))
case filtered_selectors do
[] ->
signatures =
Enum.map_join(selectors, "\n", &human_signature/1)
raise ArgumentError, """
No function selector matches current arguments!
## Arguments
#{inspect(args)}
## Available signatures
#{signatures}
"""
[selector] ->
{selector, strip_typed_args(args)}
selectors ->
signatures =
Enum.map_join(selectors, "\n", &human_signature/1)
raise ArgumentError, """
Ambiguous parameters
## Arguments
#{inspect(args)}
## Possible signatures
#{signatures}
"""
end
end
defp strip_typed_args(args) do
Enum.map(args, fn
{:typed, _type, arg} -> arg
arg -> arg
end)
end
def selector_match?(%{type: :event} = selector, args) do
event_indexed_types(selector)
|> do_selector_match?(args, true)
end
def selector_match?(selector, args) do
do_selector_match?(selector.types, args, false)
end
defp do_selector_match?(types, args, allow_nil) do
if Enum.count(types) == Enum.count(args) do
Enum.zip(types, args)
|> Enum.all?(fn
{type, {:typed, assigned_type, _arg}} -> assigned_type == type
{_type, nil} -> allow_nil == true
{type, arg} -> Ethers.Types.matches_type?(arg, type)
end)
else
false
end
end
def aggregate_input_names([%{type: :event} | _] = selectors) do
Enum.map(selectors, fn selector ->
Enum.zip(selector.input_names, selector.inputs_indexed)
|> Enum.filter(&elem(&1, 1))
|> Enum.map(&elem(&1, 0))
end)
|> Enum.zip_with(&(Enum.uniq(&1) |> Enum.join("_or_")))
end
def aggregate_input_names(selectors) do
Enum.map(selectors, & &1.input_names)
|> Enum.zip_with(&(Enum.uniq(&1) |> Enum.join("_or_")))
end
def encode_event_topics(selector, args) do
[event_topic_0(selector) | encode_event_sub_topics(selector, args)]
end
defp event_topic_0(%{method_id: method_id}) when byte_size(method_id) == 32 do
Ethers.Utils.hex_encode(method_id)
end
defp event_topic_0(selector) do
selector
|> ABI.FunctionSelector.encode()
|> Ethers.keccak_module().hash_256()
|> Ethers.Utils.hex_encode()
end
defp encode_event_sub_topics(selector, raw_args) do
event_indexed_types(selector)
|> Enum.zip(raw_args)
|> Enum.map(fn {type, value} -> do_encode_indexed_type(type, value) end)
end
defp do_encode_indexed_type(_, nil), do: nil
defp do_encode_indexed_type(type, value) when type in [:string, :bytes] do
value
|> Ethers.Utils.prepare_arg(type)
|> Ethers.keccak_module().hash_256()
|> Ethers.Utils.hex_encode()
end
defp do_encode_indexed_type({:array, _, _} = type, value), do: hashed_encode(type, value)
defp do_encode_indexed_type({:array, subtype}, value) do
# Add count to array type to remove length prefix
# Otherwise behaves just like bounded array
hashed_encode({:array, subtype, Enum.count(value)}, value)
end
defp do_encode_indexed_type({:tuple, _} = type, value), do: hashed_encode(type, value)
defp do_encode_indexed_type(type, value) do
[Ethers.Utils.prepare_arg(value, type)]
|> ABI.TypeEncoder.encode([type])
|> Ethers.Utils.hex_encode()
end
defp hashed_encode(type, value) do
[Ethers.Utils.prepare_arg(value, type)]
|> ABI.TypeEncoder.encode([type])
|> Ethers.keccak_module().hash_256()
|> Ethers.Utils.hex_encode()
end
def event_indexed_types(selector) do
Enum.zip(selector.types, selector.inputs_indexed)
|> Enum.filter(&elem(&1, 1))
|> Enum.map(&elem(&1, 0))
end
def event_non_indexed_types(selector) do
Enum.zip(selector.types, selector.inputs_indexed)
|> Enum.reject(&elem(&1, 1))
|> Enum.map(&elem(&1, 0))
end
defp do_read_abi(:abi, abi, file_path) when is_list(abi), do: {abi, file_path}
defp do_read_abi(:abi, %{"abi" => abi}, file_path), do: do_read_abi(:abi, abi, file_path)
defp do_read_abi(:abi, abi, _file_path) when is_atom(abi) do
file_path = Path.join(:code.priv_dir(:ethers), "abi/#{abi}.json")
do_read_abi(:abi_file, file_path, nil)
end
defp do_read_abi(:abi, abi, file_path) when is_binary(abi) do
abi = Ethers.json_module().decode!(abi)
do_read_abi(:abi, abi, file_path)
end
defp do_read_abi(:abi_file, file, _file_path) do
abi = File.read!(file)
do_read_abi(:abi, abi, file)
end
defp get_argument_name_ast({ast, name}) do
get_argument_name_ast(ast, String.trim(name))
end
defp get_argument_name_ast(ast, "_" <> name), do: get_argument_name_ast(ast, name)
defp get_argument_name_ast(ast, ""), do: ast
defp get_argument_name_ast({orig, ctx, md}, name) when is_atom(orig) do
name_atom = String.to_atom(Macro.underscore(name))
{name_atom, ctx, md}
end
defp maybe_read_contract_binary(:abi, abi) when is_list(abi), do: nil
defp maybe_read_contract_binary(:abi, %{"bin" => bin}) when is_binary(bin),
do: Ethers.Utils.hex_decode!(bin)
defp maybe_read_contract_binary(:abi, map) when is_map(map), do: nil
defp maybe_read_contract_binary(:abi, abi) when is_atom(abi), do: nil
defp maybe_read_contract_binary(:abi, abi) when is_binary(abi) do
abi = Ethers.json_module().decode!(abi)
maybe_read_contract_binary(:abi, abi)
end
defp maybe_read_contract_binary(:abi_file, file) do
abi = File.read!(file)
maybe_read_contract_binary(:abi, abi)
end
end