From e2583f1c69d0aa103a980132e35c8f5424aec2d7 Mon Sep 17 00:00:00 2001 From: Nikita Pozdniakov Date: Thu, 11 Jan 2024 23:20:32 +0300 Subject: [PATCH] Fix bug with match error; Add sorting before broadcasting updated token balances --- CHANGELOG.md | 1 + .../channels/address_channel.ex | 24 +++++++++- .../fetcher/token_balance_on_demand.ex | 44 ++++++++++--------- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b807cbefc0..41ce176235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- [#9139](https://github.com/blockscout/blockscout/pull/9139) - TokenBalanceOnDemand fixes - [#9125](https://github.com/blockscout/blockscout/pull/9125) - Fix Explorer.Chain.Cache.GasPriceOracle.merge_fees - [#9110](https://github.com/blockscout/blockscout/pull/9110) - Improve update_in in gas tracker - [#9109](https://github.com/blockscout/blockscout/pull/9109) - Return current exchange rate in api/v2/stats diff --git a/apps/block_scout_web/lib/block_scout_web/channels/address_channel.ex b/apps/block_scout_web/lib/block_scout_web/channels/address_channel.ex index f0ead554c7..89795fb8a7 100644 --- a/apps/block_scout_web/lib/block_scout_web/channels/address_channel.ex +++ b/apps/block_scout_web/lib/block_scout_web/channels/address_channel.ex @@ -246,7 +246,20 @@ defmodule BlockScoutWeb.AddressChannel do end defp push_current_token_balances(socket, address_current_token_balances, event_postfix, token_type) do - filtered_ctbs = address_current_token_balances |> Enum.filter(fn ctb -> ctb.token_type == token_type end) + filtered_ctbs = + address_current_token_balances + |> Enum.filter(fn ctb -> ctb.token_type == token_type end) + |> Enum.sort_by( + fn ctb -> + value = + if ctb.token.decimals, + do: Decimal.div(ctb.value, Decimal.new(Integer.pow(10, Decimal.to_integer(ctb.token.decimals)))), + else: ctb.value + + {(ctb.token.fiat_value && Decimal.mult(value, ctb.token.fiat_value)) || Decimal.new(0), value} + end, + &sorter/2 + ) push(socket, "updated_token_balances_" <> event_postfix, %{ token_balances: @@ -257,6 +270,15 @@ defmodule BlockScoutWeb.AddressChannel do }) end + defp sorter({fiat_value_1, value_1}, {fiat_value_2, value_2}) do + case {Decimal.compare(fiat_value_1, fiat_value_2), Decimal.compare(value_1, value_2)} do + {:gt, _} -> true + {:eq, :gt} -> true + {:eq, :eq} -> true + _ -> false + end + end + def push_current_coin_balance( %Phoenix.Socket{handler: BlockScoutWeb.UserSocketV2} = socket, block_number, diff --git a/apps/indexer/lib/indexer/fetcher/token_balance_on_demand.ex b/apps/indexer/lib/indexer/fetcher/token_balance_on_demand.ex index cd090f19fe..8b8336c906 100644 --- a/apps/indexer/lib/indexer/fetcher/token_balance_on_demand.ex +++ b/apps/indexer/lib/indexer/fetcher/token_balance_on_demand.ex @@ -128,28 +128,30 @@ defmodule Indexer.Fetcher.TokenBalanceOnDemand do (updated_erc_1155_ctbs ++ updated_other_ctbs) |> Enum.filter(&(!is_nil(&1))) - {:ok, - %{ - address_current_token_balances: imported_ctbs - }} = - Chain.import(%{ - address_current_token_balances: %{ - params: filtered_current_token_balances_update_params + if Enum.count(filtered_current_token_balances_update_params) > 0 do + {:ok, + %{ + address_current_token_balances: imported_ctbs + }} = + Chain.import(%{ + address_current_token_balances: %{ + params: filtered_current_token_balances_update_params + }, + broadcast: false + }) + + Publisher.broadcast( + %{ + address_current_token_balances: %{ + address_hash: to_string(address_hash), + address_current_token_balances: + imported_ctbs + |> Enum.map(fn ctb -> %CurrentTokenBalance{ctb | token: tokens[ctb.token_contract_address_hash.bytes]} end) + } }, - broadcast: false - }) - - Publisher.broadcast( - %{ - address_current_token_balances: %{ - address_hash: to_string(address_hash), - address_current_token_balances: - imported_ctbs - |> Enum.map(fn ctb -> %CurrentTokenBalance{ctb | token: tokens[ctb.token_contract_address_hash.bytes]} end) - } - }, - :on_demand - ) + :on_demand + ) + end end defp prepare_updated_balance({{:ok, updated_balance}, stale_current_token_balance}, block_number) do