add decompiled contract tab

pull/1654/head
Ayrat Badykov 6 years ago
parent 417a236d18
commit 2e0a19f1d1
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 12
      apps/block_scout_web/lib/block_scout_web/templates/address/_tabs.html.eex
  2. 7
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex
  3. 29
      apps/explorer/lib/explorer/chain.ex

@ -58,10 +58,10 @@
<% end %>
</li>
<%= if @address.has_decompiled_code? do %>
<%= if has_decompiled_code?(@address) do %>
<li class="nav-item">
<%= link(
to: address_contract_path(@conn, :index, @address.hash),
to: address_decompiled_contract_path(@conn, :index, @address.hash),
class: "nav-link #{tab_status("contracts", @conn.request_path)}") do %>
<%= gettext("Decompiled code") %>
<% end %>
@ -125,6 +125,14 @@
<%= if smart_contract_verified?(@address) do %>
<i class="far fa-check-circle"></i>
<% end %>
<%= if has_decompiled_code?(@address) do %>
<%= link(
to: address_decompiled_contract_path(@conn, :index, @address.hash),
class: "nav-link #{tab_status("contracts", @conn.request_path)}") do %>
<%= gettext("Decompiled code") %>
<% end %>
<% end %>
<% end %>
<% end %>
<%= if smart_contract_with_read_only_functions?(@address) do %>

@ -16,6 +16,7 @@ defmodule BlockScoutWeb.AddressView do
@tabs [
"coin_balances",
"contracts",
"decompiled_contracts",
"internal_transactions",
"read_contract",
"tokens",
@ -205,6 +206,11 @@ defmodule BlockScoutWeb.AddressView do
def smart_contract_with_read_only_functions?(%Address{smart_contract: nil}), do: false
def has_decompiled_code?(address) do
address.has_decompiled_code? ||
(Ecto.assoc_loaded?(address.decompiled_smart_contracts) && Enum.count(address.decompiled_smart_contracts) > 0)
end
def token_title(%Token{name: nil, contract_address_hash: contract_address_hash}) do
contract_address_hash
|> to_string
@ -288,6 +294,7 @@ defmodule BlockScoutWeb.AddressView do
defp tab_name(["transactions"]), do: gettext("Transactions")
defp tab_name(["internal_transactions"]), do: gettext("Internal Transactions")
defp tab_name(["contracts"]), do: gettext("Code")
defp tab_name(["decompiled_contracts"]), do: gettext("Decompiled Code")
defp tab_name(["read_contract"]), do: gettext("Read Contract")
defp tab_name(["coin_balances"]), do: gettext("Coin Balance History")
defp tab_name(["validations"]), do: gettext("Blocks Validated")

@ -647,17 +647,9 @@ defmodule Explorer.Chain do
"""
@spec hash_to_address(Hash.Address.t()) :: {:ok, Address.t()} | {:error, :not_found}
def hash_to_address(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do
has_decompiled_code_query =
from(decompiled_contract in DecompiledSmartContract,
where: decompiled_contract.address_hash == ^hash,
limit: 1,
select: %{has_decompiled_code?: not is_nil(decompiled_contract.address_hash)}
)
query =
from(
address in Address,
left_join: decompiled_code in subquery(has_decompiled_code_query),
preload: [
:contracts_creation_internal_transaction,
:names,
@ -665,9 +657,9 @@ defmodule Explorer.Chain do
:token,
:contracts_creation_transaction
],
where: address.hash == ^hash,
select_merge: %{has_decompiled_code?: decompiled_code.has_decompiled_code?}
where: address.hash == ^hash
)
|> with_decompiled_code_flag(hash)
query
|> Repo.one()
@ -786,6 +778,7 @@ defmodule Explorer.Chain do
],
where: address.hash == ^hash and not is_nil(address.contract_code)
)
|> with_decompiled_code_flag(hash)
address = Repo.one(query)
@ -804,6 +797,7 @@ defmodule Explorer.Chain do
preload: [
:contracts_creation_internal_transaction,
:names,
:smart_contract,
:token,
:contracts_creation_transaction,
:decompiled_smart_contracts
@ -2682,4 +2676,19 @@ defmodule Explorer.Chain do
value
end
defp with_decompiled_code_flag(query, hash) do
has_decompiled_code_query =
from(decompiled_contract in DecompiledSmartContract,
where: decompiled_contract.address_hash == ^hash,
limit: 1,
select: %{has_decompiled_code?: not is_nil(decompiled_contract.address_hash)}
)
from(
address in query,
left_join: decompiled_code in subquery(has_decompiled_code_query),
select_merge: %{has_decompiled_code?: decompiled_code.has_decompiled_code?}
)
end
end

Loading…
Cancel
Save