diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex index e6991bf1af..a9845a2dc2 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/block_controller.ex @@ -23,4 +23,10 @@ defmodule BlockScoutWeb.API.RPC.BlockController do render(conn, :error, error: "Block does not exist") end end + + def eth_block_number(conn, _params) do + max_block_number = Chain.fetch_max_block_number() + + render(conn, :eth_block_number, number: max_block_number) + end end diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex index 941a064804..1f1374adec 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/rpc/block_view.ex @@ -22,6 +22,14 @@ defmodule BlockScoutWeb.API.RPC.BlockView do RPCView.render("show.json", data: data) end + def render("eth_block_number.json", %{number: number}) do + data = %{ + "blockNumber" => to_string(number) + } + + RPCView.render("show.json", data: data) + end + def render("error.json", %{error: error}) do RPCView.render("error.json", error: error) end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 713d449d72..1f96200ae3 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1059,6 +1059,22 @@ defmodule Explorer.Chain do end end + @spec fetch_max_block_number() :: non_neg_integer + def fetch_max_block_number do + query = + from(block in Block, + select: max(block.number), + where: block.consensus == true + ) + + result = Repo.one!(query) + + case result do + nil -> 0 + _ -> result + end + end + @spec fetch_count_consensus_block() :: non_neg_integer def fetch_count_consensus_block do query =