From 8cc05a3202294ee9a4bbc7b7d1893c20f7e8237a Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Wed, 8 Apr 2020 18:32:08 +0300 Subject: [PATCH] Make a link to address page on decoded constructor argumennt of address type --- CHANGELOG.md | 1 + .../templates/address_contract/index.html.eex | 2 +- .../views/address_contract_view.ex | 32 ++++++++++++++++--- apps/block_scout_web/priv/gettext/default.pot | 4 +-- .../priv/gettext/en/LC_MESSAGES/default.po | 4 +-- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e3756ddd3..a4508c5523 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Features +- [#3069](https://github.com/poanetwork/blockscout/pull/3069) - Make a link to address page on decoded constructor argument of address type - [#3066](https://github.com/poanetwork/blockscout/pull/3066) - ERC-721 token instance page: link to token added ### Fixes diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex index c84879dc9b..4d0cf6d063 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex @@ -53,7 +53,7 @@

<%= gettext "Constructor Arguments" %>

-
<%= raw(format_constructor_arguments(@address.smart_contract)) %>
+              
<%= raw(format_constructor_arguments(@address.smart_contract, @conn)) %>
               
diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex index 5bb8b7cebb..527bb6eb97 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex @@ -2,6 +2,7 @@ defmodule BlockScoutWeb.AddressContractView do use BlockScoutWeb, :view alias ABI.{FunctionSelector, TypeDecoder} + alias Explorer.Chain alias Explorer.Chain.{Address, Data, InternalTransaction, SmartContract} def render("scripts.html", %{conn: conn}) do @@ -22,7 +23,7 @@ defmodule BlockScoutWeb.AddressContractView do def format_optimization_text(true), do: gettext("true") def format_optimization_text(false), do: gettext("false") - def format_constructor_arguments(contract) do + def format_constructor_arguments(contract, conn) do constructor_abi = Enum.find(contract.abi, fn el -> el["type"] == "constructor" && el["inputs"] != [] end) input_types = Enum.map(constructor_abi["inputs"], &FunctionSelector.parse_specification_type/1) @@ -32,11 +33,24 @@ defmodule BlockScoutWeb.AddressContractView do |> decode_data(input_types) |> Enum.zip(constructor_abi["inputs"]) |> Enum.reduce({0, "#{contract.constructor_arguments}\n\n"}, fn {val, %{"type" => type}}, {count, acc} -> + address_hash = "0x" <> Base.encode16(val, case: :lower) + + address = + case Chain.string_to_address_hash(address_hash) do + {:ok, address} -> address + _ -> nil + end + formatted_val = - if type =~ "address" || type =~ "bytes" do - Base.encode16(val, case: :lower) - else - val + cond do + type =~ "address" -> + get_formatted_address_data(address, address_hash, conn) + + type =~ "bytes" -> + Base.encode16(val, case: :lower) + + true -> + val end {count + 1, "#{acc}Arg [#{count}] (#{type}) : #{formatted_val}\n"} @@ -47,6 +61,14 @@ defmodule BlockScoutWeb.AddressContractView do _ -> contract.constructor_arguments end + defp get_formatted_address_data(address, address_hash, conn) do + if address != nil do + "" <> address_hash <> "" + else + address_hash + end + end + defp decode_data("0x" <> encoded_data, types) do decode_data(encoded_data, types) end diff --git a/apps/block_scout_web/priv/gettext/default.pot b/apps/block_scout_web/priv/gettext/default.pot index 9e0a122b09..5bd6545e50 100644 --- a/apps/block_scout_web/priv/gettext/default.pot +++ b/apps/block_scout_web/priv/gettext/default.pot @@ -1672,7 +1672,7 @@ msgid "custom RPC" msgstr "" #, elixir-format -#: lib/block_scout_web/views/address_contract_view.ex:23 +#: lib/block_scout_web/views/address_contract_view.ex:24 msgid "false" msgstr "" @@ -1714,7 +1714,7 @@ msgid "string" msgstr "" #, elixir-format -#: lib/block_scout_web/views/address_contract_view.ex:22 +#: lib/block_scout_web/views/address_contract_view.ex:23 msgid "true" msgstr "" diff --git a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po index 9e0a122b09..5bd6545e50 100644 --- a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po +++ b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po @@ -1672,7 +1672,7 @@ msgid "custom RPC" msgstr "" #, elixir-format -#: lib/block_scout_web/views/address_contract_view.ex:23 +#: lib/block_scout_web/views/address_contract_view.ex:24 msgid "false" msgstr "" @@ -1714,7 +1714,7 @@ msgid "string" msgstr "" #, elixir-format -#: lib/block_scout_web/views/address_contract_view.ex:22 +#: lib/block_scout_web/views/address_contract_view.ex:23 msgid "true" msgstr ""