Skip to content

Commit

Permalink
Merge pull request #5 from Polymarket/feat/more-endpoints
Browse files Browse the repository at this point in the history
Feat: midpoint and get open orders endpoint
  • Loading branch information
JonathanAmenechi authored Feb 28, 2022
2 parents 3b813df + 6d35914 commit 38df38c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
25 changes: 25 additions & 0 deletions examples/get_mid_market_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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")
# {'mid': '0.55'}
print(resp)
print("Done!")


main()
24 changes: 24 additions & 0 deletions examples/get_open_orders.py
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 45 additions & 4 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -30,15 +31,31 @@ 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):
"""
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):
"""
Expand Down Expand Up @@ -80,6 +97,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.get_conditional_address(), tokenID))

def create_limit_order(self, order_args: LimitOrderArgs):
"""
Creates and signs a limit order
Expand All @@ -106,6 +129,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
Expand All @@ -118,7 +155,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):
"""
Expand All @@ -130,14 +167,18 @@ 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
"""
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.get_conditional_address(), tokenID), headers=headers)

return get("{}{}".format(self.host, OPEN_ORDERS), headers=headers)

def get_order(self, order_id):
Expand Down
1 change: 1 addition & 0 deletions py_clob_client/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
POST_ORDER = "/order"
CANCEL = "/order"
CANCEL_ALL = "/cancel-all"
MID_POINT="/midpoint"
1 change: 0 additions & 1 deletion py_clob_client/http_helpers/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import requests
import json

from ..exceptions import PolyApiException

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.0.3",
version="0.0.4",
author="Jonathan Amenechi",
author_email="[email protected]",
description="Python client for the Polymarket CLOB",
Expand Down

0 comments on commit 38df38c

Please sign in to comment.