Skip to content

Commit

Permalink
feat: modify transactions/ endpoint to filter by address(sender and r…
Browse files Browse the repository at this point in the history
…ecipient)
  • Loading branch information
purbanow committed Oct 21, 2018
1 parent 82ecf9c commit 6613779
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
15 changes: 7 additions & 8 deletions apps/omg_watcher/lib/db/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions apps/omg_watcher/lib/web/controllers/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions apps/omg_watcher/test/web/controllers/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ 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},
%{"txblknum" => 3000, "txindex" => 0},
%{"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

Expand Down

0 comments on commit 6613779

Please sign in to comment.