From b2b0be905e63bcbcb3acfb5d8ec082a780b549e0 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 5 Jun 2019 15:22:38 +0300 Subject: [PATCH] fetch all transactions for csv export --- .../chain/address_transaction_csv_exporter.ex | 24 +++++++++++++- .../address_transaction_csv_exporter_test.exs | 33 +++++++++++++++++++ apps/indexer/mix.exs | 2 +- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/address_transaction_csv_exporter.ex b/apps/explorer/lib/explorer/chain/address_transaction_csv_exporter.ex index 66d47bba5b..01dccaf3e2 100644 --- a/apps/explorer/lib/explorer/chain/address_transaction_csv_exporter.ex +++ b/apps/explorer/lib/explorer/chain/address_transaction_csv_exporter.ex @@ -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() diff --git a/apps/explorer/test/explorer/chain/address_transaction_csv_exporter_test.exs b/apps/explorer/test/explorer/chain/address_transaction_csv_exporter_test.exs index 40987e1afc..ddff5e6dea 100644 --- a/apps/explorer/test/explorer/chain/address_transaction_csv_exporter_test.exs +++ b/apps/explorer/test/explorer/chain/address_transaction_csv_exporter_test.exs @@ -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 diff --git a/apps/indexer/mix.exs b/apps/indexer/mix.exs index 9b8bd4d6ed..f70d758912 100644 --- a/apps/indexer/mix.exs +++ b/apps/indexer/mix.exs @@ -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