diff --git a/examples/get_open_orders.py b/examples/get_open_orders.py index e0b56c6..4a47918 100644 --- a/examples/get_open_orders.py +++ b/examples/get_open_orders.py @@ -1,7 +1,7 @@ import os from py_clob_client.client import ClobClient -from py_clob_client.clob_types import ApiCreds, LimitOrderArgs +from py_clob_client.clob_types import ApiCreds, FilterParams, LimitOrderArgs from dotenv import load_dotenv from py_clob_client.orders.constants import BUY @@ -16,7 +16,7 @@ def main(): chain_id = 80001 client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) - resp = client.get_open_orders(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088") + resp = client.get_open_orders(FilterParams(max=1, market="16678291189211314787145083999015737376658799626183230671758641503291735614088")) print(resp) print("Done!") diff --git a/examples/get_order_history.py b/examples/get_order_history.py index e0851cd..576e4fe 100644 --- a/examples/get_order_history.py +++ b/examples/get_order_history.py @@ -1,7 +1,7 @@ import os from py_clob_client.client import ClobClient -from py_clob_client.clob_types import ApiCreds +from py_clob_client.clob_types import ApiCreds, FilterParams from dotenv import load_dotenv from pprint import pprint @@ -15,7 +15,7 @@ def main(): chain_id = 80001 client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) - resp = client.get_order_history(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088") + resp = client.get_order_history(FilterParams(max=1, market="16678291189211314787145083999015737376658799626183230671758641503291735614088")) pprint(resp) print("Done!") diff --git a/examples/get_trade_history.py b/examples/get_trade_history.py index 8b404f8..0567e40 100644 --- a/examples/get_trade_history.py +++ b/examples/get_trade_history.py @@ -1,7 +1,7 @@ import os from py_clob_client.client import ClobClient -from py_clob_client.clob_types import ApiCreds +from py_clob_client.clob_types import ApiCreds, FilterParams from dotenv import load_dotenv from pprint import pprint @@ -14,7 +14,9 @@ def main(): chain_id = 80001 client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) - resp = client.get_trade_history(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088") + resp = client.get_trade_history( + FilterParams(max=1, market="16678291189211314787145083999015737376658799626183230671758641503291735614088") + ) pprint(resp) print("Done!") diff --git a/py_clob_client/client.py b/py_clob_client/client.py index b9699fd..237ffc9 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -6,9 +6,9 @@ from .signer import Signer from .endpoints import CANCEL, CANCEL_ALL, CREATE_API_KEY, DELETE_API_KEY, GET_API_KEYS, GET_ORDER, GET_ORDER_BOOK, MID_POINT, OPEN_ORDERS, ORDER_HISTORY, POST_ORDER, PRICE, TIME, TRADE_HISTORY -from .clob_types import ApiCreds, LimitOrderArgs, MarketOrderArgs, RequestArgs +from .clob_types import ApiCreds, FilterParams, LimitOrderArgs, MarketOrderArgs, RequestArgs from .exceptions import PolyException -from .http_helpers.helpers import delete, get, post +from .http_helpers.helpers import add_query_params, delete, get, post from py_order_utils.config import get_contract_config from .constants import CREDENTIAL_CREATION_WARNING, L0, L1, L1_AUTH_UNAVAILABLE, L2, L2_AUTH_UNAVAILABLE @@ -132,7 +132,7 @@ def get_midpoint(self, tokenID): def get_price(self, tokenID, side): """ - Get the mid market price for the given market + Get the market price for the given market """ return get("{}{}?price={}&side={}".format(self.host, PRICE, tokenID, side)) @@ -200,7 +200,7 @@ def cancel_all(self): headers = create_level_2_headers(self.signer, self.creds, request_args) return delete("{}{}".format(self.host, CANCEL_ALL), headers=headers) - def get_open_orders(self, tokenID = None): + def get_open_orders(self, params: FilterParams = None): """ Gets open orders for the API key Requires Level 2 authentication @@ -208,11 +208,8 @@ def get_open_orders(self, tokenID = None): self.assert_level_2_auth() request_args = RequestArgs(method="GET", request_path=OPEN_ORDERS) headers = create_level_2_headers(self.signer, self.creds, request_args) - - if tokenID is not None: - return get("{}{}?market={}".format(self.host, OPEN_ORDERS, tokenID), headers=headers) - - return get("{}{}".format(self.host, OPEN_ORDERS), headers=headers) + url = add_query_params("{}{}".format(self.host, OPEN_ORDERS), params) + return get(url, headers=headers) def get_order_book(self, token_id): """ @@ -231,7 +228,7 @@ def get_order(self, order_id): headers = create_level_2_headers(self.signer, self.creds, request_args) return get("{}{}".format(self.host, endpoint), headers=headers) - def get_trade_history(self, tokenID = None): + def get_trade_history(self, params: FilterParams=None): """ Fetches the trade history for a user Requires Level 2 authentication @@ -239,12 +236,10 @@ def get_trade_history(self, tokenID = None): self.assert_level_2_auth() request_args = RequestArgs(method="GET", request_path=TRADE_HISTORY) headers = create_level_2_headers(self.signer, self.creds, request_args) - if tokenID is not None: - return get("{}{}?market={}".format(self.host, TRADE_HISTORY, tokenID), headers=headers) - - return get("{}{}".format(self.host, TRADE_HISTORY), headers=headers) + url = add_query_params("{}{}".format(self.host, TRADE_HISTORY), params) + return get(url, headers=headers) - def get_order_history(self, tokenID = None): + def get_order_history(self, params: FilterParams = None): """ Fetches order history for a user Requires Level 2 Authentication @@ -252,11 +247,8 @@ def get_order_history(self, tokenID = None): self.assert_level_2_auth() request_args = RequestArgs(method="GET", request_path=ORDER_HISTORY) headers = create_level_2_headers(self.signer, self.creds, request_args) - - if tokenID is not None: - return get("{}{}?market={}".format(self.host, ORDER_HISTORY, tokenID), headers=headers) - - return get("{}{}".format(self.host, ORDER_HISTORY), headers=headers) + url = add_query_params("{}{}".format(self.host, ORDER_HISTORY), params) + return get(url, headers=headers) def assert_level_1_auth(self): """ diff --git a/py_clob_client/clob_types.py b/py_clob_client/clob_types.py index 6981f5a..a5cf1fd 100644 --- a/py_clob_client/clob_types.py +++ b/py_clob_client/clob_types.py @@ -28,4 +28,10 @@ class MarketOrderArgs: token_id: str +@dataclass +class FilterParams: + market: str = None + max: int = None + start_ts: int = None + end_ts: int = None diff --git a/py_clob_client/http_helpers/helpers.py b/py_clob_client/http_helpers/helpers.py index b4f80cb..0145e27 100644 --- a/py_clob_client/http_helpers/helpers.py +++ b/py_clob_client/http_helpers/helpers.py @@ -1,5 +1,7 @@ import requests +from py_clob_client.clob_types import FilterParams + from ..exceptions import PolyApiException GET = "GET" @@ -24,3 +26,32 @@ def get(endpoint, headers=None, data=None): def delete(endpoint, headers=None, data=None): return request(endpoint, DELETE, headers, data) + +def build_query_params(url: str, param: str, val: str)->str: + url_with_params = url + last = url_with_params[-1] + # if last character in url string == "?", append the param directly: api.com?param=value + if last == "?": + url_with_params = "{}{}={}".format(url_with_params, param, val) + else: + # else add "&", then append the param + url_with_params = "{}&{}={}".format(url_with_params, param, val) + return url_with_params + +def add_query_params(base_url: str, params: FilterParams=None)->str: + """ + Adds query parameters to a url + """ + url = base_url + if params: + url = url + "?" + if params.market: + url = build_query_params(url, "market", params.market) + if params.max: + url = build_query_params(url, "max", params.max) + if params.start_ts: + url = build_query_params(url, "startTs", params.start_ts) + if params.end_ts: + url = build_query_params(url, "endTs", params.end_ts) + return url + diff --git a/setup.py b/setup.py index 7f1de8c..f7f7445 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_clob_client", - version="0.0.12", + version="0.0.13", author="Jonathan Amenechi", author_email="jonathanamenechi@gmail.com", description="Python client for the Polymarket CLOB",