From 25e4a0e7f744561f289be52a840dfa8a294dab9d Mon Sep 17 00:00:00 2001 From: sl1depengwyn Date: Mon, 17 Oct 2022 23:25:20 +0300 Subject: [PATCH] Add verified contracts controller --- .../verified_contracts_controller.ex | 72 +++++++++++++++++++ .../lib/block_scout_web/web_router.ex | 4 ++ 2 files changed, 76 insertions(+) create mode 100644 apps/block_scout_web/lib/block_scout_web/controllers/verified_contracts_controller.ex diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/verified_contracts_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/verified_contracts_controller.ex new file mode 100644 index 0000000000..a8b502a5a8 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/controllers/verified_contracts_controller.ex @@ -0,0 +1,72 @@ +defmodule BlockScoutWeb.VerifiedContractsController do + use BlockScoutWeb, :controller + + import BlockScoutWeb.Chain, + only: [paging_options: 1, next_page_params: 3, split_list_by_page: 1, fetch_page_number: 1] + + + alias BlockScoutWeb.{Controller, VerifiedContractsView} + alias Explorer.{Chain, Market} + alias Explorer.ExchangeRates.Token + alias Phoenix.View + + @necessity_by_association %{[address: :token] => :optional} + + def index(conn, %{"type" => "JSON"} = params) do + full_options = + [necessity_by_association: @necessity_by_association] + |> Keyword.merge(paging_options(params)) + |> Keyword.merge(current_filter(params)) + |> Keyword.merge(search_query(params)) + |> IO.inspect(label: "options") + + verified_contracts_plus_one = Chain.verified_contracts(full_options) + {verified_contracts, next_page} = split_list_by_page(verified_contracts_plus_one) + + items = + for contract <- verified_contracts do + token = + if contract.address.token, + do: Market.get_exchange_rate(contract.address.token.symbol), + else: Token.null() + + View.render_to_string(VerifiedContractsView, "_contract.html", + contract: contract, + token: token + ) + end + + next_page_path = + case next_page_params(next_page, verified_contracts, params) do + nil -> nil + next_page_params -> verified_contracts_path(conn, :index, Map.delete(next_page_params, "type")) + end + + json(conn, %{items: items, next_page_path: next_page_path}) + end + + def index(conn, params) do + render(conn, "index.html", + current_path: Controller.current_full_path(conn), + filter: params["filter"] + ) + end + + defp current_filter(%{"filter" => "solidity"}) do + [filter: :solidity] + end + + defp current_filter(%{"filter" => "vyper"}) do + [filter: :vyper] + end + + defp current_filter(_), do: [] + + defp search_query(%{"search" => ""}), do: [] + + defp search_query(%{"search" => search_string}) do + [search: search_string] + end + + defp search_query(_), do: [] +end diff --git a/apps/block_scout_web/lib/block_scout_web/web_router.ex b/apps/block_scout_web/lib/block_scout_web/web_router.ex index 30de6760f1..e03bd10d43 100644 --- a/apps/block_scout_web/lib/block_scout_web/web_router.ex +++ b/apps/block_scout_web/lib/block_scout_web/web_router.ex @@ -121,6 +121,8 @@ defmodule BlockScoutWeb.WebRouter do resources("/recent-transactions", RecentTransactionsController, only: [:index]) + resources("/verified-contracts", VerifiedContractsController, only: [:index]) + get("/txs", TransactionController, :index) resources "/tx", TransactionController, only: [:show] do @@ -474,6 +476,8 @@ defmodule BlockScoutWeb.WebRouter do get("/search-results", SearchController, :search_results) + get("/search-verified-contracts", VerifiedContractsController, :search_verified_contracts) + get("/csv-export", CsvExportController, :index) get("/transactions-csv", AddressTransactionController, :transactions_csv)