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 %> <% end %>
</li> </li>
<%= if @address.has_decompiled_code? do %> <%= if has_decompiled_code?(@address) do %>
<li class="nav-item"> <li class="nav-item">
<%= link( <%= 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 %> class: "nav-link #{tab_status("contracts", @conn.request_path)}") do %>
<%= gettext("Decompiled code") %> <%= gettext("Decompiled code") %>
<% end %> <% end %>
@ -125,6 +125,14 @@
<%= if smart_contract_verified?(@address) do %> <%= if smart_contract_verified?(@address) do %>
<i class="far fa-check-circle"></i> <i class="far fa-check-circle"></i>
<% end %> <% 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 %>
<% end %> <% end %>
<%= if smart_contract_with_read_only_functions?(@address) do %> <%= if smart_contract_with_read_only_functions?(@address) do %>

@ -16,6 +16,7 @@ defmodule BlockScoutWeb.AddressView do
@tabs [ @tabs [
"coin_balances", "coin_balances",
"contracts", "contracts",
"decompiled_contracts",
"internal_transactions", "internal_transactions",
"read_contract", "read_contract",
"tokens", "tokens",
@ -205,6 +206,11 @@ defmodule BlockScoutWeb.AddressView do
def smart_contract_with_read_only_functions?(%Address{smart_contract: nil}), do: false 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 def token_title(%Token{name: nil, contract_address_hash: contract_address_hash}) do
contract_address_hash contract_address_hash
|> to_string |> to_string
@ -288,6 +294,7 @@ defmodule BlockScoutWeb.AddressView do
defp tab_name(["transactions"]), do: gettext("Transactions") defp tab_name(["transactions"]), do: gettext("Transactions")
defp tab_name(["internal_transactions"]), do: gettext("Internal Transactions") defp tab_name(["internal_transactions"]), do: gettext("Internal Transactions")
defp tab_name(["contracts"]), do: gettext("Code") 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(["read_contract"]), do: gettext("Read Contract")
defp tab_name(["coin_balances"]), do: gettext("Coin Balance History") defp tab_name(["coin_balances"]), do: gettext("Coin Balance History")
defp tab_name(["validations"]), do: gettext("Blocks Validated") 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} @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 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 = query =
from( from(
address in Address, address in Address,
left_join: decompiled_code in subquery(has_decompiled_code_query),
preload: [ preload: [
:contracts_creation_internal_transaction, :contracts_creation_internal_transaction,
:names, :names,
@ -665,9 +657,9 @@ defmodule Explorer.Chain do
:token, :token,
:contracts_creation_transaction :contracts_creation_transaction
], ],
where: address.hash == ^hash, where: address.hash == ^hash
select_merge: %{has_decompiled_code?: decompiled_code.has_decompiled_code?}
) )
|> with_decompiled_code_flag(hash)
query query
|> Repo.one() |> Repo.one()
@ -786,6 +778,7 @@ defmodule Explorer.Chain do
], ],
where: address.hash == ^hash and not is_nil(address.contract_code) where: address.hash == ^hash and not is_nil(address.contract_code)
) )
|> with_decompiled_code_flag(hash)
address = Repo.one(query) address = Repo.one(query)
@ -804,6 +797,7 @@ defmodule Explorer.Chain do
preload: [ preload: [
:contracts_creation_internal_transaction, :contracts_creation_internal_transaction,
:names, :names,
:smart_contract,
:token, :token,
:contracts_creation_transaction, :contracts_creation_transaction,
:decompiled_smart_contracts :decompiled_smart_contracts
@ -2682,4 +2676,19 @@ defmodule Explorer.Chain do
value value
end 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 end

Loading…
Cancel
Save