From 345ce79e47ee7747cc9fa8c611392ab52f88c9ac Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 14 Mar 2019 11:56:44 +0300 Subject: [PATCH] add incoming transactions count to contract view --- .../templates/address/overview.html.eex | 15 +++++++++++---- .../lib/block_scout_web/views/address_view.ex | 4 ++++ apps/explorer/lib/explorer/chain.ex | 11 +++++++++++ ...0190314084907_add_index_to_to_address_hash.exs | 7 +++++++ apps/explorer/test/explorer/chain_test.exs | 14 ++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20190314084907_add_index_to_to_address_hash.exs diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex index 66c61b4957..42906111ef 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex @@ -35,10 +35,17 @@ <% end %> - - <%= Cldr.Number.to_string!(@transaction_count, format: "#,###") %> - - <%= gettext("Transactions Sent") %> + <%= if contract?(@address) do %> + + <%= Cldr.Number.to_string!(incoming_transaction_count(@address), format: "#,###") %> + + <%= gettext("Incoming Transactions") %> + <% else %> + + <%= Cldr.Number.to_string!(@transaction_count, format: "#,###") %> + + <%= gettext("Transactions Sent") %> + <% end %> <%= gettext("Last Balance Update: Block #") %><%= @address.fetched_coin_balance_block_number %> 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 0fc50bef3a..e527757b23 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 @@ -213,6 +213,10 @@ defmodule BlockScoutWeb.AddressView do def token_title(%Token{name: name, symbol: symbol}), do: "#{name} (#{symbol})" + def incoming_transaction_count(%Address{} = address) do + Chain.address_to_incoming_transaction_count(address) + end + def trimmed_hash(%Hash{} = hash) do string_hash = to_string(hash) "#{String.slice(string_hash, 0..5)}–#{String.slice(string_hash, -6..-1)}" diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index b40af9a343..6d8a532b58 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -417,6 +417,17 @@ defmodule Explorer.Chain do Repo.aggregate(query, :count, :hash) end + @spec address_to_incoming_transaction_count(Address.t()) :: non_neg_integer() + def address_to_incoming_transaction_count(%Address{hash: address_hash}) do + query = + from( + transaction in Transaction, + where: transaction.to_address_hash == ^address_hash + ) + + Repo.aggregate(query, :count, :hash) + end + @doc """ How many blocks have confirmed `block` based on the current `max_block_number` diff --git a/apps/explorer/priv/repo/migrations/20190314084907_add_index_to_to_address_hash.exs b/apps/explorer/priv/repo/migrations/20190314084907_add_index_to_to_address_hash.exs new file mode 100644 index 0000000000..a46822a17c --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20190314084907_add_index_to_to_address_hash.exs @@ -0,0 +1,7 @@ +defmodule Explorer.Repo.Migrations.AddIndexToToAddressHash do + use Ecto.Migration + + def change do + create(index(:transactions, [:to_address_hash])) + end +end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 77fcb4b2e8..accc733181 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -597,6 +597,20 @@ defmodule Explorer.ChainTest do end end + describe "address_to_incoming_transaction_count/1" do + test "without transactions" do + address = insert(:address) + + assert Chain.address_to_incoming_transaction_count(address) == 0 + end + + test "with transactions" do + %Transaction{to_address: to_address} = insert(:transaction) + + assert Chain.address_to_incoming_transaction_count(to_address) == 1 + end + end + describe "confirmations/1" do test "with block.number == block_height " do block = insert(:block)