From 8645e02e3c9ce62f55dde418679f355fde8f4934 Mon Sep 17 00:00:00 2001 From: saneery Date: Tue, 14 May 2019 13:47:19 +0300 Subject: [PATCH] use paging options struct in staking pools functions --- apps/explorer/lib/explorer/chain.ex | 8 +++++--- apps/explorer/lib/explorer/paging_options.ex | 5 +++-- apps/explorer/test/explorer/chain_test.exs | 12 +++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 33992a41d1..fe1c6b1b5f 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2833,11 +2833,13 @@ defmodule Explorer.Chain do end @doc "Get staking pools from the DB" - @spec staking_pools(filter :: :validator | :active | :inactive, lim :: integer, off :: integer) :: [map()] - def staking_pools(filter, lim, off) when is_integer(lim) and is_integer(off) do + @spec staking_pools(filter :: :validator | :active | :inactive, options :: PagingOptions.t()) :: [map()] + 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 |> staking_pool_filter(filter) - |> limit(^lim) + |> limit(^page_size) |> offset(^off) |> Repo.all() end diff --git a/apps/explorer/lib/explorer/paging_options.ex b/apps/explorer/lib/explorer/paging_options.ex index 1bac2dc71a..0828a0afad 100644 --- a/apps/explorer/lib/explorer/paging_options.ex +++ b/apps/explorer/lib/explorer/paging_options.ex @@ -4,10 +4,11 @@ defmodule Explorer.PagingOptions do 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 page_size :: non_neg_integer() + @typep page_number :: pos_integer() - defstruct [:key, :page_size] + defstruct [:key, :page_size, page_number: 1] end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index ec4fe63196..24157c3938 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -3909,7 +3909,9 @@ defmodule Explorer.ChainTest 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}) - 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 end @@ -3917,7 +3919,9 @@ defmodule Explorer.ChainTest do inserted_validator = insert(:address_name, primary: true, metadata: %{is_active: true}) 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 end @@ -3925,7 +3929,9 @@ defmodule Explorer.ChainTest do insert(:address_name, primary: true, metadata: %{is_active: true}) 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 end end