Add address struct as in API v2 to account API

pull/6544/head
Никита Поздняков 2 years ago
parent 779f89fa5e
commit 5b30b3ce99
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 1898
      apps/block_scout_web/API blueprint.md
  2. 15
      apps/block_scout_web/lib/block_scout_web/controllers/account/api/v1/user_controller.ex
  3. 32
      apps/block_scout_web/lib/block_scout_web/views/account/api/v1/user_view.ex
  4. 33
      apps/block_scout_web/test/block_scout_web/controllers/account/api/v1/user_controller_test.exs

File diff suppressed because it is too large Load Diff

@ -30,7 +30,7 @@ defmodule BlockScoutWeb.Account.Api.V1.UserController do
{:identity, [%Identity{} = identity]} <- {:identity, UserFromAuth.find_identity(uid)},
{:watchlist, %{watchlists: [watchlist | _]}} <-
{:watchlist, Repo.account_repo().preload(identity, :watchlists)},
watchlist_with_addresses <- preload_watchlist_address_fetched_coin_balance(watchlist) do
watchlist_with_addresses <- preload_watchlist_addresses(watchlist) do
conn
|> put_status(200)
|> render(:watchlist_addresses, %{
@ -98,12 +98,11 @@ defmodule BlockScoutWeb.Account.Api.V1.UserController do
{:watchlist, %{watchlists: [watchlist | _]}} <-
{:watchlist, Repo.account_repo().preload(identity, :watchlists)},
{:ok, watchlist_address} <-
WatchlistAddress.create(Map.put(watchlist_params, :watchlist_id, watchlist.id)),
watchlist_address_preloaded <- WatchlistAddress.preload_address_fetched_coin_balance(watchlist_address) do
WatchlistAddress.create(Map.put(watchlist_params, :watchlist_id, watchlist.id)) do
conn
|> put_status(200)
|> render(:watchlist_address, %{
watchlist_address: watchlist_address_preloaded,
watchlist_address: watchlist_address,
exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null()
})
end
@ -156,12 +155,11 @@ defmodule BlockScoutWeb.Account.Api.V1.UserController do
{:watchlist, %{watchlists: [watchlist | _]}} <-
{:watchlist, Repo.account_repo().preload(identity, :watchlists)},
{:ok, watchlist_address} <-
WatchlistAddress.update(Map.put(watchlist_params, :watchlist_id, watchlist.id)),
watchlist_address_preloaded <- WatchlistAddress.preload_address_fetched_coin_balance(watchlist_address) do
WatchlistAddress.update(Map.put(watchlist_params, :watchlist_id, watchlist.id)) do
conn
|> put_status(200)
|> render(:watchlist_address, %{
watchlist_address: watchlist_address_preloaded,
watchlist_address: watchlist_address,
exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null()
})
end
@ -468,9 +466,8 @@ defmodule BlockScoutWeb.Account.Api.V1.UserController do
Map.reject(map, fn {_k, v} -> is_nil(v) end)
end
defp preload_watchlist_address_fetched_coin_balance(watchlist) do
defp preload_watchlist_addresses(watchlist) do
watchlist
|> Repo.account_repo().preload(watchlist_addresses: from(wa in WatchlistAddress, order_by: [desc: wa.id]))
|> WatchlistAddress.preload_address_fetched_coin_balance()
end
end

@ -1,6 +1,8 @@
defmodule BlockScoutWeb.Account.Api.V1.UserView do
alias BlockScoutWeb.Account.Api.V1.AccountView
alias Ecto.Changeset
alias Explorer.Chain
alias BlockScoutWeb.API.V2.Helper
def render("message.json", assigns) do
AccountView.render("message.json", assigns)
@ -70,11 +72,14 @@ defmodule BlockScoutWeb.Account.Api.V1.UserView do
end
def prepare_watchlist_address(watchlist, exchange_rate) do
address = get_address(watchlist.address_hash)
%{
"id" => watchlist.id,
"address" => Helper.address_with_info(nil, address, watchlist.address_hash),
"address_hash" => watchlist.address_hash,
"name" => watchlist.name,
"address_balance" => if(watchlist.fetched_coin_balance, do: watchlist.fetched_coin_balance.value),
"address_balance" => if(address && address.fetched_coin_balance, do: address.fetched_coin_balance.value),
"exchange_rate" => exchange_rate.usd_value,
"notification_settings" => %{
"native" => %{
@ -102,9 +107,12 @@ defmodule BlockScoutWeb.Account.Api.V1.UserView do
end
def prepare_custom_abi(custom_abi) do
address = get_address(custom_abi.address_hash)
%{
"id" => custom_abi.id,
"contract_address_hash" => custom_abi.address_hash,
"contract_address" => Helper.address_with_info(nil, address, custom_abi.address_hash),
"name" => custom_abi.name,
"abi" => custom_abi.abi
}
@ -115,7 +123,14 @@ defmodule BlockScoutWeb.Account.Api.V1.UserView do
end
def prepare_address_tag(address_tag) do
%{"id" => address_tag.id, "address_hash" => address_tag.address_hash, "name" => address_tag.name}
address = get_address(address_tag.address_hash)
%{
"id" => address_tag.id,
"address_hash" => address_tag.address_hash,
"address" => Helper.address_with_info(nil, address, address_tag.address_hash),
"name" => address_tag.name
}
end
def prepare_transaction_tag(nil), do: nil
@ -125,6 +140,11 @@ defmodule BlockScoutWeb.Account.Api.V1.UserView do
end
def prepare_public_tags_request(public_tags_request) do
addresses =
Enum.map(public_tags_request.addresses, fn address_hash ->
Helper.address_with_info(nil, get_address(address_hash), address_hash)
end)
%{
"id" => public_tags_request.id,
"full_name" => public_tags_request.full_name,
@ -133,9 +153,17 @@ defmodule BlockScoutWeb.Account.Api.V1.UserView do
"website" => public_tags_request.website,
"tags" => public_tags_request.tags,
"addresses" => public_tags_request.addresses,
"addresses_with_info" => addresses,
"additional_comment" => public_tags_request.additional_comment,
"is_owner" => public_tags_request.is_owner,
"submission_date" => public_tags_request.inserted_at
}
end
defp get_address(address_hash) do
case Chain.hash_to_address(address_hash, [necessity_by_association: %{:smart_contract => :optional}], false) do
{:ok, address} -> address
_ -> nil
end
end
end

@ -1,6 +1,7 @@
defmodule BlockScoutWeb.Account.Api.V1.UserControllerTest do
use BlockScoutWeb.ConnCase
alias Explorer.Chain.Address
alias BlockScoutWeb.Models.UserFromAuth
setup %{conn: conn} do
@ -92,7 +93,21 @@ defmodule BlockScoutWeb.Account.Api.V1.UserControllerTest do
|> json_response(200))["id"]
{addr, %{"display_name" => name, "label" => name, "address_hash" => addr},
%{"address_hash" => addr, "id" => id, "name" => name}}
%{
"address_hash" => addr,
"id" => id,
"name" => name,
"address" => %{
"hash" => Address.checksum(addr),
"implementation_name" => nil,
"is_contract" => false,
"is_verified" => false,
"name" => nil,
"private_tags" => [],
"public_tags" => [],
"watchlist_names" => []
}
}}
end)
assert Enum.all?(created, fn {addr, map_tag, _} ->
@ -129,7 +144,21 @@ defmodule BlockScoutWeb.Account.Api.V1.UserControllerTest do
|> json_response(200))["id"]
{addr, %{"display_name" => name, "label" => name, "address_hash" => addr},
%{"address_hash" => addr, "id" => id, "name" => name}}
%{
"address_hash" => addr,
"id" => id,
"name" => name,
"address" => %{
"hash" => Address.checksum(addr),
"implementation_name" => nil,
"is_contract" => false,
"is_verified" => false,
"name" => nil,
"private_tags" => [],
"public_tags" => [],
"watchlist_names" => []
}
}}
end)
assert Enum.all?(created, fn {addr, map_tag, _} ->

Loading…
Cancel
Save