From 6c21376f680ac258ae27822219793eff34e07805 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 28 Feb 2022 15:25:43 -0500 Subject: [PATCH 1/4] fix: midpoint and get open orders endpoint --- examples/get_mid_market_price.py | 24 +++++++++++++++++++++++ examples/get_open_orders.py | 24 +++++++++++++++++++++++ py_clob_client/client.py | 33 +++++++++++++++++++++++++++++--- py_clob_client/endpoints.py | 1 + setup.py | 2 +- 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 examples/get_mid_market_price.py create mode 100644 examples/get_open_orders.py diff --git a/examples/get_mid_market_price.py b/examples/get_mid_market_price.py new file mode 100644 index 0000000..7c1d446 --- /dev/null +++ b/examples/get_mid_market_price.py @@ -0,0 +1,24 @@ +import os + +from py_clob_client.client import ClobClient +from py_clob_client.clob_types import ApiCreds +from dotenv import load_dotenv + +from py_clob_client.orders.constants import BUY + + +load_dotenv() + +def main(): + 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 + client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) + + resp = client.get_midpoint("16678291189211314787145083999015737376658799626183230671758641503291735614088") + print(resp) + print("Done!") + + +main() \ No newline at end of file diff --git a/examples/get_open_orders.py b/examples/get_open_orders.py new file mode 100644 index 0000000..e0b56c6 --- /dev/null +++ b/examples/get_open_orders.py @@ -0,0 +1,24 @@ +import os + +from py_clob_client.client import ClobClient +from py_clob_client.clob_types import ApiCreds, LimitOrderArgs +from dotenv import load_dotenv + +from py_clob_client.orders.constants import BUY + + +load_dotenv() + +def main(): + 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 + client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) + + resp = client.get_open_orders(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088") + print(resp) + print("Done!") + + +main() \ No newline at end of file diff --git a/py_clob_client/client.py b/py_clob_client/client.py index 6c635e7..c280914 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -5,10 +5,11 @@ from .headers import create_level_1_headers, create_level_2_headers from .signer import Signer -from .endpoints import CANCEL, CANCEL_ALL, CREATE_API_KEY, GET_API_KEYS, GET_ORDER, OPEN_ORDERS, POST_ORDER, TIME, TRADE_HISTORY +from .endpoints import CANCEL, CANCEL_ALL, CREATE_API_KEY, GET_API_KEYS, GET_ORDER, MID_POINT, OPEN_ORDERS, POST_ORDER, TIME, TRADE_HISTORY from .clob_types import ApiCreds, LimitOrderArgs, MarketOrderArgs, RequestArgs from .exceptions import PolyException from .http_helpers.helpers import 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 @@ -30,8 +31,10 @@ def __init__(self, host, chain_id: int = None, key:str = None, creds:ApiCreds = self.signer = Signer(key, chain_id) if key else None self.creds = creds self.mode = self._get_client_mode() + if chain_id: + self.contract_config = get_contract_config(chain_id) if self.signer: - self.builder = OrderBuilder(self.signer, sig_type=signature_type, funder=funder) + self.builder = OrderBuilder(self.signer, sig_type=signature_type, funder=funder) self.logger = logging.getLogger(self.__class__.__name__) def get_address(self): @@ -80,6 +83,12 @@ def get_api_keys(self): headers = create_level_2_headers(self.signer, self.creds, request_args) return get("{}{}".format(self.host, GET_API_KEYS), headers=headers) + def get_midpoint(self, tokenID): + """ + Get the mid market price for the given market + """ + return get("{}{}?market={}&tokenID={}".format(self.host, MID_POINT, self.contract_config.conditional, tokenID)) + def create_limit_order(self, order_args: LimitOrderArgs): """ Creates and signs a limit order @@ -106,6 +115,20 @@ def post_order(self, order): headers = create_level_2_headers(self.signer, self.creds, RequestArgs(method="POST", request_path=POST_ORDER, body=body)) return post("{}{}".format(self.host, POST_ORDER), headers=headers, data=body) + def create_and_post_limit_order(self, order_args: LimitOrderArgs): + """ + Utility function to create and publish a limit order + """ + lim = self.create_limit_order(order_args) + return self.post_order(lim) + + def create_and_post_market_order(self, order_args: MarketOrderArgs): + """ + Utility function to create and publish a market order + """ + mkt = self.create_market_order(order_args) + return self.post_order(mkt) + def cancel(self, order_id): """ Cancels an order @@ -130,7 +153,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): + def get_open_orders(self, tokenID = None): """ Gets open orders for the API key Requires Level 2 authentication @@ -138,6 +161,10 @@ def get_open_orders(self): 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={}&tokenID={}".format(self.host, OPEN_ORDERS, self.contract_config.conditional, tokenID), headers=headers) + return get("{}{}".format(self.host, OPEN_ORDERS), headers=headers) def get_order(self, order_id): diff --git a/py_clob_client/endpoints.py b/py_clob_client/endpoints.py index f411aaf..d869ba1 100644 --- a/py_clob_client/endpoints.py +++ b/py_clob_client/endpoints.py @@ -7,3 +7,4 @@ POST_ORDER = "/order" CANCEL = "/order" CANCEL_ALL = "/cancel-all" +MID_POINT="/midpoint" diff --git a/setup.py b/setup.py index e1402a3..68f5306 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_clob_client", - version="0.0.3", + version="0.0.4", author="Jonathan Amenechi", author_email="jonathanamenechi@gmail.com", description="Python client for the Polymarket CLOB", From 246b79d50514a6acf614103270cfa763d61d6dac Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 28 Feb 2022 15:28:55 -0500 Subject: [PATCH 2/4] chore: minor comment in example --- examples/get_mid_market_price.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/get_mid_market_price.py b/examples/get_mid_market_price.py index 7c1d446..ccb4438 100644 --- a/examples/get_mid_market_price.py +++ b/examples/get_mid_market_price.py @@ -17,6 +17,7 @@ def main(): client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) resp = client.get_midpoint("16678291189211314787145083999015737376658799626183230671758641503291735614088") + # {'mid': '0.55'} print(resp) print("Done!") From bb42555c3a82e34fed8dfda9c3511dc2631ac0cc Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 28 Feb 2022 15:55:04 -0500 Subject: [PATCH 3/4] fix: bug fix on cancel --- 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 c280914..c4db10b 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -141,7 +141,7 @@ def cancel(self, order_id): request_args = RequestArgs(method="DELETE", request_path=CANCEL, body=body) headers = create_level_2_headers(self.signer, self.creds, request_args) - return delete("{}{}".format(self.host, CANCEL), headers=headers, body=body) + return delete("{}{}".format(self.host, CANCEL), headers=headers, data=body) def cancel_all(self): """ From 6d3591464333fbc131aa96c909fa69ad93af5350 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 28 Feb 2022 16:41:04 -0500 Subject: [PATCH 4/4] fix: more helper functions --- py_clob_client/client.py | 18 ++++++++++++++++-- py_clob_client/http_helpers/helpers.py | 1 - 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/py_clob_client/client.py b/py_clob_client/client.py index c4db10b..cdf6dc4 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -42,6 +42,20 @@ def get_address(self): Returns the public address of the signer """ return self.signer.address if self.signer else None + + def get_collateral_address(self): + """ + Returns the collateral token address + """ + if self.contract_config: + return self.contract_config.get_collateral() + + def get_conditional_address(self): + """ + Returns the conditional token address + """ + if self.contract_config: + return self.contract_config.get_conditional() def get_ok(self): """ @@ -87,7 +101,7 @@ def get_midpoint(self, tokenID): """ Get the mid market price for the given market """ - return get("{}{}?market={}&tokenID={}".format(self.host, MID_POINT, self.contract_config.conditional, tokenID)) + return get("{}{}?market={}&tokenID={}".format(self.host, MID_POINT, self.get_conditional_address(), tokenID)) def create_limit_order(self, order_args: LimitOrderArgs): """ @@ -163,7 +177,7 @@ def get_open_orders(self, tokenID = None): headers = create_level_2_headers(self.signer, self.creds, request_args) if tokenID is not None: - return get("{}{}?market={}&tokenID={}".format(self.host, OPEN_ORDERS, self.contract_config.conditional, tokenID), headers=headers) + return get("{}{}?market={}&tokenID={}".format(self.host, OPEN_ORDERS, self.get_conditional_address(), tokenID), headers=headers) return get("{}{}".format(self.host, OPEN_ORDERS), headers=headers) diff --git a/py_clob_client/http_helpers/helpers.py b/py_clob_client/http_helpers/helpers.py index 78b71d0..b4f80cb 100644 --- a/py_clob_client/http_helpers/helpers.py +++ b/py_clob_client/http_helpers/helpers.py @@ -1,5 +1,4 @@ import requests -import json from ..exceptions import PolyApiException