-
Notifications
You must be signed in to change notification settings - Fork 298
feat(hip-3-pusher): Price fallback logic and staleness checks #3061
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from loguru import logger | ||
import time | ||
|
||
from hyperliquid.info import Info | ||
from hyperliquid.utils.constants import TESTNET_API_URL, MAINNET_API_URL | ||
|
@@ -28,6 +29,7 @@ def on_activeAssetCtx(self, message): | |
:return: None | ||
""" | ||
ctx = message["data"]["ctx"] | ||
self.price_state.latest_oracle_price = ctx["oraclePx"] | ||
self.price_state.latest_mark_price = ctx["markPx"] | ||
logger.debug("on_activeAssetCtx: oraclePx: {} marketPx: {}", self.price_state.latest_oracle_price, self.price_state.latest_mark_price) | ||
self.price_state.hl_oracle_price = ctx["oraclePx"] | ||
self.price_state.hl_mark_price = ctx["markPx"] | ||
logger.debug("on_activeAssetCtx: oraclePx: {} marketPx: {}", self.price_state.hl_oracle_price, self.price_state.hl_mark_price) | ||
self.price_state.latest_hl_timestamp = time.time() | ||
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would recommend storing the timestamp together with the price in one type to enforce they are updated together. something like class PriceUpdate:
asset_id: str
price: float
timestamp: int |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,73 @@ | ||
from loguru import logger | ||
import time | ||
|
||
DEFAULT_STALE_PRICE_THRESHOLD_SECONDS = 5 | ||
|
||
|
||
class PriceState: | ||
""" | ||
Maintain latest prices seen across listeners and publisher. | ||
""" | ||
def __init__(self): | ||
self.latest_oracle_price = None | ||
self.latest_mark_price = None | ||
def __init__(self, config): | ||
self.stale_price_threshold_seconds = config.get("stale_price_threshold_seconds", DEFAULT_STALE_PRICE_THRESHOLD_SECONDS) | ||
now = time.time() | ||
|
||
self.hl_oracle_price = None | ||
self.hl_mark_price = None | ||
self.latest_hl_timestamp = now | ||
|
||
self.lazer_base_price = None | ||
self.lazer_base_exponent = config["lazer"]["base_feed_exponent"] | ||
self.lazer_quote_price = None | ||
self.lazer_quote_exponent = config["lazer"]["quote_feed_exponent"] | ||
self.latest_lazer_timestamp = now | ||
|
||
self.hermes_base_price = None | ||
self.hermes_base_exponent = config["hermes"]["base_feed_exponent"] | ||
self.hermes_quote_price = None | ||
self.hermes_quote_exponent = config["hermes"]["quote_feed_exponent"] | ||
self.latest_hermes_timestamp = now | ||
|
||
def get_current_oracle_price(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth thinking through how this evolves as we need to support pushing multiple prices. Also, we might want to have this orchestration in a different module and split up the state to be managed by the individual data sources instead of a single mono-state. |
||
now = time.time() | ||
if self.hl_oracle_price: | ||
time_diff = now - self.latest_hl_timestamp | ||
if time_diff < self.stale_price_threshold_seconds: | ||
return self.hl_oracle_price | ||
else: | ||
logger.error("Hyperliquid oracle price stale by {} seconds", time_diff) | ||
else: | ||
logger.error("Hyperliquid oracle price not received yet") | ||
|
||
# fall back to Hermes | ||
if self.hermes_base_price and self.hermes_quote_price: | ||
time_diff = now - self.latest_hermes_timestamp | ||
if time_diff < self.stale_price_threshold_seconds: | ||
return self.get_hermes_price() | ||
else: | ||
logger.error("Hermes price stale by {} seconds", time_diff) | ||
else: | ||
logger.error("Hermes base/quote prices not received yet") | ||
|
||
# fall back to Lazer | ||
if self.lazer_base_price and self.lazer_quote_price: | ||
time_diff = now - self.latest_lazer_timestamp | ||
if time_diff < self.stale_price_threshold_seconds: | ||
return self.get_lazer_price() | ||
else: | ||
logger.error("Lazer price stale by {} seconds", time_diff) | ||
else: | ||
logger.error("Lazer base/quote prices not received yet") | ||
|
||
logger.error("All prices missing or stale!") | ||
return None | ||
Comment on lines
+42
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we fall back to Hermes before Lazer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I think I just mixed up the order you guys suggested for whatever reason. Dunno if currently we trust one more than the other. |
||
|
||
def get_hermes_price(self): | ||
base_price = float(self.hermes_base_price) / (10.0 ** -self.hermes_base_exponent) | ||
quote_price = float(self.hermes_quote_price) / (10.0 ** -self.hermes_quote_exponent) | ||
return str(round(base_price / quote_price, 2)) | ||
|
||
def get_lazer_price(self): | ||
base_price = float(self.lazer_base_price) / (10.0 ** -self.lazer_base_exponent) | ||
quote_price = float(self.lazer_quote_price) / (10.0 ** -self.lazer_quote_exponent) | ||
return str(round(base_price / quote_price, 2)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,15 +47,21 @@ def __init__(self, config: dict, price_state: PriceState): | |
async def run(self): | ||
while True: | ||
await asyncio.sleep(self.publish_interval) | ||
logger.debug("publish price_state: {}", vars(self.price_state)) | ||
|
||
oracle_pxs = {} | ||
oracle_px = self.price_state.get_current_oracle_price() | ||
if not oracle_px: | ||
logger.error("No valid oracle price available!") | ||
return | ||
else: | ||
logger.debug("Current oracle price: {}", oracle_px) | ||
oracle_pxs[self.market_symbol] = oracle_px | ||
|
||
mark_pxs = [] | ||
#if self.price_state.hl_mark_price: | ||
# mark_pxs.append({self.market_symbol: self.price_state.hl_mark_price}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not publish the mark prices? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They told us not to for now. Note that this will also include the actual HIP-3's market data in the calculation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are just mark prices for the HyperCore perps which are useful data points but separate. |
||
|
||
external_perp_pxs = {} | ||
if self.price_state.latest_oracle_price: | ||
oracle_pxs[self.market_symbol] = self.price_state.latest_oracle_price | ||
if self.price_state.latest_mark_price: | ||
mark_pxs.append({self.market_symbol: self.price_state.latest_mark_price}) | ||
|
||
if self.enable_publish: | ||
if self.enable_kms: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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'll eventually need to split out the price config into its own file and source the metadata (like exponent) from the data sources themselves.
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.
Agreed, will need to scale this into a general set of prices and calcs.