From 27299ad6677ccce243842f869176cb95a0637d11 Mon Sep 17 00:00:00 2001 From: Fedor Ivanov Date: Thu, 7 Mar 2024 09:16:45 +0000 Subject: [PATCH 1/2] Fix parsing when function ABI is missing the "outputs" field --- lib/abi/function_selector.ex | 6 ++++- test/abi/function_selector_test.exs | 36 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/abi/function_selector.ex b/lib/abi/function_selector.ex index 04d1d5d..cc84bb9 100644 --- a/lib/abi/function_selector.ex +++ b/lib/abi/function_selector.ex @@ -180,7 +180,11 @@ defmodule ABI.FunctionSelector do "name" => function_name, "inputs" => named_inputs, "outputs" => named_outputs - } <- item, + } <- + item + # Workaround for missing "outputs" field. + # This is a fix for https://github.com/poanetwork/ex_abi/issues/162 + |> Map.put_new("outputs", []), true <- simple_types?(named_inputs, item), true <- simple_types?(named_outputs, item) do input_types = Enum.map(named_inputs, &parse_specification_type/1) diff --git a/test/abi/function_selector_test.exs b/test/abi/function_selector_test.exs index f0f40f2..2f68898 100644 --- a/test/abi/function_selector_test.exs +++ b/test/abi/function_selector_test.exs @@ -111,6 +111,42 @@ defmodule ABI.FunctionSelectorTest do } ] == ABI.parse_specification([abi]) end + + @doc """ + Regression test for issue #162 (https://github.com/poanetwork/ex_abi/issues/162). + """ + test "with the missing outputs field" do + abi = [ + %{ + "type" => "function", + "stateMutability" => "view", + "name" => "assumeLastTokenIdMatches", + "inputs" => [ + %{ + "type" => "uint256", + "name" => "lastTokenId", + "internalType" => "uint256" + } + ] + } + ] + + expected = [ + %FunctionSelector{ + type: :function, + function: "assumeLastTokenIdMatches", + input_names: ["lastTokenId"], + types: [uint: 256], + returns: [], + return_names: [], + method_id: <<231, 40, 120, 180>>, + inputs_indexed: nil, + state_mutability: :view + } + ] + + assert ABI.parse_specification(abi) == expected + end end describe "parse_specification_item/1" do From 13a0060b3944fe93cfb9c1aba9666c431919cc83 Mon Sep 17 00:00:00 2001 From: Fedor Ivanov Date: Tue, 12 Mar 2024 12:19:43 +0000 Subject: [PATCH 2/2] Refactor docstrings and comments --- lib/abi/function_selector.ex | 24 ++++++++++++++++++++++-- test/abi/function_selector_test.exs | 3 ++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/abi/function_selector.ex b/lib/abi/function_selector.ex index cc84bb9..43a5985 100644 --- a/lib/abi/function_selector.ex +++ b/lib/abi/function_selector.ex @@ -182,8 +182,28 @@ defmodule ABI.FunctionSelector do "outputs" => named_outputs } <- item - # Workaround for missing "outputs" field. - # This is a fix for https://github.com/poanetwork/ex_abi/issues/162 + # Workaround for ABIs that are missing the "outputs" field. + # + # For instance, consider this valid ABI: + # + # ```jsonc + # // ... + # { + # "type": "function", + # "stateMutability": "view", + # "name": "assumeLastTokenIdMatches", + # "inputs": [ + # { + # "type": "uint256", + # "name": "lastTokenId", + # "internalType": "uint256" + # } + # ] + # }, + # // ... + # ``` + # If the "outputs" field is missing, we should assume it's an empty + # list to continue parsing the ABI. |> Map.put_new("outputs", []), true <- simple_types?(named_inputs, item), true <- simple_types?(named_outputs, item) do diff --git a/test/abi/function_selector_test.exs b/test/abi/function_selector_test.exs index 2f68898..fd43743 100644 --- a/test/abi/function_selector_test.exs +++ b/test/abi/function_selector_test.exs @@ -113,7 +113,8 @@ defmodule ABI.FunctionSelectorTest do end @doc """ - Regression test for issue #162 (https://github.com/poanetwork/ex_abi/issues/162). + Regression test to verify the correct parsing of ABIs that lack the + "outputs" field. """ test "with the missing outputs field" do abi = [