Add API v2 response for zkEVM batch page (partially)

pull/7584/head
POA 2 years ago
parent ec5240edd4
commit dbc3921a97
  1. 4
      apps/block_scout_web/lib/block_scout_web/api_router.ex
  2. 26
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/zkevm_controller.ex
  3. 22
      apps/block_scout_web/lib/block_scout_web/views/api/v2/zkevm_view.ex
  4. 15
      apps/explorer/lib/explorer/chain.ex

@ -209,6 +209,10 @@ defmodule BlockScoutWeb.ApiRouter do
get("/", V2.WithdrawalController, :withdrawals_list)
get("/counters", V2.WithdrawalController, :withdrawals_counters)
end
scope "/zkevm" do
get("/batches/:batch_number", V2.ZkevmController, :batch)
end
end
scope "/v1", as: :api_v1 do

@ -0,0 +1,26 @@
defmodule BlockScoutWeb.API.V2.ZkevmController do
use BlockScoutWeb, :controller
alias Explorer.Chain
action_fallback(BlockScoutWeb.API.V2.FallbackController)
@batch_necessity_by_association %{
:sequence_transaction => :optional,
:verify_transaction => :optional,
:l2_transactions => :required
}
def batch(conn, %{"batch_number" => batch_number} = _params) do
{:ok, batch} =
Chain.zkevm_batch(
batch_number,
necessity_by_association: @batch_necessity_by_association,
api?: true
)
conn
|> put_status(200)
|> render(:zkevm_batch, %{batch: batch})
end
end

@ -0,0 +1,22 @@
defmodule BlockScoutWeb.API.V2.ZkevmView do
use BlockScoutWeb, :view
def render("zkevm_batch.json", %{batch: batch}) do
%{
"number" => batch.number,
"status" => batch_status(batch),
"timestamp" => batch.timestamp
}
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

@ -64,7 +64,8 @@ defmodule Explorer.Chain do
TokenTransfer,
Transaction,
Wei,
Withdrawal
Withdrawal,
ZkevmTxnBatch
}
alias Explorer.Chain.Block.{EmissionReward, Reward}
@ -6389,6 +6390,18 @@ defmodule Explorer.Chain do
)
}
)
def zkevm_batch(number, options \\ []) when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
ZkevmTxnBatch
|> where(number: ^number)
|> join_associations(necessity_by_association)
|> select_repo(options).one()
|> case do
nil -> {:error, :not_found}
batch -> {:ok, batch}
end
end
@spec verified_contracts_top(non_neg_integer()) :: [Hash.Address.t()]

Loading…
Cancel
Save