Skip to content

Commit

Permalink
Make crosschain price optional (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-bahjati authored Feb 3, 2023
1 parent cd1fbc7 commit e5fb272
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ignore_missing_imports = true

[tool.poetry]
name = "pyth-observer"
version = "0.1.8"
version = "0.1.9"
description = "Alerts and stuff"
authors = []
readme = "README.md"
Expand Down
6 changes: 3 additions & 3 deletions pyth_observer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ async def run(self):
# for each publisher).
states = []
price_accounts = await self.get_pyth_prices(product)
crosschain_price = crosschain_prices[
b58decode(product.first_price_account_key.key).hex()
]
crosschain_price = crosschain_prices.get(
b58decode(product.first_price_account_key.key).hex(), None
)

for _, price_account in price_accounts.items():
if not price_account.aggregate_price_status:
Expand Down
31 changes: 24 additions & 7 deletions pyth_observer/check/price_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PriceFeedState:
confidence_interval_aggregate: float
coingecko_price: Optional[float]
coingecko_update: Optional[int]
crosschain_price: CrosschainPrice
crosschain_price: Optional[CrosschainPrice]


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

# Skip if publish time is zero
if not self.__state.crosschain_price["publish_time"]:
return True

is_market_open = HolidayCalendar().is_market_open(
self.__state.asset_type,
datetime.datetime.now(tz=pytz.timezone("America/New_York")),
Expand All @@ -194,6 +190,14 @@ def run(self) -> bool:
if not is_market_open:
return True

# Price should exist, it fails otherwise
if not self.__state.crosschain_price:
return False

# Skip if publish time is zero
if not self.__state.crosschain_price["publish_time"]:
return True

staleness = int(time.time()) - self.__state.crosschain_price["publish_time"]

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

def error_message(self) -> str:
publish_time = arrow.get(self.__state.crosschain_price["publish_time"])
if self.__state.crosschain_price:
publish_time = arrow.get(self.__state.crosschain_price["publish_time"])
else:
publish_time = arrow.get(0)

return dedent(
f"""
Expand All @@ -225,6 +232,10 @@ def state(self) -> PriceFeedState:
return self.__state

def run(self) -> bool:
# Skip if does not exist
if not self.__state.crosschain_price:
return True

# Skip if not trading
if self.__state.status != PythPriceStatus.TRADING:
return True
Expand Down Expand Up @@ -257,12 +268,18 @@ def run(self) -> bool:
return False

def error_message(self) -> str:
# It can never happen because of the check logic but linter could not understand it.
price = (
self.__state.crosschain_price["price"]
if self.__state.crosschain_price
else None
)
return dedent(
f"""
{self.__state.symbol} is too far at the price service.
Price: {self.__state.price_aggregate}
Price at price service: {self.__state.crosschain_price['price']}
Price at price service: {price}
"""
).strip()

Expand Down

0 comments on commit e5fb272

Please sign in to comment.