fetch all transactions for csv export

pull/2206/head
Ayrat Badykov 6 years ago
parent 1d90f370d5
commit b2b0be905e
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 24
      apps/explorer/lib/explorer/chain/address_transaction_csv_exporter.ex
  2. 33
      apps/explorer/test/explorer/chain/address_transaction_csv_exporter_test.exs
  3. 2
      apps/indexer/mix.exs

@ -1,6 +1,7 @@
defmodule Explorer.Chain.AddressTransactionCsvExporter do
alias Explorer.Chain
alias Explorer.Chain.{Address, Transaction}
alias Explorer.PagingOptions
@necessity_by_association [
necessity_by_association: %{
@ -15,13 +16,34 @@ defmodule Explorer.Chain.AddressTransactionCsvExporter do
}
]
@page_size 150
@paging_options %PagingOptions{page_size: @page_size + 1}
def export(address) do
address
|> Chain.address_to_transactions_with_rewards(@necessity_by_association)
|> fetch_all_transactions(@paging_options)
|> to_csv_format(address)
|> dump_data_to_csv()
end
defp fetch_all_transactions(address, paging_options, acc \\ []) do
options = Keyword.merge(@necessity_by_association, paging_options: paging_options)
transactions = Chain.address_to_transactions_with_rewards(address, options)
new_acc = transactions ++ acc
case Enum.split(transactions, @page_size) do
{_, _transactions, [%Transaction{block_number: block_number, index: index}]} ->
new_paging_options = %{@paging_options | key: {block_number, index}}
fetch_all_transactions(address, new_paging_options, new_acc)
{_, []} ->
new_acc
end
end
defp dump_data_to_csv(transactions) do
{:ok, path} = Briefly.create()

@ -39,5 +39,38 @@ defmodule Explorer.Chain.AddressTransactionCsvExporterTest do
assert result.hash == to_string(transaction.hash)
assert result.type == "OUT"
end
test "fetches all transactions" do
address = insert(:address)
1..200
|> Enum.map(fn _ ->
:transaction
|> insert(from_address: address)
|> with_block()
end)
|> Enum.count()
result =
address
|> AddressTransactionCsvExporter.export()
|> File.stream!()
|> NimbleCSV.RFC4180.parse_stream()
|> Stream.map(fn [hash, block_number, timestamp, from_address, to_address, created_address, type, value] ->
%{
hash: hash,
block_number: block_number,
timestamp: timestamp,
from_address: from_address,
to_address: to_address,
created_address: created_address,
type: type,
value: value
}
end)
|> Enum.to_list()
assert Enum.count(result) == 200
end
end
end

@ -49,7 +49,7 @@ defmodule Indexer.MixProject do
# JSONRPC access to Parity for `Explorer.Indexer`
{:ethereum_jsonrpc, in_umbrella: true},
# RLP encoding
{:ex_rlp, "~> 0.3"},
{:ex_rlp, "~> 0.5.2"},
# Code coverage
{:excoveralls, "~> 0.10.0", only: [:test], github: "KronicDeth/excoveralls", branch: "circle-workflows"},
# Importing to database

Loading…
Cancel
Save