From a5b9621a727458fbd8d68b2f46444d049d951b90 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Wed, 5 Aug 2020 17:34:11 +0300 Subject: [PATCH] Finally fix pagination at top tokens page --- .../block_scout_web/lib/block_scout_web/chain.ex | 16 +++++++++++++--- .../controllers/tokens/tokens_controller.ex | 15 ++------------- apps/explorer/lib/explorer/chain.ex | 7 ++++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/chain.ex b/apps/block_scout_web/lib/block_scout_web/chain.ex index 2c45e0f577..c338d446dd 100644 --- a/apps/block_scout_web/lib/block_scout_web/chain.ex +++ b/apps/block_scout_web/lib/block_scout_web/chain.ex @@ -41,7 +41,7 @@ defmodule BlockScoutWeb.Chain do end end - @page_size 50 + @page_size 10 @default_paging_options %PagingOptions{page_size: @page_size + 1} @address_hash_len 40 @tx_block_hash_len 64 @@ -108,6 +108,16 @@ defmodule BlockScoutWeb.Chain do end end + def paging_options(%{"contract_address_hash" => contract_address_hash, "holder_count" => holder_count}) do + with {holder_count, ""} <- Integer.parse(holder_count), + {:ok, contract_address_hash} <- string_to_address_hash(contract_address_hash) do + [paging_options: %{@default_paging_options | key: {holder_count, contract_address_hash}}] + else + _ -> + [paging_options: @default_paging_options] + end + end + def paging_options(%{ "block_number" => block_number_string, "transaction_index" => transaction_index_string, @@ -206,8 +216,8 @@ defmodule BlockScoutWeb.Chain do %{"hash" => hash, "fetched_coin_balance" => Decimal.to_string(fetched_coin_balance.value)} end - defp paging_params({%Token{contract_address_hash: contract_address_hash}, _}) do - %{"contract_address_hash" => contract_address_hash} + defp paging_params(%Token{contract_address_hash: contract_address_hash, holder_count: holder_count}) do + %{"contract_address_hash" => contract_address_hash, "holder_count" => holder_count} end defp paging_params({%Reward{block: %{number: number}}, _}) do diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/tokens_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/tokens_controller.ex index 383280f264..0a133d9f7f 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/tokens_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/tokens_controller.ex @@ -8,18 +8,8 @@ defmodule BlockScoutWeb.TokensController do alias Phoenix.View def index(conn, %{"type" => "JSON"} = params) do - full_options = - Keyword.merge( - [ - necessity_by_association: %{ - [contract_address: :contract_address] => :optional - } - ], - paging_options(params) - ) - tokens = - full_options + params |> paging_options() |> Chain.list_top_tokens() @@ -31,7 +21,7 @@ defmodule BlockScoutWeb.TokensController do nil next_page_params -> - address_path( + tokens_path( conn, :index, Map.delete(next_page_params, "type") @@ -64,7 +54,6 @@ defmodule BlockScoutWeb.TokensController do render(conn, "index.html", current_path: current_path(conn), - address_count: Chain.address_estimated_count(), total_supply: total_supply ) end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 0267f06819..d25cffd8a6 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1735,7 +1735,6 @@ defmodule Explorer.Chain do base_query |> page_tokens(paging_options) - |> join_associations(necessity_by_association) |> limit(^paging_options.page_size) |> Repo.all() end @@ -3165,9 +3164,11 @@ defmodule Explorer.Chain do defp page_tokens(query, %PagingOptions{key: nil}), do: query - defp page_tokens(query, %PagingOptions{key: {contract_address_hash}}) do + defp page_tokens(query, %PagingOptions{key: {holder_count, contract_address_hash}}) do from(token in query, - where: token.contract_address_hash > ^contract_address_hash + where: + (token.holder_count == ^holder_count and token.contract_address_hash > ^contract_address_hash) or + token.holder_count < ^holder_count ) end