Merge pull request #702 from poanetwork/frg-fix-dropdown-token-balances

Fix token balances' query to always consider the last block even when the value is 0.
pull/700/head
Felipe Renan 6 years ago committed by GitHub
commit 4a282bf71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      apps/explorer/lib/explorer/chain/address/token_balance.ex
  2. 23
      apps/explorer/test/explorer/chain_test.exs

@ -65,18 +65,20 @@ defmodule Explorer.Chain.Address.TokenBalance do
end
@doc """
Builds an `Ecto.Query` to fetch the last token balances.
Builds an `Ecto.Query` to fetch the last token balances that have value greater than 0.
The last token balances from an Address is the last block indexed.
"""
def last_token_balances(address_hash) do
query =
from(
tb in TokenBalance,
where: tb.address_hash == ^address_hash and tb.value > 0,
where: tb.address_hash == ^address_hash,
distinct: :token_contract_address_hash,
order_by: [desc: :block_number],
preload: :token
order_by: [desc: :block_number]
)
from(tb in subquery(query), where: tb.value > 0, preload: :token)
end
@doc """

@ -2665,6 +2665,29 @@ defmodule Explorer.ChainTest do
assert Chain.fetch_last_token_balances(address.hash) == []
end
test "does not consider other blocks when the last block has the value 0" do
address = insert(:address)
token = insert(:token, contract_address: build(:contract_address))
insert(
:token_balance,
address: address,
block_number: 1000,
token_contract_address_hash: token.contract_address_hash,
value: 5000
)
insert(
:token_balance,
address: address,
block_number: 1001,
token_contract_address_hash: token.contract_address_hash,
value: 0
)
assert Chain.fetch_last_token_balances(address.hash) == []
end
end
describe "fetch_token_holders_from_token_hash/2" do

Loading…
Cancel
Save