Skip to content

Commit

Permalink
fix: restructure dex contracts swaps pagination cursor (#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Atanasov authored Dec 18, 2024
1 parent 235b267 commit d01b5d7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
8 changes: 1 addition & 7 deletions lib/ae_mdw/dex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ defmodule AeMdw.Dex do
fn direction ->
create_txis
|> Enum.map(fn create_txi ->
cursor =
case cursor do
{account_pk, txi, log_idx} -> {create_txi, account_pk, txi, log_idx}
nil -> nil
end

key_boundary =
Collection.generate_key_boundary(
{create_txi, Collection.binary(), Collection.integer(), Collection.integer()}
Expand Down Expand Up @@ -293,7 +287,7 @@ defmodule AeMdw.Dex do
{create_txi, <<_pk::256>> = account_pk, txi, log_idx}
when is_integer(create_txi) and is_integer(txi) and is_integer(log_idx) <-
:erlang.binary_to_term(cursor_bin) do
{:ok, {account_pk, txi, log_idx}}
{:ok, {create_txi, account_pk, txi, log_idx}}
else
_invalid_cursor ->
{:error, ErrInput.Cursor.exception(value: cursor_hex)}
Expand Down
35 changes: 35 additions & 0 deletions test/integration/ae_mdw_web/controllers/dex_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Integration.AeMdwWeb.DexControllerTest do
use AeMdwWeb.ConnCase, async: false

alias AeMdw.Db.Model
alias AeMdw.IntegrationUtil

require Model

Expand Down Expand Up @@ -47,5 +48,39 @@ defmodule Integration.AeMdwWeb.DexControllerTest do

assert %{"data" => ^dex_swaps} = conn |> get(prev_dex_swaps) |> json_response(200)
end

test "it paginates", %{conn: conn} do
IntegrationUtil.test_pagination(conn, %IntegrationUtil.PaginationParams{
url: "/v3/debug/dex/#{@ae_token_id}/swaps"
})
end
end

describe "swaps endpoint for contract" do
test "it paginates", %{conn: conn} do
pubkey = "ct_2U1usf3A8ZNUcZLkZe5rEoBTxk7eJvk9fcbRDNqmRiwXCHAYN"

IntegrationUtil.test_pagination(conn, %IntegrationUtil.PaginationParams{
url: "/v3/dex/#{pubkey}/swaps"
})
end
end

describe "swaps endpoint" do
test "it paginates", %{conn: conn} do
IntegrationUtil.test_pagination(conn, %IntegrationUtil.PaginationParams{
url: "/v3/dex/swaps"
})
end
end

describe "swaps accounts endpoint" do
test "it paginates", %{conn: conn} do
account = "ak_azbNZ1XrPjXfqBqbAh1ffLNTQ1sbnuUDFvJrXjYz7JQA1saQ3"

IntegrationUtil.test_pagination(conn, %IntegrationUtil.PaginationParams{
url: "/v3/accounts/#{account}/dex/swaps"
})
end
end
end
39 changes: 39 additions & 0 deletions test/support/integration_util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ defmodule AeMdw.IntegrationUtil do
"""

import Phoenix.ConnTest
import ExUnit.Assertions

@endpoint AeMdwWeb.Endpoint

defmodule PaginationParams do
@moduledoc false
@enforce_keys [:url]
defstruct [:url, params: [], entries_range: 1..10]
end

@spec scan(any(), Conn.t(), any(), (any(), any() -> any())) :: any()
def scan(%{"next" => nil, "data" => data}, _conn, accumulator, f) do
f.(data, accumulator)
Expand All @@ -22,4 +29,36 @@ defmodule AeMdw.IntegrationUtil do
|> json_response(200)
|> scan(conn, new_acc, f)
end

@spec test_pagination(Conn.t(), PaginationParams.t()) :: any()
def test_pagination(conn, %PaginationParams{
url: url,
params: params,
entries_range: entries_range
}) do
for limit <- entries_range do
%{"data" => initial_data, "next" => next} =
conn
|> get(url, params ++ [{:limit, limit}])
|> json_response(200)

%URI{path: path, query: query} = URI.parse(next)

%{"prev" => prev, "data" => next_data} =
conn
|> get(path <> "?" <> query)
|> json_response(200)

refute initial_data == next_data

%URI{path: path, query: query} = URI.parse(prev)

%{"data" => prev_data} =
conn
|> get(path <> "?" <> query)
|> json_response(200)

assert initial_data == prev_data
end
end
end

0 comments on commit d01b5d7

Please sign in to comment.