Skip to content
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

Add Hermes as a price source #29

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.10.14
6 changes: 5 additions & 1 deletion config/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[publisher]

# Set it to either 'coin_gecko' or 'pyth_replicator'. You need to provide
# Set it to 'coin_gecko', 'pyth_replicator' or 'hermes'. You need to provide
# the configuration for the chosen engine as described below.
provider_engine = 'pyth_replicator'

Expand All @@ -21,6 +21,10 @@ endpoint = 'ws://127.0.0.1:8910'
# symbol = 'Crypto.BTC/USD'
# coin_gecko_id = 'bitcoin'

# [publisher.hermes]
# http_endpoint = 'https://hermes.pyth.network'
# ws_endpoint = 'wss://hermes.pyth.network'

[publisher.pyth_replicator]
http_endpoint = 'https://pythnet.rpcpool.com'
ws_endpoint = 'wss://pythnet.rpcpool.com'
Expand Down
5 changes: 5 additions & 0 deletions example_publisher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class PythReplicatorConfig:
manual_agg_max_slot_diff: int = ts.option(default=25)
account_update_interval_secs: int = ts.option(default=300)

@ts.settings
class HermesConfig:
http_endpoint: str
ws_endpoint: str

@ts.settings
class Config:
Expand All @@ -52,3 +56,4 @@ class Config:
product_update_interval_secs: int = ts.option(default=60)
coin_gecko: Optional[CoinGeckoConfig] = ts.option(default=None)
pyth_replicator: Optional[PythReplicatorConfig] = ts.option(default=None)
hermes: Optional[HermesConfig] = ts.option(default=None)
44 changes: 44 additions & 0 deletions example_publisher/providers/hermes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import asyncio
from typing import List, Optional
from pythclient.hermes import HermesClient, PriceFeed

from structlog import get_logger

from example_publisher.provider import Price, Provider, Symbol

from ..config import HermesConfig

log = get_logger()


class Hermes(Provider):
def __init__(self, config: HermesConfig) -> None:
self._config = config
self._client = HermesClient([], config.http_endpoint, config.ws_endpoint)
asyncio.run(self._get_hermes_prices())

def upd_products(self, product_symbols: List[Symbol]) -> None:
# TODO Optimize: Remove if possible any symbols we don't want any more
self._client.add_feed_ids(product_symbols)
pass

def latest_price(self, symbol: Symbol) -> Optional[Price]:
item = self._client.prices_dict[symbol]
return Price(
price=item.price.price,
conf=item.price.conf,
timestamp=item.price.publish_time
)

async def _get_hermes_prices(self):
print("Starting web socket...")
ws_call = self._client.ws_pyth_prices(version=1)
ws_task = asyncio.create_task(ws_call)

while True:
await asyncio.sleep(5)
if ws_task.done():
break
print("Latest prices:")
for symbol, price in self._client.prices_dict.items():
print(f"Symbol: {symbol}, Feed ID: {price.feed_id}, Price: {price.price}, Confidence: {price.conf}, Time: {price.publish_time}")
Loading
Loading