Add /addresses

pull/6677/head
Никита Поздняков 2 years ago
parent ae57b7a16f
commit 45add657b4
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 1
      apps/block_scout_web/lib/block_scout_web/api_router.ex
  2. 24
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/address_controller.ex
  3. 4
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/token_controller.ex
  4. 18
      apps/block_scout_web/lib/block_scout_web/views/api/v2/address_view.ex
  5. 40
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs

@ -120,6 +120,7 @@ defmodule BlockScoutWeb.ApiRouter do
end
scope "/addresses" do
get("/", V2.AddressController, :addresses_list)
get("/:address_hash", V2.AddressController, :address)
get("/:address_hash/counters", V2.AddressController, :counters)
get("/:address_hash/token-balances", V2.AddressController, :token_balances)

@ -14,6 +14,7 @@ defmodule BlockScoutWeb.API.V2.AddressController do
alias BlockScoutWeb.AccessHelpers
alias BlockScoutWeb.API.V2.{BlockView, TransactionView}
alias Explorer.ExchangeRates.Token
alias Explorer.{Chain, Market}
alias Indexer.Fetcher.TokenBalanceOnDemand
@ -296,4 +297,27 @@ defmodule BlockScoutWeb.API.V2.AddressController do
|> render(:tokens, %{tokens: tokens, next_page_params: next_page_params})
end
end
def addresses_list(conn, params) do
addresses =
params
|> paging_options()
|> Chain.list_top_addresses()
{addresses, next_page} = split_list_by_page(addresses)
next_page_params = next_page_params(next_page, addresses, params)
exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null()
total_supply = Chain.total_supply()
conn
|> put_status(200)
|> render(:addresses, %{
addresses: addresses,
next_page_params: next_page_params,
exchange_rate: exchange_rate,
total_supply: total_supply
})
end
end

@ -78,9 +78,7 @@ defmodule BlockScoutWeb.API.V2.TokenController do
params
|> paging_options()
tokens = filter |> Chain.list_top_tokens(paging_params) |> Market.add_price()
{tokens, next_page} = split_list_by_page(tokens)
{tokens, next_page} = filter |> Chain.list_top_tokens(paging_params) |> Market.add_price() |> split_list_by_page()
next_page_params = next_page |> next_page_params(tokens, params) |> delete_parameters_from_next_page_params()

@ -36,6 +36,24 @@ defmodule BlockScoutWeb.API.V2.AddressView do
%{"items" => Enum.map(tokens, &prepare_token_balance/1), "next_page_params" => next_page_params}
end
def render("addresses.json", %{
addresses: addresses,
next_page_params: next_page_params,
exchange_rate: exchange_rate,
total_supply: total_supply
}) do
%{
items: Enum.map(addresses, &prepare_address/1),
next_page_params: next_page_params,
exchange_rate: exchange_rate.usd_value,
total_supply: total_supply && to_string(total_supply)
}
end
def prepare_address({address, nonce}) do
nil |> Helper.address_with_info(address, address.hash) |> Map.put(:nonce, to_string(nonce))
end
def prepare_address(address, conn \\ nil) do
base_info = Helper.address_with_info(conn, address, address.hash)
is_proxy = AddressView.smart_contract_is_proxy?(address)

@ -1211,6 +1211,46 @@ defmodule BlockScoutWeb.API.V2.AddressControllerTest do
end
end
describe "/addresses" do
test "get empty list", %{conn: conn} do
request = get(conn, "/api/v2/addresses")
assert %{"items" => [], "next_page_params" => nil, "exchange_rate" => nil, "total_supply" => "0"} =
json_response(request, 200)
end
test "check pagination", %{conn: conn} do
addresses =
for i <- 0..50 do
insert(:address, nonce: i)
end
request = get(conn, "/api/v2/addresses")
assert response = json_response(request, 200)
request_2nd_page = get(conn, "/api/v2/addresses", response["next_page_params"])
assert response_2nd_page = json_response(request_2nd_page, 200)
check_paginated_response(response, response_2nd_page, addresses)
end
test "check nil", %{conn: conn} do
token = insert(:token)
request = get(conn, "/api/v2/addresses")
assert %{"items" => [token_json], "next_page_params" => nil} = json_response(request, 200)
compare_item(token, token_json)
end
end
defp compare_item(%Address{} = address, json) do
assert Address.checksum(address.hash) == json["hash"]
assert "0" == json["nonce"]
end
defp compare_item(%Transaction{} = transaction, json) do
assert to_string(transaction.hash) == json["hash"]
assert transaction.block_number == json["block"]

Loading…
Cancel
Save