From ceb971b28537e96418ef273a3effc11d5cbf6ebb Mon Sep 17 00:00:00 2001 From: Felipe Renan Date: Thu, 5 Jul 2018 15:04:24 -0300 Subject: [PATCH] Add contract creator on Account details page Now the user will be able to see the contract creator and the transaction references to it on the Account details page when the address is a contract. In order to this work we had to make a query that returns the internal transaction that has the contract creation. This way we can get the `from_address` that represents the contract creator and the transaction from the internal transaction. --- apps/explorer/lib/explorer/chain.ex | 4 +- apps/explorer/lib/explorer/chain/address.ex | 8 ++- apps/explorer/test/explorer/chain_test.exs | 4 +- .../address_transaction_controller.ex | 1 - .../templates/address/overview.html.eex | 30 +++++++++- apps/explorer_web/priv/gettext/default.pot | 10 ++++ .../priv/gettext/en/LC_MESSAGES/default.po | 10 ++++ .../address_contract_controller_test.exs | 15 ++++- .../features/pages/address_page.ex | 4 ++ .../features/viewing_addresses_test.exs | 57 +++++++++++++++++++ 10 files changed, 134 insertions(+), 9 deletions(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 8340fa7d99..a9958f6e0d 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -622,7 +622,7 @@ defmodule Explorer.Chain do query = from( address in Address, - preload: [:smart_contract], + preload: [:smart_contract, :contracts_creation_internal_transaction], where: address.hash == ^hash ) @@ -638,7 +638,7 @@ defmodule Explorer.Chain do query = from( address in Address, - preload: [:smart_contract], + preload: [:smart_contract, :contracts_creation_internal_transaction], where: address.hash == ^hash and not is_nil(address.contract_code) ) diff --git a/apps/explorer/lib/explorer/chain/address.ex b/apps/explorer/lib/explorer/chain/address.ex index 5b4de69c5c..4540e82600 100644 --- a/apps/explorer/lib/explorer/chain/address.ex +++ b/apps/explorer/lib/explorer/chain/address.ex @@ -6,7 +6,7 @@ defmodule Explorer.Chain.Address do use Explorer.Schema alias Ecto.Changeset - alias Explorer.Chain.{Block, Data, Hash, Wei, SmartContract} + alias Explorer.Chain.{Block, Data, Hash, Wei, SmartContract, InternalTransaction} @optional_attrs ~w(contract_code)a @required_attrs ~w(hash)a @@ -43,6 +43,12 @@ defmodule Explorer.Chain.Address do has_one(:smart_contract, SmartContract) + has_one( + :contracts_creation_internal_transaction, + InternalTransaction, + foreign_key: :created_contract_address_hash + ) + timestamps() end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 7b7fb27088..f56df055b0 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -832,7 +832,9 @@ defmodule Explorer.ChainTest do end test "finds an contract address" do - address = insert(:address, contract_code: Factory.data("contract_code"), smart_contract: nil) + address = + insert(:address, contract_code: Factory.data("contract_code"), smart_contract: nil) + |> Repo.preload(:contracts_creation_internal_transaction) response = Chain.find_contract_address(address.hash) diff --git a/apps/explorer_web/lib/explorer_web/controllers/address_transaction_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/address_transaction_controller.ex index ccdee7a450..0fea25a906 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/address_transaction_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/address_transaction_controller.ex @@ -26,7 +26,6 @@ defmodule ExplorerWeb.AddressTransactionController do |> Keyword.merge(current_filter(params)) transactions_plus_one = Chain.address_to_transactions(address, full_options) - {transactions, next_page} = split_list_by_page(transactions_plus_one) render( diff --git a/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex b/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex index d76d17c658..ddfa6ac02a 100644 --- a/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex +++ b/apps/explorer_web/lib/explorer_web/templates/address/overview.html.eex @@ -19,6 +19,35 @@

<%= @address %>

<%= Cldr.Number.to_string!(@transaction_count) %> <%= gettext "Transactions" %> + + <%= if contract?(@address) do %> + + <%= gettext "Contract created by" %> + <%= link( + trimmed_hash(@address.contracts_creation_internal_transaction.from_address_hash), + to: address_path( + ExplorerWeb.Endpoint, + :show, + @locale, + @address.contracts_creation_internal_transaction.from_address_hash + ) + ) %> + + <%= gettext "at" %> + + <%= link( + trimmed_hash(@address.contracts_creation_internal_transaction.transaction_hash), + to: transaction_path( + ExplorerWeb.Endpoint, + :show, + @locale, + @address.contracts_creation_internal_transaction.transaction_hash + ), + "data-test": "transaction_hash_link", + "class": "tile-title" + ) %> + + <% end %>
@@ -38,7 +67,6 @@ -