From 95803917eb676425a4ceda3fcfc78172ee6e2b48 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Wed, 24 May 2023 14:48:23 +0300 Subject: [PATCH] API v2: fix today coin price --- CHANGELOG.md | 3 ++- .../controllers/api/v2/stats_controller.ex | 2 +- apps/explorer/lib/explorer/market/market.ex | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b59bed5198..08e23cdffd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ ### Fixes -- [#7545](https://github.com/blockscout/blockscout/pull/7545) - Check if cached exchange rate is empty before replacing DB value in stats API +- [](https://github.com/blockscout/blockscout/pull/7546) - API v2: fix today coin price (use in-memory or cached in DB value) +- [#7545](https://github.com/blockscout/blockscout/pull/7545) - API v2: Check if cached exchange rate is empty before replacing DB value in stats API - [#7516](https://github.com/blockscout/blockscout/pull/7516) - Fix shrinking logo in Safari ### Chore diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/stats_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/stats_controller.ex index 5707d1b15f..d6974c090c 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/stats_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/api/v2/stats_controller.ex @@ -46,7 +46,7 @@ defmodule BlockScoutWeb.API.V2.StatsController do "total_addresses" => @api_true |> Chain.address_estimated_count() |> to_string(), "total_transactions" => TransactionCache.estimated_count() |> to_string(), "average_block_time" => AverageBlockTime.average_block_time() |> Duration.to_milliseconds(), - "coin_price" => exchange_rate.usd_value, + "coin_price" => exchange_rate.usd_value || Market.get_native_coin_exchange_rate_from_db(), "total_gas_used" => GasUsage.total() |> to_string(), "transactions_today" => Enum.at(transaction_stats, 0).number_of_transactions |> to_string(), "gas_used_today" => Enum.at(transaction_stats, 0).gas_used, diff --git a/apps/explorer/lib/explorer/market/market.ex b/apps/explorer/lib/explorer/market/market.ex index d2dead1d2e..466c6c5617 100644 --- a/apps/explorer/lib/explorer/market/market.ex +++ b/apps/explorer/lib/explorer/market/market.ex @@ -25,6 +25,24 @@ defmodule Explorer.Market do MarketHistoryCache.fetch() end + @doc """ + Retrieves today's native coin exchange rate from the database. + """ + @spec get_native_coin_exchange_rate_from_db() :: Token.t() | nil + def get_native_coin_exchange_rate_from_db do + today = + case fetch_recent_history() do + [today | _the_rest] -> today + _ -> nil + end + + if today do + Map.get(today, :closing_price) + else + nil + end + end + @doc false def bulk_insert_history(records) do records_without_zeroes =