Skip to content

Commit ca26505

Browse files
authored
Support optional fields in EventFilter typespecs (#144)
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.
1 parent c474546 commit ca26505

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

lib/ethers/contract_helpers.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ defmodule Ethers.ContractHelpers do
219219

220220
def generate_event_typespecs(selectors, arity) do
221221
Enum.map(selectors, &Enum.take(&1.types, arity))
222-
|> do_generate_typescpecs()
222+
|> do_generate_typescpecs(true)
223223
end
224224

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

232-
defp do_generate_typescpecs(types) do
232+
defp do_generate_typescpecs(types, optional? \\ false) do
233233
Enum.zip_with(types, & &1)
234234
|> Enum.map(fn type_group ->
235235
type_group
236236
|> Enum.map(&Ethers.Types.to_elixir_type/1)
237237
|> Enum.uniq()
238+
|> then(&if(optional?, do: [nil | &1], else: &1))
238239
|> Enum.reduce(fn type, acc ->
239240
quote do
240241
unquote(type) | unquote(acc)

0 commit comments

Comments
 (0)