Skip to content

Commit

Permalink
Merge pull request #15 from Polymarket/fix/filter-params
Browse files Browse the repository at this point in the history
Feat: filter params
  • Loading branch information
JonathanAmenechi authored Apr 8, 2022
2 parents 6c40214 + 6726407 commit 81bdb23
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 27 deletions.
4 changes: 2 additions & 2 deletions examples/get_open_orders.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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!")

Expand Down
4 changes: 2 additions & 2 deletions examples/get_order_history.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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!")

Expand Down
6 changes: 4 additions & 2 deletions examples/get_trade_history.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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!")

Expand Down
32 changes: 12 additions & 20 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -200,19 +200,16 @@ 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
"""
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):
"""
Expand All @@ -231,32 +228,27 @@ 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
"""
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
"""
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):
"""
Expand Down
6 changes: 6 additions & 0 deletions py_clob_client/clob_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

31 changes: 31 additions & 0 deletions py_clob_client/http_helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import requests

from py_clob_client.clob_types import FilterParams

from ..exceptions import PolyApiException

GET = "GET"
Expand All @@ -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

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_clob_client",
version="0.0.12",
version="0.0.13",
author="Jonathan Amenechi",
author_email="[email protected]",
description="Python client for the Polymarket CLOB",
Expand Down

0 comments on commit 81bdb23

Please sign in to comment.