From 27b44d12734410d1575d9f3c6dfe2076c37e4046 Mon Sep 17 00:00:00 2001 From: guibescos <59208140+guibescos@users.noreply.github.com> Date: Mon, 26 Jun 2023 13:45:25 +0100 Subject: [PATCH] Fix crosschain staleness check (#56) * Fix crosschain staleness check * Cleanup staleness * Drive-by poetry fix --- Dockerfile | 2 +- pyth_observer/check/price_feed.py | 5 ++++- pyth_observer/crosschain.py | 3 +++ tests/test_checks_price_feed.py | 7 ++++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7650e77..2c635c4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/pyth_observer/check/price_feed.py b/pyth_observer/check/price_feed.py index 5a3d803..74d8c98 100644 --- a/pyth_observer/check/price_feed.py +++ b/pyth_observer/check/price_feed.py @@ -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: diff --git a/pyth_observer/crosschain.py b/pyth_observer/crosschain.py index 11f6d77..31dc86f 100644 --- a/pyth_observer/crosschain.py +++ b/pyth_observer/crosschain.py @@ -1,3 +1,4 @@ +import time from typing import Dict, TypedDict import requests @@ -11,6 +12,7 @@ class CrosschainPrice(TypedDict): price: float conf: float publish_time: int # UNIX timestamp + snapshot_time: int # UNIX timestamp class CrosschainPriceObserver: @@ -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 } diff --git a/tests/test_checks_price_feed.py b/tests/test_checks_price_feed.py index c6107c5..03b4135 100644 --- a/tests/test_checks_price_feed.py +++ b/tests/test_checks_price_feed.py @@ -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(