Skip to content
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
2 changes: 1 addition & 1 deletion lib/ethers/contract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ defmodule Ethers.Contract do

func_args = generate_arguments(mod, abi.arity, aggregated_input_names)

func_typespec = generate_event_typespecs(abi.selectors, abi.arity)
func_typespec = generate_event_typespecs(abi.selectors)

quote context: mod, location: :keep do
if unquote(generate_docs?(name, opts[:skip_docs])) do
Expand Down
4 changes: 2 additions & 2 deletions lib/ethers/contract_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ defmodule Ethers.ContractHelpers do
|> do_generate_typescpecs()
end

def generate_event_typespecs(selectors, arity) do
Enum.map(selectors, &Enum.take(&1.types, arity))
def generate_event_typespecs(selectors) do
Enum.map(selectors, &event_indexed_types/1)
|> do_generate_typescpecs(true)
end

Expand Down
22 changes: 22 additions & 0 deletions test/ethers/contract_helpers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ defmodule Ethers.ContractHelpersTest do
end
end

describe "generate_event_typespecs" do
test "uses indexed field types when indexed and non-indexed fields are interleaved" do
# Transfer(uint256 amount, address indexed sender, bool isFinal, address indexed receiver)
# from test/support/contracts/event_mixed_index.sol
selector = %ABI.FunctionSelector{
function: "Transfer",
type: :event,
types: [{:uint, 256}, :address, :bool, :address],
inputs_indexed: [false, true, false, true],
input_names: ["amount", "sender", "isFinal", "receiver"],
returns: []
}

[sender_type, receiver_type] = ContractHelpers.generate_event_typespecs([selector])
address_typespec = Ethers.Types.to_elixir_type(:address)

# Both indexed params are address, so both typespecs should be t_address() | nil.
assert sender_type == quote(do: unquote(address_typespec) | nil)
assert receiver_type == quote(do: unquote(address_typespec) | nil)
end
end

describe "human_signature" do
test "returns the human signature of a given function" do
assert "name(uint256 id, address address)" ==
Expand Down
Loading