diff --git a/apps/omg_watcher/lib/db/transaction.ex b/apps/omg_watcher/lib/db/transaction.ex index 49b12db827..d08c810c60 100644 --- a/apps/omg_watcher/lib/db/transaction.ex +++ b/apps/omg_watcher/lib/db/transaction.ex @@ -23,7 +23,6 @@ defmodule OMG.Watcher.DB.Transaction do alias OMG.API.Utxo alias OMG.Watcher.DB alias OMG.Watcher.DB.Repo - alias OMG.Watcher.DB.TxOutput require Utxo @@ -59,16 +58,16 @@ defmodule OMG.Watcher.DB.Transaction do |> Repo.all() end - def get_by_recipient(recipient, limit) do - from(TxOutput, - where: [owner: ^recipient], - order_by: [desc: :blknum, desc: :txindex, desc: :oindex], + def get_by_address(address, limit) do + from(tx in __MODULE__, + left_join: output in assoc(tx, :outputs), + left_join: input in assoc(tx, :inputs), + where: output.owner == ^address or input.owner == ^address, + order_by: [desc: tx.blknum, desc: tx.txindex], limit: ^limit, - preload: [:creating_transaction] + preload: [outputs: output, inputs: input] ) |> Repo.all() - |> Enum.filter(fn txoutput -> txoutput.creating_transaction != nil end) - |> Enum.map(fn txoutput -> txoutput.creating_transaction end) end def get_by_blknum(blknum) do diff --git a/apps/omg_watcher/lib/web/controllers/transaction.ex b/apps/omg_watcher/lib/web/controllers/transaction.ex index a854353935..117ab183a3 100644 --- a/apps/omg_watcher/lib/web/controllers/transaction.ex +++ b/apps/omg_watcher/lib/web/controllers/transaction.ex @@ -43,15 +43,15 @@ defmodule OMG.Watcher.Web.Controller.Transaction do Retrieves a list of transactions """ def get_transactions(conn, params) do - recipient = Map.get(params, "recipient", nil) + address = Map.get(params, "address", nil) limit = Map.get(params, "limit", @default_transactions_limit) transactions = - if recipient == nil do + if address == nil do DB.Transaction.get_last(limit) else - {:ok, recipient_decode} = Crypto.decode_address(recipient) - DB.Transaction.get_by_recipient(recipient_decode, limit) + {:ok, address_decode} = Crypto.decode_address(address) + DB.Transaction.get_by_address(address_decode, limit) end respond_multiple(transactions, conn) @@ -207,7 +207,7 @@ defmodule OMG.Watcher.Web.Controller.Transaction do summary("Gets a list of transactions.") parameters do - recipient(:query, :string, "Recipient of the transaction", required: false) + address(:query, :string, "Address of the sender or recipient", required: false) limit(:query, :integer, "Limits number of transactions. Default value is 200", required: false) end diff --git a/apps/omg_watcher/test/web/controllers/transaction_test.exs b/apps/omg_watcher/test/web/controllers/transaction_test.exs index 65ebec3c02..80a50c841b 100644 --- a/apps/omg_watcher/test/web/controllers/transaction_test.exs +++ b/apps/omg_watcher/test/web/controllers/transaction_test.exs @@ -127,17 +127,17 @@ defmodule OMG.Watcher.Web.Controller.TransactionTest do end @tag fixtures: [:initial_blocks, :alice] - test "endpoint returns last 3 transactions filtered by recipient", %{ + test "endpoint returns last 3 transactions filtered by address", %{ initial_blocks: _initial_blocks, alice: alice } do limit = 3 - {:ok, recipient_encoded} = Crypto.encode_address(alice.addr) - recipient = alice.addr |> TestHelper.to_response_address() + {:ok, address_encoded} = Crypto.encode_address(alice.addr) + address = alice.addr |> TestHelper.to_response_address() %{"data" => txs, "result" => "success"} = - TestHelper.rest_call(:get, "/transactions?recipient=#{recipient_encoded}&limit=#{limit}") + TestHelper.rest_call(:get, "/transactions?address=#{address_encoded}&limit=#{limit}") assert [ %{"txblknum" => 3000, "txindex" => 1}, @@ -145,7 +145,7 @@ defmodule OMG.Watcher.Web.Controller.TransactionTest do %{"txblknum" => 2000, "txindex" => 0} ] == Enum.map(txs, fn tx -> %{"txblknum" => tx["txblknum"], "txindex" => tx["txindex"]} end) - assert [true, true, true] == for(tx <- txs, do: tx["newowner1"] == recipient or tx["newowner2"] == recipient) + assert [true, true, true] == for(tx <- txs, do: tx["newowner1"] == address or tx["newowner2"] == address) end end