Move tab controlling functions to Tokens/OverviewView

pull/774/head
Amanda Sposito 6 years ago
parent d3c47e2f86
commit 78ba601dd4
  1. 32
      apps/block_scout_web/lib/block_scout_web/templates/tokens/overview/_tabs.html.eex
  2. 11
      apps/block_scout_web/lib/block_scout_web/views/tokens/overview_view.ex
  3. 9
      apps/block_scout_web/lib/block_scout_web/views/tokens/transfer_view.ex
  4. 58
      apps/block_scout_web/test/block_scout_web/views/tokens/overview_view_test.exs
  5. 58
      apps/block_scout_web/test/block_scout_web/views/tokens/transfer_view_test.exs

@ -8,15 +8,6 @@
) %>
</li>
<%= if TransferView.smart_contract_with_read_only_functions?(@token) do %>
<li class="nav-item">
<%= link(
gettext("Read Contract"),
to: token_read_contract_path(@conn, :index, @token.contract_address_hash),
class: "nav-link #{tab_status("read_contract", @conn.request_path)}")%>
</li>
<% end %>
<li class="nav-item">
<%= link(
gettext("Token Holders"),
@ -35,6 +26,15 @@
) %>
</li>
<% end %>
<%= if smart_contract_with_read_only_functions?(@token) do %>
<li class="nav-item">
<%= link(
gettext("Read Contract"),
to: token_read_contract_path(@conn, :index, @token.contract_address_hash),
class: "nav-link #{tab_status("read_contract", @conn.request_path)}")%>
</li>
<% end %>
</ul>
<!-- MOBILE DROPDOWN NAV -->
@ -55,12 +55,7 @@
class: "dropdown-item #{tab_status("token_transfers", @conn.request_path)}",
to: token_path(@conn, :show, @token.contract_address_hash)
) %>
<%= if TransferView.smart_contract_with_read_only_functions?(@token) do %>
<%= link(
gettext("Read Contract"),
to: "#",
class: "dropdown-item #{tab_status("read_contract", @conn.request_path)}")%>
<% end %>
<%= link(
gettext("Token Holders"),
class: "dropdown-item #{tab_status("token_holders", @conn.request_path)}",
@ -74,6 +69,13 @@
to: token_inventory_path(@conn, :index, @token.contract_address_hash)
) %>
<% end %>
<%= if smart_contract_with_read_only_functions?(@token) do %>
<%= link(
gettext("Read Contract"),
to: "#",
class: "dropdown-item #{tab_status("read_contract", @conn.request_path)}")%>
<% end %>
</div>
</li>
</ul>

@ -1,8 +1,7 @@
defmodule BlockScoutWeb.Tokens.OverviewView do
use BlockScoutWeb, :view
alias Explorer.Chain.Token
alias BlockScoutWeb.Tokens.TransferView
alias Explorer.Chain.{Address, SmartContract, Token}
@tabs ["token_transfers", "token_holders", "read_contract", "inventory"]
@ -38,4 +37,12 @@ defmodule BlockScoutWeb.Tokens.OverviewView do
def display_inventory?(%Token{type: "ERC-721"}), do: true
def display_inventory?(_), do: false
def smart_contract_with_read_only_functions?(
%Token{contract_address: %Address{smart_contract: %SmartContract{}}} = token
) do
Enum.any?(token.contract_address.smart_contract.abi, & &1["constant"])
end
def smart_contract_with_read_only_functions?(%Token{contract_address: %Address{smart_contract: nil}}), do: false
end

@ -1,14 +1,5 @@
defmodule BlockScoutWeb.Tokens.TransferView do
use BlockScoutWeb, :view
alias Explorer.Chain.{Address, SmartContract, Token}
alias BlockScoutWeb.Tokens.OverviewView
def smart_contract_with_read_only_functions?(
%Token{contract_address: %Address{smart_contract: %SmartContract{}}} = token
) do
Enum.any?(token.contract_address.smart_contract.abi, & &1["constant"])
end
def smart_contract_with_read_only_functions?(%Token{contract_address: %Address{smart_contract: nil}}), do: false
end

@ -78,4 +78,62 @@ defmodule BlockScoutWeb.Tokens.OverviewViewTest do
assert OverviewView.display_inventory?(token) == false
end
end
describe "smart_contract_with_read_only_functions?/1" do
test "returns true when abi has read only functions" do
smart_contract =
insert(
:smart_contract,
abi: [
%{
"constant" => true,
"inputs" => [],
"name" => "get",
"outputs" => [%{"name" => "", "type" => "uint256"}],
"payable" => false,
"stateMutability" => "view",
"type" => "function"
}
]
)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
assert OverviewView.smart_contract_with_read_only_functions?(token)
end
test "returns false when there is no read only functions" do
smart_contract =
insert(
:smart_contract,
abi: [
%{
"constant" => false,
"inputs" => [%{"name" => "x", "type" => "uint256"}],
"name" => "set",
"outputs" => [],
"payable" => false,
"stateMutability" => "nonpayable",
"type" => "function"
}
]
)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
refute OverviewView.smart_contract_with_read_only_functions?(token)
end
test "returns false when smart contract is not verified" do
address = insert(:address, smart_contract: nil)
token = insert(:token, contract_address: address)
refute OverviewView.smart_contract_with_read_only_functions?(token)
end
end
end

@ -2,62 +2,4 @@ defmodule BlockScoutWeb.Tokens.TransferViewTest do
use BlockScoutWeb.ConnCase, async: true
alias BlockScoutWeb.Tokens.TransferView
describe "smart_contract_with_read_only_functions?/1" do
test "returns true when abi has read only functions" do
smart_contract =
insert(
:smart_contract,
abi: [
%{
"constant" => true,
"inputs" => [],
"name" => "get",
"outputs" => [%{"name" => "", "type" => "uint256"}],
"payable" => false,
"stateMutability" => "view",
"type" => "function"
}
]
)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
assert TransferView.smart_contract_with_read_only_functions?(token)
end
test "returns false when there is no read only functions" do
smart_contract =
insert(
:smart_contract,
abi: [
%{
"constant" => false,
"inputs" => [%{"name" => "x", "type" => "uint256"}],
"name" => "set",
"outputs" => [],
"payable" => false,
"stateMutability" => "nonpayable",
"type" => "function"
}
]
)
address = insert(:address, smart_contract: smart_contract)
token = insert(:token, contract_address: address)
refute TransferView.smart_contract_with_read_only_functions?(token)
end
test "returns false when smart contract is not verified" do
address = insert(:address, smart_contract: nil)
token = insert(:token, contract_address: address)
refute TransferView.smart_contract_with_read_only_functions?(token)
end
end
end

Loading…
Cancel
Save