Forráskód Böngészése

[hermes] Add get_price_feed endpoint (#883)

Ali Behjati 2 éve
szülő
commit
29dad7acc7
2 módosított fájl, 40 hozzáadás és 2 törlés
  1. 1 0
      hermes/src/api.rs
  2. 39 2
      hermes/src/api/rest.rs

+ 1 - 0
hermes/src/api.rs

@@ -48,6 +48,7 @@ pub async fn run(store: Arc<Store>, mut update_rx: Receiver<()>, rpc_addr: Strin
         .route("/ws", get(ws::ws_route_handler))
         .route("/api/latest_price_feeds", get(rest::latest_price_feeds))
         .route("/api/latest_vaas", get(rest::latest_vaas))
+        .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/price_feed_ids", get(rest::price_feed_ids))

+ 39 - 2
hermes/src/api/rest.rs

@@ -125,6 +125,42 @@ pub async fn latest_price_feeds(
     ))
 }
 
+#[derive(Debug, serde::Deserialize)]
+pub struct GetPriceFeedQueryParams {
+    id:           PriceIdInput,
+    publish_time: UnixTimestamp,
+    #[serde(default)]
+    verbose:      bool,
+    #[serde(default)]
+    binary:       bool,
+}
+
+pub async fn get_price_feed(
+    State(state): State<super::State>,
+    QsQuery(params): QsQuery<GetPriceFeedQueryParams>,
+) -> Result<Json<RpcPriceFeed>, RestError> {
+    let price_id: PriceIdentifier = params.id.into();
+
+    let price_feeds_with_update_data = state
+        .store
+        .get_price_feeds_with_update_data(
+            vec![price_id],
+            RequestTime::FirstAfter(params.publish_time),
+        )
+        .await
+        .map_err(|_| RestError::UpdateDataNotFound)?;
+
+    Ok(Json(RpcPriceFeed::from_price_feed_update(
+        price_feeds_with_update_data
+            .price_feeds
+            .into_iter()
+            .next()
+            .ok_or(RestError::UpdateDataNotFound)?,
+        params.verbose,
+        params.binary,
+    )))
+}
+
 #[derive(Debug, serde::Deserialize)]
 pub struct GetVaaQueryParams {
     id:           PriceIdInput,
@@ -133,9 +169,9 @@ pub struct GetVaaQueryParams {
 
 #[derive(Debug, serde::Serialize)]
 pub struct GetVaaResponse {
-    pub vaa:          String,
+    vaa:          String,
     #[serde(rename = "publishTime")]
-    pub publish_time: UnixTimestamp,
+    publish_time: UnixTimestamp,
 }
 
 pub async fn get_vaa(
@@ -228,6 +264,7 @@ pub async fn index() -> impl IntoResponse {
         "/api/price_feed_ids",
         "/api/latest_price_feeds?ids[]=<price_feed_id>&ids[]=<price_feed_id_2>&..(&verbose=true)(&binary=true)",
         "/api/latest_vaas?ids[]=<price_feed_id>&ids[]=<price_feed_id_2>&...",
+        "/api/get_price_feed?id=<price_feed_id>&publish_time=<publish_time_in_unix_timestamp>(&verbose=true)(&binary=true)",
         "/api/get_vaa?id=<price_feed_id>&publish_time=<publish_time_in_unix_timestamp>",
         "/api/get_vaa_ccip?data=<0x<price_feed_id_32_bytes>+<publish_time_unix_timestamp_be_8_bytes>>",
     ])