Skip to content

Commit 068c100

Browse files
authored
Improve Ethers.TxData module (#155)
* Export `TxData.to_map/2` function in docs * Move abi_decode to TxData module * Update CHANGELOG.md
1 parent 0879345 commit 068c100

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Enhancements
6+
7+
- Move and export abi decode functionality to `Ethers.TxData` module
8+
- Export `Ethers.TxData.to_map/2` in docs
9+
310
## v0.5.4 (2024-10-22)
411

512
### Bug fixes

lib/ethers.ex

+4-8
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,10 @@ defmodule Ethers do
661661

662662
defp pre_process(data, [], _action, _opts), do: {:ok, data}
663663

664-
defp post_process({:ok, resp}, %{selector: selector}, :call) when valid_result(resp) do
665-
selector
666-
|> ABI.decode(Ethers.Utils.hex_decode!(resp), :output)
667-
|> Enum.zip(selector.returns)
668-
|> Enum.map(fn {return, type} -> Utils.human_arg(return, type) end)
669-
|> case do
670-
[element] -> {:ok, element}
671-
elements -> {:ok, elements}
664+
defp post_process({:ok, resp}, %{selector: _selector} = tx_data, :call)
665+
when valid_result(resp) do
666+
with {:ok, data} <- Utils.hex_decode(resp) do
667+
TxData.abi_decode(data, tx_data, :output)
672668
end
673669
end
674670

lib/ethers/tx_data.ex

+34-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule Ethers.TxData do
44
and the target `to` address.
55
"""
66

7+
alias Ethers.Utils
8+
79
@typedoc """
810
Holds transaction data, the function selector and the default `to` address.
911
@@ -31,8 +33,12 @@ defmodule Ethers.TxData do
3133
}
3234
end
3335

34-
@doc false
36+
@doc """
37+
Converts a TxData struct and optional overrides to a map ready for RPC data.
38+
"""
3539
@spec to_map(t() | map(), Keyword.t()) :: map()
40+
def to_map(tx_data, overrides \\ [])
41+
3642
def to_map(%__MODULE__{} = tx_data, overrides) do
3743
tx_data
3844
|> get_tx_map()
@@ -48,6 +54,33 @@ defmodule Ethers.TxData do
4854
end)
4955
end
5056

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+
5184
defp get_tx_map(%{selector: %{type: :function}} = tx_data) do
5285
%{data: tx_data.data}
5386
|> maybe_add_to_address(tx_data.default_address)

0 commit comments

Comments
 (0)