diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index fa85587925..d79126f9dd 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1950,15 +1950,8 @@ defmodule Explorer.Chain do def stream_cataloged_token_contract_address_hashes(initial_acc, reducer) when is_function(reducer, 2) do Repo.transaction( fn -> - query = - from( - token in Token, - select: token.contract_address_hash, - where: token.cataloged == true, - order_by: [asc: token.updated_at] - ) - - query + Chain.Token.cataloged_tokens() + |> Ecto.Query.order_by([asc: :updated_at]) |> Repo.stream(timeout: :infinity) |> Enum.reduce(initial_acc, reducer) end, diff --git a/apps/explorer/lib/explorer/chain/token.ex b/apps/explorer/lib/explorer/chain/token.ex index 81a4c96ba9..306ded7856 100644 --- a/apps/explorer/lib/explorer/chain/token.ex +++ b/apps/explorer/lib/explorer/chain/token.ex @@ -94,4 +94,17 @@ defmodule Explorer.Chain.Token do on: tt.token_contract_address_hash == t.contract_address_hash ) end + + @doc """ + Builds an `Ecto.Query` to fetch the cataloged tokens. + + These are tokens with cataloged field set to true. + """ + def cataloged_tokens() do + from( + token in __MODULE__, + select: token.contract_address_hash, + where: token.cataloged == true + ) + end end diff --git a/apps/explorer/test/explorer/chain/token_test.exs b/apps/explorer/test/explorer/chain/token_test.exs new file mode 100644 index 0000000000..57cf42d13a --- /dev/null +++ b/apps/explorer/test/explorer/chain/token_test.exs @@ -0,0 +1,16 @@ +defmodule Explorer.Chain.TokenTest do + use Explorer.DataCase + + import Explorer.Factory + + alias Explorer.Chain + + describe "cataloged_tokens/0" do + test "filters uncataloged tokens out" do + token = insert(:token, cataloged: true) + insert(:token, cataloged: false) + + assert Repo.all(Chain.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 6d8f2f633c..2feb6aeddc 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -2756,10 +2756,27 @@ defmodule Explorer.ChainTest do assert Chain.stream_uncataloged_token_contract_address_hashes([], &[&1 | &2]) == {:ok, [uncatalog_address]} end - test "stream_cataloged_token_contract_address_hashes/2 reduces with given reducer and accumulator" do - %Token{contract_address_hash: catalog_address} = insert(:token, cataloged: true) - insert(:token, cataloged: false) - assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2]) == {:ok, [catalog_address]} + 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) + insert(:token, cataloged: false) + assert Chain.stream_cataloged_token_contract_address_hashes([], &[&1 | &2]) == {: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) + + token1 = insert(:token, %{cataloged: true, updated_at: today}) + token2 = insert(:token, %{cataloged: true, updated_at: yesterday}) + + expected_response = + [token1, token2] + |> Enum.sort(&(&1.updated_at < &2.updated_at)) + |> Enum.map(&(&1.contract_address_hash)) + + assert Chain.stream_cataloged_token_contract_address_hashes([], &(&2 ++ [&1])) == {:ok, expected_response} + end end describe "transaction_has_token_transfers?/1" do