Skip to content

Commit

Permalink
chore: accept contract param besides contract_id (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Sep 13, 2022
1 parent 0540074 commit af3471f
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 61 deletions.
49 changes: 6 additions & 43 deletions lib/ae_mdw/aexn_transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,6 @@ defmodule AeMdw.AexnTransfers do
)
end

@spec fetch_contract_recipient_transfers(
State.t(),
AeMdw.Txs.txi(),
pubkey() | nil,
pagination(),
cursor() | nil
) ::
contract_paginated_transfers()
def fetch_contract_recipient_transfers(state, contract_txi, recipient_pk, pagination, cursor) do
paginate_transfers(
state,
contract_txi,
pagination,
Model.AexnContractToTransfer,
cursor,
recipient_pk
)
end

@spec fetch_sender_transfers(State.t(), aexn_type(), pubkey(), pagination(), cursor() | nil) ::
account_paginated_transfers()
def fetch_sender_transfers(state, aexn_type, sender_pk, pagination, cursor) do
Expand Down Expand Up @@ -137,33 +118,15 @@ defmodule AeMdw.AexnTransfers do
#
defp paginate_transfers(
state,
aexn_type,
aexn_type_or_txi,
pagination,
table,
cursor,
account_pk_or_pair_pks
) do
cursor_key = deserialize_cursor(cursor)

do_paginate_transfers(
state,
aexn_type,
pagination,
table,
cursor_key,
account_pk_or_pair_pks
)
end

defp do_paginate_transfers(
state,
aexn_type_or_txi,
pagination,
table,
cursor_key,
params
) do
key_boundary = key_boundary(aexn_type_or_txi, params)
key_boundary = key_boundary(aexn_type_or_txi, account_pk_or_pair_pks)

{prev_cursor_key, transfer_keys, next_cursor_key} =
state
Expand Down Expand Up @@ -219,15 +182,15 @@ defmodule AeMdw.AexnTransfers do

defp key_boundary(type_or_txi, nil) do
{
{type_or_txi, nil, 0, nil, 0, 0},
{type_or_txi, Util.max_256bit_bin(), nil, nil, 0, 0}
{type_or_txi, <<>>, 0, <<>>, 0, 0},
{type_or_txi, Util.max_256bit_bin(), nil, <<>>, 0, 0}
}
end

defp key_boundary(type_or_txi, account_pk) do
{
{type_or_txi, account_pk, 0, nil, 0, 0},
{type_or_txi, account_pk, nil, nil, 0, 0}
{type_or_txi, account_pk, 0, <<>>, 0, 0},
{type_or_txi, account_pk, nil, <<>>, 0, 0}
}
end
end
3 changes: 3 additions & 0 deletions lib/ae_mdw/contracts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ defmodule AeMdw.Contracts do
defp convert_param(state, {"contract_id", contract_id}),
do: {:create_txi, create_txi!(state, contract_id)}

defp convert_param(state, {"contract", contract_id}),
do: {:create_txi, create_txi!(state, contract_id)}

defp convert_param(_state, {"data", data}), do: {:data_prefix, URI.decode(data)}

defp convert_param(_state, {"event", ctor_name}),
Expand Down
8 changes: 8 additions & 0 deletions lib/ae_mdw_web/controllers/aexn_transfer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ defmodule AeMdwWeb.AexnTransferController do
end
end

def aex141_transfers_from(conn, %{"contract" => contract_id} = params) do
aex141_transfers_from(conn, Map.put(params, "contract_id", contract_id))
end

def aex141_transfers_from(conn, %{"sender" => sender_id}) do
transfers_from_reply(conn, :aex141, sender_id)
end
Expand All @@ -99,6 +103,10 @@ defmodule AeMdwWeb.AexnTransferController do
end
end

def aex141_transfers_to(conn, %{"contract" => contract_id} = params) do
aex141_transfers_to(conn, Map.put(params, "contract_id", contract_id))
end

def aex141_transfers_to(conn, %{"recipient" => recipient_id}) do
transfers_to_reply(conn, :aex141, recipient_id)
end
Expand Down
132 changes: 114 additions & 18 deletions test/ae_mdw_web/controllers/aexn_transfer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,50 @@ defmodule AeMdwWeb.AexnTransferControllerTest do
conn |> with_store(store) |> get(prev_aex141_transfers) |> json_response(200)
end

test "gets some aex141 transfers sorted by asc txi filtered by contract", %{
conn: conn,
store: store
} do
sender_id = enc_id(@from_pk2)
contract_id = enc_ct(@contract_pk1)
limit = 3

assert %{"data" => aex141_transfers, "next" => next} =
conn
|> with_store(store)
|> get("/v2/aex141/transfers/from/#{sender_id}",
contract_id: contract_id,
direction: "forward",
limit: limit
)
|> json_response(200)

assert length(aex141_transfers) == 3
assert ^aex141_transfers = Enum.sort_by(aex141_transfers, & &1["call_txi"])

assert Enum.all?(
aex141_transfers,
&aex141_valid_sender_transfer?(sender_id, &1, [contract_id])
)

assert %{"data" => next_aex141_transfers, "prev" => prev_aex141_transfers} =
conn |> with_store(store) |> get(next) |> json_response(200)

assert length(next_aex141_transfers) == 3
assert ^next_aex141_transfers = Enum.sort_by(next_aex141_transfers, & &1["call_txi"])

assert List.first(next_aex141_transfers)["call_txi"] >=
List.last(aex141_transfers)["call_txi"]

assert Enum.all?(
next_aex141_transfers,
&aex141_valid_sender_transfer?(sender_id, &1, [contract_id])
)

assert %{"data" => ^aex141_transfers} =
conn |> with_store(store) |> get(prev_aex141_transfers) |> json_response(200)
end

test "returns empty list when no transfer exists", %{conn: conn} do
account_id_without_transfer = enc_id(:crypto.strong_rand_bytes(32))

Expand Down Expand Up @@ -461,6 +505,50 @@ defmodule AeMdwWeb.AexnTransferControllerTest do
conn |> with_store(store) |> get(prev_aex141_transfers) |> json_response(200)
end

test "gets some aex141 transfers sorted by asc txi filtered by contract", %{
conn: conn,
store: store
} do
recipient_id = enc_id(@to_pk2)
contract_id = enc_ct(@contract_pk2)
limit = 3

assert %{"data" => aex141_transfers, "next" => next} =
conn
|> with_store(store)
|> get("/v2/aex141/transfers/to/#{recipient_id}",
contract: contract_id,
direction: "forward",
limit: limit
)
|> json_response(200)

assert length(aex141_transfers) == limit
assert ^aex141_transfers = Enum.sort_by(aex141_transfers, & &1["call_txi"])

assert Enum.all?(
aex141_transfers,
&aex141_valid_recipient_transfer?(recipient_id, &1, [contract_id])
)

assert %{"data" => next_aex141_transfers, "prev" => prev_aex141_transfers} =
conn |> with_store(store) |> get(next) |> json_response(200)

assert length(next_aex141_transfers) == 3
assert ^next_aex141_transfers = Enum.sort_by(next_aex141_transfers, & &1["call_txi"])

assert List.first(next_aex141_transfers)["call_txi"] >=
List.last(aex141_transfers)["call_txi"]

assert Enum.all?(
next_aex141_transfers,
&aex141_valid_recipient_transfer?(recipient_id, &1, [contract_id])
)

assert %{"data" => ^aex141_transfers} =
conn |> with_store(store) |> get(prev_aex141_transfers) |> json_response(200)
end

test "returns empty list when no transfer exists", %{conn: conn} do
account_id_without_transfer = enc_id(:crypto.strong_rand_bytes(32))

Expand Down Expand Up @@ -699,30 +787,38 @@ defmodule AeMdwWeb.AexnTransferControllerTest do
contract_id == ct_id
end

defp aex141_valid_sender_transfer?(sender_id, %{
"sender" => sender,
"recipient" => recipient,
"call_txi" => call_txi,
"log_idx" => log_idx,
"token_id" => token_id,
"contract_id" => contract_id
}) do
defp aex141_valid_sender_transfer?(
sender_id,
%{
"sender" => sender,
"recipient" => recipient,
"call_txi" => call_txi,
"log_idx" => log_idx,
"token_id" => token_id,
"contract_id" => contract_id
},
contracts \\ @contracts
) do
sender == sender_id and recipient in @recipients and call_txi in @txi_range and
log_idx in @log_index_range and token_id in @aex141_token_range and
contract_id in @contracts
contract_id in contracts
end

defp aex141_valid_recipient_transfer?(recipient_id, %{
"sender" => sender,
"recipient" => recipient,
"call_txi" => call_txi,
"log_idx" => log_idx,
"token_id" => token_id,
"contract_id" => contract_id
}) do
defp aex141_valid_recipient_transfer?(
recipient_id,
%{
"sender" => sender,
"recipient" => recipient,
"call_txi" => call_txi,
"log_idx" => log_idx,
"token_id" => token_id,
"contract_id" => contract_id
},
contracts \\ @contracts
) do
sender in @senders and recipient == recipient_id and call_txi in @txi_range and
log_idx in @log_index_range and token_id in @aex141_token_range and
contract_id in @contracts
contract_id in contracts
end

defp aex141_valid_pair_transfer?(sender_id, recipient_id, %{
Expand Down

0 comments on commit af3471f

Please sign in to comment.