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 when function ABI is missing the outputs field #163

Merged
merged 2 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
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
Loading