Add address transaction count query back and include token transfers

pull/753/head
Tim Mecklem 6 years ago committed by Stamates
parent 285fa9fa1e
commit d5d039a78d
  1. 53
      apps/explorer/lib/explorer/chain.ex
  2. 72
      apps/explorer/test/explorer/chain_test.exs

@ -135,32 +135,33 @@ defmodule Explorer.Chain do
Counts the number of `t:Explorer.Chain.Transaction.t/0` to or from the `address`.
"""
@spec address_to_transaction_count(Address.t()) :: non_neg_integer()
def address_to_transaction_count(%Address{hash: _hash}) do
# {:ok, %{rows: [[result]]}} =
# SQL.query(
# Repo,
# """
# SELECT COUNT(hash) from
# (
# SELECT t0."hash" address
# FROM "transactions" AS t0
# LEFT OUTER JOIN "internal_transactions" AS i1 ON (i1."transaction_hash" = t0."hash") AND (i1."type" = 'create')
# WHERE (i1."created_contract_address_hash" = $1 AND t0."to_address_hash" IS NULL)
# UNION
# SELECT t0."hash" address
# FROM "transactions" AS t0
# WHERE (t0."to_address_hash" = $1)
# OR (t0."from_address_hash" = $1)
# ) AS hash
# """,
# [hash.bytes]
# )
# result
5
def address_to_transaction_count(%Address{hash: address_hash}) do
{:ok, %{rows: [[result]]}} =
Repo.query(
"""
SELECT COUNT(*) FROM
(
SELECT * FROM transactions AS t0 WHERE t0.from_address_hash = $1
UNION
SELECT * FROM transactions AS t0 WHERE t0.to_address_hash = $1
UNION
SELECT * FROM transactions AS t0 WHERE t0.created_contract_address_hash = $1
UNION
SELECT t0.* FROM transactions AS t0
LEFT JOIN token_transfers AS tt
ON tt.transaction_hash = t0.hash
WHERE tt.from_address_hash = $1
UNION
SELECT t0.* FROM transactions AS t0
LEFT JOIN token_transfers AS tt
ON tt.transaction_hash = t0.hash
WHERE tt.to_address_hash = $1
) as t
""",
[address_hash.bytes]
)
result
end
@doc """

@ -29,53 +29,53 @@ defmodule Explorer.ChainTest do
end
end
# describe "address_to_transaction_count/1" do
# test "without transactions" do
# address = insert(:address)
describe "address_to_transaction_count/1" do
test "without transactions" do
address = insert(:address)
# assert Chain.address_to_transaction_count(address) == 0
# end
assert Chain.address_to_transaction_count(address) == 0
end
# test "with transactions" do
# %Transaction{from_address: address} = insert(:transaction) |> Repo.preload(:from_address)
# insert(:transaction, to_address: address)
test "with transactions" do
%Transaction{from_address: address} = insert(:transaction) |> Repo.preload(:from_address)
insert(:transaction, to_address: address)
# assert Chain.address_to_transaction_count(address) == 2
# end
assert Chain.address_to_transaction_count(address) == 2
end
# test "with contract creation transactions the contract address is counted" do
# address = insert(:address)
test "with contract creation transactions the contract address is counted" do
address = insert(:address)
# insert(
# :internal_transaction_create,
# created_contract_address: address,
# index: 0,
# transaction: insert(:transaction, to_address: nil)
# )
insert(
:internal_transaction_create,
created_contract_address: address,
index: 0,
transaction: insert(:transaction, to_address: nil)
)
# assert Chain.address_to_transaction_count(address) == 1
# end
assert Chain.address_to_transaction_count(address) == 1
end
# test "doesn't double count addresses when to_address = from_address" do
# %Transaction{from_address: address} = insert(:transaction) |> Repo.preload(:from_address)
# insert(:transaction, to_address: address, from_address: address)
test "doesn't double count addresses when to_address = from_address" do
%Transaction{from_address: address} = insert(:transaction) |> Repo.preload(:from_address)
insert(:transaction, to_address: address, from_address: address)
# assert Chain.address_to_transaction_count(address) == 2
# end
assert Chain.address_to_transaction_count(address) == 2
end
# test "does not count non-contract-creation parent transactions" do
# transaction_with_to_address =
# %Transaction{} =
# :transaction
# |> insert()
# |> with_block()
test "does not count non-contract-creation parent transactions" do
transaction_with_to_address =
%Transaction{} =
:transaction
|> insert()
|> with_block()
# %InternalTransaction{created_contract_address: address} =
# insert(:internal_transaction_create, transaction: transaction_with_to_address, index: 0)
%InternalTransaction{created_contract_address: address} =
insert(:internal_transaction_create, transaction: transaction_with_to_address, index: 0)
# assert Chain.address_to_transaction_count(address) == 0
# end
# end
assert Chain.address_to_transaction_count(address) == 0
end
end
describe "address_to_transactions/2" do
test "without transactions" do

Loading…
Cancel
Save