From 97ec31e1167e18639b0dde74163fdc0c437435dd Mon Sep 17 00:00:00 2001 From: Gustavo Santos Ferreira Date: Tue, 30 Oct 2018 17:33:26 -0300 Subject: [PATCH 1/3] convert to address and then string when solidity type address is received --- .../templates/smart_contract/_function_response.html.eex | 2 +- .../lib/block_scout_web/views/smart_contract_view.ex | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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 31e629f771..c2f47607c3 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 @@ -5,6 +5,6 @@ [<%= for item <- @outputs do %> <%= if named_argument?(item) do %><%= item["name"] %> <% end %> -(<%= item["type"] %>) : <%= values(item["value"]) %><% end %>] +(<%= item["type"] %>) : <%= values(item["value"], item["type"]) %><% 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 a63d0ef0c9..f8c9fa1236 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 @@ -10,6 +10,11 @@ defmodule BlockScoutWeb.SmartContractView do def named_argument?(%{"name" => _}), do: true def named_argument?(_), do: false - def values(values) when is_list(values), do: Enum.join(values, ",") - def values(value), do: value + def values(value, "address") do + {:ok, address} = Explorer.Chain.Hash.Address.cast(value) + to_string(address) + end + + def values(values, _) when is_list(values), do: Enum.join(values, ",") + def values(value, _), do: value end From 1bbca2604ddab7fcd09d06be13fa19430de0329e Mon Sep 17 00:00:00 2001 From: Gustavo Santos Ferreira Date: Tue, 30 Oct 2018 17:36:27 -0300 Subject: [PATCH 2/3] also support address payable type --- .../lib/block_scout_web/views/smart_contract_view.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 f8c9fa1236..0d499104be 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 @@ -3,14 +3,14 @@ defmodule BlockScoutWeb.SmartContractView do def queryable?(inputs), do: Enum.any?(inputs) - def address?(type), do: type == "address" + def address?(type), do: type in ["address", "address payable"] def named_argument?(%{"name" => ""}), do: false def named_argument?(%{"name" => nil}), do: false def named_argument?(%{"name" => _}), do: true def named_argument?(_), do: false - def values(value, "address") do + def values(value, type) when type in ["address", "address payable"] do {:ok, address} = Explorer.Chain.Hash.Address.cast(value) to_string(address) end From e5c9aaa2df8bff5aea99cbe1246eabd92006ca9a Mon Sep 17 00:00:00 2001 From: Gustavo Santos Ferreira Date: Tue, 30 Oct 2018 18:03:53 -0300 Subject: [PATCH 3/3] add tests --- .../views/tokens/smart_contract_view_test.exs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/views/tokens/smart_contract_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/tokens/smart_contract_view_test.exs index 281ab779dc..503a206c25 100644 --- a/apps/block_scout_web/test/block_scout_web/views/tokens/smart_contract_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/tokens/smart_contract_view_test.exs @@ -24,6 +24,12 @@ defmodule BlockScoutWeb.SmartContractViewTest do assert SmartContractView.address?(type) end + test "returns true when the type is equal to the string 'address payable'" do + type = "address payable" + + assert SmartContractView.address?(type) + end + test "returns false when the type is not equal the string 'address'" do type = "name" @@ -57,17 +63,27 @@ defmodule BlockScoutWeb.SmartContractViewTest do end end - describe "values/1" do - test "joins the values when it is a list" do + describe "values/2" do + test "joins the values when it is a list of a given type" do values = [8, 6, 9, 2, 2, 37] - assert SmartContractView.values(values) == "8,6,9,2,2,37" + assert SmartContractView.values(values, "type") == "8,6,9,2,2,37" + end + + test "convert the value to string receiving a value and the 'address' type" do + value = <<95, 38, 9, 115, 52, 182, 163, 43, 121, 81, 223, 97, 253, 12, 88, 3, 236, 93, 131, 84>> + assert SmartContractView.values(value, "address") == "0x5f26097334b6a32b7951df61fd0c5803ec5d8354" + end + + test "convert the value to string receiving a value and the 'address payable' type" do + value = <<95, 38, 9, 115, 52, 182, 163, 43, 121, 81, 223, 97, 253, 12, 88, 3, 236, 93, 131, 84>> + assert SmartContractView.values(value, "address payable") == "0x5f26097334b6a32b7951df61fd0c5803ec5d8354" end - test "returns the value" do + test "returns the value when the type is neither 'address' nor 'address payable'" do value = "POA" - assert SmartContractView.values(value) == "POA" + assert SmartContractView.values(value, "not address") == "POA" end end end