Skip to content

Commit

Permalink
Merge pull request #46 from Polymarket/feat/orderbook-hash
Browse files Browse the repository at this point in the history
Feat/ adding orderbook summary hash support
  • Loading branch information
poly-rodr authored Feb 8, 2023
2 parents 8b9bde2 + af2f8dc commit 0e2599a
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 9 deletions.
10 changes: 6 additions & 4 deletions examples/get_orderbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ def main():
host = "http://localhost:8080"
client = ClobClient(host)

print(
client.get_order_book(
"16678291189211314787145083999015737376658799626183230671758641503291735614088"
)
orderbook = client.get_order_book(
"16678291189211314787145083999015737376658799626183230671758641503291735614088"
)
print("orderbook", orderbook)

hash = client.get_order_book_hash(orderbook)
print("orderbook hash", hash)


main()
15 changes: 11 additions & 4 deletions py_clob_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
OrderArgs,
RequestArgs,
TradeNotificationParams,
OrderBookSummary,
)
from .exceptions import PolyException
from .http_helpers.helpers import (
Expand All @@ -43,9 +44,8 @@
add_trade_notifications_query_params,
)
from py_order_utils.config import get_contract_config
from py_order_utils.model import BUY as UtilsBuy
from .constants import L0, L1, L1_AUTH_UNAVAILABLE, L2, L2_AUTH_UNAVAILABLE
from .order_builder.constants import BUY, SELL
from .utilities import parse_raw_orderbook_summary, generate_orderbook_summary_hash


class ClobClient:
Expand Down Expand Up @@ -311,11 +311,18 @@ def get_orders(self, params: FilterParams = None):
url = add_query_params("{}{}".format(self.host, ORDERS), params)
return get(url, headers=headers)

def get_order_book(self, token_id):
def get_order_book(self, token_id) -> OrderBookSummary:
"""
Fetches the orderbook for the token_id
"""
return get("{}{}?token_id={}".format(self.host, GET_ORDER_BOOK, token_id))
raw_obs = get("{}{}?token_id={}".format(self.host, GET_ORDER_BOOK, token_id))
return parse_raw_orderbook_summary(raw_obs)

def get_order_book_hash(self, orderbook: OrderBookSummary) -> str:
"""
Calculates the hash for the given orderbook
"""
return generate_orderbook_summary_hash(orderbook)

def get_order(self, order_id):
"""
Expand Down
33 changes: 33 additions & 0 deletions py_clob_client/clob_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass
from typing import Any
from dataclasses import dataclass, asdict
from json import dumps

from .constants import ZERO_ADDRESS

Expand Down Expand Up @@ -76,3 +78,34 @@ class FilterParams:
@dataclass
class TradeNotificationParams:
index: int = None


@dataclass
class OrderSummary:
price: str = None
size: str = None

@property
def __dict__(self):
return asdict(self)

@property
def json(self):
return dumps(self.__dict__)


@dataclass
class OrderBookSummary:
market: str = None
asset_id: str = None
bids: list[OrderSummary] = None
asks: list[OrderSummary] = None
hash: str = None

@property
def __dict__(self):
return asdict(self)

@property
def json(self):
return dumps(self.__dict__, separators=(",", ":"))
31 changes: 31 additions & 0 deletions py_clob_client/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import hashlib
import json

from .clob_types import OrderBookSummary, OrderSummary


def parse_raw_orderbook_summary(raw_obs: any) -> OrderBookSummary:
bids = []
for bid in raw_obs["bids"]:
bids.append(OrderSummary(size=bid["size"], price=bid["price"]))

asks = []
for ask in raw_obs["asks"]:
asks.append(OrderSummary(size=ask["size"], price=ask["price"]))

orderbookSummary = OrderBookSummary(
market=raw_obs["market"],
asset_id=raw_obs["asset_id"],
bids=bids,
asks=asks,
hash=raw_obs["hash"],
)

return orderbookSummary


def generate_orderbook_summary_hash(orderbook: OrderBookSummary) -> str:
orderbook.hash = ""
hash = hashlib.sha1(str(orderbook.json).encode("utf-8")).hexdigest()
orderbook.hash = hash
return hash
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.1.13",
version="0.2.0",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand Down
146 changes: 146 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from unittest import TestCase

from py_clob_client.utilities import (
parse_raw_orderbook_summary,
generate_orderbook_summary_hash,
)


class TestUtilities(TestCase):
def test_parse_raw_orderbook_summary(self):
raw_obs = {
"market": "0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af",
"asset_id": "1343197538147866997676250008839231694243646439454152539053893078719042421992",
"bids": [
{"price": "0.15", "size": "100"},
{"price": "0.31", "size": "148.56"},
{"price": "0.33", "size": "58"},
{"price": "0.5", "size": "100"},
],
"asks": [],
"hash": "9d6d9e8831a150ac4cd878f99f7b2c6d419b875f",
}

orderbook_summary = parse_raw_orderbook_summary(raw_obs)

self.assertEqual(
orderbook_summary.market,
"0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af",
)
self.assertEqual(
orderbook_summary.asset_id,
"1343197538147866997676250008839231694243646439454152539053893078719042421992",
)
self.assertEqual(
orderbook_summary.hash, "9d6d9e8831a150ac4cd878f99f7b2c6d419b875f"
)

self.assertIsNotNone(orderbook_summary.asks)
self.assertIsNotNone(orderbook_summary.bids)

self.assertEqual(len(orderbook_summary.asks), 0)
self.assertEqual(len(orderbook_summary.bids), 4)

self.assertEqual(orderbook_summary.bids[0].price, "0.15")
self.assertEqual(orderbook_summary.bids[0].size, "100")
self.assertEqual(orderbook_summary.bids[1].price, "0.31")
self.assertEqual(orderbook_summary.bids[1].size, "148.56")
self.assertEqual(orderbook_summary.bids[2].price, "0.33")
self.assertEqual(orderbook_summary.bids[2].size, "58")
self.assertEqual(orderbook_summary.bids[3].price, "0.5")
self.assertEqual(orderbook_summary.bids[3].size, "100")

raw_obs = {
"market": "0xaabbcc",
"asset_id": "100",
"bids": [],
"asks": [],
"hash": "7f81a35a09e1933a96b05edb51ac4be4a6163146",
}

orderbook_summary = parse_raw_orderbook_summary(raw_obs)

self.assertEqual(
orderbook_summary.market,
"0xaabbcc",
)
self.assertEqual(
orderbook_summary.asset_id,
"100",
)
self.assertEqual(
orderbook_summary.hash, "7f81a35a09e1933a96b05edb51ac4be4a6163146"
)

self.assertIsNotNone(orderbook_summary.asks)
self.assertIsNotNone(orderbook_summary.bids)

self.assertEqual(len(orderbook_summary.asks), 0)
self.assertEqual(len(orderbook_summary.bids), 0)

def test_generate_orderbook_summary_hash(self):
raw_obs = {
"market": "0xaabbcc",
"asset_id": "100",
"bids": [
{"price": "0.3", "size": "100"},
{"price": "0.4", "size": "100"},
],
"asks": [
{"price": "0.6", "size": "100"},
{"price": "0.7", "size": "100"},
],
"hash": "",
}

orderbook_summary = parse_raw_orderbook_summary(raw_obs)
self.assertEqual(
generate_orderbook_summary_hash(orderbook_summary),
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
)
self.assertEqual(
orderbook_summary.hash,
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
)

raw_obs = {
"market": "0xaabbcc",
"asset_id": "100",
"bids": [
{"price": "0.3", "size": "100"},
{"price": "0.4", "size": "100"},
],
"asks": [
{"price": "0.6", "size": "100"},
{"price": "0.7", "size": "100"},
],
"hash": "b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
}

orderbook_summary = parse_raw_orderbook_summary(raw_obs)
self.assertEqual(
generate_orderbook_summary_hash(orderbook_summary),
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
)
self.assertEqual(
orderbook_summary.hash,
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
)

raw_obs = {
"market": "0xaabbcc",
"asset_id": "100",
"bids": [],
"asks": [],
"hash": "",
}

orderbook_summary = parse_raw_orderbook_summary(raw_obs)
self.assertEqual(
generate_orderbook_summary_hash(orderbook_summary),
"7f81a35a09e1933a96b05edb51ac4be4a6163146",
)
self.assertEqual(
orderbook_summary.hash,
"7f81a35a09e1933a96b05edb51ac4be4a6163146",
)

0 comments on commit 0e2599a

Please sign in to comment.