Merge pull request #9139 from blockscout/np-fix-token-balance-on-demand

Fix bug with match error; Add sorting before broadcasting updated tok…
pull/9198/head
Victor Baranov 10 months ago committed by GitHub
commit 9f23f398f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      CHANGELOG.md
  2. 24
      apps/block_scout_web/lib/block_scout_web/channels/address_channel.ex
  3. 44
      apps/indexer/lib/indexer/fetcher/token_balance_on_demand.ex

@ -12,6 +12,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
- [#9124](https://github.com/blockscout/blockscout/pull/9124) - EIP-1167 display multiple sources of implementation
- [#9110](https://github.com/blockscout/blockscout/pull/9110) - Improve update_in in gas tracker

@ -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,

@ -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

Loading…
Cancel
Save