Skip to content

Commit

Permalink
Support optional fields in EventFilter typespecs (#144)
Browse files Browse the repository at this point in the history
The current typespec generator emits a dialyzer warning when nil values are set for the indexed topics of an `EventFilter`:

```elixir
The function call will not succeed.

Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil)

breaks the contract
(Ethers.Types.t_address(), Ethers.Types.t_address()) :: Ethers.EventFilter.t()
```

This can be fixed by making the arguments nullable for events.
  • Loading branch information
NduatiK authored Oct 14, 2024
1 parent c474546 commit ca26505
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/ethers/contract_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ defmodule Ethers.ContractHelpers do

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

def generate_struct_typespecs(args, selector) do
Expand All @@ -229,12 +229,13 @@ defmodule Ethers.ContractHelpers do
{:%, [], [{:__MODULE__, [], Elixir}, {:%{}, [], Enum.zip(args, types)}]}
end

defp do_generate_typescpecs(types) do
defp do_generate_typescpecs(types, optional? \\ false) do
Enum.zip_with(types, & &1)
|> Enum.map(fn type_group ->
type_group
|> Enum.map(&Ethers.Types.to_elixir_type/1)
|> Enum.uniq()
|> then(&if(optional?, do: [nil | &1], else: &1))
|> Enum.reduce(fn type, acc ->
quote do
unquote(type) | unquote(acc)
Expand Down

0 comments on commit ca26505

Please sign in to comment.