diff --git a/apps/explorer_web/lib/explorer_web/exchange_rates/usd.ex b/apps/explorer_web/lib/explorer_web/exchange_rates/usd.ex
index 49da8e9c0f..9cbcfc1a43 100644
--- a/apps/explorer_web/lib/explorer_web/exchange_rates/usd.ex
+++ b/apps/explorer_web/lib/explorer_web/exchange_rates/usd.ex
@@ -17,6 +17,12 @@ defmodule ExplorerWeb.ExchangeRates.USD do
alias Explorer.Chain.Wei
alias Explorer.ExchangeRates.Token
+ def from(nil), do: null()
+
+ def from(%Decimal{} = usd_decimal) do
+ %__MODULE__{value: usd_decimal}
+ end
+
def from(nil, _), do: null()
def from(_, nil), do: null()
diff --git a/apps/explorer_web/lib/explorer_web/templates/chain/show.html.eex b/apps/explorer_web/lib/explorer_web/templates/chain/show.html.eex
index 2860572a8d..756951f1a6 100644
--- a/apps/explorer_web/lib/explorer_web/templates/chain/show.html.eex
+++ b/apps/explorer_web/lib/explorer_web/templates/chain/show.html.eex
@@ -28,16 +28,16 @@
<%= gettext "Price" %>
- $<%= format_exchange_rate(@exchange_rate) %> <%= gettext "USD" %>
+ <%= format_exchange_rate(@exchange_rate) %>
<%= gettext "Market Cap" %>
- $<%= format_market_cap(@exchange_rate) %> <%= gettext "USD" %>
+ <%= format_market_cap(@exchange_rate) %>
<%= gettext "24h Volume" %>
- $<%= format_volume_24h(@exchange_rate) %> <%= gettext "USD" %>
+ <%= format_volume_24h(@exchange_rate) %>
diff --git a/apps/explorer_web/lib/explorer_web/views/address_view.ex b/apps/explorer_web/lib/explorer_web/views/address_view.ex
index 9377602581..0f1dc16c9a 100644
--- a/apps/explorer_web/lib/explorer_web/views/address_view.ex
+++ b/apps/explorer_web/lib/explorer_web/views/address_view.ex
@@ -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
diff --git a/apps/explorer_web/lib/explorer_web/views/chain_view.ex b/apps/explorer_web/lib/explorer_web/views/chain_view.ex
index 42d081905c..4e666c639e 100644
--- a/apps/explorer_web/lib/explorer_web/views/chain_view.ex
+++ b/apps/explorer_web/lib/explorer_web/views/chain_view.ex
@@ -2,6 +2,7 @@ defmodule ExplorerWeb.ChainView do
use ExplorerWeb, :view
alias Explorer.ExchangeRates.Token
+ alias ExplorerWeb.ExchangeRates.USD
def encode_market_history_data(market_history_data) do
market_history_data
@@ -13,20 +14,21 @@ defmodule ExplorerWeb.ChainView do
end
end
- def format_exchange_rate(%Token{usd_value: nil}), do: nil
-
def format_exchange_rate(%Token{usd_value: usd_value}) do
- Cldr.Number.to_string!(usd_value, fractional_digits: 6)
+ usd_value
+ |> USD.from()
+ |> format_usd_value()
end
def format_volume_24h(%Token{volume_24h_usd: volume_24h}) do
- format_number(volume_24h)
+ volume_24h
+ |> USD.from()
+ |> format_usd_value()
end
def format_market_cap(%Token{market_cap_usd: market_cap}) do
- format_number(market_cap)
+ market_cap
+ |> USD.from()
+ |> format_usd_value()
end
-
- defp format_number(nil), do: nil
- defp format_number(number), do: Cldr.Number.to_string!(number)
end
diff --git a/apps/explorer_web/lib/explorer_web/views/currency_helpers.ex b/apps/explorer_web/lib/explorer_web/views/currency_helpers.ex
index 57442b1ccb..859ae1bc81 100644
--- a/apps/explorer_web/lib/explorer_web/views/currency_helpers.ex
+++ b/apps/explorer_web/lib/explorer_web/views/currency_helpers.ex
@@ -14,10 +14,10 @@ defmodule ExplorerWeb.CurrencyHelpers do
## Examples
iex> format_usd_value(%USD{value: Decimal.new(5)})
- "$5 USD"
+ "$5.00 USD"
iex> format_usd_value(%USD{value: Decimal.new(5000)})
- "$5,000 USD"
+ "$5,000.00 USD"
iex> format_usd_value(%USD{value: Decimal.new(0.000005)})
"$0.000005 USD"
@@ -28,7 +28,7 @@ defmodule ExplorerWeb.CurrencyHelpers do
def format_usd_value(%USD{value: nil}), do: nil
def format_usd_value(%USD{value: value}) do
- case Number.to_string(value, format: "#,##0.##################") do
+ case Number.to_string(value, format: "#,##0.00################") do
{:ok, formatted} -> "$#{formatted} " <> gettext("USD")
_ -> nil
end
diff --git a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex
index ebff9ee4ec..a9136ae0bc 100644
--- a/apps/explorer_web/lib/explorer_web/views/transaction_view.ex
+++ b/apps/explorer_web/lib/explorer_web/views/transaction_view.ex
@@ -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
diff --git a/apps/explorer_web/lib/explorer_web/views/wei_helpers.ex b/apps/explorer_web/lib/explorer_web/views/wei_helpers.ex
index 74f6a5f227..edb6691de8 100644
--- a/apps/explorer_web/lib/explorer_web/views/wei_helpers.ex
+++ b/apps/explorer_web/lib/explorer_web/views/wei_helpers.ex
@@ -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
diff --git a/apps/explorer_web/priv/gettext/default.pot b/apps/explorer_web/priv/gettext/default.pot
index 9cd97eeb32..cbe266e6d6 100644
--- a/apps/explorer_web/priv/gettext/default.pot
+++ b/apps/explorer_web/priv/gettext/default.pot
@@ -330,12 +330,12 @@ msgstr ""
#: lib/explorer_web/templates/pending_transaction/index.html.eex:40
#: lib/explorer_web/templates/transaction/index.html.eex:38
#: lib/explorer_web/templates/transaction_internal_transaction/index.html.eex:30
-#: lib/explorer_web/views/wei_helpers.ex:84
+#: lib/explorer_web/views/wei_helpers.ex:71
msgid "Ether"
msgstr ""
#: lib/explorer_web/views/block_view.ex:22
-#: lib/explorer_web/views/wei_helpers.ex:83
+#: lib/explorer_web/views/wei_helpers.ex:70
msgid "Gwei"
msgstr ""
@@ -376,7 +376,7 @@ msgid "Type"
msgstr ""
#: lib/explorer_web/templates/transaction/overview.html.eex:145
-#: lib/explorer_web/views/wei_helpers.ex:82
+#: lib/explorer_web/views/wei_helpers.ex:69
msgid "Wei"
msgstr ""
@@ -422,9 +422,6 @@ msgstr ""
msgid "Price"
msgstr ""
-#: lib/explorer_web/templates/chain/show.html.eex:31
-#: lib/explorer_web/templates/chain/show.html.eex:36
-#: lib/explorer_web/templates/chain/show.html.eex:40
#: lib/explorer_web/views/currency_helpers.ex:32
msgid "USD"
msgstr ""
diff --git a/apps/explorer_web/priv/gettext/en/LC_MESSAGES/default.po b/apps/explorer_web/priv/gettext/en/LC_MESSAGES/default.po
index 1619d10355..8a986f4e12 100644
--- a/apps/explorer_web/priv/gettext/en/LC_MESSAGES/default.po
+++ b/apps/explorer_web/priv/gettext/en/LC_MESSAGES/default.po
@@ -342,12 +342,12 @@ msgstr ""
#: lib/explorer_web/templates/pending_transaction/index.html.eex:40
#: lib/explorer_web/templates/transaction/index.html.eex:38
#: lib/explorer_web/templates/transaction_internal_transaction/index.html.eex:30
-#: lib/explorer_web/views/wei_helpers.ex:84
+#: lib/explorer_web/views/wei_helpers.ex:71
msgid "Ether"
msgstr "POA"
#: lib/explorer_web/views/block_view.ex:22
-#: lib/explorer_web/views/wei_helpers.ex:83
+#: lib/explorer_web/views/wei_helpers.ex:70
msgid "Gwei"
msgstr ""
@@ -388,7 +388,7 @@ msgid "Type"
msgstr ""
#: lib/explorer_web/templates/transaction/overview.html.eex:145
-#: lib/explorer_web/views/wei_helpers.ex:82
+#: lib/explorer_web/views/wei_helpers.ex:69
msgid "Wei"
msgstr ""
@@ -434,9 +434,6 @@ msgstr ""
msgid "Price"
msgstr ""
-#: lib/explorer_web/templates/chain/show.html.eex:31
-#: lib/explorer_web/templates/chain/show.html.eex:36
-#: lib/explorer_web/templates/chain/show.html.eex:40
#: lib/explorer_web/views/currency_helpers.ex:32
msgid "USD"
msgstr ""
diff --git a/apps/explorer_web/test/explorer_web/exchange_rates/usd_test.exs b/apps/explorer_web/test/explorer_web/exchange_rates/usd_test.exs
index 2144e31ba3..d1781811e1 100644
--- a/apps/explorer_web/test/explorer_web/exchange_rates/usd_test.exs
+++ b/apps/explorer_web/test/explorer_web/exchange_rates/usd_test.exs
@@ -38,5 +38,11 @@ defmodule ExplorerWeb.ExchangeRates.USDTest do
assert %USD{value: Decimal.new(0.000005)} == USD.from(wei, token)
end
+
+ test "returns USD struct from decimal usd value" do
+ value = Decimal.new(0.000005)
+
+ assert %USD{value: ^value} = USD.from(value)
+ end
end
end
diff --git a/apps/explorer_web/test/explorer_web/features/viewing_addresses_test.exs b/apps/explorer_web/test/explorer_web/features/viewing_addresses_test.exs
index d157de021f..86688e02b2 100644
--- a/apps/explorer_web/test/explorer_web/features/viewing_addresses_test.exs
+++ b/apps/explorer_web/test/explorer_web/features/viewing_addresses_test.exs
@@ -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
diff --git a/apps/explorer_web/test/explorer_web/views/chain_view_test.exs b/apps/explorer_web/test/explorer_web/views/chain_view_test.exs
index 71cbf1646f..89c47e061e 100644
--- a/apps/explorer_web/test/explorer_web/views/chain_view_test.exs
+++ b/apps/explorer_web/test/explorer_web/views/chain_view_test.exs
@@ -1,3 +1,33 @@
defmodule ExplorerWeb.ChainViewTest do
use ExplorerWeb.ConnCase, async: true
+
+ alias Explorer.ExchangeRates.Token
+ alias ExplorerWeb.ChainView
+
+ describe "format_exchange_rate/1" do
+ test "returns a formatted usd value from a `Token`'s usd_value" do
+ token = %Token{usd_value: Decimal.new(5.45)}
+
+ assert "$5.45 USD" == ChainView.format_exchange_rate(token)
+ assert nil == ChainView.format_exchange_rate(%Token{usd_value: nil})
+ end
+ end
+
+ describe "format_volume_24h/1" do
+ test "returns a formatted usd value from a `Token`'s volume_24h_usd" do
+ token = %Token{volume_24h_usd: Decimal.new(5.456)}
+
+ assert "$5.456 USD" == ChainView.format_volume_24h(token)
+ assert nil == ChainView.format_volume_24h(%Token{volume_24h_usd: nil})
+ end
+ end
+
+ describe "format_market_cap/1" do
+ test "returns a formatted usd value from a `Token`'s market_cap_usd" do
+ token = %Token{market_cap_usd: Decimal.new(5.4)}
+
+ assert "$5.40 USD" == ChainView.format_market_cap(token)
+ assert nil == ChainView.format_market_cap(%Token{market_cap_usd: nil})
+ end
+ end
end
diff --git a/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs b/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs
index 67dffb9547..0e3b2d1fc7 100644
--- a/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs
+++ b/apps/explorer_web/test/explorer_web/views/transaction_view_test.exs
@@ -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