update method that return pools list

pull/2036/head
saneery 6 years ago
parent 4808a8f063
commit 0c0662d86e
  1. 46
      apps/explorer/lib/explorer/chain.ex
  2. 34
      apps/explorer/test/explorer/chain_test.exs

@ -34,6 +34,7 @@ defmodule Explorer.Chain do
BlockNumberCache, BlockNumberCache,
Data, Data,
DecompiledSmartContract, DecompiledSmartContract,
StakingPool,
Hash, Hash,
Import, Import,
InternalTransaction, InternalTransaction,
@ -2909,7 +2910,7 @@ defmodule Explorer.Chain do
def staking_pools(filter, %PagingOptions{page_size: page_size, page_number: page_number} \\ @default_paging_options) do def staking_pools(filter, %PagingOptions{page_size: page_size, page_number: page_number} \\ @default_paging_options) do
off = page_size * (page_number - 1) off = page_size * (page_number - 1)
Address.Name StakingPool
|> staking_pool_filter(filter) |> staking_pool_filter(filter)
|> limit(^page_size) |> limit(^page_size)
|> offset(^off) |> offset(^off)
@ -2919,55 +2920,36 @@ defmodule Explorer.Chain do
@doc "Get count of staking pools from the DB" @doc "Get count of staking pools from the DB"
@spec staking_pools_count(filter :: :validator | :active | :inactive) :: integer @spec staking_pools_count(filter :: :validator | :active | :inactive) :: integer
def staking_pools_count(filter) do def staking_pools_count(filter) do
Address.Name StakingPool
|> staking_pool_filter(filter) |> staking_pool_filter(filter)
|> Repo.aggregate(:count, :address_hash) |> Repo.aggregate(:count, :staking_address_hash)
end end
defp staking_pool_filter(query, :validator) do defp staking_pool_filter(query, :validator) do
where( where(
query, query,
[address], [pool],
fragment( pool.is_active == true and
""" pool.is_deleted == false and
(?->>'is_active')::boolean = true and pool.is_validator == true
(?->>'deleted')::boolean is not true and
(?->>'is_validator')::boolean = true
""",
address.metadata,
address.metadata,
address.metadata
)
) )
end end
defp staking_pool_filter(query, :active) do defp staking_pool_filter(query, :active) do
where( where(
query, query,
[address], [pool],
fragment( pool.is_active == true and
""" pool.is_deleted == false
(?->>'is_active')::boolean = true and
(?->>'deleted')::boolean is not true
""",
address.metadata,
address.metadata
)
) )
end end
defp staking_pool_filter(query, :inactive) do defp staking_pool_filter(query, :inactive) do
where( where(
query, query,
[address], [pool],
fragment( pool.is_active == false and
""" pool.is_deleted == false
(?->>'is_active')::boolean = false and
(?->>'deleted')::boolean is not true
""",
address.metadata,
address.metadata
)
) )
end end

@ -3951,54 +3951,54 @@ defmodule Explorer.ChainTest do
describe "staking_pools/3" do describe "staking_pools/3" do
test "validators staking pools" do test "validators staking pools" do
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: true}) inserted_validator = insert(:staking_pool, is_active: true, is_validator: true)
insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: false}) insert(:staking_pool, is_active: true, is_validator: false)
options = %PagingOptions{page_size: 20, page_number: 1} options = %PagingOptions{page_size: 20, page_number: 1}
assert [gotten_validator] = Chain.staking_pools(:validator, options) assert [gotten_validator] = Chain.staking_pools(:validator, options)
assert inserted_validator.address_hash == gotten_validator.address_hash assert inserted_validator.staking_address_hash == gotten_validator.staking_address_hash
end end
test "active staking pools" do test "active staking pools" do
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true}) inserted_pool = insert(:staking_pool, is_active: true)
insert(:address_name, primary: true, metadata: %{is_active: false}) insert(:staking_pool, is_active: false)
options = %PagingOptions{page_size: 20, page_number: 1} options = %PagingOptions{page_size: 20, page_number: 1}
assert [gotten_validator] = Chain.staking_pools(:active, options) assert [gotten_pool] = Chain.staking_pools(:active, options)
assert inserted_validator.address_hash == gotten_validator.address_hash assert inserted_pool.staking_address_hash == gotten_pool.staking_address_hash
end end
test "inactive staking pools" do test "inactive staking pools" do
insert(:address_name, primary: true, metadata: %{is_active: true}) insert(:staking_pool, is_active: true)
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: false}) inserted_pool = insert(:staking_pool, is_active: false)
options = %PagingOptions{page_size: 20, page_number: 1} options = %PagingOptions{page_size: 20, page_number: 1}
assert [gotten_validator] = Chain.staking_pools(:inactive, options) assert [gotten_pool] = Chain.staking_pools(:inactive, options)
assert inserted_validator.address_hash == gotten_validator.address_hash assert inserted_pool.staking_address_hash == gotten_pool.staking_address_hash
end end
end end
describe "staking_pools_count/1" do describe "staking_pools_count/1" do
test "validators staking pools" do test "validators staking pools" do
insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: true}) insert(:staking_pool, is_active: true, is_validator: true)
insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: false}) insert(:staking_pool, is_active: true, is_validator: false)
assert Chain.staking_pools_count(:validator) == 1 assert Chain.staking_pools_count(:validator) == 1
end end
test "active staking pools" do test "active staking pools" do
insert(:address_name, primary: true, metadata: %{is_active: true}) insert(:staking_pool, is_active: true)
insert(:address_name, primary: true, metadata: %{is_active: false}) insert(:staking_pool, is_active: false)
assert Chain.staking_pools_count(:active) == 1 assert Chain.staking_pools_count(:active) == 1
end end
test "inactive staking pools" do test "inactive staking pools" do
insert(:address_name, primary: true, metadata: %{is_active: true}) insert(:staking_pool, is_active: true)
insert(:address_name, primary: true, metadata: %{is_active: false}) insert(:staking_pool, is_active: false)
assert Chain.staking_pools_count(:inactive) == 1 assert Chain.staking_pools_count(:inactive) == 1
end end

Loading…
Cancel
Save