Skip to content

Commit 7036e70

Browse files
aniani
authored andcommitted
fixed tests and cleaned up
1 parent 6d1d4d8 commit 7036e70

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

examples/read_hermes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@ async def get_hermes_prices():
88
hermes_client = HermesClient([])
99
feed_ids = await hermes_client.get_price_feed_ids()
1010
feed_ids_rel = feed_ids[:2]
11-
version = 1
11+
version_http = 2
12+
version_ws = 1
1213

1314
hermes_client.add_feed_ids(feed_ids_rel)
1415

15-
prices_latest = await hermes_client.get_all_prices(version=version)
16+
prices_latest = await hermes_client.get_all_prices(version=version_http)
1617

1718
sd = list(prices_latest.keys())[0]
1819

1920
for feed_id, price_feed in prices_latest.items():
2021
print("Initial prices")
21-
price_latest = price_feed["price"].price
22-
conf_latest = price_feed["price"].conf
23-
print(f"Feed ID: {feed_id}, Price: {price_latest}, Confidence: {conf_latest}, Time: {price_feed['price'].publish_time}")
22+
print(f"Feed ID: {feed_id}, Price: {price_feed['price'].price}, Confidence: {price_feed['price'].conf}, Time: {price_feed['price'].publish_time}")
2423

2524
print("Starting web socket...")
26-
ws_call = hermes_client.ws_pyth_prices(version=version)
27-
asyncio.create_task(ws_call)
25+
ws_call = hermes_client.ws_pyth_prices(version=version_ws)
26+
ws_task = asyncio.create_task(ws_call)
2827

2928
while True:
3029
await asyncio.sleep(5)
30+
if ws_task.done():
31+
break
3132
print("Latest prices:")
3233
for feed_id, price_feed in hermes_client.prices_dict.items():
33-
print(f"Feed ID: {feed_id}, Price: {price_latest}, Confidence: {conf_latest}, Time: {price_feed['price'].publish_time}")
34+
print(f"Feed ID: {feed_id}, Price: {price_feed['price'].price}, Confidence: {price_feed['price'].conf}, Time: {price_feed['price'].publish_time}")
3435

3536
asyncio.run(get_hermes_prices())

pythclient/hermes.py

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, feed_ids: list[str], endpoint=HERMES_ENDPOINT_HTTPS, ws_endpo
3333
self.client = httpx.AsyncClient()
3434
self.endpoint = endpoint
3535
self.ws_endpoint = ws_endpoint
36-
self.feed_batch_size = feed_batch_size
36+
self.feed_batch_size = feed_batch_size # max number of feed IDs to query at once in https requests
3737

3838
async def get_price_feed_ids(self) -> list[str]:
3939
"""
@@ -70,7 +70,7 @@ def extract_price_feed_v1(data: dict) -> PriceFeed:
7070
@staticmethod
7171
def extract_price_feed_v2(data: dict) -> list[PriceFeed]:
7272
"""
73-
Extracts PriceFeed objects from the v2 JSON response (multiple price feeds) from Hermes.
73+
Extracts PriceFeed objects from the v2 JSON response (array of price feeds) from Hermes.
7474
"""
7575
update_data = data["binary"]["data"]
7676

@@ -140,7 +140,7 @@ async def get_all_prices(self, version=2) -> dict[str, PriceFeed]:
140140
"""
141141
Queries the Hermes http endpoint for the latest price feeds for all feed IDs in the class object.
142142
143-
There are limitations on the number of feed IDs that can be queried at once, so this function queries the feed IDs in batches.
143+
There is a limit on the number of feed IDs that can be queried at once, so this function queries the feed IDs in batches.
144144
"""
145145
pyth_prices_latest = []
146146
i = 0
@@ -159,7 +159,7 @@ async def ws_pyth_prices(self, version=1):
159159
"""
160160
if version != 1:
161161
parse_unsupported_version(version)
162-
162+
163163
async with websockets.connect(self.ws_endpoint) as ws:
164164
while True:
165165
# add new price feed ids to the ws subscription
@@ -187,34 +187,4 @@ async def ws_pyth_prices(self, version=1):
187187
self.prices_dict[feed_id] = self.extract_price_feed_v1(new_feed)
188188

189189
except Exception as e:
190-
raise Exception(f"Error in price_update message: {msg}") from e
191-
192-
193-
async def main():
194-
hermes_client = HermesClient([])
195-
feed_ids = await hermes_client.get_price_feed_ids()
196-
feed_ids_rel = feed_ids[:50]
197-
version = 2
198-
199-
hermes_client.add_feed_ids(feed_ids_rel)
200-
201-
prices_latest = await hermes_client.get_pyth_prices_latest(feed_ids[:50], version=version)
202-
203-
try:
204-
price_at_time = await hermes_client.get_pyth_price_at_time(feed_ids[0], 1_700_000_000, version=version)
205-
print(price_at_time)
206-
except Exception as e:
207-
print(f"Error in get_pyth_price_at_time, {e}")
208-
209-
all_prices = await hermes_client.get_all_prices(version=version)
210-
211-
print("Starting web socket...")
212-
ws_call = hermes_client.ws_pyth_prices(version=version)
213-
asyncio.create_task(ws_call)
214-
215-
while True:
216-
await asyncio.sleep(1)
217-
218-
219-
if __name__ == "__main__":
220-
asyncio.run(main())
190+
raise Exception(f"Error in price_update message: {msg}") from e

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='pythclient',
10-
version='0.1.22',
10+
version='0.1.23',
1111
packages=['pythclient'],
1212
author='Pyth Developers',
1313
author_email='contact@pyth.network',

tests/test_hermes.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ def data_v2():
5050
},
5151
"parsed": [
5252
{
53-
"id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
54-
"price": {
55-
"price": "5190075917635",
56-
"conf": "2661582364",
57-
"expo": -8,
58-
"publish_time": 1708363256
59-
},
60-
"ema_price": {
61-
"price": "5209141800000",
62-
"conf": "3290086600",
63-
"expo": -8,
64-
"publish_time": 1708363256
65-
},
66-
"metadata": {
67-
"slot": 125976528,
68-
"proof_available_time": 1708363257,
69-
"prev_publish_time": 1708363255
70-
}
53+
"id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
54+
"price": {
55+
"price": "5190075917635",
56+
"conf": "2661582364",
57+
"expo": -8,
58+
"publish_time": 1708363256
59+
},
60+
"ema_price": {
61+
"price": "5209141800000",
62+
"conf": "3290086600",
63+
"expo": -8,
64+
"publish_time": 1708363256
65+
},
66+
"metadata": {
67+
"slot": 125976528,
68+
"proof_available_time": 1708363257,
69+
"prev_publish_time": 1708363255
70+
}
7171
}
7272
]
7373
}
@@ -79,7 +79,7 @@ def mock_get_price_feed_ids(mocker: MockerFixture):
7979
return async_mock
8080

8181
@pytest.mark.asyncio
82-
async def test_hermes_add_feed_ids(hermes_client: HermesClient):
82+
async def test_hermes_add_feed_ids(hermes_client: HermesClient, mock_get_price_feed_ids: AsyncMock):
8383
mock_get_price_feed_ids.return_value = ["ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"]
8484

8585
feed_ids = await hermes_client.get_price_feed_ids()
@@ -89,10 +89,8 @@ async def test_hermes_add_feed_ids(hermes_client: HermesClient):
8989

9090
hermes_client.add_feed_ids(feed_ids)
9191

92-
assert hermes_client.feed_ids == list(set(feed_ids_pre + feed_ids))
93-
assert hermes_client.pending_feed_ids == list(set(pending_feed_ids_pre + feed_ids))
94-
95-
92+
assert set(hermes_client.feed_ids) == set(feed_ids_pre + feed_ids)
93+
assert set(hermes_client.pending_feed_ids) == set(pending_feed_ids_pre + feed_ids)
9694

9795
def test_hermes_extract_price_feed_v1(hermes_client: HermesClient, data_v1: dict):
9896
price_feed = hermes_client.extract_price_feed_v1(data_v1)
@@ -103,5 +101,6 @@ def test_hermes_extract_price_feed_v1(hermes_client: HermesClient, data_v1: dict
103101
def test_hermes_extract_price_feed_v2(hermes_client: HermesClient, data_v2: dict):
104102
price_feed = hermes_client.extract_price_feed_v2(data_v2)
105103

106-
assert isinstance(price_feed, dict)
107-
assert set(price_feed.keys()) == set(PriceFeed.__annotations__.keys())
104+
assert isinstance(price_feed, list)
105+
assert isinstance(price_feed[0], dict)
106+
assert set(price_feed[0].keys()) == set(PriceFeed.__annotations__.keys())

0 commit comments

Comments
 (0)