use paging options struct in staking pools functions

pull/1916/head
saneery 6 years ago
parent 8bbdb17265
commit 8645e02e3c
  1. 8
      apps/explorer/lib/explorer/chain.ex
  2. 5
      apps/explorer/lib/explorer/paging_options.ex
  3. 12
      apps/explorer/test/explorer/chain_test.exs

@ -2833,11 +2833,13 @@ defmodule Explorer.Chain do
end end
@doc "Get staking pools from the DB" @doc "Get staking pools from the DB"
@spec staking_pools(filter :: :validator | :active | :inactive, lim :: integer, off :: integer) :: [map()] @spec staking_pools(filter :: :validator | :active | :inactive, options :: PagingOptions.t()) :: [map()]
def staking_pools(filter, lim, off) when is_integer(lim) and is_integer(off) 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 Address.Name
|> staking_pool_filter(filter) |> staking_pool_filter(filter)
|> limit(^lim) |> limit(^page_size)
|> offset(^off) |> offset(^off)
|> Repo.all() |> Repo.all()
end end

@ -4,10 +4,11 @@ defmodule Explorer.PagingOptions do
number and index. number and index.
""" """
@type t :: %__MODULE__{key: key, page_size: page_size} @type t :: %__MODULE__{key: key, page_size: page_size, page_number: page_number}
@typep key :: any() @typep key :: any()
@typep page_size :: non_neg_integer() @typep page_size :: non_neg_integer()
@typep page_number :: pos_integer()
defstruct [:key, :page_size] defstruct [:key, :page_size, page_number: 1]
end end

@ -3909,7 +3909,9 @@ defmodule Explorer.ChainTest do
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: true}) 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}) insert(:address_name, primary: true, metadata: %{is_active: true, is_validator: false})
assert [gotten_validator] = Chain.staking_pools(:validator, 20, 0) 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.address_hash == gotten_validator.address_hash
end end
@ -3917,7 +3919,9 @@ defmodule Explorer.ChainTest do
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true}) inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true})
insert(:address_name, primary: true, metadata: %{is_active: false}) insert(:address_name, primary: true, metadata: %{is_active: false})
assert [gotten_validator] = Chain.staking_pools(:active, 20, 0) 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 inserted_validator.address_hash == gotten_validator.address_hash
end end
@ -3925,7 +3929,9 @@ defmodule Explorer.ChainTest do
insert(:address_name, primary: true, metadata: %{is_active: true}) insert(:address_name, primary: true, metadata: %{is_active: true})
inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: false}) inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: false})
assert [gotten_validator] = Chain.staking_pools(:inactive, 20, 0) 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 inserted_validator.address_hash == gotten_validator.address_hash
end end
end end

Loading…
Cancel
Save