From c3d67c8c9480d9520db6910dfa5cf18ad4ac7ee9 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Tue, 1 Aug 2023 13:13:36 +0300 Subject: [PATCH] Fix Rootstock charts API --- CHANGELOG.md | 1 + .../controllers/api/v2/stats_controller.ex | 17 ++++++++- .../explorer/lib/explorer/chain/supply/rsk.ex | 37 ++++++++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a537a51303..7a093cb632 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#8040](https://github.com/blockscout/blockscout/pull/8040) - Resolve issue with Docker image for Mac M1/M2 - [#8060](https://github.com/blockscout/blockscout/pull/8060) - Fix eth_getLogs API endpoint +- [#8082](https://github.com/blockscout/blockscout/pull/8082) - Fix Rootstock charts API ### 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 6ffbd0c6bb..921ed5c4bf 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 @@ -125,5 +125,20 @@ defmodule BlockScoutWeb.API.V2.StatsController do defp available_supply(:ok, exchange_rate), do: exchange_rate.available_supply || 0 - defp available_supply({:ok, supply_for_days}, _exchange_rate), do: supply_for_days + defp available_supply({:ok, supply_for_days}, _exchange_rate) do + supply_for_days + |> Jason.encode() + |> case do + {:ok, _data} -> + current_date = + supply_for_days + |> Map.keys() + |> Enum.max(Date) + + Map.get(supply_for_days, current_date) + + _ -> + nil + end + end end diff --git a/apps/explorer/lib/explorer/chain/supply/rsk.ex b/apps/explorer/lib/explorer/chain/supply/rsk.ex index 1037dd4cf7..2eb9caa933 100644 --- a/apps/explorer/lib/explorer/chain/supply/rsk.ex +++ b/apps/explorer/lib/explorer/chain/supply/rsk.ex @@ -5,7 +5,7 @@ defmodule Explorer.Chain.Supply.RSK do use Explorer.Chain.Supply - import Ecto.Query, only: [from: 2] + import Ecto.Query, only: [from: 2, subquery: 1] import EthereumJSONRPC, only: [integer_to_quantity: 1] alias EthereumJSONRPC.FetchedBalances @@ -16,7 +16,9 @@ defmodule Explorer.Chain.Supply.RSK do @cache_name :rsk_balance @balance_key :balance + @rsk_bridge_contract_address "0x0000000000000000000000000000000001000006" + @spec market_cap(any()) :: Decimal.t() def market_cap(%{usd_value: usd_value}) when not is_nil(usd_value) do btc = circulating() @@ -25,31 +27,32 @@ defmodule Explorer.Chain.Supply.RSK do def market_cap(_), do: Decimal.new(0) - @doc "Equivalent to getting the circulating value " + @doc "Equivalent to getting the circulating value" def supply_for_days(days) do now = Timex.now() - balances_query = + base_query = from(balance in CoinBalance, join: block in Block, on: block.number == balance.block_number, where: block.consensus == true, - where: balance.address_hash == ^"0x0000000000000000000000000000000001000006", - where: block.timestamp > ^Timex.shift(now, days: -days), - distinct: fragment("date_trunc('day', ?)", block.timestamp), - select: {block.timestamp, balance.value} + where: balance.address_hash == ^@rsk_bridge_contract_address, + select: %{timestamp: block.timestamp, value: balance.value} + ) + + balances_query = + from(q in subquery(base_query), + where: q.timestamp > ^Timex.shift(now, days: -days), + distinct: fragment("date_trunc('day', ?)", q.timestamp), + select: {q.timestamp, q.value} ) balance_before_query = - from(balance in CoinBalance, - join: block in Block, - on: block.number == balance.block_number, - where: block.consensus == true, - where: balance.address_hash == ^"0x0000000000000000000000000000000001000006", - where: block.timestamp <= ^Timex.shift(Timex.now(), days: -days), - order_by: [desc: block.timestamp], + from(q in subquery(base_query), + where: q.timestamp <= ^Timex.shift(Timex.now(), days: -days), + order_by: [desc: q.timestamp], limit: 1, - select: balance.value + select: q.value ) by_day = @@ -105,7 +108,7 @@ defmodule Explorer.Chain.Supply.RSK do max_number = BlockNumber.get_max() params = [ - %{block_quantity: integer_to_quantity(max_number), hash_data: "0x0000000000000000000000000000000001000006"} + %{block_quantity: integer_to_quantity(max_number), hash_data: @rsk_bridge_contract_address} ] json_rpc_named_arguments = Application.get_env(:explorer, :json_rpc_named_arguments) @@ -116,7 +119,7 @@ defmodule Explorer.Chain.Supply.RSK do errors: [], params_list: [ %{ - address_hash: "0x0000000000000000000000000000000001000006", + address_hash: @rsk_bridge_contract_address, value: value } ]