Skip to content

Commit

Permalink
fix parsing of multidim tuples in fun specs (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 authored Feb 21, 2022
1 parent 10070f6 commit 10c7407
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/abi/function_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ defmodule ABI.FunctionSelector do
def simple_types?([], _item), do: true

def simple_types?([%{"type" => tuple_type, "components" => current_types} | types], item)
when tuple_type in ["tuple", "tuple[]"] do
when tuple_type in ["tuple", "tuple[]", "tuple[][]"] do
case simple_types?(current_types, item) do
true ->
simple_types?(types, item)
Expand Down Expand Up @@ -306,6 +306,11 @@ defmodule ABI.FunctionSelector do
{:array, {:tuple, sub_types}}
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}}}
end

def parse_specification_type(%{
"type" => "tuple[" <> tail,
"components" => components
Expand Down
4 changes: 2 additions & 2 deletions lib/abi/type_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ defmodule ABI.TypeEncoder do
do_encode_type({:uint, 160}, data, static_acc, dynamic_acc)
end

defp do_encode_type(type = {:tuple, types}, tuple_parameters, static_acc, dynamic_acc)
defp do_encode_type(type = {:tuple, _types}, tuple_parameters, static_acc, dynamic_acc)
when is_tuple(tuple_parameters) do
list_parameters = Tuple.to_list(tuple_parameters)

do_encode_type({:tuple, types}, list_parameters, static_acc, dynamic_acc)
do_encode_type(type, list_parameters, static_acc, dynamic_acc)
end

defp do_encode_type(type = {:tuple, types}, list_parameters, static_acc, dynamic_acc)
Expand Down
40 changes: 40 additions & 0 deletions test/abi/function_selector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,46 @@ defmodule ABI.FunctionSelectorTest do

assert FunctionSelector.parse_specification_type(type) == expected
end

test "parses multidimensional tuple array" do
abi = %{
"constant" => true,
"inputs" => [
%{
"name" => "swaps",
"type" => "tuple[][]",
"interalType" => "struct ExchangeProxy.Swap[][]",
"components" => [
%{
"name" => "foo",
"type" => "uint256"
},
%{
"name" => "bar",
"type" => "uint256"
}
]
}
],
"name" => "batchSwapExactOut",
"outputs" => [%{"name" => "totalAmountIn", "type" => "uint256"}],
"payable" => true,
"stateMutability" => "payable",
"type" => "function"
}

assert [
%ABI.FunctionSelector{
function: "batchSwapExactOut",
input_names: ["swaps"],
inputs_indexed: nil,
method_id: <<33, 173, 158, 39>>,
returns: [uint: 256],
type: :function,
types: [array: {:array, {:tuple, [uint: 256, uint: 256]}}]
}
] == ABI.parse_specification([abi])
end
end

describe "parse_specification_item/1" do
Expand Down

0 comments on commit 10c7407

Please sign in to comment.