|
| 1 | +from loguru import logger |
| 2 | +import time |
| 3 | + |
| 4 | +DEFAULT_STALE_PRICE_THRESHOLD_SECONDS = 5 |
| 5 | + |
| 6 | + |
1 | 7 | class PriceState:
|
2 | 8 | """
|
3 | 9 | Maintain latest prices seen across listeners and publisher.
|
4 | 10 | """
|
5 |
| - def __init__(self): |
6 |
| - self.latest_oracle_price = None |
7 |
| - self.latest_mark_price = None |
| 11 | + def __init__(self, config): |
| 12 | + self.stale_price_threshold_seconds = config.get("stale_price_threshold_seconds", DEFAULT_STALE_PRICE_THRESHOLD_SECONDS) |
| 13 | + now = time.time() |
| 14 | + |
| 15 | + self.hl_oracle_price = None |
| 16 | + self.hl_mark_price = None |
| 17 | + self.latest_hl_timestamp = now |
| 18 | + |
8 | 19 | self.lazer_base_price = None
|
| 20 | + self.lazer_base_exponent = config["lazer"]["base_feed_exponent"] |
9 | 21 | self.lazer_quote_price = None
|
| 22 | + self.lazer_quote_exponent = config["lazer"]["quote_feed_exponent"] |
| 23 | + self.latest_lazer_timestamp = now |
| 24 | + |
10 | 25 | self.hermes_base_price = None
|
| 26 | + self.hermes_base_exponent = config["hermes"]["base_feed_exponent"] |
11 | 27 | self.hermes_quote_price = None
|
| 28 | + self.hermes_quote_exponent = config["hermes"]["quote_feed_exponent"] |
| 29 | + self.latest_hermes_timestamp = now |
| 30 | + |
| 31 | + def get_current_oracle_price(self): |
| 32 | + now = time.time() |
| 33 | + if self.hl_oracle_price: |
| 34 | + time_diff = now - self.latest_hl_timestamp |
| 35 | + if time_diff < self.stale_price_threshold_seconds: |
| 36 | + return self.hl_oracle_price |
| 37 | + else: |
| 38 | + logger.error("Hyperliquid oracle price stale by {} seconds", time_diff) |
| 39 | + else: |
| 40 | + logger.error("Hyperliquid oracle price not received yet") |
| 41 | + |
| 42 | + # fall back to Hermes |
| 43 | + if self.hermes_base_price and self.hermes_quote_price: |
| 44 | + time_diff = now - self.latest_hermes_timestamp |
| 45 | + if time_diff < self.stale_price_threshold_seconds: |
| 46 | + return self.get_hermes_price() |
| 47 | + else: |
| 48 | + logger.error("Hermes price stale by {} seconds", time_diff) |
| 49 | + else: |
| 50 | + logger.error("Hermes base/quote prices not received yet") |
| 51 | + |
| 52 | + # fall back to Lazer |
| 53 | + if self.lazer_base_price and self.lazer_quote_price: |
| 54 | + time_diff = now - self.latest_lazer_timestamp |
| 55 | + if time_diff < self.stale_price_threshold_seconds: |
| 56 | + return self.get_lazer_price() |
| 57 | + else: |
| 58 | + logger.error("Lazer price stale by {} seconds", time_diff) |
| 59 | + else: |
| 60 | + logger.error("Lazer base/quote prices not received yet") |
| 61 | + |
| 62 | + logger.error("All prices missing or stale!") |
| 63 | + return None |
| 64 | + |
| 65 | + def get_hermes_price(self): |
| 66 | + base_price = float(self.hermes_base_price) / (10.0 ** -self.hermes_base_exponent) |
| 67 | + quote_price = float(self.hermes_quote_price) / (10.0 ** -self.hermes_quote_exponent) |
| 68 | + return str(round(base_price / quote_price, 2)) |
| 69 | + |
| 70 | + def get_lazer_price(self): |
| 71 | + base_price = float(self.lazer_base_price) / (10.0 ** -self.lazer_base_exponent) |
| 72 | + quote_price = float(self.lazer_quote_price) / (10.0 ** -self.lazer_quote_exponent) |
| 73 | + return str(round(base_price / quote_price, 2)) |
0 commit comments