Finally fix pagination at top tokens page

pull/3224/head
Victor Baranov 4 years ago
parent 1fcb141ed5
commit a5b9621a72
  1. 16
      apps/block_scout_web/lib/block_scout_web/chain.ex
  2. 15
      apps/block_scout_web/lib/block_scout_web/controllers/tokens/tokens_controller.ex
  3. 7
      apps/explorer/lib/explorer/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

@ -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

@ -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

Loading…
Cancel
Save