Skip to content

Commit

Permalink
Merge pull request #14 from Polymarket/fix/order-history
Browse files Browse the repository at this point in the history
Fix: order history
  • Loading branch information
JonathanAmenechi authored Apr 5, 2022
2 parents e407d9a + 519e464 commit 6c40214
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
23 changes: 23 additions & 0 deletions examples/get_order_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

from py_clob_client.client import ClobClient
from py_clob_client.clob_types import ApiCreds
from dotenv import load_dotenv
from pprint import pprint


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_order_history(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088")
pprint(resp)
print("Done!")


main()
22 changes: 22 additions & 0 deletions examples/get_trade_history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os

from py_clob_client.client import ClobClient
from py_clob_client.clob_types import ApiCreds
from dotenv import load_dotenv
from pprint import pprint

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_trade_history(tokenID="16678291189211314787145083999015737376658799626183230671758641503291735614088")
pprint(resp)
print("Done!")


main()
32 changes: 32 additions & 0 deletions examples/market_buy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os

from py_clob_client.client import ClobClient
from py_clob_client.clob_types import ApiCreds, LimitOrderArgs, MarketOrderArgs
from dotenv import load_dotenv
from pprint import pprint
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)

# Create and sign a market order, using 100 USDC size to buy as many tokens as possible
# Note: Size in market buy is the COLLATERAL size used to initiate the buy
# While in a market sell, size is the amount of YES/NO tokens used to initiate the sell
order_args = MarketOrderArgs(
size=100.0,
side=BUY,
token_id="16678291189211314787145083999015737376658799626183230671758641503291735614088"
)
mkt_order = client.create_market_order(order_args)
resp = client.post_order(mkt_order)
pprint(resp)
print("Done!")


main()
23 changes: 21 additions & 2 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .headers import create_level_1_headers, create_level_2_headers
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, POST_ORDER, PRICE, TIME, TRADE_HISTORY
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 .exceptions import PolyException
from .http_helpers.helpers import delete, get, post
Expand Down Expand Up @@ -231,14 +231,33 @@ 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):
def get_trade_history(self, tokenID = 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)

def get_order_history(self, tokenID = 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)

def assert_level_1_auth(self):
"""
Level 1 Poly Auth
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 @@ -8,6 +8,7 @@
OPEN_ORDERS = "/open-orders"
POST_ORDER = "/order"
CANCEL = "/order"
ORDER_HISTORY = "/order-history"
CANCEL_ALL = "/cancel-all"
MID_POINT="/midpoint"
PRICE = "/price"
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.11",
version="0.0.12",
author="Jonathan Amenechi",
author_email="[email protected]",
description="Python client for the Polymarket CLOB",
Expand Down

0 comments on commit 6c40214

Please sign in to comment.