Skip to content

Commit e5fb272

Browse files
authored
Make crosschain price optional (#54)
1 parent cd1fbc7 commit e5fb272

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ignore_missing_imports = true
44

55
[tool.poetry]
66
name = "pyth-observer"
7-
version = "0.1.8"
7+
version = "0.1.9"
88
description = "Alerts and stuff"
99
authors = []
1010
readme = "README.md"

pyth_observer/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ async def run(self):
8989
# for each publisher).
9090
states = []
9191
price_accounts = await self.get_pyth_prices(product)
92-
crosschain_price = crosschain_prices[
93-
b58decode(product.first_price_account_key.key).hex()
94-
]
92+
crosschain_price = crosschain_prices.get(
93+
b58decode(product.first_price_account_key.key).hex(), None
94+
)
9595

9696
for _, price_account in price_accounts.items():
9797
if not price_account.aggregate_price_status:

pyth_observer/check/price_feed.py

+24-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class PriceFeedState:
2525
confidence_interval_aggregate: float
2626
coingecko_price: Optional[float]
2727
coingecko_update: Optional[int]
28-
crosschain_price: CrosschainPrice
28+
crosschain_price: Optional[CrosschainPrice]
2929

3030

3131
PriceFeedCheckConfig = Dict[str, str | float | int | bool]
@@ -181,10 +181,6 @@ def run(self) -> bool:
181181
if self.__state.status != PythPriceStatus.TRADING:
182182
return True
183183

184-
# Skip if publish time is zero
185-
if not self.__state.crosschain_price["publish_time"]:
186-
return True
187-
188184
is_market_open = HolidayCalendar().is_market_open(
189185
self.__state.asset_type,
190186
datetime.datetime.now(tz=pytz.timezone("America/New_York")),
@@ -194,6 +190,14 @@ def run(self) -> bool:
194190
if not is_market_open:
195191
return True
196192

193+
# Price should exist, it fails otherwise
194+
if not self.__state.crosschain_price:
195+
return False
196+
197+
# Skip if publish time is zero
198+
if not self.__state.crosschain_price["publish_time"]:
199+
return True
200+
197201
staleness = int(time.time()) - self.__state.crosschain_price["publish_time"]
198202

199203
# Pass if current staleness is less than `max_staleness`
@@ -204,7 +208,10 @@ def run(self) -> bool:
204208
return False
205209

206210
def error_message(self) -> str:
207-
publish_time = arrow.get(self.__state.crosschain_price["publish_time"])
211+
if self.__state.crosschain_price:
212+
publish_time = arrow.get(self.__state.crosschain_price["publish_time"])
213+
else:
214+
publish_time = arrow.get(0)
208215

209216
return dedent(
210217
f"""
@@ -225,6 +232,10 @@ def state(self) -> PriceFeedState:
225232
return self.__state
226233

227234
def run(self) -> bool:
235+
# Skip if does not exist
236+
if not self.__state.crosschain_price:
237+
return True
238+
228239
# Skip if not trading
229240
if self.__state.status != PythPriceStatus.TRADING:
230241
return True
@@ -257,12 +268,18 @@ def run(self) -> bool:
257268
return False
258269

259270
def error_message(self) -> str:
271+
# It can never happen because of the check logic but linter could not understand it.
272+
price = (
273+
self.__state.crosschain_price["price"]
274+
if self.__state.crosschain_price
275+
else None
276+
)
260277
return dedent(
261278
f"""
262279
{self.__state.symbol} is too far at the price service.
263280
264281
Price: {self.__state.price_aggregate}
265-
Price at price service: {self.__state.crosschain_price['price']}
282+
Price at price service: {price}
266283
"""
267284
).strip()
268285

0 commit comments

Comments
 (0)