diff --git a/apps/block_scout_web/lib/block_scout_web/views/chain_view.ex b/apps/block_scout_web/lib/block_scout_web/views/chain_view.ex
index e328453b19..7f49d2d239 100644
--- a/apps/block_scout_web/lib/block_scout_web/views/chain_view.ex
+++ b/apps/block_scout_web/lib/block_scout_web/views/chain_view.ex
@@ -2,14 +2,4 @@ defmodule BlockScoutWeb.ChainView do
use BlockScoutWeb, :view
alias BlockScoutWeb.LayoutView
-
- def encode_market_history_data(market_history_data) do
- market_history_data
- |> Enum.map(fn day -> Map.take(day, [:closing_price, :date]) end)
- |> Jason.encode()
- |> case do
- {:ok, data} -> data
- _ -> []
- end
- end
end
diff --git a/apps/block_scout_web/priv/gettext/default.pot b/apps/block_scout_web/priv/gettext/default.pot
index afe4821af4..21dbeaf767 100644
--- a/apps/block_scout_web/priv/gettext/default.pot
+++ b/apps/block_scout_web/priv/gettext/default.pot
@@ -118,7 +118,7 @@ msgid "All"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:27
+#: lib/block_scout_web/templates/chain/show.html.eex:37
msgid "Average block time"
msgstr ""
@@ -175,7 +175,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:57
+#: lib/block_scout_web/templates/chain/show.html.eex:67
#: lib/block_scout_web/templates/layout/_topnav.html.eex:16
#: lib/block_scout_web/templates/layout/_topnav.html.eex:20
msgid "Blocks"
@@ -540,7 +540,7 @@ msgid "Logs"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:18
+#: lib/block_scout_web/templates/chain/show.html.eex:28
#: lib/block_scout_web/templates/layout/app.html.eex:49
#: lib/block_scout_web/views/address_view.ex:117
msgid "Market Cap"
@@ -574,7 +574,7 @@ msgid "More internal transactions have come in"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:81
+#: lib/block_scout_web/templates/chain/show.html.eex:91
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:14
#: lib/block_scout_web/templates/transaction/index.html.eex:14
msgid "More transactions have come in"
@@ -703,7 +703,7 @@ msgid "Position %{index}"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:11
+#: lib/block_scout_web/templates/chain/show.html.eex:21
#: lib/block_scout_web/templates/layout/app.html.eex:50
msgid "Price"
msgstr ""
@@ -951,7 +951,7 @@ msgid "Total Supply"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:35
+#: lib/block_scout_web/templates/chain/show.html.eex:45
msgid "Total transactions"
msgstr ""
@@ -985,7 +985,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/block_transaction/index.html.eex:26
#: lib/block_scout_web/templates/block_transaction/index.html.eex:35
-#: lib/block_scout_web/templates/chain/show.html.eex:78
+#: lib/block_scout_web/templates/chain/show.html.eex:88
#: lib/block_scout_web/templates/layout/_topnav.html.eex:35
#: lib/block_scout_web/views/address_view.ex:253
msgid "Transactions"
@@ -1060,12 +1060,12 @@ msgid "Verify & publish"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:56
+#: lib/block_scout_web/templates/chain/show.html.eex:66
msgid "View All Blocks →"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:77
+#: lib/block_scout_web/templates/chain/show.html.eex:87
msgid "View All Transactions →"
msgstr ""
@@ -1105,7 +1105,7 @@ msgid "WEI"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:43
+#: lib/block_scout_web/templates/chain/show.html.eex:53
msgid "Wallet addresses"
msgstr ""
@@ -1189,8 +1189,8 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:19
#: lib/block_scout_web/templates/address_validation/index.html.eex:63
#: lib/block_scout_web/templates/address_validation/index.html.eex:82
-#: lib/block_scout_web/templates/chain/show.html.eex:69
-#: lib/block_scout_web/templates/chain/show.html.eex:95
+#: lib/block_scout_web/templates/chain/show.html.eex:79
+#: lib/block_scout_web/templates/chain/show.html.eex:105
#: lib/block_scout_web/templates/tokens/read_contract/index.html.eex:25
msgid "Loading..."
msgstr ""
@@ -1416,7 +1416,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:26
#: lib/block_scout_web/templates/address_transaction/index.html.eex:55
#: lib/block_scout_web/templates/address_validation/index.html.eex:70
-#: lib/block_scout_web/templates/chain/show.html.eex:61
+#: lib/block_scout_web/templates/chain/show.html.eex:71
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:23
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:23
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:21
@@ -1458,6 +1458,7 @@ msgstr ""
#, elixir-format
#: lib/block_scout_web/templates/address_coin_balance/index.html.eex:24
+#: lib/block_scout_web/templates/chain/show.html.eex:11
msgid "Loading chart"
msgstr ""
@@ -1468,6 +1469,7 @@ msgstr ""
#, elixir-format
#: lib/block_scout_web/templates/address_coin_balance/index.html.eex:27
+#: lib/block_scout_web/templates/chain/show.html.eex:14
msgid "There was a problem loading the chart."
msgstr ""
@@ -1537,6 +1539,6 @@ msgid "Emission Contract"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:87
+#: lib/block_scout_web/templates/chain/show.html.eex:97
msgid "Something went wrong, click to retry."
msgstr ""
diff --git a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
index 41d0f4e0c2..85bdad66cb 100644
--- a/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
+++ b/apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
@@ -118,7 +118,7 @@ msgid "All"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:27
+#: lib/block_scout_web/templates/chain/show.html.eex:37
msgid "Average block time"
msgstr ""
@@ -175,7 +175,7 @@ msgid "BlockScout provides analytics data, API, and Smart Contract tools for the
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:57
+#: lib/block_scout_web/templates/chain/show.html.eex:67
#: lib/block_scout_web/templates/layout/_topnav.html.eex:16
#: lib/block_scout_web/templates/layout/_topnav.html.eex:20
msgid "Blocks"
@@ -540,7 +540,7 @@ msgid "Logs"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:18
+#: lib/block_scout_web/templates/chain/show.html.eex:28
#: lib/block_scout_web/templates/layout/app.html.eex:49
#: lib/block_scout_web/views/address_view.ex:117
msgid "Market Cap"
@@ -574,7 +574,7 @@ msgid "More internal transactions have come in"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:81
+#: lib/block_scout_web/templates/chain/show.html.eex:91
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:14
#: lib/block_scout_web/templates/transaction/index.html.eex:14
msgid "More transactions have come in"
@@ -703,7 +703,7 @@ msgid "Position %{index}"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:11
+#: lib/block_scout_web/templates/chain/show.html.eex:21
#: lib/block_scout_web/templates/layout/app.html.eex:50
msgid "Price"
msgstr ""
@@ -951,7 +951,7 @@ msgid "Total Supply"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:35
+#: lib/block_scout_web/templates/chain/show.html.eex:45
msgid "Total transactions"
msgstr ""
@@ -985,7 +985,7 @@ msgstr ""
#: lib/block_scout_web/templates/block_transaction/index.html.eex:23
#: lib/block_scout_web/templates/block_transaction/index.html.eex:26
#: lib/block_scout_web/templates/block_transaction/index.html.eex:35
-#: lib/block_scout_web/templates/chain/show.html.eex:78
+#: lib/block_scout_web/templates/chain/show.html.eex:88
#: lib/block_scout_web/templates/layout/_topnav.html.eex:35
#: lib/block_scout_web/views/address_view.ex:253
msgid "Transactions"
@@ -1060,12 +1060,12 @@ msgid "Verify & publish"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:56
+#: lib/block_scout_web/templates/chain/show.html.eex:66
msgid "View All Blocks →"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:77
+#: lib/block_scout_web/templates/chain/show.html.eex:87
msgid "View All Transactions →"
msgstr ""
@@ -1105,7 +1105,7 @@ msgid "WEI"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:43
+#: lib/block_scout_web/templates/chain/show.html.eex:53
msgid "Wallet addresses"
msgstr ""
@@ -1189,8 +1189,8 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:19
#: lib/block_scout_web/templates/address_validation/index.html.eex:63
#: lib/block_scout_web/templates/address_validation/index.html.eex:82
-#: lib/block_scout_web/templates/chain/show.html.eex:69
-#: lib/block_scout_web/templates/chain/show.html.eex:95
+#: lib/block_scout_web/templates/chain/show.html.eex:79
+#: lib/block_scout_web/templates/chain/show.html.eex:105
#: lib/block_scout_web/templates/tokens/read_contract/index.html.eex:25
msgid "Loading..."
msgstr ""
@@ -1416,7 +1416,7 @@ msgstr ""
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:26
#: lib/block_scout_web/templates/address_transaction/index.html.eex:55
#: lib/block_scout_web/templates/address_validation/index.html.eex:70
-#: lib/block_scout_web/templates/chain/show.html.eex:61
+#: lib/block_scout_web/templates/chain/show.html.eex:71
#: lib/block_scout_web/templates/pending_transaction/index.html.eex:23
#: lib/block_scout_web/templates/tokens/holder/index.html.eex:23
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:21
@@ -1458,6 +1458,7 @@ msgstr ""
#, elixir-format
#: lib/block_scout_web/templates/address_coin_balance/index.html.eex:24
+#: lib/block_scout_web/templates/chain/show.html.eex:11
msgid "Loading chart"
msgstr ""
@@ -1468,6 +1469,7 @@ msgstr ""
#, elixir-format
#: lib/block_scout_web/templates/address_coin_balance/index.html.eex:27
+#: lib/block_scout_web/templates/chain/show.html.eex:14
msgid "There was a problem loading the chart."
msgstr ""
@@ -1537,6 +1539,6 @@ msgid "Emission Contract"
msgstr ""
#, elixir-format
-#: lib/block_scout_web/templates/chain/show.html.eex:87
+#: lib/block_scout_web/templates/chain/show.html.eex:97
msgid "Something went wrong, click to retry."
msgstr ""
diff --git a/apps/block_scout_web/test/block_scout_web/controllers/chain/market_history_chart_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/chain/market_history_chart_controller_test.exs
new file mode 100644
index 0000000000..e2958800fe
--- /dev/null
+++ b/apps/block_scout_web/test/block_scout_web/controllers/chain/market_history_chart_controller_test.exs
@@ -0,0 +1,24 @@
+defmodule BlockScoutWeb.Chain.MarketHistoryChartControllerTest do
+ use BlockScoutWeb.ConnCase
+
+ describe "GET show/2" do
+ test "returns error when not an ajax request" do
+ path = market_history_chart_path(BlockScoutWeb.Endpoint, :show)
+
+ conn = get(build_conn(), path)
+
+ assert conn.status == 422
+ end
+
+ test "returns ok when request is ajax" do
+ path = market_history_chart_path(BlockScoutWeb.Endpoint, :show)
+
+ conn =
+ build_conn()
+ |> put_req_header("x-requested-with", "xmlhttprequest")
+ |> get(path)
+
+ assert json_response(conn, 200)
+ end
+ end
+end
diff --git a/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs
index e58db4c249..de589ff505 100644
--- a/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs
+++ b/apps/block_scout_web/test/block_scout_web/controllers/chain_controller_test.exs
@@ -49,16 +49,6 @@ defmodule BlockScoutWeb.ChainControllerTest do
refute(Enum.member?(response["blocks"], old_block))
end
- test "returns market history data", %{conn: conn} do
- today = Date.utc_today()
- for day <- -40..0, do: insert(:market_history, date: Date.add(today, day))
-
- conn = get(conn, "/")
-
- assert Map.has_key?(conn.assigns, :market_history_data)
- assert length(conn.assigns.market_history_data) == 30
- end
-
test "displays miner primary address names" do
miner_name = "POA Miner Pool"
%{address: miner_address} = insert(:address_name, name: miner_name, primary: true)
diff --git a/apps/block_scout_web/test/block_scout_web/views/chain_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/chain_view_test.exs
deleted file mode 100644
index 87508724c7..0000000000
--- a/apps/block_scout_web/test/block_scout_web/views/chain_view_test.exs
+++ /dev/null
@@ -1,19 +0,0 @@
-defmodule BlockScoutWeb.ChainViewTest do
- use BlockScoutWeb.ConnCase, async: true
-
- alias BlockScoutWeb.ChainView
-
- describe "encode_market_history_data/1" do
- test "returns a JSON encoded market history data" do
- market_history_data = [
- %Explorer.Market.MarketHistory{
- closing_price: Decimal.new("0.078"),
- date: ~D[2018-08-20]
- }
- ]
-
- assert "[{\"closing_price\":\"0.078\",\"date\":\"2018-08-20\"}]" ==
- ChainView.encode_market_history_data(market_history_data)
- end
- end
-end
diff --git a/apps/explorer/lib/explorer/exchange_rates/source.ex b/apps/explorer/lib/explorer/exchange_rates/source.ex
index 7401d27a0d..584f7bf147 100644
--- a/apps/explorer/lib/explorer/exchange_rates/source.ex
+++ b/apps/explorer/lib/explorer/exchange_rates/source.ex
@@ -3,6 +3,7 @@ defmodule Explorer.ExchangeRates.Source do
Behaviour for fetching exchange rates from external sources.
"""
+ alias Explorer.ExchangeRates.Source.CoinMarketCap
alias Explorer.ExchangeRates.Token
alias HTTPoison.{Error, Response}
@@ -11,6 +12,31 @@ defmodule Explorer.ExchangeRates.Source do
"""
@spec fetch_exchange_rates(module) :: {:ok, [Token.t()]} | {:error, any}
def fetch_exchange_rates(source \\ exchange_rates_source()) do
+ if(source == CoinMarketCap) do
+ fetch_exchange_rates_from_paginable_source(source)
+ else
+ fetch_exchange_rates_request(source)
+ end
+ end
+
+ defp fetch_exchange_rates_from_paginable_source(source, page \\ 1) do
+ case HTTPoison.get(source.source_url(page), headers()) do
+ {:ok, %Response{body: body, status_code: 200}} ->
+ cond do
+ body =~ Explorer.coin() -> {:ok, source.format_data(body)}
+ page == source.max_page_number -> {:error, "exchange rates not found for this network"}
+ true -> fetch_exchange_rates_from_paginable_source(source, page + 1)
+ end
+
+ {:ok, %Response{body: body, status_code: status_code}} when status_code in 400..499 ->
+ {:error, decode_json(body)["error"]}
+
+ {:error, %Error{reason: reason}} ->
+ {:error, reason}
+ end
+ end
+
+ defp fetch_exchange_rates_request(source) do
case HTTPoison.get(source.source_url(), headers()) do
{:ok, %Response{body: body, status_code: 200}} ->
{:ok, source.format_data(body)}
@@ -43,6 +69,8 @@ defmodule Explorer.ExchangeRates.Source do
def to_decimal(nil), do: nil
+ def to_decimal(%Decimal{} = value), do: value
+
def to_decimal(value) when is_float(value) do
Decimal.from_float(value)
end
diff --git a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex b/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
index 568a9f36d1..4030eed06f 100644
--- a/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
+++ b/apps/explorer/lib/explorer/exchange_rates/source/coin_market_cap.ex
@@ -31,9 +31,15 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCap do
@impl Source
def source_url do
- "#{base_url()}/v1/ticker/?limit=0"
+ source_url(1)
end
+ def source_url(page) do
+ "#{base_url()}/v1/ticker/?start=#{page - 1}00"
+ end
+
+ def max_page_number, do: 10
+
defp base_url do
config(:base_url) || "https://api.coinmarketcap.com"
end