-
Notifications
You must be signed in to change notification settings - Fork 29
Add a way to fetch all contract events #222
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cbd5d0
Add a way to fetch all contract events
ddallaire 86c1df9
Merge pull request #1 from ddallaire/feat/get-contract-events
ddallaire efecb06
Add a way to fetch all contract events
ddallaire ddc8115
Merge branch 'main' into feat/get-contract-events
alisinabh 11012d0
Revert get_logs_for_contract in favor of combined event filters
alisinabh 2c9e29a
Add combined event filters for fetching multiple events in one request
alisinabh e8a0a19
Accept EventFilters modules for fetching all contract events
alisinabh 71e5cb7
Potential fix for pull request finding
alisinabh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| defmodule Ethers.CombinedEventFilter do | ||
| @moduledoc """ | ||
| A filter matching any of multiple events (OR semantics) in a single `eth_getLogs` request. | ||
|
|
||
| Combined event filters are created with `Ethers.EventFilter.combine/1` and can be used | ||
| anywhere a regular event filter is accepted (e.g. `Ethers.get_logs/2` and | ||
| `Ethers.batch/2`). To match all events of a contract, pass its EventFilters module | ||
| directly to `Ethers.get_logs/2` (or `Ethers.EventFilter.combine/1`) which internally | ||
| translates it to a combined filter of all the contract events. | ||
|
|
||
| The topics of the combined filters are sent to the RPC endpoint as an OR-ed list of | ||
| `topic_0` values so filtering happens server side. Fetched logs are decoded using the | ||
| event selector matching their first topic. | ||
|
|
||
| ## Limitations | ||
|
|
||
| - Only filters with wildcard indexed arguments (all `nil`) can be combined. Combining | ||
| filters with indexed-argument values would create cross-product OR semantics on the | ||
| topics and match unintended logs. | ||
| - All combined filters must belong to the same address. Filters with conflicting | ||
| default addresses cannot be combined since `eth_getLogs` accepts a single address. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```elixir | ||
| filter = | ||
| Ethers.EventFilter.combine([ | ||
| Ethers.Contracts.ERC20.EventFilters.transfer(nil, nil), | ||
| Ethers.Contracts.ERC20.EventFilters.approval(nil, nil) | ||
| ]) | ||
|
|
||
| Ethers.get_logs(filter, address: "0x...") | ||
| {:ok, [%Ethers.Event{...}, ...]} | ||
|
|
||
| # Or fetch all events of a contract by passing its EventFilters module | ||
| Ethers.get_logs(Ethers.Contracts.ERC20.EventFilters, address: "0x...") | ||
| {:ok, [%Ethers.Event{...}, ...]} | ||
| ``` | ||
| """ | ||
|
|
||
| alias Ethers.ContractHelpers | ||
| alias Ethers.Event | ||
| alias Ethers.EventFilter | ||
|
|
||
| import Ethers.Types, only: [is_module: 1] | ||
|
|
||
| @typedoc """ | ||
| Holds the combined topics (a single OR-ed list of `topic_0` values), the event | ||
| selectors indexed by their `topic_0` and the default address. | ||
| """ | ||
| @type t :: %__MODULE__{ | ||
| topics: [[binary()]], | ||
| selectors: %{binary() => ABI.FunctionSelector.t()}, | ||
| default_address: nil | Ethers.Types.t_address() | ||
| } | ||
|
|
||
| @enforce_keys [:topics, :selectors] | ||
| defstruct [:topics, :selectors, :default_address] | ||
|
|
||
| @doc false | ||
| @spec new([EventFilter.t() | module()] | module()) :: t() | ||
| def new(events_module) when is_module(events_module), do: new([events_module]) | ||
|
|
||
| def new(filters_or_modules) when is_list(filters_or_modules) do | ||
| case Enum.flat_map(filters_or_modules, &expand_filters/1) do | ||
| [] -> | ||
| raise ArgumentError, "cannot combine an empty list of event filters" | ||
|
|
||
| event_filters -> | ||
| Enum.each(event_filters, &validate_filter!/1) | ||
|
|
||
| {topic_0s, selectors} = Enum.reduce(event_filters, {[], %{}}, &collect_topic_0/2) | ||
|
|
||
| %__MODULE__{ | ||
| topics: [Enum.reverse(topic_0s)], | ||
| selectors: selectors, | ||
| default_address: combined_default_address!(event_filters) | ||
| } | ||
| end | ||
| end | ||
|
|
||
| @doc false | ||
| @spec from_events_module(module()) :: t() | ||
| def from_events_module(events_module), do: new([events_module]) | ||
|
|
||
| @doc false | ||
| @spec all!(module()) :: t() | ||
| def all!(events_module) when is_module(events_module) do | ||
| if Code.ensure_loaded?(events_module) and function_exported?(events_module, :__all__, 0) do | ||
| events_module.__all__() | ||
| else | ||
| raise ArgumentError, | ||
| "#{inspect(events_module)} is not an EventFilters module of an Ethers contract" | ||
| end | ||
| end | ||
|
|
||
| @doc false | ||
| @spec to_map(t(), Keyword.t()) :: map() | ||
| def to_map(%__MODULE__{} = combined_filter, overrides) do | ||
| %{topics: combined_filter.topics} | ||
| |> maybe_add_address(combined_filter.default_address) | ||
| |> then(&Enum.into(overrides, &1)) | ||
| end | ||
|
|
||
| @doc """ | ||
| Decodes fetched logs using the matching event selectors of the combined filter. | ||
|
|
||
| Logs not matching any of the combined events are discarded. (Cannot happen with logs | ||
| fetched using the combined filter itself, since the RPC endpoint only returns logs | ||
| matching the requested topics) | ||
| """ | ||
| @spec decode_logs([map()], t()) :: [Event.t()] | ||
| def decode_logs(logs, %__MODULE__{selectors: selectors}) when is_list(logs) do | ||
| Enum.flat_map(logs, fn log -> | ||
| [topic_0 | _] = Map.fetch!(log, "topics") | ||
|
|
||
| case Map.fetch(selectors, String.downcase(topic_0)) do | ||
| {:ok, selector} -> [Event.decode(log, selector)] | ||
| :error -> [] | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| defp expand_filters(%EventFilter{} = filter), do: [filter] | ||
|
|
||
| defp expand_filters(events_module) when is_module(events_module) do | ||
| unless Code.ensure_loaded?(events_module) and | ||
| function_exported?(events_module, :__events__, 0) do | ||
| raise ArgumentError, | ||
| "#{inspect(events_module)} is not an EventFilters module of an Ethers contract" | ||
| end | ||
|
|
||
| case events_module.__events__() do | ||
| [] -> | ||
| raise ArgumentError, "#{inspect(events_module)} does not have any events" | ||
|
|
||
| selectors -> | ||
| Enum.map(selectors, &wildcard_filter(&1, events_module.__default_address__())) | ||
| end | ||
| end | ||
|
|
||
| defp expand_filters(other) do | ||
| raise ArgumentError, | ||
| "expected an Ethers.EventFilter struct or an EventFilters module, got: #{inspect(other)}" | ||
| end | ||
|
|
||
| defp wildcard_filter(selector, default_address) do | ||
| wildcard_args = Enum.map(ContractHelpers.event_indexed_types(selector), fn _ -> nil end) | ||
|
|
||
| selector | ||
| |> ContractHelpers.encode_event_topics(wildcard_args) | ||
| |> EventFilter.new(selector, default_address) | ||
| end | ||
|
|
||
| defp collect_topic_0(%EventFilter{topics: [topic_0 | _]} = filter, {topic_0s, selectors}) do | ||
| topic_0 = String.downcase(topic_0) | ||
|
|
||
| if Map.has_key?(selectors, topic_0) do | ||
| {topic_0s, selectors} | ||
| else | ||
| {[topic_0 | topic_0s], Map.put(selectors, topic_0, filter.selector)} | ||
| end | ||
| end | ||
|
|
||
| defp validate_filter!(%EventFilter{topics: [_topic_0 | sub_topics]} = filter) do | ||
| if Enum.all?(sub_topics, &is_nil/1) do | ||
| :ok | ||
| else | ||
| raise ArgumentError, | ||
| "cannot combine event filter with indexed-argument values: #{inspect(filter)}" <> | ||
| " (use nil as a wildcard for all indexed arguments)" | ||
| end | ||
| end | ||
|
|
||
| defp combined_default_address!(event_filters) do | ||
| event_filters | ||
| |> Enum.map(& &1.default_address) | ||
| |> Enum.reject(&is_nil/1) | ||
| |> Enum.uniq_by(&String.downcase/1) | ||
| |> case do | ||
| [] -> | ||
| nil | ||
|
|
||
| [address] -> | ||
| address | ||
|
|
||
| addresses -> | ||
| raise ArgumentError, | ||
| "cannot combine event filters with conflicting default addresses: " <> | ||
| "#{inspect(addresses)} (eth_getLogs accepts a single address," <> | ||
| " combine filters of the same contract)" | ||
| end | ||
| end | ||
|
|
||
| defp maybe_add_address(map, nil), do: map | ||
| defp maybe_add_address(map, address), do: Map.put(map, :address, address) | ||
|
|
||
| defimpl Inspect do | ||
| import Inspect.Algebra | ||
|
|
||
| def inspect( | ||
| %{topics: [topic_0s], selectors: selectors, default_address: default_address}, | ||
| opts | ||
| ) do | ||
| default_address = | ||
| case default_address do | ||
| nil -> | ||
| [] | ||
|
|
||
| _ -> | ||
| [ | ||
| line(), | ||
| color("default_address: ", :default, opts), | ||
| color(inspect(default_address), :string, opts) | ||
| ] | ||
| end | ||
|
|
||
| events = | ||
| topic_0s | ||
| |> Enum.map(&Map.fetch!(selectors, &1)) | ||
| |> Enum.map(fn selector -> | ||
| concat([ | ||
| line(), | ||
| color("event", :atom, opts), | ||
| " ", | ||
| color(ABI.FunctionSelector.encode(selector), :call, opts) | ||
| ]) | ||
| end) | ||
|
|
||
| inner = concat(events ++ default_address) | ||
|
|
||
| concat([ | ||
| color("#Ethers.CombinedEventFilter<", :map, opts), | ||
| nest(inner, 2), | ||
| break(""), | ||
| color(">", :map, opts) | ||
| ]) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.