forked from blockscout/blockscout
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test to fail suite if event map doesn't cover all the events in t…
…he application (#594) * Generate event map + add test. * Move event map template to external file. * Regenerate map.
- Loading branch information
1 parent
2796401
commit ea9c730
Showing
4 changed files
with
90 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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 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 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,68 @@ | ||
# This file is auto generated, changes will be lost upon regeneration | ||
defmodule Explorer.Celo.ContractEvents.EventMap do | ||
@moduledoc "Map event names and event topics to concrete contract event structs" | ||
|
||
alias Explorer.Celo.ContractEvents.EventTransformer | ||
alias Explorer.Repo | ||
|
||
@doc "Convert ethrpc log parameters to CeloContractEvent insertion parameters" | ||
def rpc_to_event_params(logs) when is_list(logs) do | ||
logs | ||
|> Enum.map(fn params = %{first_topic: event_topic} -> | ||
case event_for_topic(event_topic) do | ||
nil -> | ||
nil | ||
|
||
event -> | ||
event | ||
|> struct!() | ||
|> EventTransformer.from_params(params) | ||
|> EventTransformer.to_celo_contract_event_params() | ||
end | ||
end) | ||
|> Enum.reject(&is_nil/1) | ||
end | ||
|
||
@doc "Convert CeloContractEvent instance to their concrete types" | ||
def celo_contract_event_to_concrete_event(events) when is_list(events) do | ||
events | ||
|> Enum.map(&celo_contract_event_to_concrete_event/1) | ||
|> Enum.reject(&is_nil/1) | ||
end | ||
|
||
def celo_contract_event_to_concrete_event(%{topic: topic} = params) do | ||
case event_for_topic(topic) do | ||
nil -> | ||
nil | ||
|
||
event -> | ||
event | ||
|> struct!() | ||
|> EventTransformer.from_celo_contract_event(params) | ||
end | ||
end | ||
|
||
@doc "Run ecto query and convert all CeloContractEvents into their concrete types" | ||
def query_all(query) do | ||
query | ||
|> Repo.all() | ||
|> celo_contract_event_to_concrete_event() | ||
end | ||
|
||
@doc "Convert concrete event to CeloContractEvent insertion parameters" | ||
def event_to_contract_event_params(events) when is_list(events) do | ||
events |> Enum.map(&event_to_contract_event_params/1) | ||
end | ||
|
||
def event_to_contract_event_params(event) do | ||
event |> EventTransformer.to_celo_contract_event_params() | ||
end | ||
|
||
@topic_to_event %{ | ||
<%= for module <- @modules do %> "<%= module.topic %>" => | ||
<%= module %>, | ||
<% end %>} | ||
|
||
def event_for_topic(topic), do: Map.get(@topic_to_event, topic) | ||
def map, do: @topic_to_event | ||
end |
This file contains 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