Forbidden to notify address check

account
Oleg Sovetnik 3 years ago committed by Viktor Baranov
parent d8244f1cc7
commit 5cebbe81d6
  1. 15
      apps/block_scout_web/lib/block_scout_web/models/add_watchlist_address.ex
  2. 68
      apps/explorer/lib/explorer/accounts/notify/forbidden_address.ex
  3. 30
      apps/explorer/lib/explorer/accounts/notify/notifier.ex

@ -9,24 +9,25 @@ defmodule AddWatchlistAddress do
call(watchlist, params)
"""
alias Explorer.{Chain, Repo}
alias Explorer.Accounts.Notify.ForbiddenAddress
alias Explorer.Accounts.{Watchlist, WatchlistAddress}
alias Explorer.Chain.Address
alias Explorer.Repo
def call(watchlist_id, %{"address_hash" => address_hash_string} = params) do
case format_address(address_hash_string) do
case ForbiddenAddress.check(address_hash_string) do
{:ok, address_hash} ->
try_create_watchlist_address(watchlist_id, address_hash, params)
:error ->
{:error, "Wrong address, "}
{:error, message} ->
{:error, message}
end
end
defp try_create_watchlist_address(watchlist_id, address_hash, params) do
case find_watchlist_address(watchlist_id, address_hash) do
%WatchlistAddress{} ->
{:error, "Address already exists!"}
{:error, "Address already added it this watchlist"}
nil ->
with {:ok, %Address{} = address} <- find_or_create_address(address_hash) do
@ -69,10 +70,6 @@ defmodule AddWatchlistAddress do
defp to_bool("true"), do: true
defp to_bool("false"), do: false
defp format_address(address_hash_string) do
Chain.string_to_address_hash(address_hash_string)
end
defp find_watchlist_address(watchlist_id, address_hash) do
Repo.get_by(WatchlistAddress,
address_hash: address_hash,

@ -0,0 +1,68 @@
defmodule Explorer.Accounts.Notify.ForbiddenAddress do
@moduledoc """
Check if address is forbidden to notify
"""
@blacklist [
"0x0000000000000000000000000000000000000000",
"0x000000000000000000000000000000000000dEaD"
]
# alias Explorer.Repo
alias Explorer.{Chain, Repo}
alias Explorer.Chain.{Address, Token}
import Ecto.Query, only: [from: 2]
import Explorer.Chain, only: [string_to_address_hash: 1]
def check(address_string) when is_bitstring(address_string) do
case format_address(address_string) do
{:error, message} ->
{:error, message}
address_hash ->
check(address_hash)
end
end
def check(%Explorer.Chain.Hash{} = address_hash) do
cond do
address_hash in blacklist() ->
{:error, "This address is blacklisted."}
is_contract(address_hash) ->
{:error, "This address isn't personal."}
address_hash ->
{:ok, address_hash}
end
end
defp is_contract(%Explorer.Chain.Hash{} = address_hash) do
query =
from(
token in Token,
where: token.contract_address_hash == ^address_hash
)
contract_addresses = Repo.all(query)
List.first(contract_addresses)
end
defp format_address(address_hash_string) do
case Chain.string_to_address_hash(address_hash_string) do
{:ok, address_hash} ->
address_hash
:error ->
{:error, "Address "}
end
end
defp blacklist do
Enum.map(
@blacklist,
&format_address(&1)
)
end
end

@ -3,7 +3,7 @@ defmodule Explorer.Accounts.Notify.Notifier do
Composing notification, store and send it to email
"""
alias Explorer.Accounts.Notify.{Email, Summary}
alias Explorer.Accounts.Notify.{Email, ForbiddenAddress, Summary}
alias Explorer.Accounts.{WatchlistAddress, WatchlistNotification}
alias Explorer.Chain.{TokenTransfer, Transaction}
alias Explorer.{Mailer, Repo}
@ -52,15 +52,25 @@ defmodule Explorer.Accounts.Notify.Notifier do
end
defp notify_watchlist(%Explorer.Accounts.WatchlistAddress{} = address, summary, direction) do
with %WatchlistNotification{} = notification <-
build_watchlist_notification(address, summary, direction) do
notification
|> query_notification(address)
|> Repo.all()
|> case do
[] -> save_and_send_notification(notification, address)
_ -> :ok
end
case ForbiddenAddress.check(address.address_hash) do
{:ok, _address_hash} ->
with %WatchlistNotification{} = notification <-
build_watchlist_notification(
address,
summary,
direction
) do
notification
|> query_notification(address)
|> Repo.all()
|> case do
[] -> save_and_send_notification(notification, address)
_ -> :ok
end
end
{:error, _message} ->
nil
end
end

Loading…
Cancel
Save