add token transfers csv endpoint

pull/2242/head
Ayrat Badykov 6 years ago
parent e5f9247254
commit 5f77a6a879
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 21
      apps/block_scout_web/lib/block_scout_web/controllers/address_transaction_controller.ex
  2. 2
      apps/block_scout_web/lib/block_scout_web/router.ex
  3. 18
      apps/block_scout_web/test/block_scout_web/controllers/address_transaction_controller_test.exs

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

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

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

Loading…
Cancel
Save