Skip to content

Commit

Permalink
Fix parsing when function ABI is missing the outputs field (#163)
Browse files Browse the repository at this point in the history
* Fix parsing when function ABI is missing the "outputs" field

* Refactor docstrings and comments
  • Loading branch information
fedor-ivn authored Mar 12, 2024
1 parent e3dc19b commit 67eb2b9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/abi/function_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,31 @@ defmodule ABI.FunctionSelector do
"name" => function_name,
"inputs" => named_inputs,
"outputs" => named_outputs
} <- item,
} <-
item
# 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
input_types = Enum.map(named_inputs, &parse_specification_type/1)
Expand Down
37 changes: 37 additions & 0 deletions test/abi/function_selector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ defmodule ABI.FunctionSelectorTest do
}
] == ABI.parse_specification([abi])
end

@doc """
Regression test to verify the correct parsing of ABIs that lack the
"outputs" field.
"""
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
Expand Down

0 comments on commit 67eb2b9

Please sign in to comment.