Return current exchange rate in api/v2/stats

pull/9109/head
Maxim Filonov 10 months ago committed by Viktor Baranov
parent 7b000d839f
commit 8a31151460
  1. 1
      CHANGELOG.md
  2. 2
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/stats_controller.ex
  3. 22
      apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex
  4. 2
      apps/explorer/lib/explorer/market/market_history.ex
  5. 8
      apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs

@ -6,6 +6,7 @@
### Fixes
- [#9109](https://github.com/blockscout/blockscout/pull/9109) - Return current exchange rate in api/v2/stats
- [#9102](https://github.com/blockscout/blockscout/pull/9102) - Fix some log topics for Suave and Polygon Edge
- [#9075](https://github.com/blockscout/blockscout/pull/9075) - Fix fetching contract codes
- [#9073](https://github.com/blockscout/blockscout/pull/9073) - Allow payable function with output appear in the Read tab

@ -27,7 +27,7 @@ defmodule BlockScoutWeb.API.V2.StatsController do
:standard
end
exchange_rate_from_db = Market.get_native_coin_exchange_rate_from_db()
exchange_rate_from_db = Market.get_coin_exchange_rate()
transaction_stats = Helper.get_transaction_stats()

@ -10,6 +10,7 @@ defmodule Explorer.ExchangeRates do
require Logger
alias Explorer.Chain.Events.Publisher
alias Explorer.Market
alias Explorer.ExchangeRates.{Source, Token}
@interval Application.compile_env(:explorer, __MODULE__)[:cache_period]
@ -123,10 +124,29 @@ defmodule Explorer.ExchangeRates do
@spec fetch_rates :: Task.t()
defp fetch_rates do
Task.Supervisor.async_nolink(Explorer.MarketTaskSupervisor, fn ->
Source.fetch_exchange_rates()
case Source.fetch_exchange_rates() do
{:ok, tokens} -> {:ok, add_coin_info_from_db(tokens)}
err -> err
end
end)
end
defp add_coin_info_from_db(tokens) do
case Market.fetch_recent_history() do
[today | _the_rest] ->
tvl_from_history = Map.get(today, :tvl)
tokens
|> Enum.map(fn
%Token{tvl_usd: nil} = token -> %{token | tvl_usd: tvl_from_history}
token -> token
end)
_ ->
tokens
end
end
defp list_from_store(:ets) do
table_name()
|> :ets.tab2list()

@ -20,7 +20,7 @@ defmodule Explorer.Market.MarketHistory do
* `:date` - The date in UTC.
* `:opening_price` - Opening price in USD.
* `:market_cap` - Market cap in USD.
* `:market_cap` - TVL in USD.
* `:tvl` - TVL in USD.
"""
@type t :: %__MODULE__{
closing_price: Decimal.t() | nil,

@ -1,5 +1,5 @@
defmodule Explorer.ExchangeRatesTest do
use ExUnit.Case, async: false
use Explorer.DataCase
import Mox
@ -7,12 +7,16 @@ defmodule Explorer.ExchangeRatesTest do
alias Explorer.ExchangeRates
alias Explorer.ExchangeRates.Token
alias Explorer.ExchangeRates.Source.TestSource
alias Explorer.Market.MarketHistoryCache
@moduletag :capture_log
setup :verify_on_exit!
setup do
Supervisor.terminate_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()})
Supervisor.restart_child(Explorer.Supervisor, {ConCache, MarketHistoryCache.cache_name()})
# Use TestSource mock and ets table for this test set
source_configuration = Application.get_env(:explorer, Explorer.ExchangeRates.Source)
rates_configuration = Application.get_env(:explorer, Explorer.ExchangeRates)
@ -24,6 +28,8 @@ defmodule Explorer.ExchangeRatesTest do
on_exit(fn ->
Application.put_env(:explorer, Explorer.ExchangeRates.Source, source_configuration)
Application.put_env(:explorer, Explorer.ExchangeRates, rates_configuration)
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.Blocks.child_id())
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.Blocks.child_id())
end)
end

Loading…
Cancel
Save