Sfoglia il codice sorgente

feat(hermes): only return unique price updates on get_* methods

Ali Behjati 2 anni fa
parent
commit
d07579e0a6

+ 1 - 1
hermes/Cargo.lock

@@ -1574,7 +1574,7 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
 
 [[package]]
 name = "hermes"
-version = "0.4.2"
+version = "0.4.3"
 dependencies = [
  "anyhow",
  "async-trait",

+ 1 - 1
hermes/Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name        = "hermes"
-version     = "0.4.2"
+version     = "0.4.3"
 description = "Hermes is an agent that provides Verified Prices from the Pythnet Pyth Oracle."
 edition     = "2021"
 

+ 4 - 0
hermes/src/api/rest.rs

@@ -33,6 +33,7 @@ pub use {
 };
 
 pub enum RestError {
+    BenchmarkPriceNotUnique,
     UpdateDataNotFound,
     CcipUpdateDataNotFound,
     InvalidCCIPInput,
@@ -42,6 +43,9 @@ pub enum RestError {
 impl IntoResponse for RestError {
     fn into_response(self) -> Response {
         match self {
+            RestError::BenchmarkPriceNotUnique => {
+                (StatusCode::NOT_FOUND, "Benchmark price is not unique").into_response()
+            }
             RestError::UpdateDataNotFound => {
                 (StatusCode::NOT_FOUND, "Update data not found").into_response()
             }

+ 9 - 0
hermes/src/api/rest/get_price_feed.rs

@@ -89,6 +89,15 @@ pub async fn get_price_feed(
         .next()
         .ok_or(RestError::UpdateDataNotFound)?;
 
+    // Currently Benchmarks API doesn't support returning the previous publish time. So we
+    // are assuming that it is doing the same filter as us and returns not found if the
+    // price update is not unique.
+    if let Some(prev_publish_time) = price_feed.prev_publish_time {
+        if prev_publish_time == price_feed.price_feed.get_price_unchecked().publish_time {
+            return Err(RestError::BenchmarkPriceNotUnique);
+        }
+    }
+
     // Note: This is a hack to get around the fact that Benchmark doesn't give per price feed
     // update data. Since we request only for a single feed then the whole prices update data
     // is this price feed update data.

+ 15 - 6
hermes/src/api/rest/get_vaa.rs

@@ -97,13 +97,22 @@ pub async fn get_vaa(
         .map(|bytes| base64_standard_engine.encode(bytes))
         .ok_or(RestError::UpdateDataNotFound)?;
 
-    let publish_time = price_feeds_with_update_data
+    let price_feed = price_feeds_with_update_data
         .price_feeds
-        .get(0)
-        .ok_or(RestError::UpdateDataNotFound)?
-        .price_feed
-        .get_price_unchecked()
-        .publish_time;
+        .into_iter()
+        .next()
+        .ok_or(RestError::UpdateDataNotFound)?;
+
+    let publish_time = price_feed.price_feed.get_price_unchecked().publish_time;
+
+    // Currently Benchmarks API doesn't support returning the previous publish time. So we
+    // are assuming that it is doing the same filter as us and returns not found if the
+    // price update is not unique.
+    if let Some(prev_publish_time) = price_feed.prev_publish_time {
+        if prev_publish_time == price_feed.price_feed.get_price_unchecked().publish_time {
+            return Err(RestError::BenchmarkPriceNotUnique);
+        }
+    }
 
     Ok(Json(GetVaaResponse { vaa, publish_time }))
 }