Răsfoiți Sursa

list chain ids

Jayant Krishnamurthy 2 ani în urmă
părinte
comite
5bbc96c104

+ 3 - 0
pyth-rng/src/api.rs

@@ -15,12 +15,15 @@ use {
         collections::HashMap,
         sync::Arc,
     },
+    utoipa::ToSchema,
 };
 pub use {
+    chain_ids::*,
     index::*,
     revelation::*,
 };
 
+mod chain_ids;
 mod index;
 mod revelation;
 

+ 28 - 0
pyth-rng/src/api/chain_ids.rs

@@ -0,0 +1,28 @@
+use {
+    crate::api::{
+        ChainId,
+        RestError,
+    },
+    anyhow::Result,
+    axum::{
+        extract::State,
+        Json,
+    },
+    utoipa::ToSchema,
+};
+
+/// Get the list of supported chain ids
+#[utoipa::path(
+get,
+path = "/v1/chains",
+responses(
+(status = 200, description = "Successfully retrieved the list of chain ids", body = GetRandomValueResponse),
+)
+)]
+pub async fn chain_ids(
+    State(state): State<crate::api::ApiState>,
+) -> Result<Json<Vec<ChainId>>, RestError> {
+    let chain_ids = state.chains.iter().map(|(id, _)| id.clone()).collect();
+
+    Ok(Json(chain_ids))
+}

+ 3 - 2
pyth-rng/src/api/revelation.rs

@@ -25,10 +25,10 @@ use {
 /// This endpoint will not return the random value unless someone has requested the sequence number on-chain.
 ///
 /// Every blockchain supported by this service has a distinct sequence of random numbers and chain_id.
-/// Callers should pass the
+/// Callers must pass the appropriate chain_id to ensure they fetch the correct random number.
 #[utoipa::path(
 get,
-path = "/v1/revelation/{chain_id}/{sequence}",
+path = "/v1/chains/{chain_id}/revelations/{sequence}",
 responses(
 (status = 200, description = "Random value successfully retrieved", body = GetRandomValueResponse),
 (status = 403, description = "Random value cannot currently be retrieved", body = String)
@@ -72,6 +72,7 @@ pub async fn revelation(
 #[derive(Debug, serde::Serialize, serde::Deserialize, IntoParams)]
 #[into_params(parameter_in=Path)]
 pub struct GetRandomValueQueryParams {
+    #[param(value_type = String)]
     pub chain_id: ChainId,
     pub sequence: u64,
 }

+ 7 - 2
pyth-rng/src/command/run.rs

@@ -35,10 +35,11 @@ pub async fn run(opts: &RunOptions) -> Result<(), Box<dyn Error>> {
     #[openapi(
     paths(
     crate::api::revelation,
+    crate::api::chain_ids,
     ),
     components(
     schemas(
-    crate::api::GetRandomValueResponse
+    crate::api::GetRandomValueResponse,
     )
     ),
     tags(
@@ -97,7 +98,11 @@ pub async fn run(opts: &RunOptions) -> Result<(), Box<dyn Error>> {
     let app = app
         .merge(SwaggerUi::new("/docs").url("/docs/openapi.json", ApiDoc::openapi()))
         .route("/", get(api::index))
-        .route("/v1/revelation/:chain_id/:sequence", get(api::revelation))
+        .route("/v1/chains", get(api::chain_ids))
+        .route(
+            "/v1/chains/:chain_id/revelations/:sequence",
+            get(api::revelation),
+        )
         .with_state(api_state)
         // Permissive CORS layer to allow all origins
         .layer(CorsLayer::permissive());