From 6f9c1b506a2087403e3a6ec5a8260ad487491fba Mon Sep 17 00:00:00 2001 From: Ali Behjati Date: Thu, 19 Oct 2023 13:57:53 +0200 Subject: [PATCH] feat(hermes): add twap endpoint for demo --- hermes/src/api.rs | 1 + hermes/src/api/rest.rs | 2 + hermes/src/api/rest/latest_twap_updates.rs | 62 ++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 hermes/src/api/rest/latest_twap_updates.rs diff --git a/hermes/src/api.rs b/hermes/src/api.rs index 900144eedc..f219da8bd9 100644 --- a/hermes/src/api.rs +++ b/hermes/src/api.rs @@ -149,6 +149,7 @@ pub async fn run(opts: RunOptions, state: ApiState) -> Result<()> { .route("/api/get_price_feed", get(rest::get_price_feed)) .route("/api/get_vaa", get(rest::get_vaa)) .route("/api/get_vaa_ccip", get(rest::get_vaa_ccip)) + .route("/api/latest_twap_updates", get(rest::latest_twap_updates)) .route("/api/latest_price_feeds", get(rest::latest_price_feeds)) .route("/api/latest_vaas", get(rest::latest_vaas)) .route("/api/price_feed_ids", get(rest::price_feed_ids)) diff --git a/hermes/src/api/rest.rs b/hermes/src/api/rest.rs index 6e80fcb909..b6a27063bb 100644 --- a/hermes/src/api/rest.rs +++ b/hermes/src/api/rest.rs @@ -15,6 +15,7 @@ mod get_vaa; mod get_vaa_ccip; mod index; mod latest_price_feeds; +mod latest_twap_updates; mod latest_vaas; mod live; mod price_feed_ids; @@ -26,6 +27,7 @@ pub use { get_vaa_ccip::*, index::*, latest_price_feeds::*, + latest_twap_updates::*, latest_vaas::*, live::*, price_feed_ids::*, diff --git a/hermes/src/api/rest/latest_twap_updates.rs b/hermes/src/api/rest/latest_twap_updates.rs new file mode 100644 index 0000000000..35433060d1 --- /dev/null +++ b/hermes/src/api/rest/latest_twap_updates.rs @@ -0,0 +1,62 @@ +use { + super::verify_price_ids_exist, + crate::{ + aggregate::RequestTime, + api::{ + rest::RestError, + types::PriceIdInput, + }, + state::cache::AggregateCache, + }, + anyhow::Result, + axum::{ + extract::State, + Json, + }, + pyth_sdk::PriceIdentifier, + pythnet_sdk::messages::{ + Message, + MessageType, + }, + serde_qs::axum::QsQuery, + utoipa::IntoParams, +}; + +#[derive(Debug, serde::Deserialize, IntoParams)] +#[into_params(parameter_in=Query)] +pub struct LatestTwapUpdatesQueryParams { + /// Get the most recent price update for this set of price feed ids. + /// + /// This parameter can be provided multiple times to retrieve multiple price updates, + /// for example see the following query string: + /// + /// ``` + /// ?ids[]=a12...&ids[]=b4c... + /// ``` + #[param(rename = "ids[]")] + #[param(example = "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43")] + ids: Vec, +} + +pub async fn latest_twap_updates( + State(state): State, + QsQuery(params): QsQuery, +) -> Result>, RestError> { + let price_ids: Vec = params.ids.into_iter().map(|id| id.into()).collect(); + + verify_price_ids_exist(&state, &price_ids).await?; + + state + .state + .fetch_message_states( + price_ids + .iter() + .map(|price_id| price_id.to_bytes()) + .collect(), + RequestTime::Latest, + crate::state::cache::MessageStateFilter::Only(MessageType::TwapMessage), + ) + .await + .map_err(|_| RestError::UpdateDataNotFound) + .map(|messages| Json(messages.iter().map(|message| message.message).collect())) +}