Add filtering by token type in /addresses/../tokens

pull/6642/head
Никита Поздняков 2 years ago
parent 842fa08f48
commit 325cb1d2bb
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 6
      apps/block_scout_web/lib/block_scout_web/controllers/api/v2/address_controller.ex
  2. 8
      apps/explorer/lib/explorer/chain.ex
  3. 21
      apps/explorer/lib/explorer/chain/address/current_token_balance.ex

@ -279,7 +279,11 @@ defmodule BlockScoutWeb.API.V2.AddressController do
{:not_found, {:ok, _address}} <- {:not_found, Chain.hash_to_address(address_hash, [], false)} do {:not_found, {:ok, _address}} <- {:not_found, Chain.hash_to_address(address_hash, [], false)} do
results_plus_one = results_plus_one =
address_hash address_hash
|> Chain.fetch_last_token_balances(paging_options(params)) |> Chain.fetch_last_token_balances(
params
|> paging_options()
|> Keyword.merge(token_transfers_types_options(params))
)
|> Market.add_price() |> Market.add_price()
{tokens, next_page} = split_list_by_page(results_plus_one) {tokens, next_page} = split_list_by_page(results_plus_one)

@ -5145,10 +5145,12 @@ defmodule Explorer.Chain do
end end
@spec fetch_last_token_balances(Hash.Address.t(), [paging_options]) :: [] @spec fetch_last_token_balances(Hash.Address.t(), [paging_options]) :: []
def fetch_last_token_balances(address_hash, paging_options) do def fetch_last_token_balances(address_hash, options) do
filter = Keyword.get(options, :token_type)
address_hash address_hash
|> CurrentTokenBalance.last_token_balances(paging_options) |> CurrentTokenBalance.last_token_balances(options, filter)
|> page_current_token_balances(paging_options) |> page_current_token_balances(options)
|> Repo.all() |> Repo.all()
end end

@ -158,7 +158,22 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do
@doc """ @doc """
Builds an `t:Ecto.Query.t/0` to fetch the current token balances of the given address. Builds an `t:Ecto.Query.t/0` to fetch the current token balances of the given address.
""" """
def last_token_balances(address_hash) do def last_token_balances(address_hash, type \\ [])
def last_token_balances(address_hash, [type | _]) do
from(
ctb in __MODULE__,
where: ctb.address_hash == ^address_hash,
where: ctb.value > 0,
where: ctb.token_type == ^type,
left_join: t in Token,
on: ctb.token_contract_address_hash == t.contract_address_hash,
select: {ctb, t},
order_by: [desc: ctb.value, asc: t.type, asc: t.name]
)
end
def last_token_balances(address_hash, _) do
from( from(
ctb in __MODULE__, ctb in __MODULE__,
where: ctb.address_hash == ^address_hash, where: ctb.address_hash == ^address_hash,
@ -173,11 +188,11 @@ defmodule Explorer.Chain.Address.CurrentTokenBalance do
@doc """ @doc """
Builds an `t:Ecto.Query.t/0` to fetch the current token balances of the given address (paginated version). Builds an `t:Ecto.Query.t/0` to fetch the current token balances of the given address (paginated version).
""" """
def last_token_balances(address_hash, options) do def last_token_balances(address_hash, options, type) do
paging_options = Keyword.get(options, :paging_options, @default_paging_options) paging_options = Keyword.get(options, :paging_options, @default_paging_options)
address_hash address_hash
|> last_token_balances() |> last_token_balances(type)
|> limit(^paging_options.page_size) |> limit(^paging_options.page_size)
end end

Loading…
Cancel
Save