Add verified contracts controller

pull/6324/head
sl1depengwyn 2 years ago
parent ecf3c98967
commit 25e4a0e7f7
  1. 72
      apps/block_scout_web/lib/block_scout_web/controllers/verified_contracts_controller.ex
  2. 4
      apps/block_scout_web/lib/block_scout_web/web_router.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

@ -121,6 +121,8 @@ defmodule BlockScoutWeb.WebRouter do
resources("/recent-transactions", RecentTransactionsController, only: [:index]) resources("/recent-transactions", RecentTransactionsController, only: [:index])
resources("/verified-contracts", VerifiedContractsController, only: [:index])
get("/txs", TransactionController, :index) get("/txs", TransactionController, :index)
resources "/tx", TransactionController, only: [:show] do resources "/tx", TransactionController, only: [:show] do
@ -474,6 +476,8 @@ defmodule BlockScoutWeb.WebRouter do
get("/search-results", SearchController, :search_results) get("/search-results", SearchController, :search_results)
get("/search-verified-contracts", VerifiedContractsController, :search_verified_contracts)
get("/csv-export", CsvExportController, :index) get("/csv-export", CsvExportController, :index)
get("/transactions-csv", AddressTransactionController, :transactions_csv) get("/transactions-csv", AddressTransactionController, :transactions_csv)

Loading…
Cancel
Save