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

Add state_mutability to ABI.FunctionSelector #109

Merged
merged 1 commit into from
Sep 22, 2022
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
10 changes: 10 additions & 0 deletions lib/abi/function_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule ABI.FunctionSelector do
types: [type],
returns: [type],
type: :event | :function | :constructor | :error,
state_mutability: :pure | :view | :non_payable | :payable | nil,
inputs_indexed: [boolean]
}

Expand All @@ -48,6 +49,7 @@ defmodule ABI.FunctionSelector do
:method_id,
:type,
:inputs_indexed,
:state_mutability,
input_names: [],
types: [],
returns: []
Expand All @@ -66,6 +68,13 @@ defmodule ABI.FunctionSelector do
"tuple"
]

@state_mutability %{
"pure" => :pure,
"view" => :view,
"nonPayable" => :non_payable,
"payable" => :payable
}

@doc """
Decodes a function selector to a struct.

Expand Down Expand Up @@ -181,6 +190,7 @@ defmodule ABI.FunctionSelector do
types: input_types,
returns: output_types,
input_names: input_names,
state_mutability: @state_mutability[item["stateMutability"]],
type: :function
}

Expand Down
23 changes: 22 additions & 1 deletion test/abi/function_selector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ defmodule ABI.FunctionSelectorTest do
method_id: <<33, 173, 158, 39>>,
returns: [uint: 256],
type: :function,
types: [array: {:array, {:tuple, [uint: 256, uint: 256]}}]
types: [array: {:array, {:tuple, [uint: 256, uint: 256]}}],
state_mutability: :payable
}
] == ABI.parse_specification([abi])
end
Expand Down Expand Up @@ -342,6 +343,26 @@ defmodule ABI.FunctionSelectorTest do

assert expected_type == selector.types
end

test "with stateMutability set" do
~w(pure view nonPayable payable)
|> Enum.zip(~w(pure view non_payable payable)a)
|> Enum.each(fn {state_mutability, state_mutability_atom} ->
function = %{
"inputs" => [
%{"internalType" => "uint160[]", "name" => "exitIds", "type" => "uint160[]"}
],
"name" => "standardExits",
"outputs" => [],
"payable" => false,
"stateMutability" => state_mutability,
"type" => "function"
}

assert %FunctionSelector{state_mutability: ^state_mutability_atom} =
FunctionSelector.parse_specification_item(function)
end)
end
end

describe "simple_types?/1" do
Expand Down