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. 47
      apps/block_scout_web/lib/block_scout_web/views/api/v2/zkevm_view.ex
  4. 14
      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,7 +34,32 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
batches: batches,
next_page_params: next_page_params
}) do
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
defp batch_status(batch) do
sequence_id = Map.get(batch, :sequence_id)
verify_id = Map.get(batch, :verify_id)
cond do
is_nil(sequence_id) && is_nil(verify_id) -> "Unfinalized"
!is_nil(sequence_id) && is_nil(verify_id) -> "L1 Sequence Confirmed"
!is_nil(verify_id) -> "Finalized"
end
end
defp render_zkevm_batches(batches) do
batches
|> Enum.map(fn batch ->
Task.async(fn ->
@ -63,25 +88,5 @@ defmodule BlockScoutWeb.API.V2.ZkevmView do
end)
|> Task.yield_many(:infinity)
|> Enum.map(fn {_task, {:ok, item}} -> item end)
%{
items: items,
next_page_params: next_page_params
}
end
def render("zkevm_batches_count.json", %{count: count}) do
count
end
defp batch_status(batch) do
sequence_id = Map.get(batch, :sequence_id)
verify_id = Map.get(batch, :verify_id)
cond do
is_nil(sequence_id) && is_nil(verify_id) -> "Unfinalized"
!is_nil(sequence_id) && is_nil(verify_id) -> "L1 Sequence Confirmed"
!is_nil(verify_id) -> "Finalized"
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]
)
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)
|> select_repo(options).all()
end
select_repo(options).all(query)
end
defp page_zkevm_batches(query, %PagingOptions{key: nil}), do: query

Loading…
Cancel
Save