From aa60d14e1bcf634dfd64833b181ab4687dbb2cd3 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Tue, 25 May 2021 15:36:35 +0300 Subject: [PATCH] Get token balance query improvement: instead of address_token_balances use address_current_token_balances table --- .../api/rpc/address_controller_test.exs | 8 ++-- apps/explorer/lib/explorer/etherscan.ex | 9 ++-- .../explorer/test/explorer/etherscan_test.exs | 41 ++----------------- 3 files changed, 12 insertions(+), 46 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 bdafb56b32..38d614bdd4 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 @@ -2462,13 +2462,13 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do end test "with contract address and address with existing balance in token_balances table", %{conn: conn} do - token_balance = insert(:token_balance) + current_token_balance = insert(:address_current_token_balance) params = %{ "module" => "account", "action" => "tokenbalance", - "contractaddress" => to_string(token_balance.token_contract_address_hash), - "address" => to_string(token_balance.address_hash) + "contractaddress" => to_string(current_token_balance.token_contract_address_hash), + "address" => to_string(current_token_balance.address_hash) } assert response = @@ -2476,7 +2476,7 @@ defmodule BlockScoutWeb.API.RPC.AddressControllerTest do |> get("/api", params) |> json_response(200) - assert response["result"] == to_string(token_balance.value) + assert response["result"] == to_string(current_token_balance.value) assert response["status"] == "1" assert response["message"] == "OK" assert :ok = ExJsonSchema.Validator.validate(tokenbalance_schema(), response) diff --git a/apps/explorer/lib/explorer/etherscan.ex b/apps/explorer/lib/explorer/etherscan.ex index a91cf9286f..1e888c66f2 100644 --- a/apps/explorer/lib/explorer/etherscan.ex +++ b/apps/explorer/lib/explorer/etherscan.ex @@ -293,12 +293,11 @@ defmodule Explorer.Etherscan do ) do query = from( - tb in TokenBalance, - where: tb.token_contract_address_hash == ^contract_address_hash, - where: tb.address_hash == ^address_hash, - order_by: [desc: :block_number], + ctb in CurrentTokenBalance, + where: ctb.token_contract_address_hash == ^contract_address_hash, + where: ctb.address_hash == ^address_hash, limit: 1, - select: tb + select: ctb ) Repo.one(query) diff --git a/apps/explorer/test/explorer/etherscan_test.exs b/apps/explorer/test/explorer/etherscan_test.exs index f528d215b0..0e1bf0922a 100644 --- a/apps/explorer/test/explorer/etherscan_test.exs +++ b/apps/explorer/test/explorer/etherscan_test.exs @@ -1586,46 +1586,13 @@ defmodule Explorer.EtherscanTest do describe "get_token_balance/2" do test "with a single matching token_balance record" do - token_balance = - %{token_contract_address_hash: contract_address_hash, address_hash: address_hash} = insert(:token_balance) + address_current_token_balance = + %{token_contract_address_hash: contract_address_hash, address_hash: address_hash} = + insert(:address_current_token_balance) found_token_balance = Etherscan.get_token_balance(contract_address_hash, address_hash) - assert found_token_balance.id == token_balance.id - end - - test "returns token balance in latest block" do - token = insert(:token) - - contract_address_hash = token.contract_address_hash - - address = insert(:address) - - token_details1 = %{ - token_contract_address_hash: contract_address_hash, - address: address, - block_number: 5 - } - - token_details2 = %{ - token_contract_address_hash: contract_address_hash, - address: address, - block_number: 15 - } - - token_details3 = %{ - token_contract_address_hash: contract_address_hash, - address: address, - block_number: 10 - } - - _token_balance1 = insert(:token_balance, token_details1) - token_balance2 = insert(:token_balance, token_details2) - _token_balance3 = insert(:token_balance, token_details3) - - found_token_balance = Etherscan.get_token_balance(contract_address_hash, address.hash) - - assert found_token_balance.id == token_balance2.id + assert found_token_balance.id == address_current_token_balance.id end end