From 920925ccdc4e8858c1a79bbf419365e75e0eb837 Mon Sep 17 00:00:00 2001 From: Lucas Narciso Date: Fri, 2 Nov 2018 01:41:52 -0300 Subject: [PATCH] Fix address[] display when reading Smart Contracts --- .../templates/smart_contract/_functions.html.eex | 4 ++-- .../lib/block_scout_web/views/smart_contract_view.ex | 6 ++++++ .../views/tokens/smart_contract_view_test.exs | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_functions.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_functions.html.eex index e5f193652a..3008b37cf8 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_functions.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/smart_contract/_functions.html.eex @@ -45,13 +45,13 @@
<%= output["value"] %> - + <%= gettext("WEI")%> <%= gettext("ETH")%>
<% else %> - <%= output["value"] %> + <%= values(output["value"], output["type"]) %> <% end %> <% 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 0d499104be..c96c5efe91 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,12 @@ defmodule BlockScoutWeb.SmartContractView do def named_argument?(%{"name" => _}), do: true def named_argument?(_), do: false + def values(addresses, type) when type == "address[]" do + addresses + |> Enum.map(&values(&1, "address")) + |> Enum.join(", ") + end + def values(value, type) when type in ["address", "address payable"] do {:ok, address} = Explorer.Chain.Hash.Address.cast(value) to_string(address) 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 503a206c25..5ab8133bf5 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 @@ -80,6 +80,16 @@ defmodule BlockScoutWeb.SmartContractViewTest do assert SmartContractView.values(value, "address payable") == "0x5f26097334b6a32b7951df61fd0c5803ec5d8354" end + test "convert each value to string and join them when receiving 'address[]' as the type" do + value = [ + <<95, 38, 9, 115, 52, 182, 163, 43, 121, 81, 223, 97, 253, 12, 88, 3, 236, 93, 131, 84>>, + <<207, 38, 14, 163, 23, 85, 86, 55, 197, 95, 112, 229, 93, 186, 141, 90, 216, 65, 76, 176>> + ] + + assert SmartContractView.values(value, "address[]") == + "0x5f26097334b6a32b7951df61fd0c5803ec5d8354, 0xcf260ea317555637c55f70e55dba8d5ad8414cb0" + end + test "returns the value when the type is neither 'address' nor 'address payable'" do value = "POA"