From 5f77a6a879e143a5fe3369d56cb317fd07229633 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 11 Jun 2019 14:57:51 +0300 Subject: [PATCH] add token transfers csv endpoint --- .../address_transaction_controller.ex | 21 +++++++++++++++++++ .../lib/block_scout_web/router.ex | 2 ++ .../address_transaction_controller_test.exs | 18 ++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex index da22b8c042..808d470dde 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex @@ -10,6 +10,7 @@ defmodule BlockScoutWeb.AddressTransactionController do alias BlockScoutWeb.TransactionView alias Explorer.{Chain, Market} + alias Explorer.Chain.AddressTokenTransferCsvExporter alias Explorer.ExchangeRates.Token alias Indexer.Fetcher.CoinBalanceOnDemand alias Phoenix.View @@ -106,4 +107,24 @@ defmodule BlockScoutWeb.AddressTransactionController do not_found(conn) end end + + def token_transfers_csv(conn, %{"address_id" => address_hash_string}) do + with {:ok, address_hash} <- Chain.string_to_address_hash(address_hash_string), + {:ok, address} <- Chain.hash_to_address(address_hash) do + address + |> AddressTokenTransferCsvExporter.export() + |> Enum.into( + conn + |> put_resp_content_type("application/csv") + |> put_resp_header("content-disposition", "attachment; filename=transactions.csv") + |> send_chunked(200) + ) + else + :error -> + unprocessable_entity(conn) + + {:error, :not_found} -> + not_found(conn) + end + end end diff --git a/apps/block_scout_web/lib/block_scout_web/router.ex b/apps/block_scout_web/lib/block_scout_web/router.ex index 786c77cdff..b1a4ecc8d2 100644 --- a/apps/block_scout_web/lib/block_scout_web/router.ex +++ b/apps/block_scout_web/lib/block_scout_web/router.ex @@ -244,6 +244,8 @@ defmodule BlockScoutWeb.Router do get("/token_autocomplete", ChainController, :token_autocomplete) + get("/token_transfers_csv", AddressTransactionController, :token_transfers_csv) + get("/chain_blocks", ChainController, :chain_blocks, as: :chain_blocks) get("/api_docs", APIDocsController, :index) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/address_transaction_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/address_transaction_controller_test.exs index 028387501a..1f94a7b8ea 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/address_transaction_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/address_transaction_controller_test.exs @@ -132,4 +132,22 @@ defmodule BlockScoutWeb.AddressTransactionControllerTest do end) end end + + describe "GET token_transfers_csv/2" do + test "exports token transfers to csv", %{conn: conn} do + address = insert(:address) + + transaction = + :transaction + |> insert(from_address: address) + |> with_block() + + insert(:token_transfer, transaction: transaction, from_address: address) + insert(:token_transfer, transaction: transaction, to_address: address) + + conn = get(conn, "/token_transfers_csv", %{"address_id" => to_string(address.hash)}) + + assert conn.resp_body |> String.split("\n") |> Enum.count() == 4 + end + end end