@@ -4,6 +4,8 @@ defmodule Ethers.TxData do
4
4
and the target `to` address.
5
5
"""
6
6
7
+ alias Ethers.Utils
8
+
7
9
@ typedoc """
8
10
Holds transaction data, the function selector and the default `to` address.
9
11
@@ -31,8 +33,12 @@ defmodule Ethers.TxData do
31
33
}
32
34
end
33
35
34
- @ doc false
36
+ @ doc """
37
+ Converts a TxData struct and optional overrides to a map ready for RPC data.
38
+ """
35
39
@ spec to_map ( t ( ) | map ( ) , Keyword . t ( ) ) :: map ( )
40
+ def to_map ( tx_data , overrides \\ [ ] )
41
+
36
42
def to_map ( % __MODULE__ { } = tx_data , overrides ) do
37
43
tx_data
38
44
|> get_tx_map ( )
@@ -48,6 +54,33 @@ defmodule Ethers.TxData do
48
54
end )
49
55
end
50
56
57
+ @ doc """
58
+ ABI decodes a function input/output given a TxData or FunctionSelector
59
+ """
60
+ @ spec abi_decode ( binary ( ) , ABI.FunctionSelector . t ( ) | t ( ) , type :: :input | :output ) ::
61
+ { :ok , any ( ) | [ any ( ) ] }
62
+ def abi_decode ( data , tx_data_or_selector , type \\ :output )
63
+
64
+ def abi_decode ( data , % { selector: % ABI.FunctionSelector { } = selector } , type ) ,
65
+ do: abi_decode ( data , selector , type )
66
+
67
+ def abi_decode ( data , % ABI.FunctionSelector { } = selector , type ) do
68
+ types =
69
+ case type do
70
+ :input -> selector . types
71
+ :output -> selector . returns
72
+ end
73
+
74
+ selector
75
+ |> ABI . decode ( data , type )
76
+ |> Enum . zip ( types )
77
+ |> Enum . map ( fn { return , type } -> Utils . human_arg ( return , type ) end )
78
+ |> case do
79
+ [ element ] -> { :ok , element }
80
+ elements -> { :ok , elements }
81
+ end
82
+ end
83
+
51
84
defp get_tx_map ( % { selector: % { type: :function } } = tx_data ) do
52
85
% { data: tx_data . data }
53
86
|> maybe_add_to_address ( tx_data . default_address )
0 commit comments