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

This commit is contained in:
Ali Behjati 2023-10-24 17:17:34 +02:00
parent 8bee2e8e12
commit d07579e0a6
5 changed files with 30 additions and 8 deletions

2
hermes/Cargo.lock generated
View File

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

View File

@ -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"

View File

@ -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()
}

View File

@ -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.

View File

@ -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 }))
}