From a840085ef4741decbfcdf0f9e80cc5429677e3ef Mon Sep 17 00:00:00 2001 From: talha Date: Mon, 24 Jul 2023 13:05:14 +0500 Subject: [PATCH] Added POST & DELTE /orders v2 Routes --- pyproject.toml | 2 +- src/firefly_exchange_client/client.py | 75 ++++++++++++++++++++++++ src/firefly_exchange_client/constants.py | 5 ++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0b83a62..9f72ade 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "firefly_exchange_client" -version = "0.6.0" +version = "0.6.1" description = "Library to interact with firefly exchange protocol including its off-chain api-gateway and on-chain contracts" readme = "README.md" requires-python = ">=3.8" diff --git a/src/firefly_exchange_client/client.py b/src/firefly_exchange_client/client.py index 9b4e948..3c714c6 100644 --- a/src/firefly_exchange_client/client.py +++ b/src/firefly_exchange_client/client.py @@ -308,6 +308,48 @@ async def cancel_all_orders(self, symbol:MARKET_SYMBOLS, status: List[ORDER_STAT return False + async def post_cancel_order_v2(self,params:OrderCancellationRequest): + """ + POST cancel order request to Firefly + Inputs: + - params(dict): a dictionary with OrderCancellationRequest required params + Returns: + - dict: response from orders delete API Firefly + """ + + return self.apis.delete( + SERVICE_URLS["V2_ORDERS"]["ORDERS_HASH"], + { + "symbol": params["symbol"], + "orderHashes":params["hashes"], + "cancelSignature":params["signature"] + }, + auth_required=True + ) + + async def cancel_all_orders_v2(self,symbol:MARKET_SYMBOLS): + """ + GETs all open orders for the specified symbol, creates a cancellation request + for all orders and POSTs the cancel order request to Firefly + Inputs: + - symbol(MARKET_SYMBOLS) + Returns: + - dict: response from orders delete API Firefly + """ + orders = await self.get_orders({ + "symbol":symbol, + "statuses":[ORDER_STATUS.OPEN, ORDER_STATUS.PARTIAL_FILLED] + }) + + hashes = [] + for i in orders: + hashes.append(i["hash"]) + + if len(hashes) > 0: + req = self.create_signed_cancel_orders(symbol,hashes) + return await self.post_cancel_order_v2(req) + + return False async def post_signed_order(self, params:PlaceOrderRequest): """ Creates an order from provided params and signs it using the private @@ -342,6 +384,39 @@ async def post_signed_order(self, params:PlaceOrderRequest): auth_required=True ) + async def post_signed_order_v2(self, params:PlaceOrderRequest): + """ + Creates an order from provided params and signs it using the private + key of the account + + Inputs: + params (OrderSignatureRequest): parameters to create order with + + Returns: + OrderSignatureResponse: order raw info and generated signature + """ + + return self.apis.post( + SERVICE_URLS["V2_ORDERS"]["ORDERS"], + { + "symbol": params["symbol"], + "price": to_wei(params["price"], "ether"), + "quantity": to_wei(params["quantity"], "ether"), + "leverage": to_wei(params["leverage"], "ether"), + "userAddress": params["maker"], + "orderType": params["orderType"].value, + "side": params["side"].value, + "reduceOnly": params["reduceOnly"], + "salt": params["salt"], + "expiration": params["expiration"], + "orderSignature": params["orderSignature"], + "timeInForce": default_enum_value(params, "timeInForce", TIME_IN_FORCE.GOOD_TILL_TIME), + "postOnly": default_value(params, "postOnly", False), + "clientId": "firefly-client: {}".format(params["clientId"]) if "clientId" in params else "firefly-client" + }, + auth_required=True + ) + ## Contract calls async def deposit_margin_to_bank(self, amount): """ diff --git a/src/firefly_exchange_client/constants.py b/src/firefly_exchange_client/constants.py index 057ab41..063c1fd 100644 --- a/src/firefly_exchange_client/constants.py +++ b/src/firefly_exchange_client/constants.py @@ -103,6 +103,11 @@ "ORDERS": "/orders", "ORDERS_HASH": "/orders/hash", }, + "V2_ORDERS": { + "ORDERS": "/v2/user/orders", + "ORDERS_HASH": "/v2/user/orders/hash", + }, + } EIP712_CANCEL_ORDER_STRUCT_STRING ="CancelLimitOrder(string action,bytes32[] orderHashes)"