make tab visible if contract has any decompiled code

pull/1654/head
Ayrat Badykov 6 years ago
parent e27c4fd33d
commit db5853cc43
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 10
      apps/block_scout_web/lib/block_scout_web/templates/address/_tabs.html.eex
  2. 11
      apps/explorer/lib/explorer/chain.ex
  3. 16
      apps/explorer/lib/explorer/chain/address.ex
  4. 17
      apps/explorer/test/explorer/chain_test.exs

@ -57,6 +57,16 @@
<% end %>
<% end %>
</li>
<%= if @address.has_decompiled_code? do %>
<li class="nav-item">
<%= link(
to: address_contract_path(@conn, :index, @address.hash),
class: "nav-link #{tab_status("contracts", @conn.request_path)}") do %>
<%= gettext("Decompiled code") %>
<% end %>
</li>
<% end %>
<% end %>
<%= if smart_contract_with_read_only_functions?(@address) do %>

@ -647,9 +647,17 @@ 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,
@ -657,7 +665,8 @@ defmodule Explorer.Chain do
:token,
:contracts_creation_transaction
],
where: address.hash == ^hash
where: address.hash == ^hash,
select_merge: %{has_decompiled_code?: decompiled_code.has_decompiled_code?}
)
query

@ -8,7 +8,19 @@ defmodule Explorer.Chain.Address do
use Explorer.Schema
alias Ecto.Changeset
alias Explorer.Chain.{Address, Block, Data, Hash, InternalTransaction, SmartContract, Token, Transaction, Wei}
alias Explorer.Chain.{
Address,
Block,
Data,
DecompiledSmartContract,
Hash,
InternalTransaction,
SmartContract,
Token,
Transaction,
Wei
}
@optional_attrs ~w(contract_code fetched_coin_balance fetched_coin_balance_block_number nonce)a
@required_attrs ~w(hash)a
@ -62,6 +74,7 @@ defmodule Explorer.Chain.Address do
field(:fetched_coin_balance_block_number, :integer)
field(:contract_code, Data)
field(:nonce, :integer)
field(:has_decompiled_code?, :boolean, virtual: true)
has_one(:smart_contract, SmartContract)
has_one(:token, Token, foreign_key: :contract_address_hash)
@ -79,6 +92,7 @@ defmodule Explorer.Chain.Address do
)
has_many(:names, Address.Name, foreign_key: :address_hash)
has_many(:decompiled_smart_contract, DecompiledSmartContract, foreign_key: :address_hash)
timestamps()
end

@ -854,6 +854,23 @@ defmodule Explorer.ChainTest do
assert {:ok, address} = Chain.hash_to_address(address.hash)
end
test "has_decompiled_code? is true if there are decompiled contracts" do
address = insert(:address)
insert(:decompiled_smart_contract, address_hash: address.hash)
{:ok, found_address} = Chain.hash_to_address(address.hash)
assert found_address.has_decompiled_code?
end
test "has_decompiled_code? is false if there are no decompiled contracts" do
address = insert(:address)
{:ok, found_address} = Chain.hash_to_address(address.hash)
refute found_address.has_decompiled_code?
end
end
describe "token_contract_address_from_token_name/1" do

Loading…
Cancel
Save