diff --git a/Procfile b/Procfile index 19f5df330d..4a9580c896 100644 --- a/Procfile +++ b/Procfile @@ -4,3 +4,4 @@ scheduler: bin/start-pgbouncer-stunnel mix exq.start scheduler blocks: bin/start-pgbouncer-stunnel mix scrape.blocks 1000000 receipts: bin/start-pgbouncer-stunnel mix scrape.receipts 10000 internal_transactions: bin/start-pgbouncer-stunnel mix scrape.internal_transactions 10000 +balances: bin/start-pgbouncer-stunnel mix scrape.balances 1000000 diff --git a/lib/explorer/skipped_balances.ex b/lib/explorer/skipped_balances.ex new file mode 100644 index 0000000000..380c879115 --- /dev/null +++ b/lib/explorer/skipped_balances.ex @@ -0,0 +1,21 @@ +defmodule Explorer.SkippedBalances do + @moduledoc "Gets a list of Addresses that do not have balances." + + alias Explorer.Address + alias Explorer.Repo + + import Ecto.Query, only: [from: 2] + + def fetch(count) do + query = + from( + address in Address, + select: address.hash, + where: is_nil(address.balance), + limit: ^count + ) + + query + |> Repo.all() + end +end diff --git a/lib/mix/tasks/scrape.balances.ex b/lib/mix/tasks/scrape.balances.ex new file mode 100644 index 0000000000..bd282b7592 --- /dev/null +++ b/lib/mix/tasks/scrape.balances.ex @@ -0,0 +1,26 @@ +defmodule Mix.Tasks.Scrape.Balances do + @moduledoc "Populate Address balances." + + use Mix.Task + + alias Explorer.Repo + alias Explorer.SkippedBalances + alias Explorer.BalanceImporter + + def run([]), do: run(1) + + def run(count) do + [:postgrex, :ecto, :ethereumex, :tzdata] + |> Enum.each(&Application.ensure_all_started/1) + + Repo.start_link() + Exq.start_link(mode: :enqueuer) + + "#{count}" + |> String.to_integer() + |> SkippedBalances.fetch() + |> Flow.from_enumerable() + |> Flow.map(&BalanceImporter.import/1) + |> Enum.to_list() + end +end diff --git a/test/explorer/skipped_balances_test.exs b/test/explorer/skipped_balances_test.exs new file mode 100644 index 0000000000..38913da829 --- /dev/null +++ b/test/explorer/skipped_balances_test.exs @@ -0,0 +1,18 @@ +defmodule Explorer.SkippedBalancesTest do + use Explorer.DataCase + + alias Explorer.SkippedBalances + + describe "fetch/1" do + test "returns a list of address hashes that do not have balances" do + insert(:address, hash: "0xcashews", balance: nil) + assert SkippedBalances.fetch(1) == ["0xcashews"] + end + + test "only get a limited set of addresses" do + insert_list(10, :address, balance: nil) + insert_list(5, :address, balance: 55) + assert length(SkippedBalances.fetch(7)) == 7 + end + end +end