Use custom format for ether/gwei/wei display to remove decimal commas

Old:
0.000,005,000,000,000,000 POA

New:
0.000005 POA

Co-authored-by: tmecklem <timothy@mecklem.com>
pull/202/head
jimmay5469 7 years ago
parent 67b61f98e2
commit 0246dc45db
  1. 2
      apps/explorer_web/lib/explorer_web/views/address_view.ex
  2. 2
      apps/explorer_web/lib/explorer_web/views/transaction_view.ex
  3. 31
      apps/explorer_web/lib/explorer_web/views/wei_helpers.ex
  4. 2
      apps/explorer_web/test/explorer_web/features/viewing_addresses_test.exs
  5. 4
      apps/explorer_web/test/explorer_web/views/transaction_view_test.exs

@ -13,7 +13,7 @@ defmodule ExplorerWeb.AddressView do
Returns a formatted address balance and includes the unit.
"""
def balance(%Address{fetched_balance: balance}) do
format_wei_value(balance, :ether, fractional_digits: 18)
format_wei_value(balance, :ether)
end
def formatted_usd(%Address{fetched_balance: nil}, _), do: nil

@ -34,7 +34,7 @@ defmodule ExplorerWeb.TransactionView do
end
defp fee_to_currency({fee_type, fee}, denomination: denomination) do
{fee_type, format_wei_value(Wei.from(fee, :wei), denomination, fractional_digits: 18)}
{fee_type, format_wei_value(Wei.from(fee, :wei), denomination)}
end
defp fee_to_currency({fee_type, fee}, exchange_rate: %Token{} = exchange_rate) do

@ -9,7 +9,7 @@ defmodule ExplorerWeb.WeiHelpers do
@valid_units ~w(wei gwei ether)a
@type format_option :: {:fractional_digits, pos_integer()} | {:include_unit_label, boolean()}
@type format_option :: {:include_unit_label, boolean()}
@type format_options :: [format_option()]
@ -22,7 +22,6 @@ defmodule ExplorerWeb.WeiHelpers do
The third argument allows for keyword options to be passed for formatting the
converted number.
* `:fractional_digits` - Integer. Number of fractional digits to include
* `:include_unit_label` - Boolean (Defaults to `true`). Flag for if the unit
label should be included in the returned string
@ -40,11 +39,10 @@ defmodule ExplorerWeb.WeiHelpers do
# With formatting options
iex> format_wei_value(
...> %Wei{value: Decimal.new(1)},
...> :wei,
...> fractional_digits: 3
...> %Wei{value: Decimal.new(1000500000000000000)},
...> :ether
...> )
"1.000 Wei"
"1.0005 POA"
iex> format_wei_value(
...> %Wei{value: Decimal.new(10)},
@ -55,12 +53,10 @@ defmodule ExplorerWeb.WeiHelpers do
"""
@spec format_wei_value(Wei.t(), Wei.unit(), format_options()) :: String.t()
def format_wei_value(%Wei{} = wei, unit, options \\ []) when unit in @valid_units do
number_format_options = build_number_format_options(options)
converted_value =
wei
|> Wei.to(unit)
|> Cldr.Number.to_string!(number_format_options)
|> Cldr.Number.to_string!(format: "#,##0.##################")
if Keyword.get(options, :include_unit_label, true) do
display_unit = display_unit(unit)
@ -70,24 +66,7 @@ defmodule ExplorerWeb.WeiHelpers do
end
end
defp build_number_format_options(options) do
Enum.reduce(options, [], fn option, formatted_options ->
case parse_number_format_option(option) do
nil -> formatted_options
{key, value} -> Keyword.put(formatted_options, key, value)
end
end)
end
defp display_unit(:wei), do: gettext("Wei")
defp display_unit(:gwei), do: gettext("Gwei")
defp display_unit(:ether), do: gettext("Ether")
defguardp is_fractional_digit(digits) when is_integer(digits) and digits > 0
defp parse_number_format_option({:fractional_digits, digits}) when is_fractional_digit(digits) do
{:fractional_digits, digits}
end
defp parse_number_format_option(_), do: nil
end

@ -41,7 +41,7 @@ defmodule ExplorerWeb.ViewingAddressesTest do
session
|> AddressPage.visit_page(address)
|> assert_text(AddressPage.balance(), "0.000,000,000,000,000,500 POA")
|> assert_text(AddressPage.balance(), "0.0000000000000005 POA")
end
describe "viewing transactions" do

@ -20,7 +20,7 @@ defmodule ExplorerWeb.TransactionViewTest do
token = %Token{usd_value: Decimal.new(0.50)}
expected_value = "<= 0.009,000,000,000,000,000 POA"
expected_value = "<= 0.009 POA"
assert expected_value == TransactionView.formatted_fee(transaction, denomination: :ether)
assert "<= $0.0045 USD" == TransactionView.formatted_fee(transaction, exchange_rate: token)
end
@ -31,7 +31,7 @@ defmodule ExplorerWeb.TransactionViewTest do
transaction = build(:transaction, gas_price: gas_price, receipt: receipt)
token = %Token{usd_value: Decimal.new(0.50)}
expected_value = "0.003,102,702,000,000,000 POA"
expected_value = "0.003102702 POA"
assert expected_value == TransactionView.formatted_fee(transaction, denomination: :ether)
assert "$0.001551351 USD" == TransactionView.formatted_fee(transaction, exchange_rate: token)
end

Loading…
Cancel
Save