diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 81f6061da5..5f89001786 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2916,8 +2916,10 @@ defmodule Explorer.Chain do reducer :: (entry :: Hash.Address.t(), accumulator -> accumulator) ) :: {:ok, accumulator} when accumulator: term() - def stream_cataloged_token_contract_address_hashes(initial, reducer) when is_function(reducer, 2) do - Token.cataloged_tokens() + def stream_cataloged_token_contract_address_hashes(initial, reducer, hours_ago_updated \\ 48) + when is_function(reducer, 2) do + hours_ago_updated + |> Token.cataloged_tokens() |> order_by(asc: :updated_at) |> Repo.stream_reduce(initial, reducer) end diff --git a/apps/explorer/lib/explorer/chain/token.ex b/apps/explorer/lib/explorer/chain/token.ex index 275fd73659..b5cb289044 100644 --- a/apps/explorer/lib/explorer/chain/token.ex +++ b/apps/explorer/lib/explorer/chain/token.ex @@ -105,11 +105,14 @@ defmodule Explorer.Chain.Token do These are tokens with cataloged field set to true. """ - def cataloged_tokens do + def cataloged_tokens(hours \\ 48) do + date_now = DateTime.utc_now() + hours_ago_date = DateTime.add(date_now, -:timer.hours(hours), :millisecond) + from( token in __MODULE__, select: token.contract_address_hash, - where: token.cataloged == true + where: token.cataloged == true and token.updated_at <= ^hours_ago_date ) end end diff --git a/apps/explorer/test/explorer/chain/token_test.exs b/apps/explorer/test/explorer/chain/token_test.exs index 0cab67ecff..df8650530f 100644 --- a/apps/explorer/test/explorer/chain/token_test.exs +++ b/apps/explorer/test/explorer/chain/token_test.exs @@ -3,14 +3,27 @@ defmodule Explorer.Chain.TokenTest do import Explorer.Factory - alias Explorer.Chain + alias Explorer.Chain.Token + alias Explorer.Repo describe "cataloged_tokens/0" do test "filters only cataloged tokens" do - token = insert(:token, cataloged: true) + {:ok, date} = DateTime.now("Etc/UTC") + hours_ago_date = DateTime.add(date, -:timer.hours(60), :millisecond) + token = insert(:token, cataloged: true, updated_at: hours_ago_date) insert(:token, cataloged: false) - assert Repo.all(Chain.Token.cataloged_tokens()) == [token.contract_address_hash] + assert Repo.all(Token.cataloged_tokens()) == [token.contract_address_hash] + end + + test "filter tokens by updated_at filed" do + {:ok, date} = DateTime.now("Etc/UTC") + hours_ago_date = DateTime.add(date, -:timer.hours(60), :millisecond) + + token = insert(:token, cataloged: true, updated_at: hours_ago_date) + insert(:token, cataloged: true) + + assert Repo.all(Token.cataloged_tokens()) == [token.contract_address_hash] end end end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 872a3bc5c0..3a073fa4ab 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -3522,24 +3522,27 @@ defmodule Explorer.ChainTest do describe "stream_cataloged_token_contract_address_hashes/2" do test "reduces with given reducer and accumulator" do - %Token{contract_address_hash: catalog_address} = insert(:token, cataloged: true) + today = DateTime.utc_now() + yesterday = Timex.shift(today, days: -1) + %Token{contract_address_hash: catalog_address} = insert(:token, cataloged: true, updated_at: yesterday) insert(:token, cataloged: false) - assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2]) == {:ok, [catalog_address]} + assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2], 1) == {:ok, [catalog_address]} end test "sorts the tokens by updated_at in ascending order" do today = DateTime.utc_now() yesterday = Timex.shift(today, days: -1) + two_days_ago = Timex.shift(today, days: -2) - token1 = insert(:token, %{cataloged: true, updated_at: today}) - token2 = insert(:token, %{cataloged: true, updated_at: yesterday}) + token1 = insert(:token, %{cataloged: true, updated_at: yesterday}) + token2 = insert(:token, %{cataloged: true, updated_at: two_days_ago}) expected_response = [token1, token2] |> Enum.sort(&(Timex.to_unix(&1.updated_at) < Timex.to_unix(&2.updated_at))) |> Enum.map(& &1.contract_address_hash) - assert Chain.stream_cataloged_token_contract_address_hashes([], &(&2 ++ [&1])) == {:ok, expected_response} + assert Chain.stream_cataloged_token_contract_address_hashes([], &(&2 ++ [&1]), 12) == {:ok, expected_response} end end