Skip to content

Commit

Permalink
Merge pull request #68 from Polymarket/feat/multiple-tokens-endpoints
Browse files Browse the repository at this point in the history
Adding new endpoints
  • Loading branch information
poly-rodr authored Feb 14, 2024
2 parents 4f2996f + 995ac1d commit d8bc842
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
34 changes: 34 additions & 0 deletions examples/get_last_trades_prices.py
Original file line number Diff line number Diff line change
@@ -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()
37 changes: 37 additions & 0 deletions examples/get_mid_markets_prices.py
Original file line number Diff line number Diff line change
@@ -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()
34 changes: 34 additions & 0 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,6 +53,7 @@
OrdersScoringParams,
OrderType,
PartialCreateOrderOptions,
BookParams,
)
from .exceptions import PolyException
from .http_helpers.helpers import (
Expand Down Expand Up @@ -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_prices(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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
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 @@ -21,6 +21,12 @@ class RequestArgs:
body: Any = None


@dataclass
class BookParams:
token_id: str
side: str = ''


@dataclass
class OrderArgs:
token_id: str
Expand Down
4 changes: 4 additions & 0 deletions py_clob_client/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
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.12.0",
version="0.13.0",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand Down

0 comments on commit d8bc842

Please sign in to comment.