Refactor cataloged token query to Chain.Token

pull/1095/head
William Sanches 6 years ago
parent 5b14292aa9
commit 8ee4e62087
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 11
      apps/explorer/lib/explorer/chain.ex
  2. 13
      apps/explorer/lib/explorer/chain/token.ex
  3. 16
      apps/explorer/test/explorer/chain/token_test.exs
  4. 19
      apps/explorer/test/explorer/chain_test.exs

@ -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,

@ -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

@ -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

@ -2756,12 +2756,29 @@ 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
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
test "returns true if transaction has token transfers" do
transaction = insert(:transaction)

Loading…
Cancel
Save