Fix token holders' tab at the Token's page

This page was breaking when the given Token has the total supply nil.

To fix that, we added a view function to check if we should show the
total supply percentage. Since we want to display just for Tokens that
have total supply, we are ignoring for the ones that don't have.
pull/791/head
Felipe Renan 6 years ago
parent 3a2c6f93ed
commit f3b49958ec
  1. 2
      apps/block_scout_web/lib/block_scout_web/templates/tokens/holder/_token_balances.html.eex
  2. 18
      apps/block_scout_web/lib/block_scout_web/views/tokens/holder_view.ex
  3. 20
      apps/block_scout_web/test/block_scout_web/views/tokens/holder_view_test.exs

@ -10,7 +10,7 @@
<%= format_token_balance_value(@token_balance.value, @token) %> <%= @token.symbol %>
</span>
<%= if @token.total_supply > 0 do %>
<%= if show_total_supply_percentage?(@token.total_supply) do %>
(<%= total_supply_percentage(@token_balance.value, @token.total_supply) %>)
<% end %>
</span>

@ -4,6 +4,24 @@ defmodule BlockScoutWeb.Tokens.HolderView do
alias BlockScoutWeb.Tokens.{OverviewView, TokenView}
alias Explorer.Chain.{Token}
@doc """
Checks if the total supply percentage must be shown.
## Examples
iex> BlockScoutWeb.Tokens.HolderView.show_total_supply_percentage?(nil)
false
iex> BlockScoutWeb.Tokens.HolderView.show_total_supply_percentage?(0)
false
iex> BlockScoutWeb.Tokens.HolderView.show_total_supply_percentage?(100)
true
"""
def show_total_supply_percentage?(nil), do: false
def show_total_supply_percentage?(total_supply), do: total_supply > 0
@doc """
Calculates the percentage of the value from the given total supply.

@ -6,6 +6,26 @@ defmodule BlockScoutWeb.Tokens.HolderViewTest do
doctest BlockScoutWeb.Tokens.HolderView, import: true
describe "show_total_supply_percentage?/1" do
test "returns false when the total supply is nil" do
%Token{total_supply: total_supply} = build(:token, total_supply: nil)
refute HolderView.show_total_supply_percentage?(total_supply)
end
test "returns false when the total supply is 0" do
%Token{total_supply: total_supply} = build(:token, total_supply: 0)
refute HolderView.show_total_supply_percentage?(total_supply)
end
test "returns true when the total supply is greater than 0" do
%Token{total_supply: total_supply} = build(:token, total_supply: 1000)
assert HolderView.show_total_supply_percentage?(total_supply)
end
end
describe "total_supply_percentage/2" do
test "returns the percentage of the Token total supply" do
%Token{total_supply: total_supply} = build(:token, total_supply: 1000)

Loading…
Cancel
Save