From 451d7c5f61a1b4924ec57926eeded62c7db1c878 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Thu, 13 May 2021 19:45:28 +0300 Subject: [PATCH] Fix tokenlist API endpoint return only tokens with positive balance --- .../api/rpc/address_controller_test.exs | 8 +-- apps/explorer/lib/explorer/etherscan.ex | 14 ++--- .../explorer/test/explorer/etherscan_test.exs | 52 +------------------ 3 files changed, 13 insertions(+), 61 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs index 9cbc801d63..bdafb56b32 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs @@ -2560,7 +2560,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do end test "with address with existing balance in token_balances table", %{conn: conn} do - token_balance = :token_balance |> insert() |> Repo.preload(:token) + token_balance = :address_current_token_balance |> insert() |> Repo.preload(:token) params = %{ "module" => "account", @@ -2593,9 +2593,9 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do test "with address with multiple tokens", %{conn: conn} do address = insert(:address) other_address = insert(:address) - insert(:token_balance, address: address) - insert(:token_balance, address: address) - insert(:token_balance, address: other_address) + insert(:address_current_token_balance, address: address) + insert(:address_current_token_balance, address: address) + insert(:address_current_token_balance, address: other_address) params = %{ "module" => "account", diff --git a/apps/explorer/lib/explorer/etherscan.ex b/apps/explorer/lib/explorer/etherscan.ex index eb7f743ad4..104b4afe5d 100644 --- a/apps/explorer/lib/explorer/etherscan.ex +++ b/apps/explorer/lib/explorer/etherscan.ex @@ -7,7 +7,7 @@ defmodule Explorer.Etherscan do alias Explorer.Etherscan.Logs alias Explorer.{Chain, Repo} - alias Explorer.Chain.Address.TokenBalance + alias Explorer.Chain.Address.{CurrentTokenBalance, TokenBalance} alias Explorer.Chain.{Block, Hash, InternalTransaction, TokenTransfer, Transaction} @default_options %{ @@ -312,14 +312,14 @@ defmodule Explorer.Etherscan do def list_tokens(%Hash{byte_count: unquote(Hash.Address.byte_count())} = address_hash) do query = from( - tb in TokenBalance, - inner_join: t in assoc(tb, :token), - where: tb.address_hash == ^address_hash, + ctb in CurrentTokenBalance, + inner_join: t in assoc(ctb, :token), + where: ctb.address_hash == ^address_hash, + where: ctb.value > 0, distinct: :token_contract_address_hash, - order_by: [desc: :block_number], select: %{ - balance: tb.value, - contract_address_hash: tb.token_contract_address_hash, + balance: ctb.value, + contract_address_hash: ctb.token_contract_address_hash, name: t.name, decimals: t.decimals, symbol: t.symbol, diff --git a/apps/explorer/test/explorer/etherscan_test.exs b/apps/explorer/test/explorer/etherscan_test.exs index 6dea36d9bb..dca3c3e339 100644 --- a/apps/explorer/test/explorer/etherscan_test.exs +++ b/apps/explorer/test/explorer/etherscan_test.exs @@ -1598,59 +1598,11 @@ defmodule Explorer.EtherscanTest do address = insert(:address) token_balance = - :token_balance + :address_current_token_balance |> insert(address: address) |> Repo.preload(:token) - insert(:token_balance, address: build(:address)) - - token_list = Etherscan.list_tokens(address.hash) - - expected_tokens = [ - %{ - balance: token_balance.value, - contract_address_hash: token_balance.token_contract_address_hash, - name: token_balance.token.name, - decimals: token_balance.token.decimals, - symbol: token_balance.token.symbol, - type: token_balance.token.type - } - ] - - assert token_list == expected_tokens - end - - test "returns the latest known balance per token" do - # The latest balance is the one with the latest block number - address = insert(:address) - token = insert(:token) - - token_balance_details1 = %{ - address: address, - token_contract_address_hash: token.contract_address.hash, - block_number: 1 - } - - token_balance_details2 = %{ - address: address, - token_contract_address_hash: token.contract_address.hash, - block_number: 2 - } - - token_balance_details3 = %{ - address: address, - token_contract_address_hash: token.contract_address.hash, - block_number: 3 - } - - insert(:token_balance, token_balance_details1) - - token_balance = - :token_balance - |> insert(token_balance_details3) - |> Repo.preload(:token) - - insert(:token_balance, token_balance_details2) + insert(:address_current_token_balance, address: build(:address)) token_list = Etherscan.list_tokens(address.hash)