Skip to content

Commit

Permalink
feat: Ignore events older than 24 hours in Explorer.Account.Notifier.… (
Browse files Browse the repository at this point in the history
blockscout#11654)

* feat: Ignore events older than 24 hours in Explorer.Account.Notifier.Notify

* Fix tests
  • Loading branch information
nikitosing authored Jan 15, 2025
1 parent 83ffe3c commit 5fff3e0
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 106 deletions.
24 changes: 10 additions & 14 deletions apps/explorer/lib/explorer/account/notifier/notify.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Explorer.Account.Notifier.Notify do

alias Explorer.Account.Notifier.{Email, ForbiddenAddress, Summary}
alias Explorer.Account.{WatchlistAddress, WatchlistNotification}
alias Explorer.Chain.{TokenTransfer, Transaction}
alias Explorer.Chain.Transaction
alias Explorer.{Mailer, Repo}

require Logger
Expand All @@ -20,20 +20,16 @@ defmodule Explorer.Account.Notifier.Notify do
Enum.map(transactions, fn transaction -> process(transaction) end)
end

defp process(%TokenTransfer{} = transfer) do
Logger.debug(transfer, fetcher: :account)

transfer
|> Summary.process()
|> Enum.map(fn summary -> notify_watchlists(summary) end)
end

defp process(%Transaction{} = transaction) do
Logger.debug(transaction, fetcher: :account)

transaction
|> Summary.process()
|> Enum.map(fn summary -> notify_watchlists(summary) end)
if DateTime.after?(transaction.block_timestamp, DateTime.add(DateTime.utc_now(), -1, :day)) do
Logger.debug(transaction, fetcher: :account)

transaction
|> Summary.process()
|> Enum.map(fn summary -> notify_watchlists(summary) end)
else
nil
end
end

defp process(_), do: nil
Expand Down
16 changes: 0 additions & 16 deletions apps/explorer/lib/explorer/account/notifier/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ defmodule Explorer.Account.Notifier.Summary do
end)
end

def process(%Chain.TokenTransfer{} = transfer) do
preloaded_transfer = preload(transfer)

summary = fetch_summary(preloaded_transfer.transaction, preloaded_transfer)

if summary != :nothing do
[summary]
else
[]
end
end

def process(_), do: nil

def handle_collection(_transaction, []), do: []
Expand Down Expand Up @@ -231,8 +219,4 @@ defmodule Explorer.Account.Notifier.Summary do
defp preload(%Chain.Transaction{} = transaction) do
Repo.preload(transaction, token_transfers: :token)
end

defp preload(%Chain.TokenTransfer{} = transfer) do
Repo.preload(transfer, [:transaction, :token])
end
end
28 changes: 28 additions & 0 deletions apps/explorer/test/explorer/account/notifier/notify_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,33 @@ defmodule Explorer.Account.Notifier.NotifyTest do

Application.put_env(:explorer, Explorer.Account, old_envs)
end

test "ignore events older than 24 hrs" do
wa =
%WatchlistAddress{address_hash: address_hash} =
build(:account_watchlist_address, watch_coin_input: true)
|> Repo.account_repo().insert!()

_watchlist_address = Repo.preload(wa, watchlist: :identity)

transaction =
%Transaction{
from_address: _from_address,
to_address: _to_address,
block_number: _block_number,
hash: _transaction_hash
} =
with_block(
insert(:transaction, to_address: %Chain.Address{hash: address_hash}),
insert(:block, timestamp: DateTime.add(DateTime.utc_now(), -25, :hour))
)

notify = Notify.call([transaction])

assert WatchlistNotification
|> Repo.account_repo().all() == []

assert notify == [nil]
end
end
end
Loading

0 comments on commit 5fff3e0

Please sign in to comment.