From 97dc5adbadedb467813cf1af442c281d4b12fa0b Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Fri, 30 Aug 2019 11:56:25 +0300 Subject: [PATCH] redirect to token instance transfers page --- .../tokens/instance/transfer_controller.ex | 6 ++- .../controllers/tokens/instance_controller.ex | 8 ++-- .../tokens/inventory_controller.ex | 4 +- .../tokens/instance/transfer/index.html.eex | 38 +++++++++++++++++++ .../tokens/inventory/_token.html.eex | 2 +- .../views/tokens/instance/overview_view.ex | 21 ++++++++++ .../views/tokens/instance/transfer_view.ex | 2 + .../tokens/instance_controller_test.exs | 6 ++- 8 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 apps/block_scout_web/lib/block_scout_web/templates/tokens/instance/transfer/index.html.eex create mode 100644 apps/block_scout_web/lib/block_scout_web/views/tokens/instance/overview_view.ex diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance/transfer_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance/transfer_controller.ex index e0190af2a1..52be59dca7 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance/transfer_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance/transfer_controller.ex @@ -3,9 +3,11 @@ defmodule BlockScoutWeb.Tokens.Instance.TransferController do alias Explorer.{Chain, Market} - def show(conn, %{"token_id" => token_id, "id" => token_address_hash}) do + def index(conn, %{"token_id" => token_id, "instance_id" => token_address_hash}) do + options = [necessity_by_association: %{[contract_address: :smart_contract] => :optional}] + with {:ok, hash} <- Chain.string_to_address_hash(token_address_hash), - {:ok, token} <- Chain.token_from_address_hash(hash), + {:ok, token} <- Chain.token_from_address_hash(hash, options), {:ok, token_transfer} <- Chain.erc721_token_instance_from_token_id_and_token_address(token_id, hash) do render( diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance_controller.ex index 24dc551347..cbe27009cf 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/instance_controller.ex @@ -5,10 +5,10 @@ defmodule BlockScoutWeb.Tokens.InstanceController do def show(conn, %{"token_id" => token_id, "id" => token_address_hash}) do with {:ok, hash} <- Chain.string_to_address_hash(token_address_hash), - {:ok, token_address} <- Chain.hash_to_address(hash, []), - {:ok, token_transfer} <- - Chain.erc721_token_instance_from_token_id_and_token_address(token_id, token_address.hash) do - json(conn, token_transfer) + {:ok, _token_address} <- Chain.token_from_address_hash(hash, []), + {:ok, _token_transfer} <- + Chain.erc721_token_instance_from_token_id_and_token_address(token_id, hash) |> IO.inspect() do + redirect(conn, to: token_instance_transfer_path(conn, :index, token_id, token_address_hash)) else _ -> not_found(conn) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex index b9783b2c63..d695277765 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/tokens/inventory_controller.ex @@ -39,7 +39,9 @@ defmodule BlockScoutWeb.Tokens.InventoryController do View.render_to_string( InventoryView, "_token.html", - token_transfer: token_transfer + token_transfer: token_transfer, + token: token, + conn: conn ) end) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/tokens/instance/transfer/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/tokens/instance/transfer/index.html.eex new file mode 100644 index 0000000000..3de4ae1273 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/templates/tokens/instance/transfer/index.html.eex @@ -0,0 +1,38 @@ +
+ <%= render( + OverviewView, + "_details.html", + token: @token, + total_token_transfers: @total_token_transfers, + token_id: @token_instance.token_id, + conn: @conn + ) %> + +
+
+ <%= render OverviewView, "_tabs.html", assigns %> +
+

<%= gettext "Token Transfers" %>

+ + <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "top", cur_page_number: "1", show_pagination_limit: true, data_next_page_button: true, data_prev_page_button: true %> + + + + + +
+ <%= render BlockScoutWeb.CommonComponentsView, "_tile-loader.html" %> +
+ + <%= render BlockScoutWeb.CommonComponentsView, "_pagination_container.html", position: "bottom", cur_page_number: "1", show_pagination_limit: true, data_next_page_button: true, data_prev_page_button: true %> + +
+
+
+
diff --git a/apps/block_scout_web/lib/block_scout_web/templates/tokens/inventory/_token.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/tokens/inventory/_token.html.eex index 88c36e5158..11c52c364f 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/tokens/inventory/_token.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/tokens/inventory/_token.html.eex @@ -10,7 +10,7 @@ <%= gettext "Token ID" %>: - <%= @token_transfer.token_id %> + <%= link(@token_transfer.token_id, to: token_instance_path(@conn, :show, to_string(@token_transfer.token_id), @token.contract_address_hash)) %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/overview_view.ex b/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/overview_view.ex new file mode 100644 index 0000000000..bb5eca56f9 --- /dev/null +++ b/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/overview_view.ex @@ -0,0 +1,21 @@ +defmodule BlockScoutWeb.Tokens.Instance.OverviewView do + use BlockScoutWeb, :view + + alias Explorer.Chain.Token + alias BlockScoutWeb.CurrencyHelpers + + def token_name?(%Token{name: nil}), do: false + def token_name?(%Token{name: _}), do: true + + def decimals?(%Token{decimals: nil}), do: false + def decimals?(%Token{decimals: _}), do: true + + def total_supply?(%Token{total_supply: nil}), do: false + def total_supply?(%Token{total_supply: _}), do: true + + def total_supply_usd(token) do + tokens = CurrencyHelpers.divide_decimals(token.total_supply, token.decimals) + price = token.usd_value + Decimal.mult(tokens, price) + end +end diff --git a/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/transfer_view.ex b/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/transfer_view.ex index 0be8cb0531..2cf314efa1 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/transfer_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/tokens/instance/transfer_view.ex @@ -1,3 +1,5 @@ defmodule BlockScoutWeb.Tokens.Instance.TransferView do use BlockScoutWeb, :view + + alias BlockScoutWeb.Tokens.Instance.OverviewView end diff --git a/apps/block_scout_web/test/block_scout_web/controllers/tokens/instance_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/tokens/instance_controller_test.exs index 5c3fbbf59d..03d3010b2d 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/tokens/instance_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/tokens/instance_controller_test.exs @@ -2,9 +2,11 @@ defmodule BlockScoutWeb.Tokens.InstanceControllerTest do use BlockScoutWeb.ConnCase, async: false describe "GET show/2" do - test "returns erc721 token with valid params", %{conn: conn} do + test "redirects with valid params", %{conn: conn} do contract_address = insert(:address) + insert(:token, contract_address: contract_address) + token_id = 10 insert(:token_transfer, @@ -15,7 +17,7 @@ defmodule BlockScoutWeb.Tokens.InstanceControllerTest do conn = get(conn, token_instance_path(BlockScoutWeb.Endpoint, :show, token_id, to_string(contract_address.hash))) - assert conn.status == 200 + assert conn.status == 302 end end end