|
|
@ -559,22 +559,67 @@ defmodule Explorer.Chain do |
|
|
|
@spec address_hash_to_token_transfers_including_contract(Hash.Address.t(), Keyword.t()) :: [TokenTransfer.t()] |
|
|
|
@spec address_hash_to_token_transfers_including_contract(Hash.Address.t(), Keyword.t()) :: [TokenTransfer.t()] |
|
|
|
def address_hash_to_token_transfers_including_contract(address_hash, options \\ []) do |
|
|
|
def address_hash_to_token_transfers_including_contract(address_hash, options \\ []) do |
|
|
|
paging_options = Keyword.get(options, :paging_options, @default_paging_options) |
|
|
|
paging_options = Keyword.get(options, :paging_options, @default_paging_options) |
|
|
|
|
|
|
|
from_block = Keyword.get(options, :from_block) |
|
|
|
|
|
|
|
to_block = Keyword.get(options, :to_block) |
|
|
|
|
|
|
|
|
|
|
|
query = |
|
|
|
query = |
|
|
|
from( |
|
|
|
from_block |
|
|
|
token_transfer in TokenTransfer, |
|
|
|
|> query_address_hash_to_token_transfers_including_contract(to_block, address_hash) |
|
|
|
where: token_transfer.to_address_hash == ^address_hash, |
|
|
|
|> order_by([token_transfer], asc: token_transfer.block_number, asc: token_transfer.log_index) |
|
|
|
or_where: token_transfer.from_address_hash == ^address_hash, |
|
|
|
|
|
|
|
or_where: token_transfer.token_contract_address_hash == ^address_hash |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query |
|
|
|
query |
|
|
|
|> handle_paging_options(paging_options) |
|
|
|
|> handle_token_transfer_paging_options(paging_options) |
|
|
|
|> preload(transaction: :block) |
|
|
|
|> preload(transaction: :block) |
|
|
|
|> preload(:token) |
|
|
|
|> preload(:token) |
|
|
|
|> Repo.all() |
|
|
|
|> Repo.all() |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp query_address_hash_to_token_transfers_including_contract(nil, to_block, address_hash) |
|
|
|
|
|
|
|
when not is_nil(to_block) do |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
token_transfer in TokenTransfer, |
|
|
|
|
|
|
|
where: |
|
|
|
|
|
|
|
(token_transfer.to_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.from_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.token_contract_address_hash == ^address_hash) and |
|
|
|
|
|
|
|
token_transfer.block_number <= ^to_block |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp query_address_hash_to_token_transfers_including_contract(from_block, nil, address_hash) |
|
|
|
|
|
|
|
when not is_nil(from_block) do |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
token_transfer in TokenTransfer, |
|
|
|
|
|
|
|
where: |
|
|
|
|
|
|
|
(token_transfer.to_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.from_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.token_contract_address_hash == ^address_hash) and |
|
|
|
|
|
|
|
token_transfer.block_number >= ^from_block |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp query_address_hash_to_token_transfers_including_contract(from_block, to_block, address_hash) |
|
|
|
|
|
|
|
when not is_nil(from_block) and not is_nil(to_block) do |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
token_transfer in TokenTransfer, |
|
|
|
|
|
|
|
where: |
|
|
|
|
|
|
|
(token_transfer.to_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.from_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.token_contract_address_hash == ^address_hash) and |
|
|
|
|
|
|
|
(token_transfer.block_number >= ^from_block and token_transfer.block_number <= ^to_block) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp query_address_hash_to_token_transfers_including_contract(_, _, address_hash) do |
|
|
|
|
|
|
|
from( |
|
|
|
|
|
|
|
token_transfer in TokenTransfer, |
|
|
|
|
|
|
|
where: |
|
|
|
|
|
|
|
token_transfer.to_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.from_address_hash == ^address_hash or |
|
|
|
|
|
|
|
token_transfer.token_contract_address_hash == ^address_hash |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@spec address_to_logs(Hash.Address.t(), Keyword.t()) :: [Log.t()] |
|
|
|
@spec address_to_logs(Hash.Address.t(), Keyword.t()) :: [Log.t()] |
|
|
|
def address_to_logs(address_hash, options \\ []) when is_list(options) do |
|
|
|
def address_to_logs(address_hash, options \\ []) when is_list(options) do |
|
|
|
paging_options = Keyword.get(options, :paging_options) || %PagingOptions{page_size: 50} |
|
|
|
paging_options = Keyword.get(options, :paging_options) || %PagingOptions{page_size: 50} |
|
|
@ -4258,6 +4303,14 @@ defmodule Explorer.Chain do |
|
|
|
|> limit(^paging_options.page_size) |
|
|
|
|> limit(^paging_options.page_size) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp handle_token_transfer_paging_options(query, nil), do: query |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defp handle_token_transfer_paging_options(query, paging_options) do |
|
|
|
|
|
|
|
query |
|
|
|
|
|
|
|
|> TokenTransfer.page_token_transfer(paging_options) |
|
|
|
|
|
|
|
|> limit(^paging_options.page_size) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
defp join_association(query, [{association, nested_preload}], necessity) |
|
|
|
defp join_association(query, [{association, nested_preload}], necessity) |
|
|
|
when is_atom(association) and is_atom(nested_preload) do |
|
|
|
when is_atom(association) and is_atom(nested_preload) do |
|
|
|
case necessity do |
|
|
|
case necessity do |
|
|
|