From f34960d6067eae24b10243bca8e528947ad82f23 Mon Sep 17 00:00:00 2001 From: Tim Mecklem Date: Tue, 24 Apr 2018 10:30:39 -0400 Subject: [PATCH] Add initial `address_to_internal_transactions` function to Chain context Co-authored-by: stamates --- apps/explorer/lib/explorer/chain.ex | 10 +++++ apps/explorer/test/explorer/chain_test.exs | 45 +++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index b110fedc5d..62ef17fdc0 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -415,6 +415,16 @@ defmodule Explorer.Chain do |> Repo.aggregate(:count, :id) end + def address_to_internal_transactions(%Address{id: id}, options \\ []) do + necessity_by_association = Keyword.get(options, :necessity_by_association, %{}) + address_fields = [:to_address_id, :from_address_id] + + InternalTransaction + |> where_address_fields_match(address_fields, id) + |> join_associations(necessity_by_association) + |> Repo.all() + end + @doc """ `t:Explorer.Chain.InternalTransaction/0`s in `t:Explorer.Chain.Transaction.t/0` with `hash` diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 4a1d17476c..b02e96bb17 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -588,6 +588,49 @@ defmodule Explorer.ChainTest do end end + describe "address_to_internal_transactions/1" do + test "with single transaction containing an internal transaction" do + address = insert(:address) + transaction = insert(:transaction, to_address_id: address.id) + + %InternalTransaction{id: expected_id} = + insert(:internal_transaction, transaction_id: transaction.id, to_address_id: address.id) + + result = address |> Chain.address_to_internal_transactions() |> List.first() + assert result.id == expected_id + end + + test "loads associations in necessity_by_association" do + address = insert(:address) + transaction = insert(:transaction, to_address_id: address.id) + insert(:internal_transaction, transaction_id: transaction.id, to_address_id: address.id) + + assert [ + %InternalTransaction{ + from_address: %Ecto.Association.NotLoaded{}, + to_address: %Ecto.Association.NotLoaded{}, + transaction: %Ecto.Association.NotLoaded{} + } + ] = Chain.address_to_internal_transactions(address) + + assert [ + %InternalTransaction{ + from_address: %Address{}, + to_address: %Address{}, + transaction: %Transaction{} + } + ] = + Chain.address_to_internal_transactions( + address, + necessity_by_association: %{ + from_address: :optional, + to_address: :optional, + transaction: :optional + } + ) + end + end + describe "transaction_hash_to_internal_transactions/1" do test "without transaction" do assert Chain.transaction_hash_to_internal_transactions("unknown").entries == [] @@ -615,7 +658,7 @@ defmodule Explorer.ChainTest do assert Enum.member?(results, second.id) end - test "with transaction with internal transactions loads associations with in necessity_by_assocation" do + test "with transaction with internal transactions loads associations with in necessity_by_association" do %Transaction{hash: hash, id: transaction_id} = insert(:transaction) insert(:internal_transaction, transaction_id: transaction_id, index: 0) insert(:internal_transaction, transaction_id: transaction_id, index: 1)