From 98ac08e7c21b6b9d9189f6ab404e897971a2bde7 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:23:04 -0400 Subject: [PATCH 1/4] feat: integrate filter params into clients --- examples/get_open_orders.py | 4 ++-- examples/get_order_history.py | 4 ++-- examples/get_trade_history.py | 9 +++++--- py_clob_client/client.py | 30 +++++++++---------------- py_clob_client/clob_types.py | 6 +++++ py_clob_client/http_helpers/helpers.py | 31 ++++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 26 deletions(-) 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..0fcd018 100644 --- a/examples/get_trade_history.py +++ b/examples/get_trade_history.py @@ -1,20 +1,23 @@ 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 load_dotenv() def main(): - host = "http://localhost:8080" + # host = "http://localhost:8080" + host = "https://clob-staging.polymarket.com" key = os.getenv("PK") creds = ApiCreds(api_key=os.getenv("CLOB_API_KEY"), api_secret=os.getenv("CLOB_SECRET"), api_passphrase=os.getenv("CLOB_PASS_PHRASE")) 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..1467a9e 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 @@ -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 + From 0ed37d50656c83372025298c39c490a0fc83ffbe Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:23:33 -0400 Subject: [PATCH 2/4] chore: bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 4b75b71a72d356fb23822401fd9de2b74bc3e8dd Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:28:00 -0400 Subject: [PATCH 3/4] chore: fix examples --- examples/get_trade_history.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/get_trade_history.py b/examples/get_trade_history.py index 0fcd018..0567e40 100644 --- a/examples/get_trade_history.py +++ b/examples/get_trade_history.py @@ -8,8 +8,7 @@ load_dotenv() def main(): - # host = "http://localhost:8080" - host = "https://clob-staging.polymarket.com" + host = "http://localhost:8080" key = os.getenv("PK") creds = ApiCreds(api_key=os.getenv("CLOB_API_KEY"), api_secret=os.getenv("CLOB_SECRET"), api_passphrase=os.getenv("CLOB_PASS_PHRASE")) chain_id = 80001 From 672640710c6f1d1d0520b82832f42b8c0374c848 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:28:52 -0400 Subject: [PATCH 4/4] chore: docstring fix --- py_clob_client/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py_clob_client/client.py b/py_clob_client/client.py index 1467a9e..237ffc9 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -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))