From 9908cbb7dc81416acd7d0220076bc60f58f50b03 Mon Sep 17 00:00:00 2001 From: Nikita Pozdniakov Date: Wed, 30 Aug 2023 15:39:46 +0300 Subject: [PATCH] Hotfix for proper addresses' tokens displaying --- CHANGELOG.md | 1 + .../api/v2/address_controller_test.exs | 16 ++++++++++++---- .../chain/address/current_token_balance.ex | 2 +- apps/explorer/test/support/factory.ex | 18 +++++++++++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ccd93ca59..d9d72cf680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Fixes +- [#8354](https://github.com/blockscout/blockscout/pull/8354) - Hotfix for proper addresses' tokens displaying - [#8350](https://github.com/blockscout/blockscout/pull/8350) - Add Base Mainnet support for tx actions - [#8282](https://github.com/blockscout/blockscout/pull/8282) - NFT fetcher improvements - [#8287](https://github.com/blockscout/blockscout/pull/8287) - Add separate hackney pool for TokenInstance fetchers diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs index adfba52476..ee6f156fa5 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/v2/address_controller_test.exs @@ -1535,21 +1535,29 @@ defmodule BlockScoutWeb.API.V2.AddressControllerTest do ctbs_erc_20 = for _ <- 0..50 do - insert(:address_current_token_balance_with_token_id, address: address, token_type: "ERC-20", token_id: nil) + insert(:address_current_token_balance_with_token_id_and_fixed_token_type, + address: address, + token_type: "ERC-20", + token_id: nil + ) |> Repo.preload([:token]) end - |> Enum.sort_by(fn x -> x.value end, :asc) + |> Enum.sort_by(fn x -> Decimal.to_float(Decimal.mult(x.value, x.token.fiat_value)) end, :asc) ctbs_erc_721 = for _ <- 0..50 do - insert(:address_current_token_balance_with_token_id, address: address, token_type: "ERC-721", token_id: nil) + insert(:address_current_token_balance_with_token_id_and_fixed_token_type, + address: address, + token_type: "ERC-721", + token_id: nil + ) |> Repo.preload([:token]) end |> Enum.sort_by(fn x -> x.value end, :asc) ctbs_erc_1155 = for _ <- 0..50 do - insert(:address_current_token_balance_with_token_id, + insert(:address_current_token_balance_with_token_id_and_fixed_token_type, address: address, token_type: "ERC-1155", token_id: Enum.random(1..100_000) diff --git a/apps/explorer/lib/explorer/chain/address/current_token_balance.ex b/apps/explorer/lib/explorer/chain/address/current_token_balance.ex index 88c4594a3a..b14bf07dc8 100644 --- a/apps/explorer/lib/explorer/chain/address/current_token_balance.ex +++ b/apps/explorer/lib/explorer/chain/address/current_token_balance.ex @@ -188,10 +188,10 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do ctb in __MODULE__, where: ctb.address_hash == ^address_hash, where: ctb.value > 0, - where: ctb.token_type == ^type, left_join: t in assoc(ctb, :token), on: ctb.token_contract_address_hash == t.contract_address_hash, preload: [token: t], + where: t.type == ^type, select: ctb, select_merge: ^%{fiat_value: fiat_balance}, order_by: ^[desc_nulls_last: fiat_balance], diff --git a/apps/explorer/test/support/factory.ex b/apps/explorer/test/support/factory.ex index 28093d950e..da2e13f666 100644 --- a/apps/explorer/test/support/factory.ex +++ b/apps/explorer/test/support/factory.ex @@ -898,7 +898,23 @@ defmodule Explorer.Factory do %CurrentTokenBalance{ address: build(:address), - token_contract_address_hash: insert(:token).contract_address_hash, + token_contract_address_hash: insert(:token, type: token_type).contract_address_hash, + block_number: block_number(), + value: Enum.random(1_000_000_000_000_000_000..10_000_000_000_000_000_000), + value_fetched_at: DateTime.utc_now(), + token_id: token_id, + token_type: token_type + } + end + + def address_current_token_balance_with_token_id_and_fixed_token_type_factory(%{ + token_type: token_type, + address: address, + token_id: token_id + }) do + %CurrentTokenBalance{ + address: address, + token_contract_address_hash: insert(:token, type: token_type).contract_address_hash, block_number: block_number(), value: Enum.random(1_000_000_000_000_000_000..10_000_000_000_000_000_000), value_fetched_at: DateTime.utc_now(),