-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hermes support #54
Merged
hermes support #54
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ffdd934
hermes support
5691b9f
add dep
8389737
address comments
c22f8ba
dep add
0faca3d
dep fix
6d1d4d8
fix an arg error
7036e70
fixed tests and cleaned up
6fdcec0
better context management
41426be
increase coverage a bit
ab9149a
add more test coverage, for socket cases
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import asyncio | ||
|
||
from pythclient.hermes import HermesClient | ||
|
||
async def get_hermes_prices(): | ||
hermes_client = HermesClient([]) | ||
feed_ids = await hermes_client.get_price_feed_ids() | ||
feed_ids_rel = feed_ids[:2] | ||
version_http = 1 | ||
version_ws = 1 | ||
|
||
hermes_client.add_feed_ids(feed_ids_rel) | ||
|
||
prices_latest = await hermes_client.get_all_prices(version=version_http) | ||
|
||
for feed_id, price_feed in prices_latest.items(): | ||
print("Initial prices") | ||
print(f"Feed ID: {feed_id}, Price: {price_feed['price'].price}, Confidence: {price_feed['price'].conf}, Time: {price_feed['price'].publish_time}") | ||
|
||
print("Starting web socket...") | ||
ws_call = hermes_client.ws_pyth_prices(version=version_ws) | ||
ws_task = asyncio.create_task(ws_call) | ||
|
||
while True: | ||
await asyncio.sleep(5) | ||
if ws_task.done(): | ||
break | ||
print("Latest prices:") | ||
for feed_id, price_feed in hermes_client.prices_dict.items(): | ||
print(f"Feed ID: {feed_id}, Price: {price_feed['price'].price}, Confidence: {price_feed['price'].conf}, Time: {price_feed['price'].publish_time}") | ||
|
||
asyncio.run(get_hermes_prices()) |
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,192 @@ | ||
import asyncio | ||
from typing import TypedDict | ||
import httpx | ||
import os | ||
import json | ||
import websockets | ||
|
||
from .price_feeds import Price | ||
|
||
HERMES_ENDPOINT_HTTPS = "https://hermes.pyth.network/" | ||
HERMES_ENDPOINT_WSS = "wss://hermes.pyth.network/ws" | ||
|
||
|
||
class PriceFeed(TypedDict): | ||
feed_id: str | ||
price: Price | ||
ema_price: Price | ||
update_data: list[str] | ||
|
||
|
||
def parse_unsupported_version(version): | ||
if isinstance(version, int): | ||
raise ValueError("Version number {version} not supported") | ||
else: | ||
raise TypeError("Version must be an integer") | ||
|
||
|
||
class HermesClient: | ||
def __init__(self, feed_ids: list[str], endpoint=HERMES_ENDPOINT_HTTPS, ws_endpoint=HERMES_ENDPOINT_WSS, feed_batch_size=100): | ||
self.feed_ids = feed_ids | ||
self.pending_feed_ids = feed_ids | ||
self.prices_dict: dict[str, PriceFeed] = {} | ||
self.endpoint = endpoint | ||
self.ws_endpoint = ws_endpoint | ||
self.feed_batch_size = feed_batch_size # max number of feed IDs to query at once in https requests | ||
|
||
async def get_price_feed_ids(self) -> list[str]: | ||
""" | ||
Queries the Hermes https endpoint for a list of the IDs of all Pyth price feeds. | ||
""" | ||
|
||
url = os.path.join(self.endpoint, "api/price_feed_ids") | ||
|
||
async with httpx.AsyncClient() as client: | ||
data = (await client.get(url)).json() | ||
|
||
return data | ||
|
||
def add_feed_ids(self, feed_ids: list[str]): | ||
self.feed_ids += feed_ids | ||
self.feed_ids = list(set(self.feed_ids)) | ||
self.pending_feed_ids += feed_ids | ||
|
||
@staticmethod | ||
def extract_price_feed_v1(data: dict) -> PriceFeed: | ||
""" | ||
Extracts PriceFeed object from the v1 JSON response (individual price feed) from Hermes. | ||
""" | ||
price = Price.from_dict(data["price"]) | ||
ema_price = Price.from_dict(data["ema_price"]) | ||
update_data = data["vaa"] | ||
price_feed = { | ||
"feed_id": data["id"], | ||
"price": price, | ||
"ema_price": ema_price, | ||
"update_data": [update_data], | ||
} | ||
return price_feed | ||
|
||
@staticmethod | ||
def extract_price_feed_v2(data: dict) -> list[PriceFeed]: | ||
""" | ||
Extracts PriceFeed objects from the v2 JSON response (array of price feeds) from Hermes. | ||
""" | ||
update_data = data["binary"]["data"] | ||
|
||
price_feeds = [] | ||
|
||
for feed in data["parsed"]: | ||
price = Price.from_dict(feed["price"]) | ||
ema_price = Price.from_dict(feed["ema_price"]) | ||
price_feed = { | ||
"feed_id": feed["id"], | ||
"price": price, | ||
"ema_price": ema_price, | ||
"update_data": update_data, | ||
} | ||
price_feeds.append(price_feed) | ||
|
||
return price_feeds | ||
|
||
async def get_pyth_prices_latest(self, feedIds: list[str], version=2) -> list[PriceFeed]: | ||
""" | ||
Queries the Hermes https endpoint for the latest price feeds for a list of Pyth feed IDs. | ||
""" | ||
if version==1: | ||
url = os.path.join(self.endpoint, "api/latest_price_feeds") | ||
params = {"ids[]": feedIds, "binary": "true"} | ||
elif version==2: | ||
url = os.path.join(self.endpoint, "v2/updates/price/latest") | ||
params = {"ids[]": feedIds, "encoding": "base64", "parsed": "true"} | ||
else: | ||
parse_unsupported_version(version) | ||
|
||
async with httpx.AsyncClient() as client: | ||
data = (await client.get(url, params=params)).json() | ||
|
||
if version==1: | ||
results = [] | ||
for res in data: | ||
price_feed = self.extract_price_feed_v1(res) | ||
results.append(price_feed) | ||
elif version==2: | ||
results = self.extract_price_feed_v2(data) | ||
|
||
return results | ||
|
||
async def get_pyth_price_at_time(self, feed_id: str, timestamp: int, version=2) -> PriceFeed: | ||
""" | ||
Queries the Hermes https endpoint for the price feed for a Pyth feed ID at a given timestamp. | ||
""" | ||
if version==1: | ||
url = os.path.join(self.endpoint, "api/get_price_feed") | ||
params = {"id": feed_id, "publish_time": timestamp, "binary": "true"} | ||
elif version==2: | ||
url = os.path.join(self.endpoint, f"v2/updates/price/{timestamp}") | ||
params = {"ids[]": [feed_id], "encoding": "base64", "parsed": "true"} | ||
else: | ||
parse_unsupported_version(version) | ||
|
||
async with httpx.AsyncClient() as client: | ||
data = (await client.get(url, params=params)).json() | ||
|
||
if version==1: | ||
price_feed = self.extract_price_feed_v1(data) | ||
elif version==2: | ||
price_feed = self.extract_price_feed_v2(data)[0] | ||
|
||
return price_feed | ||
|
||
async def get_all_prices(self, version=2) -> dict[str, PriceFeed]: | ||
""" | ||
Queries the Hermes http endpoint for the latest price feeds for all feed IDs in the class object. | ||
|
||
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. | ||
""" | ||
pyth_prices_latest = [] | ||
i = 0 | ||
while len(self.feed_ids[i : i + self.feed_batch_size]) > 0: | ||
pyth_prices_latest += await self.get_pyth_prices_latest( | ||
self.feed_ids[i : i + self.feed_batch_size], | ||
version=version, | ||
) | ||
i += self.feed_batch_size | ||
|
||
return dict([(feed['feed_id'], feed) for feed in pyth_prices_latest]) | ||
|
||
async def ws_pyth_prices(self, version=1): | ||
""" | ||
Opens a websocket connection to Hermes for latest prices for all feed IDs in the class object. | ||
""" | ||
if version != 1: | ||
parse_unsupported_version(version) | ||
|
||
async with websockets.connect(self.ws_endpoint) as ws: | ||
while True: | ||
# add new price feed ids to the ws subscription | ||
if len(self.pending_feed_ids) > 0: | ||
json_subscribe = { | ||
"ids": self.pending_feed_ids, | ||
"type": "subscribe", | ||
"verbose": True, | ||
"binary": True, | ||
} | ||
await ws.send(json.dumps(json_subscribe)) | ||
self.pending_feed_ids = [] | ||
|
||
msg = json.loads(await ws.recv()) | ||
if msg.get("type") == "response": | ||
if msg.get("status") != "success": | ||
raise Exception("Error in subscribing to websocket") | ||
try: | ||
if msg["type"] != "price_update": | ||
continue | ||
|
||
feed_id = msg["price_feed"]["id"] | ||
new_feed = msg["price_feed"] | ||
|
||
self.prices_dict[feed_id] = self.extract_price_feed_v1(new_feed) | ||
|
||
except Exception as e: | ||
raise Exception(f"Error in price_update message: {msg}") from e |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does
self.pending_feed_ids
not require uniqueness?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we always call
list(set(self.feed_ids))
, so the feed_ids will always be uniqueThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's possible for
self.pending_feed_ids
to contain duplicates if the same feed IDs are added more than once before they are cleared, for e.g.add_feed_ids
method is called with['feed1', 'feed2']
self.pending_feed_ids
is performed,add_feed_ids
is called again with['feed2', 'feed3']
self.feed_ids
will now contain['feed1', 'feed2', 'feed3']
without duplicates because it's converted to a set and then back to a listself.pending_feed_ids
will contain['feed1', 'feed2', 'feed2', 'feed3']
, where'feed2'
is duplicated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imo you can do something like this to make it cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another thing that we can possibly do here is to validate that the
feed_ids
passed in is in the format we expecte.g.
and then call
validate_feed_ids
in the first line ofadd_feed_ids
and write tests to ensure that invalid ids (e.g. 30 chars, invalid chars, etc) throw an error