Add API v2 output for Latest Confirmed Batches homepage section

pull/7584/head
POA 2 years ago
parent b4e7a12c6d
commit c445cdb290
  1. 1
      apps/block_scout_web/lib/block_scout_web/api_router.ex
  2. 13
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/zkevm_controller.ex
  3. 67
      apps/block_scout_web/lib/block_scout_web/views/api/v2/zkevm_view.ex
  4. 22
      apps/explorer/lib/explorer/chain.ex

@ -185,6 +185,7 @@ defmodule BlockScoutWeb.ApiRouter do
get("/transactions", V2.MainPageController, :transactions)
get("/transactions/watchlist", V2.MainPageController, :watchlist_transactions)
get("/indexing-status", V2.MainPageController, :indexing_status)
get("/zkevm-confirmed-batches", V2.ZkevmController, :batches_confirmed)
end
scope "/stats" do

@ -62,4 +62,17 @@ defmodule BlockScoutWeb.API.V2.ZkevmController do
|> put_status(200)
|> render(:zkevm_batches_count, %{count: count})
end
def batches_confirmed(conn, _params) do
batches =
[]
|> Keyword.put(:necessity_by_association, @batches_necessity_by_association)
|> Keyword.put(:api?, true)
|> Keyword.put(:confirmed?, true)
|> Chain.zkevm_batches()
conn
|> put_status(200)
|> render(:zkevm_batches, %{batches: batches})
end
end

@ -34,42 +34,16 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
batches: batches,
next_page_params: next_page_params
}) do
items =
batches
|> Enum.map(fn batch ->
Task.async(fn ->
tx_count =
Repo.replica().aggregate(
from(
t in ZkevmBatchTxn,
where: t.batch_number == ^batch.number
),
:count,
timeout: :infinity
)
sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do
batch.sequence_transaction.hash
end
%{
"number" => batch.number,
"timestamp" => batch.timestamp,
"tx_count" => tx_count,
"sequence_tx_hash" => sequence_tx_hash
}
end)
end)
|> Task.yield_many(:infinity)
|> Enum.map(fn {_task, {:ok, item}} -> item end)
%{
items: items,
items: render_zkevm_batches(batches),
next_page_params: next_page_params
}
end
def render("zkevm_batches.json", %{batches: batches}) do
%{items: render_zkevm_batches(batches)}
end
def render("zkevm_batches_count.json", %{count: count}) do
count
end
@ -84,4 +58,35 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
!is_nil(verify_id) -> "Finalized"
end
end
defp render_zkevm_batches(batches) do
batches
|> Enum.map(fn batch ->
Task.async(fn ->
tx_count =
Repo.replica().aggregate(
from(
t in ZkevmBatchTxn,
where: t.batch_number == ^batch.number
),
:count,
timeout: :infinity
)
sequence_tx_hash =
if not is_nil(batch.sequence_transaction) do
batch.sequence_transaction.hash
end
%{
"number" => batch.number,
"timestamp" => batch.timestamp,
"tx_count" => tx_count,
"sequence_tx_hash" => sequence_tx_hash
}
end)
end)
|> Task.yield_many(:infinity)
|> Enum.map(fn {_task, {:ok, item}} -> item end)
end
end

@ -6424,7 +6424,6 @@ defmodule Explorer.Chain do
end
def zkevm_batches(options \\ []) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
base_query =
@ -6432,11 +6431,22 @@ defmodule Explorer.Chain do
order_by: [desc: tb.number]
)
base_query
|> join_associations(necessity_by_association)
|> page_zkevm_batches(paging_options)
|> limit(^paging_options.page_size)
|> select_repo(options).all()
query =
if Keyword.get(options, :confirmed?, false) do
base_query
|> join_associations(necessity_by_association)
|> where([tb], not is_nil(tb.sequence_id) and tb.sequence_id > 0)
|> limit(10)
else
paging_options = Keyword.get(options, :paging_options, @default_paging_options)
base_query
|> join_associations(necessity_by_association)
|> page_zkevm_batches(paging_options)
|> limit(^paging_options.page_size)
end
select_repo(options).all(query)
end
defp page_zkevm_batches(query, %PagingOptions{key: nil}), do: query

Loading…
Cancel
Save