From c1b1f69be4e83f825a55b15c21e43968a3dd80d9 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 9 Jul 2019 14:57:33 +0300 Subject: [PATCH] show external libraries in UI --- .../templates/address_contract/index.html.eex | 4 ++-- .../views/address_contract_view.ex | 6 ++++++ apps/explorer/lib/explorer/chain.ex | 9 +++++++-- .../lib/explorer/smart_contract/publisher.ex | 19 +++++++++++++++---- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex index 505413aa96..9d229916b4 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_contract/index.html.eex @@ -52,13 +52,13 @@
<%= @address.smart_contract.constructor_arguments %>
<% end %> - <%= if @address.smart_contract.external_libraries do %> + <%= if @address.smart_contract.external_libraries && @address.smart_contract.external_libraries != [] do %>

<%= gettext "External libraries" %>

-
<%= format_smart_contract_abi(@address.smart_contract.abi) %>
+                
<%= format_external_libraries(@address.smart_contract.external_libraries) %>
                 
diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex index 4dc99a2a68..5de263f820 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_contract_view.ex @@ -21,6 +21,12 @@ defmodule BlockScoutWeb.AddressContractView do def format_optimization_text(true), do: gettext("true") def format_optimization_text(false), do: gettext("false") + def format_external_libraries(libraries) do + Enum.reduce(libraries, "", fn %{name: name, address_hash: address_hash}, acc -> + acc <> name <> " : " <> address_hash <> "\n" + end) + end + def contract_lines_with_index(contract_source_code) do contract_lines = String.split(contract_source_code, "\n") diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index fcad694a58..93dca921b9 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2394,8 +2394,13 @@ defmodule Explorer.Chain do naming the address for reference. """ @spec create_smart_contract(map()) :: {:ok, SmartContract.t()} | {:error, Ecto.Changeset.t()} - def create_smart_contract(attrs \\ %{}) do - smart_contract_changeset = SmartContract.changeset(%SmartContract{}, attrs) + def create_smart_contract(attrs \\ %{}, external_libraries \\ []) do + new_contract = %SmartContract{} + + smart_contract_changeset = + new_contract + |> SmartContract.changeset(attrs) + |> Changeset.put_change(:external_libraries, external_libraries) insert_result = Multi.new() diff --git a/apps/explorer/lib/explorer/smart_contract/publisher.ex b/apps/explorer/lib/explorer/smart_contract/publisher.ex index 42ce646fb1..ae5a24c088 100644 --- a/apps/explorer/lib/explorer/smart_contract/publisher.ex +++ b/apps/explorer/lib/explorer/smart_contract/publisher.ex @@ -36,9 +36,9 @@ defmodule Explorer.SmartContract.Publisher do end defp publish_smart_contract(address_hash, params, abi) do - address_hash - |> attributes(params, abi) - |> Chain.create_smart_contract() + attrs = address_hash |> attributes(params, abi) + + Chain.create_smart_contract(attrs, attrs.external_libraries) end defp unverified_smart_contract(address_hash, params, error) do @@ -64,6 +64,8 @@ defmodule Explorer.SmartContract.Publisher do nil end + prepared_external_libraries = prepare_external_libraies(params["external_libraries"]) + %{ address_hash: address_hash, name: params["name"], @@ -73,11 +75,20 @@ defmodule Explorer.SmartContract.Publisher do optimization: params["optimization"], contract_source_code: params["contract_source_code"], constructor_arguments: clean_constructor_arguments, - external_libaries: params["external_libraries"], + external_libraries: prepared_external_libraries, abi: abi } end + defp prepare_external_libraies(nil), do: [] + + defp prepare_external_libraies(map) do + map + |> Enum.map(fn {key, value} -> + %{name: key, address_hash: value} + end) + end + defp add_external_libraries(params, external_libraries) do clean_external_libraries = Enum.reduce(1..5, %{}, fn number, acc ->