forked from blockscout/blockscout
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/production-zksync'
- Loading branch information
Showing
57 changed files
with
4,000 additions
and
513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
apps/block_scout_web/lib/block_scout_web/controllers/api/v2/zksync_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
defmodule BlockScoutWeb.API.V2.ZkSyncController do | ||
use BlockScoutWeb, :controller | ||
|
||
import BlockScoutWeb.Chain, | ||
only: [ | ||
next_page_params: 4, | ||
paging_options: 1, | ||
split_list_by_page: 1 | ||
] | ||
|
||
alias Explorer.Chain.ZkSync.{Reader, TransactionBatch} | ||
|
||
action_fallback(BlockScoutWeb.API.V2.FallbackController) | ||
|
||
@batch_necessity_by_association %{ | ||
:commit_transaction => :optional, | ||
:prove_transaction => :optional, | ||
:execute_transaction => :optional, | ||
:l2_transactions => :optional | ||
} | ||
|
||
@batches_necessity_by_association %{ | ||
:commit_transaction => :optional, | ||
:prove_transaction => :optional, | ||
:execute_transaction => :optional | ||
} | ||
|
||
@doc """ | ||
Function to handle GET requests to `/api/v2/zksync/batches/:batch_number` endpoint. | ||
""" | ||
@spec batch(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def batch(conn, %{"batch_number" => batch_number} = _params) do | ||
case Reader.batch( | ||
batch_number, | ||
necessity_by_association: @batch_necessity_by_association, | ||
api?: true | ||
) do | ||
{:ok, batch} -> | ||
conn | ||
|> put_status(200) | ||
|> render(:zksync_batch, %{batch: batch}) | ||
|
||
{:error, :not_found} = res -> | ||
res | ||
end | ||
end | ||
|
||
@doc """ | ||
Function to handle GET requests to `/api/v2/zksync/batches` endpoint. | ||
""" | ||
@spec batches(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def batches(conn, params) do | ||
{batches, next_page} = | ||
params | ||
|> paging_options() | ||
|> Keyword.put(:necessity_by_association, @batches_necessity_by_association) | ||
|> Keyword.put(:api?, true) | ||
|> Reader.batches() | ||
|> split_list_by_page() | ||
|
||
next_page_params = | ||
next_page_params( | ||
next_page, | ||
batches, | ||
params, | ||
fn %TransactionBatch{number: number} -> %{"number" => number} end | ||
) | ||
|
||
conn | ||
|> put_status(200) | ||
|> render(:zksync_batches, %{ | ||
batches: batches, | ||
next_page_params: next_page_params | ||
}) | ||
end | ||
|
||
@doc """ | ||
Function to handle GET requests to `/api/v2/zksync/batches/count` endpoint. | ||
""" | ||
@spec batches_count(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def batches_count(conn, _params) do | ||
conn | ||
|> put_status(200) | ||
|> render(:zksync_batches_count, %{count: Reader.batches_count(api?: true)}) | ||
end | ||
|
||
@doc """ | ||
Function to handle GET requests to `/api/v2/main-page/zksync/batches/confirmed` endpoint. | ||
""" | ||
@spec batches_confirmed(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def batches_confirmed(conn, _params) do | ||
batches = | ||
[] | ||
|> Keyword.put(:necessity_by_association, @batches_necessity_by_association) | ||
|> Keyword.put(:api?, true) | ||
|> Keyword.put(:confirmed?, true) | ||
|> Reader.batches() | ||
|
||
conn | ||
|> put_status(200) | ||
|> render(:zksync_batches, %{batches: batches}) | ||
end | ||
|
||
@doc """ | ||
Function to handle GET requests to `/api/v2/main-page/zksync/batches/latest-number` endpoint. | ||
""" | ||
@spec batch_latest_number(Plug.Conn.t(), map()) :: Plug.Conn.t() | ||
def batch_latest_number(conn, _params) do | ||
conn | ||
|> put_status(200) | ||
|> render(:zksync_batch_latest_number, %{number: batch_latest_number()}) | ||
end | ||
|
||
defp batch_latest_number do | ||
case Reader.batch(:latest, api?: true) do | ||
{:ok, batch} -> batch.number | ||
{:error, :not_found} -> 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.