fix: Sanitize topic value before making db query (#10481)

* Sanitize topic value before making db query

* Process review comment
vb-nft-collection-trigger-metadata-refetch-admin-api-endpoint
Victor Baranov 4 months ago committed by GitHub
parent 3ab691d0e0
commit 25339b9479
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 42
      apps/explorer/lib/explorer/etherscan/logs.ex

@ -275,6 +275,8 @@ defmodule Explorer.Etherscan.Logs do
}
defp where_topic_match(query, filter) do
filter = sanitize_filter_topics(filter)
case Enum.filter(@topics, &filter[&1]) do
[] ->
query
@ -287,6 +289,46 @@ defmodule Explorer.Etherscan.Logs do
end
end
defp sanitize_filter_topics(filter) do
@topics
|> Enum.reduce(filter, fn topic, acc ->
topic_value = filter[topic]
sanitized_value =
topic_value
|> List.wrap()
|> Enum.map(&sanitize_topic_value/1)
|> Enum.reject(&is_nil/1)
|> case do
[] -> nil
[topic] -> topic
topics -> topics
end
Map.put(acc, topic, sanitized_value)
end)
end
defp sanitize_topic_value(topic_value) do
case topic_value do
%Explorer.Chain.Hash{} ->
topic_value
_ ->
sanitize_string_topic_value(topic_value)
end
end
defp sanitize_string_topic_value(topic_value) do
case Chain.string_to_block_hash(topic_value) do
{:ok, _} ->
topic_value
_ ->
nil
end
end
defp where_multiple_topics_match(query, filter) do
Enum.reduce(Map.keys(@topic_operations), query, fn topic_operation, acc_query ->
where_multiple_topics_match(acc_query, filter, topic_operation, filter[topic_operation])

Loading…
Cancel
Save