From fcd408c7cd45961beb34746ddf515484fecc1a4f Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Wed, 14 Feb 2024 17:38:20 -0300 Subject: [PATCH 1/2] Adding new endpoints --- examples/get_last_trades_prices.py | 34 +++++++++++++++++++++++++++ examples/get_mid_markets_prices.py | 37 ++++++++++++++++++++++++++++++ py_clob_client/client.py | 34 +++++++++++++++++++++++++++ py_clob_client/clob_types.py | 6 +++++ py_clob_client/endpoints.py | 4 ++++ setup.py | 2 +- 6 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 examples/get_last_trades_prices.py create mode 100644 examples/get_mid_markets_prices.py diff --git a/examples/get_last_trades_prices.py b/examples/get_last_trades_prices.py new file mode 100644 index 0000000..590d09a --- /dev/null +++ b/examples/get_last_trades_prices.py @@ -0,0 +1,34 @@ +import os + +from py_clob_client.client import ClobClient +from py_clob_client.clob_types import BookParams +from dotenv import load_dotenv +from pprint import pprint + +from py_clob_client.constants import MUMBAI + + +load_dotenv() + + +def main(): + host = "http://localhost:8080" + key = os.getenv("PK") + chain_id = MUMBAI + client = ClobClient(host, key=key, chain_id=chain_id) + + resp = client.get_last_trades_prices( + params=[ + BookParams( + token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088" + ), + BookParams( + token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992" + ), + ] + ) + pprint(resp) + print("Done!") + + +main() diff --git a/examples/get_mid_markets_prices.py b/examples/get_mid_markets_prices.py new file mode 100644 index 0000000..e6f7867 --- /dev/null +++ b/examples/get_mid_markets_prices.py @@ -0,0 +1,37 @@ +import os + +from py_clob_client.client import ClobClient +from py_clob_client.clob_types import ApiCreds, BookParams +from dotenv import load_dotenv +from py_clob_client.constants import MUMBAI + + +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 = MUMBAI + client = ClobClient(host, key=key, chain_id=chain_id, creds=creds) + + resp = client.get_midpoints( + params=[ + BookParams( + token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088" + ), + BookParams( + token_id="1343197538147866997676250008839231694243646439454152539053893078719042421992" + ), + ] + ) + print(resp) + print("Done!") + + +main() diff --git a/py_clob_client/client.py b/py_clob_client/client.py index 88cd331..c152af9 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -34,6 +34,10 @@ GET_SAMPLING_SIMPLIFIED_MARKETS, GET_SAMPLING_MARKETS, GET_MARKET_TRADES_EVENTS, + GET_LAST_TRADES_PRICES, + MID_POINTS, + GET_ORDER_BOOKS, + GET_PRICES, ) from .clob_types import ( ApiCreds, @@ -49,6 +53,7 @@ OrdersScoringParams, OrderType, PartialCreateOrderOptions, + BookParams, ) from .exceptions import PolyException from .http_helpers.helpers import ( @@ -232,12 +237,26 @@ def get_midpoint(self, token_id): """ return get("{}{}?token_id={}".format(self.host, MID_POINT, token_id)) + def get_midpoints(self, params: list[BookParams]): + """ + Get the mid market prices for a set of token ids + """ + body = [{"token_id": param.token_id} for param in params] + return get("{}{}".format(self.host, MID_POINTS), data=body) + def get_price(self, token_id, side): """ Get the market price for the given market """ return get("{}{}?token_id={}&side={}".format(self.host, PRICE, token_id, side)) + def get_price(self, params: list[BookParams]): + """ + Get the market prices for a set + """ + body = [{"token_id": param.token_id, "side": param.side} for param in params] + return get("{}{}".format(self.host, GET_PRICES), data=body) + def get_tick_size(self, token_id: str) -> TickSize: if token_id in self.__tick_sizes: return self.__tick_sizes[token_id] @@ -381,6 +400,14 @@ def get_order_book(self, token_id) -> OrderBookSummary: raw_obs = get("{}{}?token_id={}".format(self.host, GET_ORDER_BOOK, token_id)) return parse_raw_orderbook_summary(raw_obs) + def get_order_books(self, params: list[BookParams]) -> list[OrderBookSummary]: + """ + Fetches the orderbook for a set of token ids + """ + body = [{"token_id": param.token_id} for param in params] + raw_obs = get("{}{}".format(self.host, GET_ORDER_BOOKS), data=body) + return [parse_raw_orderbook_summary(r) for r in raw_obs] + def get_order_book_hash(self, orderbook: OrderBookSummary) -> str: """ Calculates the hash for the given orderbook @@ -415,6 +442,13 @@ def get_last_trade_price(self, token_id): """ return get("{}{}?token_id={}".format(self.host, GET_LAST_TRADE_PRICE, token_id)) + def get_last_trades_prices(self, params: list[BookParams]): + """ + Fetches the last trades prices for a set of token ids + """ + body = [{"token_id": param.token_id} for param in params] + return get("{}{}".format(self.host, GET_LAST_TRADES_PRICES), data=body) + def assert_level_1_auth(self): """ Level 1 Poly Auth diff --git a/py_clob_client/clob_types.py b/py_clob_client/clob_types.py index 3617818..6a51c46 100644 --- a/py_clob_client/clob_types.py +++ b/py_clob_client/clob_types.py @@ -21,6 +21,12 @@ class RequestArgs: body: Any = None +@dataclass +class BookParams: + token_id: str + side: str + + @dataclass class OrderArgs: token_id: str diff --git a/py_clob_client/endpoints.py b/py_clob_client/endpoints.py index 18c7f05..e2c7a9d 100644 --- a/py_clob_client/endpoints.py +++ b/py_clob_client/endpoints.py @@ -5,6 +5,7 @@ DERIVE_API_KEY = "/auth/derive-api-key" TRADES = "/trades" GET_ORDER_BOOK = "/book" +GET_ORDER_BOOKS = "/books" GET_ORDER = "/order/" ORDERS = "/orders" POST_ORDER = "/order" @@ -13,8 +14,11 @@ CANCEL_ALL = "/cancel-all" CANCEL_MARKET_ORDERS = "/cancel-market-orders" MID_POINT = "/midpoint" +MID_POINTS = "/midpoints" PRICE = "/price" +GET_PRICES = "/prices" GET_LAST_TRADE_PRICE = "/last-trade-price" +GET_LAST_TRADES_PRICES = "/last-trades-prices" GET_NOTIFICATIONS = "/notifications" DROP_NOTIFICATIONS = "/notifications" GET_BALANCE_ALLOWANCE = "/balance-allowance" diff --git a/setup.py b/setup.py index 443e403..109de4d 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_clob_client", - version="0.12.0", + version="0.13.0", author="Polymarket Engineering", author_email="engineering@polymarket.com", maintainer="Polymarket Engineering", From 995ac1df1dce8d968fefb78bcfb3fbd324687b9f Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Wed, 14 Feb 2024 18:39:23 -0300 Subject: [PATCH 2/2] fixes --- py_clob_client/client.py | 2 +- py_clob_client/clob_types.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/py_clob_client/client.py b/py_clob_client/client.py index c152af9..1c54453 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -250,7 +250,7 @@ def get_price(self, token_id, side): """ return get("{}{}?token_id={}&side={}".format(self.host, PRICE, token_id, side)) - def get_price(self, params: list[BookParams]): + def get_prices(self, params: list[BookParams]): """ Get the market prices for a set """ diff --git a/py_clob_client/clob_types.py b/py_clob_client/clob_types.py index 6a51c46..b6c50a3 100644 --- a/py_clob_client/clob_types.py +++ b/py_clob_client/clob_types.py @@ -24,7 +24,7 @@ class RequestArgs: @dataclass class BookParams: token_id: str - side: str + side: str = '' @dataclass