Fix parsing when function ABI is missing the `outputs` field (#163)

* Fix parsing when function ABI is missing the "outputs" field

* Refactor docstrings and comments
This commit is contained in:
Fedor Ivanov 2024-03-12 15:26:11 +03:00 committed by GitHub
parent e3dc19b814
commit 67eb2b9d5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 1 deletions

View File

@ -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)

View File

@ -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