Standardize transaction fetch

pull/306/head
Stamates 7 years ago
parent 12f8b6f16f
commit 04702e0755
  1. 61
      apps/explorer/lib/explorer/chain.ex

@ -149,17 +149,11 @@ defmodule Explorer.Chain do
when is_list(options) do when is_list(options) do
direction = Keyword.get(options, :direction) direction = Keyword.get(options, :direction)
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, %PagingOptions{page_size: 50})
Transaction options
|> load_contract_creation() |> Keyword.get(:paging_options, %PagingOptions{page_size: 50})
|> select_merge([_, internal_transaction], %{ |> fetch_transactions()
created_contract_address_hash: internal_transaction.created_contract_address_hash
})
|> page_transaction(paging_options)
|> limit(^paging_options.page_size)
|> where_address_fields_match(address_hash, direction) |> where_address_fields_match(address_hash, direction)
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()
end end
@ -425,18 +419,12 @@ defmodule Explorer.Chain do
@spec block_to_transactions(Block.t(), [paging_options | necessity_by_association_option]) :: [Transaction.t()] @spec block_to_transactions(Block.t(), [paging_options | necessity_by_association_option]) :: [Transaction.t()]
def block_to_transactions(%Block{hash: block_hash}, options \\ []) when is_list(options) do def block_to_transactions(%Block{hash: block_hash}, options \\ []) when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, %PagingOptions{page_size: 50})
Transaction options
|> load_contract_creation() |> Keyword.get(:paging_options, %PagingOptions{page_size: 50})
|> select_merge([_, internal_transaction], %{ |> fetch_transactions()
created_contract_address_hash: internal_transaction.created_contract_address_hash
})
|> join(:inner, [transaction], block in assoc(transaction, :block)) |> join(:inner, [transaction], block in assoc(transaction, :block))
|> where([_, _, block], block.hash == ^block_hash) |> where([_, _, block], block.hash == ^block_hash)
|> page_transaction(paging_options)
|> limit(^paging_options.page_size)
|> order_by([transaction], desc: transaction.index)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()
end end
@ -690,12 +678,8 @@ defmodule Explorer.Chain do
when is_list(options) do when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
Transaction fetch_transactions()
|> where(hash: ^hash) |> where(hash: ^hash)
|> load_contract_creation()
|> select_merge([_, internal_transaction], %{
created_contract_address_hash: internal_transaction.created_contract_address_hash
})
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.one() |> Repo.one()
|> case do |> case do
@ -1923,16 +1907,11 @@ defmodule Explorer.Chain do
@spec recent_collated_transactions([paging_options | necessity_by_association_option]) :: [Transaction.t()] @spec recent_collated_transactions([paging_options | necessity_by_association_option]) :: [Transaction.t()]
def recent_collated_transactions(options \\ []) when is_list(options) do def recent_collated_transactions(options \\ []) when is_list(options) do
necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) necessity_by_association = Keyword.get(options, :necessity_by_association, %{})
paging_options = Keyword.get(options, :paging_options, %PagingOptions{page_size: 50})
Transaction options
|> load_contract_creation() |> Keyword.get(:paging_options, %PagingOptions{page_size: 50})
|> select_merge([_, internal_transaction], %{ |> fetch_transactions()
created_contract_address_hash: internal_transaction.created_contract_address_hash
})
|> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index)) |> where([transaction], not is_nil(transaction.block_number) and not is_nil(transaction.index))
|> page_transaction(paging_options)
|> limit(^paging_options.page_size)
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index) |> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()
@ -1968,9 +1947,9 @@ defmodule Explorer.Chain do
paging_options = Keyword.get(options, :paging_options, %PagingOptions{page_size: 50}) paging_options = Keyword.get(options, :paging_options, %PagingOptions{page_size: 50})
Transaction Transaction
|> where([transaction], is_nil(transaction.block_hash))
|> page_pending_transaction(paging_options) |> page_pending_transaction(paging_options)
|> limit(^paging_options.page_size) |> limit(^paging_options.page_size)
|> where([transaction], is_nil(transaction.block_hash))
|> order_by([transaction], desc: transaction.inserted_at, desc: transaction.hash) |> order_by([transaction], desc: transaction.inserted_at, desc: transaction.hash)
|> join_associations(necessity_by_association) |> join_associations(necessity_by_association)
|> Repo.all() |> Repo.all()
@ -2242,6 +2221,16 @@ defmodule Explorer.Chain do
end) end)
end end
defp fetch_transactions(paging_options \\ nil) do
Transaction
|> load_contract_creation()
|> select_merge([_, internal_transaction], %{
created_contract_address_hash: internal_transaction.created_contract_address_hash
})
|> order_by([transaction], desc: transaction.block_number, desc: transaction.index)
|> handle_paging_options(paging_options)
end
defp for_parent_transaction(query, %Hash{byte_count: unquote(Hash.Full.byte_count())} = hash) do defp for_parent_transaction(query, %Hash{byte_count: unquote(Hash.Full.byte_count())} = hash) do
from( from(
child in query, child in query,
@ -2460,6 +2449,14 @@ defmodule Explorer.Chain do
{:ok, for(transaction <- transactions, do: transaction.hash)} {:ok, for(transaction <- transactions, do: transaction.hash)}
end end
defp handle_paging_options(query, nil), do: query
defp handle_paging_options(query, paging_options) do
query
|> page_transaction(paging_options)
|> limit(^paging_options.page_size)
end
defp join_association(query, association, necessity) when is_atom(association) do defp join_association(query, association, necessity) when is_atom(association) do
case necessity do case necessity do
:optional -> :optional ->

Loading…
Cancel
Save