From 515f29330f810667b61fdeb8bd9f41b3dd841fc1 Mon Sep 17 00:00:00 2001 From: nikitosing Date: Mon, 26 Jul 2021 16:10:19 +0700 Subject: [PATCH] Fix error response in contract's output --- CHANGELOG.md | 1 + .../smart_contract/_function_response.html.eex | 14 +++++++++----- .../block_scout_web/views/smart_contract_view.ex | 2 ++ .../explorer/lib/explorer/smart_contract/reader.ex | 11 ++++++++--- .../test/explorer/smart_contract/reader_test.exs | 6 ++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e8d0db64a..79189a3add 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#4401](https://github.com/blockscout/blockscout/pull/4401) - Fix displaying of token holders with the same amount ### Chore +- [#4439](https://github.com/blockscout/blockscout/pull/4439) - Fix revert response in contract's output ## 3.7.2-beta diff --git a/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_function_response.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_function_response.html.eex index a6110cc3d3..3f82cef96f 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_function_response.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_function_response.html.eex @@ -1,10 +1,14 @@
 [ <%= @function_name %> method Response ]
-
-[<%= for item <- @outputs do %>
-<%= if named_argument?(item) do %><%= item["name"] %><% end %>
-<%= raw(values_with_type(item["value"], item["type"])) %>
-<% end %>]
+<%= case @outputs do %>
+    <% {:error, message} -> %>
+        <%=raw(values_with_type(message, :error))%>
+    <% _ -> %>
+        [<%= for item <- @outputs do %>
+            <%= if named_argument?(item) do %><%= item["name"] %><% end %>
+            <%= raw(values_with_type(item["value"], item["type"])) %>
+        <% end %>]
+<% end %>
 
diff --git a/apps/block_scout_web/lib/block_scout_web/views/smart_contract_view.ex b/apps/block_scout_web/lib/block_scout_web/views/smart_contract_view.ex index d75ba3a24e..2705acf2de 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/smart_contract_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/smart_contract_view.ex @@ -100,6 +100,8 @@ defmodule BlockScoutWeb.SmartContractView do def values_with_type(value, :bool, _components), do: render_type_value("bool", to_string(value)) + def values_with_type(value, :error, _components), do: render_type_value("error", value) + def values_with_type(value, type, _components) do render_type_value(type, binary_to_utf_string(value)) end diff --git a/apps/explorer/lib/explorer/smart_contract/reader.ex b/apps/explorer/lib/explorer/smart_contract/reader.ex index 1a36a35ce6..7c244cb7f4 100644 --- a/apps/explorer/lib/explorer/smart_contract/reader.ex +++ b/apps/explorer/lib/explorer/smart_contract/reader.ex @@ -450,10 +450,15 @@ defmodule Explorer.SmartContract.Reader do def link_outputs_and_values(blockchain_values, outputs, method_id) do default_value = Enum.map(outputs, fn _ -> "" end) - {_, value} = Map.get(blockchain_values, method_id, {:ok, default_value}) - for {output, index} <- Enum.with_index(outputs) do - new_value(output, List.wrap(value), index) + case Map.get(blockchain_values, method_id, {:ok, default_value}) do + {:ok, value} -> + for {output, index} <- Enum.with_index(outputs) do + new_value(output, List.wrap(value), index) + end + + {:error, message} -> + {:error, message} end end diff --git a/apps/explorer/test/explorer/smart_contract/reader_test.exs b/apps/explorer/test/explorer/smart_contract/reader_test.exs index d6b4ce5cc5..3b9fb2d053 100644 --- a/apps/explorer/test/explorer/smart_contract/reader_test.exs +++ b/apps/explorer/test/explorer/smart_contract/reader_test.exs @@ -357,6 +357,12 @@ defmodule Explorer.SmartContract.ReaderTest do } ] = Reader.link_outputs_and_values(blockchain_values, outputs, function_name) end + + test "save error message" do + blockchain_values = %{"check" => {:error, "Reverted"}} + + assert {:error, "Reverted"} == Reader.link_outputs_and_values(blockchain_values, [], "check") + end end defp blockchain_get_function_mock do