update pools fetcher

pull/2036/head
saneery 6 years ago
parent 109bba9dc1
commit 8d2ecb9f61
  1. 34
      apps/indexer/lib/indexer/fetcher/staking_pools.ex
  2. 8
      apps/indexer/test/indexer/fetcher/staking_pools_test.exs

@ -9,6 +9,7 @@ defmodule Indexer.Fetcher.StakingPools do
require Logger
alias Explorer.Chain
alias Explorer.Chain.StakingPool
alias Explorer.Staking.PoolsReader
alias Indexer.BufferedTask
alias Indexer.Fetcher.StakingPools.Supervisor, as: StakingPoolsSupervisor
@ -71,7 +72,7 @@ defmodule Indexer.Fetcher.StakingPools do
def entry(pool_address) do
%{
staking_address: pool_address,
staking_address_hash: pool_address,
retries_count: 0
}
end
@ -79,7 +80,7 @@ defmodule Indexer.Fetcher.StakingPools do
defp fetch_from_blockchain(addresses) do
addresses
|> Enum.filter(&(&1.retries_count <= @max_retries))
|> Enum.map(fn %{staking_address: staking_address} = pool ->
|> Enum.map(fn %{staking_address_hash: staking_address} = pool ->
case PoolsReader.pool_data(staking_address) do
{:ok, data} ->
Map.merge(pool, data)
@ -93,11 +94,17 @@ defmodule Indexer.Fetcher.StakingPools do
defp import_pools(pools) do
{failed, success} =
Enum.reduce(pools, {[], []}, fn
%{error: _error, staking_address: address}, {failed, success} ->
%{error: _error, staking_address_hash: address}, {failed, success} ->
{[address | failed], success}
pool, {failed, success} ->
{failed, [changeset(pool) | success]}
%{staking_address_hash: address} = pool, {failed, success} ->
changeset = StakingPool.changeset(%StakingPool{}, pool)
if changeset.valid? do
{failed, [changeset.changes | success]}
else
{[address | failed], success}
end
end)
import_params = %{
@ -117,21 +124,4 @@ defmodule Indexer.Fetcher.StakingPools do
failed
end
defp changeset(%{staking_address: staking_address} = pool) do
{:ok, mining_address} = Chain.Hash.Address.cast(pool[:mining_address])
data =
pool
|> Map.delete(:staking_address)
|> Map.put(:mining_address, mining_address)
|> Map.put(:is_pool, true)
%{
name: "anonymous",
primary: true,
address_hash: staking_address,
metadata: data
}
end
end

@ -6,7 +6,7 @@ defmodule Indexer.Fetcher.StakingPoolsTest do
alias Indexer.Fetcher.StakingPools
alias Explorer.Staking.PoolsReader
alias Explorer.Chain.Address
alias Explorer.Chain.StakingPool
@moduletag :capture_log
@ -33,15 +33,15 @@ defmodule Indexer.Fetcher.StakingPoolsTest do
success_address =
list
|> List.first()
|> Map.get(:staking_address)
|> Map.get(:staking_address_hash)
get_pool_data_from_blockchain()
assert {:retry, retry_list} = StakingPools.run(list, nil)
assert Enum.count(retry_list) == 2
pool = Explorer.Repo.get_by(Address.Name, address_hash: success_address)
assert pool.name == "anonymous"
pool = Explorer.Repo.get_by(StakingPool, staking_address_hash: success_address)
assert pool.is_active == true
end
end

Loading…
Cancel
Save