-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from Polymarket/feat/orderbook-hash
Feat/ adding orderbook summary hash support
- Loading branch information
Showing
6 changed files
with
228 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |