diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f508dbd2..72dcde5cbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ ## Current ### Features -- [#2667](https://github.com/poanetwork/blockscout/pull/2667) - Add ETS-based cache for accounts page - [#2679](https://github.com/poanetwork/blockscout/pull/2679) - added fixed height for card chain blocks and card chain transactions - [#2678](https://github.com/poanetwork/blockscout/pull/2678) - fixed dashboard banner height bug - [#2672](https://github.com/poanetwork/blockscout/pull/2672) - added new theme for xUSDT +- [#2667](https://github.com/poanetwork/blockscout/pull/2667) - Add ETS-based cache for accounts page - [#2666](https://github.com/poanetwork/blockscout/pull/2666) - fetch token counters in parallel - [#2665](https://github.com/poanetwork/blockscout/pull/2665) - new menu layout for mobile devices - [#2663](https://github.com/poanetwork/blockscout/pull/2663) - Fetch address counters in parallel @@ -19,6 +19,7 @@ - [#2684](https://github.com/poanetwork/blockscout/pull/2684) - do not filter pending logs - [#2682](https://github.com/poanetwork/blockscout/pull/2682) - Use Task.start instead of Task.async in caches - [#2671](https://github.com/poanetwork/blockscout/pull/2671) - fixed buttons color at smart contract section +- [#2660](https://github.com/poanetwork/blockscout/pull/2660) - set correct last value for coin balances chart data - [#2619](https://github.com/poanetwork/blockscout/pull/2619) - Enforce DB transaction's order to prevent deadlocks ### Chore diff --git a/apps/block_scout_web/test/block_scout_web/controllers/address_coin_balance_by_day_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/address_coin_balance_by_day_controller_test.exs index 1496bb24e9..df714247c2 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/address_coin_balance_by_day_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/address_coin_balance_by_day_controller_test.exs @@ -5,8 +5,8 @@ defmodule BlockScoutWeb.AddressCoinBalanceByDayControllerTest do test "returns the coin balance history grouped by date", %{conn: conn} do address = insert(:address) noon = Timex.now() |> Timex.beginning_of_day() |> Timex.set(hour: 12) - block = insert(:block, timestamp: noon) - block_one_day_ago = insert(:block, timestamp: Timex.shift(noon, days: -1)) + block = insert(:block, timestamp: noon, number: 2) + block_one_day_ago = insert(:block, timestamp: Timex.shift(noon, days: -1), number: 1) insert(:fetched_balance, address_hash: address.hash, value: 1000, block_number: block.number) insert(:fetched_balance, address_hash: address.hash, value: 2000, block_number: block_one_day_ago.number) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index d39945034d..36cfae424e 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -3128,9 +3128,17 @@ defmodule Explorer.Chain do address_hash |> CoinBalance.balances_by_day(latest_block_timestamp) |> Repo.all() + |> replace_last_value(latest_block_timestamp) |> normalize_balances_by_day() end + # https://github.com/poanetwork/blockscout/issues/2658 + defp replace_last_value(items, %{value: value, timestamp: timestamp}) do + List.replace_at(items, -1, %{date: Date.convert!(timestamp, Calendar.ISO), value: value}) + end + + defp replace_last_value(items, _), do: items + defp normalize_balances_by_day(balances_by_day) do result = balances_by_day diff --git a/apps/explorer/lib/explorer/chain/address/coin_balance.ex b/apps/explorer/lib/explorer/chain/address/coin_balance.ex index 537f6a0483..f3647a8f54 100644 --- a/apps/explorer/lib/explorer/chain/address/coin_balance.ex +++ b/apps/explorer/lib/explorer/chain/address/coin_balance.ex @@ -112,7 +112,7 @@ defmodule Explorer.Chain.Address.CoinBalance do |> join(:inner, [cb], b in Block, on: cb.block_number == b.number) |> where([cb], cb.address_hash == ^address_hash) |> last(:block_number) - |> select([cb, b], %{timestamp: b.timestamp}) + |> select([cb, b], %{timestamp: b.timestamp, value: cb.value}) end def changeset(%__MODULE__{} = balance, params) do diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 187d503621..3ac60b6963 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -3879,9 +3879,9 @@ defmodule Explorer.ChainTest do address = insert(:address) today = NaiveDateTime.utc_now() noon = Timex.set(today, hour: 12) - block = insert(:block, timestamp: noon) + block = insert(:block, timestamp: noon, number: 50) yesterday = Timex.shift(noon, days: -1) - block_one_day_ago = insert(:block, timestamp: yesterday) + block_one_day_ago = insert(:block, timestamp: yesterday, number: 49) insert(:fetched_balance, address_hash: address.hash, value: 1000, block_number: block.number) insert(:fetched_balance, address_hash: address.hash, value: 2000, block_number: block_one_day_ago.number) @@ -3908,6 +3908,22 @@ defmodule Explorer.ChainTest do %{date: today |> NaiveDateTime.to_date() |> Date.to_string(), value: Decimal.new("1E-15")} ] end + + test "uses last block value if there a couple of change in the same day" do + address = insert(:address) + today = NaiveDateTime.utc_now() + past = Timex.shift(today, hours: -1) + + block_now = insert(:block, timestamp: today, number: 1) + insert(:fetched_balance, address_hash: address.hash, value: 1, block_number: block_now.number) + + block_past = insert(:block, timestamp: past, number: 2) + insert(:fetched_balance, address_hash: address.hash, value: 0, block_number: block_past.number) + + [balance] = Chain.address_to_balances_by_day(address.hash) + + assert balance.value == Decimal.new(0) + end end describe "block_combined_rewards/1" do