From 95b4594ad8db66d44645fb183bbde9ecfc689a3b Mon Sep 17 00:00:00 2001 From: jimmay5469 Date: Thu, 17 May 2018 22:39:53 -0400 Subject: [PATCH] Address link helper --- .../explorer/lib/explorer/chain/statistics.ex | 2 +- .../templates/address/_link.html.eex | 8 +++++++ .../index.html.eex | 14 ++--------- .../address_transaction/index.html.eex | 14 ++--------- .../block_transaction/index.html.eex | 14 ++--------- .../templates/chain/_blocks.html.eex | 7 +----- .../templates/chain/_transactions.html.eex | 14 ++--------- .../pending_transaction/index.html.eex | 24 ++----------------- .../templates/transaction/index.html.eex | 14 ++--------- .../templates/transaction/show.html.eex | 14 ++--------- .../lib/explorer_web/views/address_view.ex | 4 ++++ 11 files changed, 28 insertions(+), 101 deletions(-) create mode 100644 apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex diff --git a/apps/explorer/lib/explorer/chain/statistics.ex b/apps/explorer/lib/explorer/chain/statistics.ex index f25518acfa..d5819c01f5 100644 --- a/apps/explorer/lib/explorer/chain/statistics.ex +++ b/apps/explorer/lib/explorer/chain/statistics.ex @@ -110,7 +110,7 @@ defmodule Explorer.Chain.Statistics do from( block in Block, order_by: [desc: block.number], - preload: [:transactions], + preload: [:transactions, :miner], limit: 5 ) diff --git a/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex b/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex new file mode 100644 index 0000000000..d0c546b3d9 --- /dev/null +++ b/apps/explorer_web/lib/explorer_web/templates/address/_link.html.eex @@ -0,0 +1,8 @@ +<%= if @address do %> + <%= link to: address_path(@conn, :show, @conn.assigns.locale, @address), + "data-toggle": "tooltip", + "data-placement": "top", + title: @address do %> + <%= @address |> hash |> String.slice(0..3) %> <%= @address |> hash |> String.slice(-4..-1) %> + <% end %> +<% end %> diff --git a/apps/explorer_web/lib/explorer_web/templates/address_internal_transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/address_internal_transaction/index.html.eex index d90dcbb07b..67f3f8733d 100644 --- a/apps/explorer_web/lib/explorer_web/templates/address_internal_transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/address_internal_transaction/index.html.eex @@ -82,20 +82,10 @@ <%= ExplorerWeb.BlockView.age(internal_transaction.transaction.block) %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, internal_transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: internal_transaction.from_address.hash do %> - <%= String.slice(internal_transaction.from_address.hash, 0..3) %> <%= String.slice(internal_transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: internal_transaction.from_address %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, internal_transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: internal_transaction.to_address.hash do %> - <%= String.slice(internal_transaction.to_address.hash, 0..3) %> <%= String.slice(internal_transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: internal_transaction.to_address %> <%= ExplorerWeb.TransactionView.value(internal_transaction) %> diff --git a/apps/explorer_web/lib/explorer_web/templates/address_transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/address_transaction/index.html.eex index ebd6337c33..612417aa4f 100644 --- a/apps/explorer_web/lib/explorer_web/templates/address_transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/address_transaction/index.html.eex @@ -87,20 +87,10 @@ <%= transaction.block.timestamp |> Timex.from_now %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= ExplorerWeb.TransactionView.value(transaction, include_label: false) %> <%= ExplorerWeb.TransactionView.fee(transaction) %> diff --git a/apps/explorer_web/lib/explorer_web/templates/block_transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/block_transaction/index.html.eex index f7c66e4c82..4647da946e 100644 --- a/apps/explorer_web/lib/explorer_web/templates/block_transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/block_transaction/index.html.eex @@ -157,21 +157,11 @@ <%= transaction.block.timestamp |> Timex.from_now %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %>
- <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= ExplorerWeb.TransactionView.value(transaction) %> diff --git a/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex b/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex index 60bd856e19..d59be8fe7e 100644 --- a/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/chain/_blocks.html.eex @@ -29,12 +29,7 @@ <%= block.gas_used |> Cldr.Number.to_string! %> - <%= link to: block_path(@conn, :show, @conn.assigns.locale, block.number), - "data-toggle": "tooltip", - "data-placement": "top", - title: block.miner do %> - <%= String.slice(block.miner, 0..3) %> <%= String.slice(block.miner, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: block.miner %> <% end %> diff --git a/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex b/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex index 84e68d2412..a948df1b2b 100644 --- a/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/chain/_transactions.html.eex @@ -22,21 +22,11 @@ - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= ExplorerWeb.TransactionView.value(transaction, include_label: false) %> <%= transaction.block.timestamp |> Timex.from_now() %> diff --git a/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex index 87f97d8cb5..9e351851c9 100644 --- a/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/pending_transaction/index.html.eex @@ -54,30 +54,10 @@ <%= last_seen(transaction) %> - <% from_address_hash = from_address_hash(transaction) %> - <%= if from_address_hash do %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> - <% else %> - <%= gettext "Pending" %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %> - <% to_address_hash = to_address_hash(transaction) %> - <%= if to_address_hash do %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> - <% else %> - <%= gettext "Pending" %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= ExplorerWeb.TransactionView.value(transaction, include_label: false) %> diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex index f5cd471773..c7d32899f8 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction/index.html.eex @@ -57,20 +57,10 @@ <%= transaction.block.timestamp |> Timex.from_now %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= ExplorerWeb.TransactionView.value(transaction, include_label: false) %> diff --git a/apps/explorer_web/lib/explorer_web/templates/transaction/show.html.eex b/apps/explorer_web/lib/explorer_web/templates/transaction/show.html.eex index 5a2e022b9a..e84f5b0f66 100644 --- a/apps/explorer_web/lib/explorer_web/templates/transaction/show.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/transaction/show.html.eex @@ -34,20 +34,10 @@ <%= transaction.call_type %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.from_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.from_address.hash do %> - <%= String.slice(transaction.from_address.hash, 0..3) %> <%= String.slice(transaction.from_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.from_address %> - <%= link to: address_path(@conn, :show, @conn.assigns.locale, transaction.to_address.hash), - "data-toggle": "tooltip", - "data-placement": "top", - title: transaction.to_address.hash do %> - <%= String.slice(transaction.to_address.hash, 0..3) %> <%= String.slice(transaction.to_address.hash, -4..-1) %> - <% end %> + <%= render ExplorerWeb.AddressView, "_link.html", conn: @conn, address: transaction.to_address %> <%= value(transaction, include_label: false) %> <%= gas(transaction) %> diff --git a/apps/explorer_web/lib/explorer_web/views/address_view.ex b/apps/explorer_web/lib/explorer_web/views/address_view.ex index 682704aac0..aca598d545 100644 --- a/apps/explorer_web/lib/explorer_web/views/address_view.ex +++ b/apps/explorer_web/lib/explorer_web/views/address_view.ex @@ -13,4 +13,8 @@ defmodule ExplorerWeb.AddressView do def balance(%Address{fetched_balance: balance}) do format_wei_value(balance, :ether, fractional_digits: 18) end + + def hash(%Address{hash: hash}) do + to_string(hash) + end end