|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
defmodule BlockScoutWeb.Account.Api.V1.UserControllerTest do |
|
|
|
|
use BlockScoutWeb.ConnCase |
|
|
|
|
|
|
|
|
|
alias Explorer.Repo |
|
|
|
|
alias Explorer.Chain.Address |
|
|
|
|
alias BlockScoutWeb.Models.UserFromAuth |
|
|
|
|
|
|
|
|
@ -573,6 +574,111 @@ defmodule BlockScoutWeb.Account.Api.V1.UserControllerTest do |
|
|
|
|
|> json_response(422) == %{"errors" => %{"watchlist_id" => ["Address already added to the watch list"]}} |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
test "watchlist address returns with token balances info", %{conn: conn} do |
|
|
|
|
watchlist_address_map = build(:watchlist_address) |
|
|
|
|
|
|
|
|
|
conn |
|
|
|
|
|> post( |
|
|
|
|
"/api/account/v1/user/watchlist", |
|
|
|
|
watchlist_address_map |
|
|
|
|
) |
|
|
|
|
|> json_response(200) |
|
|
|
|
|
|
|
|
|
watchlist_address_map_1 = build(:watchlist_address) |
|
|
|
|
|
|
|
|
|
conn |
|
|
|
|
|> post( |
|
|
|
|
"/api/account/v1/user/watchlist", |
|
|
|
|
watchlist_address_map_1 |
|
|
|
|
) |
|
|
|
|
|> json_response(200) |
|
|
|
|
|
|
|
|
|
values = |
|
|
|
|
for _i <- 0..149 do |
|
|
|
|
ctb = |
|
|
|
|
insert(:address_current_token_balance_with_token_id, |
|
|
|
|
address: Repo.get_by(Address, hash: watchlist_address_map["address_hash"]) |
|
|
|
|
) |
|
|
|
|
|> Repo.preload([:token]) |
|
|
|
|
|
|
|
|
|
Decimal.div( |
|
|
|
|
Decimal.mult(ctb.value, ctb.token.fiat_value), |
|
|
|
|
Decimal.new(10 ** Decimal.to_integer(ctb.token.decimals)) |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
values_1 = |
|
|
|
|
for _i <- 0..200 do |
|
|
|
|
ctb = |
|
|
|
|
insert(:address_current_token_balance_with_token_id, |
|
|
|
|
address: Repo.get_by(Address, hash: watchlist_address_map_1["address_hash"]) |
|
|
|
|
) |
|
|
|
|
|> Repo.preload([:token]) |
|
|
|
|
|
|
|
|
|
Decimal.div( |
|
|
|
|
Decimal.mult(ctb.value, ctb.token.fiat_value), |
|
|
|
|
Decimal.new(10 ** Decimal.to_integer(ctb.token.decimals)) |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|> Enum.sort(fn x1, x2 -> Decimal.compare(x1, x2) in [:gt, :eq] end) |
|
|
|
|
|> Enum.take(150) |
|
|
|
|
|
|
|
|
|
[wa2, wa1] = conn |> get("/api/account/v1/user/watchlist") |> json_response(200) |
|
|
|
|
|
|
|
|
|
assert wa1["tokens_fiat_value"] |> Decimal.new() |> Decimal.round(14) == |
|
|
|
|
values |> Enum.reduce(Decimal.new(0), fn x, acc -> Decimal.add(x, acc) end) |> Decimal.round(14) |
|
|
|
|
|
|
|
|
|
assert wa1["tokens_count"] == 150 |
|
|
|
|
assert wa1["tokens_overflow"] == false |
|
|
|
|
|
|
|
|
|
assert wa2["tokens_fiat_value"] |> Decimal.new() |> Decimal.round(14) == |
|
|
|
|
values_1 |> Enum.reduce(Decimal.new(0), fn x, acc -> Decimal.add(x, acc) end) |> Decimal.round(14) |
|
|
|
|
|
|
|
|
|
assert wa2["tokens_count"] == 150 |
|
|
|
|
assert wa2["tokens_overflow"] == true |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
test "watchlist address returns with token balances info + handle nil fiat values", %{conn: conn} do |
|
|
|
|
watchlist_address_map = build(:watchlist_address) |
|
|
|
|
|
|
|
|
|
conn |
|
|
|
|
|> post( |
|
|
|
|
"/api/account/v1/user/watchlist", |
|
|
|
|
watchlist_address_map |
|
|
|
|
) |
|
|
|
|
|> json_response(200) |
|
|
|
|
|
|
|
|
|
values = |
|
|
|
|
for _i <- 0..148 do |
|
|
|
|
ctb = |
|
|
|
|
insert(:address_current_token_balance_with_token_id, |
|
|
|
|
address: Repo.get_by(Address, hash: watchlist_address_map["address_hash"]) |
|
|
|
|
) |
|
|
|
|
|> Repo.preload([:token]) |
|
|
|
|
|
|
|
|
|
Decimal.div( |
|
|
|
|
Decimal.mult(ctb.value, ctb.token.fiat_value), |
|
|
|
|
Decimal.new(10 ** Decimal.to_integer(ctb.token.decimals)) |
|
|
|
|
) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
token = insert(:token, fiat_value: nil) |
|
|
|
|
|
|
|
|
|
insert(:address_current_token_balance_with_token_id, |
|
|
|
|
address: Repo.get_by(Address, hash: watchlist_address_map["address_hash"]), |
|
|
|
|
token: token, |
|
|
|
|
token_contract_address_hash: token.contract_address_hash |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
[wa1] = conn |> get("/api/account/v1/user/watchlist") |> json_response(200) |
|
|
|
|
|
|
|
|
|
assert wa1["tokens_fiat_value"] |> Decimal.new() |> Decimal.round(14) == |
|
|
|
|
values |> Enum.reduce(Decimal.new(0), fn x, acc -> Decimal.add(x, acc) end) |> Decimal.round(14) |
|
|
|
|
|
|
|
|
|
assert wa1["tokens_count"] == 150 |
|
|
|
|
assert wa1["tokens_overflow"] == false |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
test "post api key", %{conn: conn} do |
|
|
|
|
post_api_key_response = |
|
|
|
|
conn |
|
|
|
|