add incoming transactions count to contract view

pull/1562/head
Ayrat Badykov 6 years ago
parent 5d5b8fa84d
commit 345ce79e47
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 7
      apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex
  2. 4
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex
  3. 11
      apps/explorer/lib/explorer/chain.ex
  4. 7
      apps/explorer/priv/repo/migrations/20190314084907_add_index_to_to_address_hash.exs
  5. 14
      apps/explorer/test/explorer/chain_test.exs

@ -35,10 +35,17 @@
<% end %>
<span>
<span class="address-detail-item">
<%= if contract?(@address) do %>
<span data-selector="transaction-count">
<%= Cldr.Number.to_string!(incoming_transaction_count(@address), format: "#,###") %>
</span>
<%= gettext("Incoming Transactions") %>
<% else %>
<span data-selector="transaction-count">
<%= Cldr.Number.to_string!(@transaction_count, format: "#,###") %>
</span>
<%= gettext("Transactions Sent") %>
<% end %>
</span>
<span class="address-detail-item">
<%= gettext("Last Balance Update: Block #") %><span data-selector="fetched-coin-balance-block-number"><%= @address.fetched_coin_balance_block_number %></span>

@ -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)}"

@ -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`

@ -0,0 +1,7 @@
defmodule Explorer.Repo.Migrations.AddIndexToToAddressHash do
use Ecto.Migration
def change do
create(index(:transactions, [:to_address_hash]))
end
end

@ -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)

Loading…
Cancel
Save