Add /tokens endpoint

pull/6677/head
Никита Поздняков 2 years ago
parent d5c4cc643d
commit ae57b7a16f
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. 27
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/token_controller.ex
  3. 4
      apps/block_scout_web/lib/block_scout_web/views/api/v2/token_view.ex
  4. 39
      apps/block_scout_web/test/block_scout_web/controllers/api/v2/token_controller_test.exs

@ -134,6 +134,7 @@ defmodule BlockScoutWeb.ApiRouter do
end
scope "/tokens" do
get("/", V2.TokenController, :tokens_list)
get("/:address_hash", V2.TokenController, :token)
get("/:address_hash/counters", V2.TokenController, :counters)
get("/:address_hash/transfers", V2.TokenController, :transfers)

@ -3,7 +3,7 @@ defmodule BlockScoutWeb.API.V2.TokenController do
alias BlockScoutWeb.AccessHelpers
alias BlockScoutWeb.API.V2.TransactionView
alias Explorer.Chain
alias Explorer.{Chain, Market}
import BlockScoutWeb.Chain, only: [split_list_by_page: 1, paging_options: 1, next_page_params: 3]
import BlockScoutWeb.PagingHelper, only: [delete_parameters_from_next_page_params: 1]
@ -16,7 +16,7 @@ defmodule BlockScoutWeb.API.V2.TokenController do
{:not_found, {:ok, token}} <- {:not_found, Chain.token_from_address_hash(address_hash)} do
conn
|> put_status(200)
|> render(:token, %{token: token})
|> render(:token, %{token: Market.add_price(token)})
end
end
@ -65,4 +65,27 @@ defmodule BlockScoutWeb.API.V2.TokenController do
|> render(:token_balances, %{token_balances: token_balances, next_page_params: next_page_params, token: token})
end
end
def tokens_list(conn, params) do
filter =
if Map.has_key?(params, "filter") do
Map.get(params, "filter")
else
nil
end
paging_params =
params
|> paging_options()
tokens = filter |> Chain.list_top_tokens(paging_params) |> Market.add_price()
{tokens, next_page} = split_list_by_page(tokens)
next_page_params = next_page |> next_page_params(tokens, params) |> delete_parameters_from_next_page_params()
conn
|> put_status(200)
|> render(:tokens, %{tokens: tokens, next_page_params: next_page_params})
end
end

@ -27,6 +27,10 @@ defmodule BlockScoutWeb.API.V2.TokenView do
}
end
def render("tokens.json", %{tokens: tokens, next_page_params: next_page_params}) do
%{"items" => Enum.map(tokens, &render("token.json", %{token: &1})), "next_page_params" => next_page_params}
end
def exchange_rate(%{usd_value: usd_value}) when not is_nil(usd_value), do: to_string(usd_value)
def exchange_rate(_), do: nil

@ -191,13 +191,50 @@ defmodule BlockScoutWeb.API.V2.TokenControllerTest do
end
end
describe "/tokens" do
test "get empty list", %{conn: conn} do
request = get(conn, "/api/v2/tokens")
assert %{"items" => [], "next_page_params" => nil} = json_response(request, 200)
end
test "check pagination", %{conn: conn} do
tokens =
for i <- 0..50 do
insert(:token, holder_count: i)
end
request = get(conn, "/api/v2/tokens")
assert response = json_response(request, 200)
request_2nd_page = get(conn, "/api/v2/tokens", response["next_page_params"])
assert response_2nd_page = json_response(request_2nd_page, 200)
check_paginated_response(response, response_2nd_page, tokens)
end
test "check nil", %{conn: conn} do
token = insert(:token)
request = get(conn, "/api/v2/tokens")
assert %{"items" => [token_json], "next_page_params" => nil} = json_response(request, 200)
compare_item(token, token_json)
end
end
def compare_item(%Token{} = token, json) do
assert Address.checksum(token.contract_address.hash) == json["address"]
assert token.symbol == json["symbol"]
assert token.name == json["name"]
assert to_string(token.decimals) == json["decimals"]
assert token.type == json["type"]
assert token.holder_count == json["holders"]
assert (is_nil(token.holder_count) and is_nil(json["holders"])) or
(to_string(token.holder_count) == json["holders"] and !is_nil(token.holder_count))
assert to_string(token.total_supply) == json["total_supply"]
assert Map.has_key?(json, "exchange_rate")
end

Loading…
Cancel
Save