diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/_responsive_hash.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/_responsive_hash.html.eex index fd2259bbbe..bea2a2159f 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address/_responsive_hash.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/_responsive_hash.html.eex @@ -1,7 +1,6 @@ <%= if name = primary_name(@address) do %> - <%= name %> - <%= name %> + <%= name %> (<%= short_hash(@address) %>...) <% else %> <%= if assigns[:truncate] do %> <%= BlockScoutWeb.AddressView.trimmed_hash(@address.hash) %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex index 71eb6473be..25515972a0 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex @@ -219,4 +219,14 @@ defmodule BlockScoutWeb.AddressView do defp tab_name(["internal_transactions"]), do: gettext("Internal Transactions") defp tab_name(["contracts"]), do: gettext("Code") defp tab_name(["read_contract"]), do: gettext("Read Contract") + + def short_hash(%Address{hash: hash}) do + << + "0x", + short_address::binary-size(6), + _rest::binary + >> = to_string(hash) + + "0x" <> short_address + end end diff --git a/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs index 2d8843a680..c2ccb131b7 100644 --- a/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/address_view_test.exs @@ -321,4 +321,12 @@ defmodule BlockScoutWeb.AddressViewTest do assert AddressView.current_tab_name(path) == "Read Contract" end end + + describe "short_hash/1" do + test "returns a shortened hash of 6 hex characters" do + address = insert(:address) + assert "0x" <> short_hash = AddressView.short_hash(address) + assert String.length(short_hash) == 6 + end + end end