diff --git a/apps/block_scout_web/assets/js/pages/address.js b/apps/block_scout_web/assets/js/pages/address.js index ddafbcb1e2..1ffad53708 100644 --- a/apps/block_scout_web/assets/js/pages/address.js +++ b/apps/block_scout_web/assets/js/pages/address.js @@ -34,6 +34,13 @@ export function reducer (state = initialState, action) { channelDisconnected: true }) } + case 'COUNTERS_FETCHED': { + console.log(action) + return Object.assign({}, state, { + transactionCount: action.transactionCount, + validationCount: action.validationCount + }) + } case 'RECEIVED_NEW_BLOCK': { if (state.channelDisconnected) return state @@ -81,8 +88,13 @@ const elements = { return { transactionCount: numeral($el.text()).value() } }, render ($el, state, oldState) { - if (oldState.transactionCount === state.transactionCount) return - $el.empty().append(numeral(state.transactionCount).format()) + if (state.transactionCount) { + if (oldState.transactionCount === state.transactionCount) return + $el.empty().append('>= ' + numeral(state.transactionCount).format() + ' Transactions') + $el.show() + } else { + $el.hide() + } } }, '[data-selector="fetched-coin-balance-block-number"]': { @@ -99,12 +111,29 @@ const elements = { return { validationCount: numeral($el.text()).value() } }, render ($el, state, oldState) { - if (oldState.validationCount === state.validationCount) return - $el.empty().append(numeral(state.validationCount).format()) + if (state.validationCount) { + if (oldState.validationCount === state.validationCount) return + $el.empty().append(numeral(state.validationCount).format() + ' Blocks Validated') + $el.show() + } else { + $el.hide() + } } } } +function loadCounters (store) { + const $element = $('[data-async-counters]') + const path = $element.data().asyncCounters + + function fetchCounters () { + $.getJSON(path) + .done(response => store.dispatch(Object.assign({type: 'COUNTERS_FETCHED'}, humps.camelizeKeys(response)))) + } + + fetchCounters() +} + const $addressDetailsPage = $('[data-page="address-details"]') if ($addressDetailsPage.length) { const store = createStore(reducer) @@ -149,4 +178,6 @@ if ($addressDetailsPage.length) { type: 'RECEIVED_UPDATED_BALANCE', msg: humps.camelizeKeys(msg) })) + + loadCounters(store) } diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex index b26624cfb2..a64d6ed735 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_controller.ex @@ -5,7 +5,6 @@ defmodule BlockScoutWeb.AddressController do alias BlockScoutWeb.AddressView alias Explorer.{Chain, Market} - alias Explorer.Chain.Hash alias Explorer.ExchangeRates.Token alias Phoenix.View @@ -69,26 +68,25 @@ defmodule BlockScoutWeb.AddressController do end def address_counters(conn, %{"id" => address_hash_string}) do - case Chain.string_to_address_hash(address_hash_string) do - {:ok, address_hash} -> - {transaction_count, validation_count} = transaction_and_validation_count(address_hash) + with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), + {:ok, address} <- Chain.hash_to_address(address_hash) do + {transaction_count, validation_count} = transaction_and_validation_count(address) - json(conn, %{transaction_count: transaction_count, validation_count: validation_count}) - - _ -> - not_found(conn) + json(conn, %{transaction_count: transaction_count, validation_count: validation_count}) + else + _ -> not_found(conn) end end - defp transaction_and_validation_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do + defp transaction_and_validation_count(address) do transaction_count_task = Task.async(fn -> - transaction_count(address_hash) + transaction_count(address) end) validation_count_task = Task.async(fn -> - validation_count(address_hash) + validation_count(address) end) [transaction_count_task, validation_count_task] @@ -108,11 +106,19 @@ defmodule BlockScoutWeb.AddressController do |> List.to_tuple() end - defp transaction_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do - Chain.total_transactions_sent_by_address(address_hash) + defp transaction_count(address) do + if contract?(address) do + Chain.address_to_incoming_transaction_count(address.hash) + else + Chain.total_transactions_sent_by_address(address.hash) + end end - defp validation_count(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do - Chain.address_to_validation_count(address_hash) + defp validation_count(address) do + Chain.address_to_validation_count(address.hash) end + + def contract?(%{contract_code: nil}), do: false + + def contract?(%{contract_code: _}), do: true end 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 414f4750ee..de40e821f2 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 @@ -71,18 +71,8 @@ <% end %> - <%= if contract?(@address) do %> - <%= gettext(">=") %> - - <%= incoming_transaction_count(@address.hash) %> - - <%= gettext("Incoming Transactions") %> - <% else %> - - <%= BlockScoutWeb.Cldr.Number.to_string!(0, format: "#,###") %> - - <%= gettext("Transactions Sent") %> - <% end %> + + <%= if @address.fetched_coin_balance_block_number do %> @@ -92,9 +82,7 @@ - <%= BlockScoutWeb.Cldr.Number.to_string!(0, format: "#,###") %> - <%= gettext("Blocks Validated") %> 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 15405beecb..4937377f74 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,12 +219,6 @@ defmodule BlockScoutWeb.AddressView do def token_title(%Token{name: name, symbol: symbol}), do: "#{name} (#{symbol})" - def incoming_transaction_count(address_hash) do - address_hash - |> Chain.address_to_incoming_transaction_count() - |> BlockScoutWeb.Cldr.Number.to_string!(format: "#,###") - end - def trimmed_hash(%Hash{} = hash) do string_hash = to_string(hash) "#{String.slice(string_hash, 0..5)}–#{String.slice(string_hash, -6..-1)}"