diff --git a/config/prod.exs b/config/prod.exs index 36a52820..9d77f76d 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -12,8 +12,7 @@ config :starknet_explorer, StarknetExplorerWeb.Endpoint, config :swoosh, api_client: Swoosh.ApiClient.Finch, finch_name: StarknetExplorer.Finch # Do not print debug messages in production -# config :logger, level: :info -config :logger, level: :debug +config :logger, level: :info # Runtime production configuration, including reading # of environment variables, is done on config/runtime.exs. diff --git a/lib/starknet_explorer/block/block.ex b/lib/starknet_explorer/block/block.ex index 8d7df2fc..c501bd8e 100644 --- a/lib/starknet_explorer/block/block.ex +++ b/lib/starknet_explorer/block/block.ex @@ -431,15 +431,12 @@ defmodule StarknetExplorer.Block do where: b.network == ^network, limit: ^amount - query - |> Repo.all() - |> Repo.preload(:transactions) + Repo.all(query) |> Enum.map(fn block -> - block - |> Map.put( - :transactions, - Enum.filter(block.transactions, fn tx -> tx.network == network end) - ) + preload_query = + from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network + + Repo.preload(block, transactions: preload_query) end) end @@ -479,13 +476,15 @@ defmodule StarknetExplorer.Block do from b in Block, where: b.hash == ^hash and b.network == ^network - case preload_transactions do - true -> - Repo.one(query) - |> Repo.preload(:transactions) + block = Repo.one(query) + + if preload_transactions do + preload_query = + from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network - false -> - Repo.one(query) + Repo.preload(block, transactions: preload_query) + else + block end end @@ -504,22 +503,30 @@ defmodule StarknetExplorer.Block do from b in Block, where: b.number == ^num and b.network == ^network - if preload_transactions do - Repo.one(query) - |> Repo.preload(:transactions) + block = Repo.one(query) + + if preload_transactions and not is_nil(block) do + preload_query = + from tx in Transaction, where: tx.block_number == ^block.number and tx.network == ^network + + Repo.preload(block, transactions: preload_query) else - Repo.one(query) + block end end def get_by_number_with_receipts_preload(num, network) do + preload_query = + from tx in Transaction, + where: tx.block_number == ^num and tx.network == ^network, + preload: :receipt + query = from b in Block, where: b.number == ^num and b.network == ^network, - preload: [transactions: :receipt] + preload: [transactions: ^preload_query] Repo.one(query) - |> Repo.preload(:transactions) end def get_by_partial_num(num, network) do diff --git a/lib/starknet_explorer/transaction.ex b/lib/starknet_explorer/transaction.ex index 827da4be..59ebd2f2 100644 --- a/lib/starknet_explorer/transaction.ex +++ b/lib/starknet_explorer/transaction.ex @@ -186,11 +186,15 @@ defmodule StarknetExplorer.Transaction do end def paginate_transactions(params, network) do - Transaction - |> where([tx], tx.network == ^network) - |> preload(:block) - |> order_by(desc: :block_number) - |> Repo.paginate(params) + query = + from tx in Transaction, + join: b in Block, + as: :block, + on: tx.block_number == b.number and tx.network == ^network, + where: tx.network == ^network, + order_by: [desc: :block_number] + + Repo.paginate(query, params) end def paginate_transactions_for_index(params, network, filter \\ "ALL")