Get token balance query improvement: instead of address_token_balances use address_current_token_balances table

pull/4137/head
Viktor Baranov 4 years ago
parent 5c0a8831fd
commit aa60d14e1b
  1. 8
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/address_controller_test.exs
  2. 9
      apps/explorer/lib/explorer/etherscan.ex
  3. 41
      apps/explorer/test/explorer/etherscan_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)

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

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

Loading…
Cancel
Save