Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing tuples #164

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions lib/abi/function_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -311,34 +311,28 @@ defmodule ABI.FunctionSelector do
end
end

@doc false
def parse_specification_type(%{"type" => "tuple", "components" => components}) do
sub_types = for component <- components, do: parse_specification_type(component)
{:tuple, sub_types}
defp replace_tuple({:array, inner}, sub_types) do
{:array, replace_tuple(inner, sub_types)}
end

def parse_specification_type(%{"type" => "tuple[]", "components" => components}) do
sub_types = for component <- components, do: parse_specification_type(component)
{:array, {:tuple, sub_types}}
defp replace_tuple({:array, inner, size}, sub_types) do
{:array, replace_tuple(inner, sub_types), size}
end

def parse_specification_type(%{"type" => "tuple[][]", "components" => components}) do
sub_types = for component <- components, do: parse_specification_type(component)
{:array, {:array, {:tuple, sub_types}}}
defp replace_tuple(:tuple, sub_types) do
{:tuple, sub_types}
end

def parse_specification_type(%{
"type" => "tuple[" <> tail,
"components" => components
}) do
sub_types = for component <- components, do: parse_specification_type(component)
defp replace_tuple(other, _) do
other
end

size =
tail
|> String.replace("]", "")
|> String.to_integer()
def parse_specification_type(%{"type" => "tuple" <> _ = type, "components" => components}) do
sub_types = for component <- components, do: parse_specification_type(component)

{:array, {:tuple, sub_types}, size}
type
|> decode_type()
|> replace_tuple(sub_types)
end

def parse_specification_type(%{"type" => type}), do: decode_type(type)
Expand Down
32 changes: 32 additions & 0 deletions test/abi/function_selector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ defmodule ABI.FunctionSelectorTest do
assert expected_type == selector.types
end

test "parses fixed 2D array of tuples" do
function = %{
"inputs" => [],
"name" => "createTupleArray",
"outputs" => [
%{
"components" => [
%{
"internalType" => "uint256",
"name" => "element1",
"type" => "uint256"
},
%{"internalType" => "bool", "name" => "element2", "type" => "bool"}
],
"internalType" => "struct StorageB.MyTuple[2][]",
"name" => "",
"type" => "tuple[2][]"
}
],
"stateMutability" => "pure",
"type" => "function"
}

expected = [
array: {:array, {:tuple, [{:uint, 256}, :bool]}, 2}
]

selector = FunctionSelector.parse_specification_item(function)

assert expected == selector.returns
end

test "with stateMutability set" do
~w(pure view nonpayable payable)
|> Enum.zip(~w(pure view non_payable payable)a)
Expand Down
Loading