Add initial `address_to_internal_transactions` function to Chain context

Co-authored-by: stamates <stamates@hotmail.com>
pull/156/head
Tim Mecklem 7 years ago
parent b0bc8ff932
commit f34960d606
  1. 10
      apps/explorer/lib/explorer/chain.ex
  2. 45
      apps/explorer/test/explorer/chain_test.exs

@ -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`

@ -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)

Loading…
Cancel
Save