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

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

Loading…
Cancel
Save