Skip to content

Commit

Permalink
Fix create orders partial failure response (#174)
Browse files Browse the repository at this point in the history
1. Fix create orders partial failure response (in case that one order was created and another failed, return details about the created order and what went wrong with the other)
2. Update e2e tests
  • Loading branch information
Luke-Rogerson authored Mar 28, 2024
1 parent 72fb8fc commit 2c1f872
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion e2e/maker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
orbs-orderbook-sdk==0.9.1
orbs-orderbook-sdk==0.10.1
requests==2.31.0
1 change: 0 additions & 1 deletion e2e/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

@pytest.fixture
def ob_client():
print("HI")
yield OrderBookSDK(base_url=BASE_URL, api_key=API_KEY)


Expand Down
95 changes: 94 additions & 1 deletion e2e/tests/maker_endpoints_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"""

import pytest
from orbs_orderbook import CreateOrderInput
from orbs_orderbook import (
CreateMultipleOrdersInput,
CreateOrderInput,
OrderWithSignature,
)
from orbs_orderbook.exceptions import ErrApiRequest
from conftest import CLIENT_OID, SIZE, SYMBOL, API_KEY

Expand Down Expand Up @@ -147,3 +151,92 @@ def test_get_orders_for_user(ob_client, ob_signer, create_new_orders):
assert res.data[0]["size"], "size not returned"
assert res.data[0]["side"], "side not returned"
assert res.data[0]["symbol"], "symbol not returned"


def test_create_multiple_orders_successfully(ob_client, ob_signer):
order_one = CreateOrderInput(
price="0.86500000",
size="40",
symbol="MATIC-USDC",
side="buy",
client_order_id="550e8400-e29b-41d4-a716-446655440000",
)
order_one_sig, order_one_msg = ob_signer.prepare_and_sign_order(order_one)

order_two = CreateOrderInput(
price="0.87",
size="40",
symbol="MATIC-USDC",
side="sell",
client_order_id="650e8400-e29b-41d4-a716-446655440001",
)
order_two_sig, order_two_msg = ob_signer.prepare_and_sign_order(order_two)

create_orders_input = CreateMultipleOrdersInput(
symbol="MATIC-USDC",
orders=[
OrderWithSignature(
order=order_one,
signature=order_one_sig,
message=order_one_msg,
),
OrderWithSignature(
order=order_two,
signature=order_two_sig,
message=order_two_msg,
),
],
)

res = ob_client.create_multiple_orders(create_orders_input)

assert len(res.created) == 2
assert res.status == 201


def test_create_multiple_orders_rejects_order_with_same_client_oid(
ob_client, ob_signer
):
"""
Test that given two orders with the same clientOrderId, the API will reject the second order.
"""
same_client_oid = "550e8400-e29b-41d4-a716-446655440000"

order_one = CreateOrderInput(
price="0.86500000",
size="40",
symbol="MATIC-USDC",
side="buy",
client_order_id=same_client_oid,
)
order_one_sig, order_one_msg = ob_signer.prepare_and_sign_order(order_one)

order_two = CreateOrderInput(
price="0.87",
size="40",
symbol="MATIC-USDC",
side="sell",
client_order_id=same_client_oid,
)
order_two_sig, order_two_msg = ob_signer.prepare_and_sign_order(order_two)

create_orders_input = CreateMultipleOrdersInput(
symbol="MATIC-USDC",
orders=[
OrderWithSignature(
order=order_one,
signature=order_one_sig,
message=order_one_msg,
),
OrderWithSignature(
order=order_two,
signature=order_two_sig,
message=order_two_msg,
),
],
)

res = ob_client.create_multiple_orders(create_orders_input)

assert len(res.created) == 1
assert res.status == 409
2 changes: 1 addition & 1 deletion e2e/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
orbs-orderbook-sdk==0.9.2
orbs-orderbook-sdk==0.10.1
pytest==7.4.4
redis==5.0.3
2 changes: 1 addition & 1 deletion transport/rest/create_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (h *Handler) CreateOrders(w http.ResponseWriter, r *http.Request) {

if len(createdOrders) != len(args.Orders) {
logctx.Warn(ctx, "not all orders were created", logger.String("userId", user.Id.String()), logger.Int("numOfOrders", len(createdOrders)), logger.Int("numOfOrdersRequested", len(args.Orders)))
restutils.WriteJSONResponse(ctx, w, http.StatusBadRequest, response, logger.String("userId", user.Id.String()))
restutils.WriteJSONResponse(ctx, w, http.StatusMultiStatus, response, logger.String("userId", user.Id.String()))
return
}

Expand Down

0 comments on commit 2c1f872

Please sign in to comment.