Add JSON handle to address token transfer controller

pull/1116/head
William Sanches 6 years ago
parent 6783fe572f
commit e5d7fae2e3
No known key found for this signature in database
GPG Key ID: 27250E49FB133014
  1. 59
      apps/block_scout_web/lib/block_scout_web/controllers/address_token_transfer_controller.ex
  2. 108
      apps/block_scout_web/test/block_scout_web/controllers/address_token_transfer_controller_test.exs

@ -1,8 +1,10 @@
defmodule BlockScoutWeb.AddressTokenTransferController do defmodule BlockScoutWeb.AddressTokenTransferController do
use BlockScoutWeb, :controller use BlockScoutWeb, :controller
alias Explorer.{Chain, Market} alias BlockScoutWeb.TransactionView
alias Explorer.ExchangeRates.Token alias Explorer.ExchangeRates.Token
alias Explorer.{Chain, Market}
alias Phoenix.View
import BlockScoutWeb.AddressController, only: [transaction_count: 1, validation_count: 1] import BlockScoutWeb.AddressController, only: [transaction_count: 1, validation_count: 1]
@ -11,12 +13,16 @@ defmodule BlockScoutWeb.AddressTokenTransferController do
def index( def index(
conn, conn,
%{"address_id" => address_hash_string, "address_token_id" => token_hash_string} = params %{
"address_id" => address_hash_string,
"address_token_id" => token_hash_string,
"type" => "JSON"
} = params
) do ) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token_hash} <- Chain.string_to_address_hash(token_hash_string), {:ok, token_hash} <- Chain.string_to_address_hash(token_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash), {:ok, address} <- Chain.hash_to_address(address_hash),
{:ok, token} <- Chain.token_from_address_hash(token_hash) do {:ok, _} <- Chain.token_from_address_hash(token_hash) do
transactions = transactions =
Chain.address_to_transactions_with_token_transfers( Chain.address_to_transactions_with_token_transfers(
address_hash, address_hash,
@ -26,15 +32,58 @@ defmodule BlockScoutWeb.AddressTokenTransferController do
{transactions_paginated, next_page} = split_list_by_page(transactions) {transactions_paginated, next_page} = split_list_by_page(transactions)
next_page_path =
case next_page_params(next_page, transactions_paginated, params) do
nil ->
nil
next_page_params ->
address_token_transfers_path(
conn,
:index,
address_hash_string,
token_hash_string,
Map.delete(next_page_params, "type")
)
end
transfers_json =
Enum.map(transactions_paginated, fn transaction ->
View.render_to_string(
TransactionView,
"_tile.html",
conn: conn,
transaction: transaction,
current_address: address
)
end)
json(conn, %{items: transfers_json, next_page_path: next_page_path})
else
:error ->
unprocessable_entity(conn)
{:error, :not_found} ->
not_found(conn)
end
end
def index(
conn,
%{"address_id" => address_hash_string, "address_token_id" => token_hash_string}
) do
with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string),
{:ok, token_hash} <- Chain.string_to_address_hash(token_hash_string),
{:ok, address} <- Chain.hash_to_address(address_hash),
{:ok, token} <- Chain.token_from_address_hash(token_hash) do
render( render(
conn, conn,
"index.html", "index.html",
address: address, address: address,
exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(),
next_page_params: next_page_params(next_page, transactions_paginated, params), current_path: current_path(conn),
token: token, token: token,
transaction_count: transaction_count(address), transaction_count: transaction_count(address),
transactions: transactions_paginated,
validation_count: validation_count(address) validation_count: validation_count(address)
) )
else else

@ -1,7 +1,8 @@
defmodule BlockScoutWeb.AddressTokenTransferControllerTest do defmodule BlockScoutWeb.AddressTokenTransferControllerTest do
use BlockScoutWeb.ConnCase use BlockScoutWeb.ConnCase
import BlockScoutWeb.Router.Helpers, only: [address_token_transfers_path: 4] import BlockScoutWeb.Router.Helpers,
only: [address_token_transfers_path: 4, address_token_transfers_path: 5]
alias Explorer.Chain.{Address, Token} alias Explorer.Chain.{Address, Token}
@ -15,6 +16,7 @@ defmodule BlockScoutWeb.AddressTokenTransferControllerTest do
test "with invalid token hash", %{conn: conn} do test "with invalid token hash", %{conn: conn} do
address_hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b" address_hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b"
conn = get(conn, address_token_transfers_path(conn, :index, address_hash, "invalid_address")) conn = get(conn, address_token_transfers_path(conn, :index, address_hash, "invalid_address"))
assert html_response(conn, 422) assert html_response(conn, 422)
@ -35,25 +37,34 @@ defmodule BlockScoutWeb.AddressTokenTransferControllerTest do
assert html_response(conn, 404) assert html_response(conn, 404)
end end
end
describe "GET index/2 JSON" do
test "without token transfers for a token", %{conn: conn} do test "without token transfers for a token", %{conn: conn} do
%Address{hash: address_hash} = insert(:address) %Address{hash: address_hash} = insert(:address)
%Token{contract_address_hash: token_hash} = insert(:token) %Token{contract_address_hash: token_hash} = insert(:token)
conn = get(conn, address_token_transfers_path(conn, :index, address_hash, token_hash)) conn =
get(conn, address_token_transfers_path(conn, :index, address_hash, token_hash), %{
type: "JSON"
})
assert html_response(conn, 200) assert json_response(conn, 200) == %{"items" => [], "next_page_path" => nil}
assert conn.assigns.transactions == []
end end
test "returns the transactions that have token transfers for the given address and token", %{conn: conn} do test "returns correct next_page_path", %{conn: conn} do
address = insert(:address) address = insert(:address)
token = insert(:token) token = insert(:token)
page_last_transfer =
1..50
|> Enum.map(fn index ->
block = insert(:block, number: 1000 - index)
transaction = transaction =
:transaction :transaction
|> insert() |> insert()
|> with_block() |> with_block(block)
insert( insert(
:token_transfer, :token_transfer,
@ -62,21 +73,11 @@ defmodule BlockScoutWeb.AddressTokenTransferControllerTest do
token_contract_address: token.contract_address token_contract_address: token.contract_address
) )
conn = get(conn, address_token_transfers_path(conn, :index, address.hash, token.contract_address_hash)) transaction
end)
transaction_hashes = Enum.map(conn.assigns.transactions, & &1.hash) |> List.last()
assert html_response(conn, 200)
assert transaction_hashes == [transaction.hash]
end
test "returns next page of results based on last seen transactions", %{conn: conn} do
address = insert(:address)
token = insert(:token)
second_page_transactions = Enum.each(51..60, fn index ->
1..50
|> Enum.map(fn index ->
block = insert(:block, number: 1000 - index) block = insert(:block, number: 1000 - index)
transaction = transaction =
@ -90,25 +91,68 @@ defmodule BlockScoutWeb.AddressTokenTransferControllerTest do
transaction: transaction, transaction: transaction,
token_contract_address: token.contract_address token_contract_address: token.contract_address
) )
transaction
end) end)
|> Enum.map(& &1.hash)
transaction = conn =
:transaction get(
|> insert() conn,
|> with_block(insert(:block, number: 1002)) address_token_transfers_path(conn, :index, address.hash, token.contract_address_hash),
%{type: "JSON"}
)
expected_path =
address_token_transfers_path(conn, :index, address.hash, token.contract_address_hash, %{
block_number: page_last_transfer.block_number,
index: page_last_transfer.index
})
assert Map.get(json_response(conn, 200), "next_page_path") == expected_path
end
test "with invalid address hash", %{conn: conn} do
token_hash = "0xc8982771dd50285389c352c175ada74d074427c7"
conn =
get(conn, address_token_transfers_path(conn, :index, "invalid_address", token_hash), %{
type: "JSON"
})
assert html_response(conn, 422)
end
test "with invalid token hash", %{conn: conn} do
address_hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b"
conn = conn =
get(conn, address_token_transfers_path(conn, :index, address.hash, token.contract_address_hash), %{ get(conn, address_token_transfers_path(conn, :index, address_hash, "invalid_address"), %{
"block_number" => Integer.to_string(transaction.block_number), type: "JSON"
"index" => Integer.to_string(transaction.index)
}) })
actual_transactions = Enum.map(conn.assigns.transactions, & &1.hash) assert html_response(conn, 422)
end
assert second_page_transactions == actual_transactions test "with an address that doesn't exist in our database", %{conn: conn} do
address_hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b"
%Token{contract_address_hash: token_hash} = insert(:token)
conn =
get(conn, address_token_transfers_path(conn, :index, address_hash, token_hash), %{
type: "JSON"
})
assert html_response(conn, 404)
end
test "with a token that doesn't exist in our database", %{conn: conn} do
%Address{hash: address_hash} = insert(:address)
token_hash = "0x8bf38d4764929064f2d4d3a56520a76ab3df415b"
conn =
get(conn, address_token_transfers_path(conn, :index, address_hash, token_hash), %{
type: "JSON"
})
assert html_response(conn, 404)
end end
end end
end end

Loading…
Cancel
Save