Skip to content

Commit

Permalink
Fix crosschain staleness check (#56)
Browse files Browse the repository at this point in the history
* Fix crosschain staleness check

* Cleanup staleness

* Drive-by poetry fix
  • Loading branch information
guibescos authored Jun 26, 2023
1 parent 7c05112 commit 27b44d1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ENV \
POETRY_NO_INTERACTION=1

# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python
RUN curl -sSL https://install.python-poetry.org | python
ENV PATH="$POETRY_HOME/bin:$PATH"

WORKDIR $APP_PATH
Expand Down
5 changes: 4 additions & 1 deletion pyth_observer/check/price_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ def run(self) -> bool:
if not self.__state.crosschain_price["publish_time"]:
return True

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

# Pass if current staleness is less than `max_staleness`
if staleness < self.__max_staleness:
Expand Down
3 changes: 3 additions & 0 deletions pyth_observer/crosschain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Dict, TypedDict

import requests
Expand All @@ -11,6 +12,7 @@ class CrosschainPrice(TypedDict):
price: float
conf: float
publish_time: int # UNIX timestamp
snapshot_time: int # UNIX timestamp


class CrosschainPriceObserver:
Expand Down Expand Up @@ -51,6 +53,7 @@ async def get_crosschain_prices(self) -> Dict[str, CrosschainPrice]:
"price": int(data["price"]["price"]) * 10 ** data["price"]["expo"],
"conf": int(data["price"]["conf"]) * 10 ** data["price"]["expo"],
"publish_time": data["price"]["publish_time"],
"snapshot_time": int(time.time()),
}
for data in price_feeds
}
7 changes: 6 additions & 1 deletion tests/test_checks_price_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def test_price_feed_offline_check():
confidence_interval_aggregate=10.0,
coingecko_price=1005.0,
coingecko_update=0,
crosschain_price={"price": 1003.0, "conf": 10.0, "publish_time": 123},
crosschain_price={
"price": 1003.0,
"conf": 10.0,
"publish_time": 123,
"snapshot_time": 123,
},
)

assert PriceFeedOfflineCheck(
Expand Down

0 comments on commit 27b44d1

Please sign in to comment.