Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hermes): add twap endpoint for demo #1113

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hermes/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions hermes/src/api/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@ pub use {
get_vaa_ccip::*,
index::*,
latest_price_feeds::*,
latest_twap_updates::*,
latest_vaas::*,
live::*,
price_feed_ids::*,
Expand Down
62 changes: 62 additions & 0 deletions hermes/src/api/rest/latest_twap_updates.rs
Original file line number Diff line number Diff line change
@@ -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<PriceIdInput>,
}

pub async fn latest_twap_updates(
State(state): State<crate::api::ApiState>,
QsQuery(params): QsQuery<LatestTwapUpdatesQueryParams>,
) -> Result<Json<Vec<Message>>, RestError> {
let price_ids: Vec<PriceIdentifier> = 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()))
}
Loading