|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + api::{ |
| 4 | + rest::RestError, |
| 5 | + types::{ |
| 6 | + BinaryUpdate, |
| 7 | + EncodingType, |
| 8 | + ParsedPublisherStakeCapsUpdate, |
| 9 | + }, |
| 10 | + ApiState, |
| 11 | + }, |
| 12 | + state::Aggregates, |
| 13 | + }, |
| 14 | + anyhow::Result, |
| 15 | + axum::{ |
| 16 | + extract::State, |
| 17 | + Json, |
| 18 | + }, |
| 19 | + base64::{ |
| 20 | + engine::general_purpose::STANDARD as base64_standard_engine, |
| 21 | + Engine as _, |
| 22 | + }, |
| 23 | + serde::{ |
| 24 | + Deserialize, |
| 25 | + Serialize, |
| 26 | + }, |
| 27 | + serde_qs::axum::QsQuery, |
| 28 | + utoipa::{ |
| 29 | + IntoParams, |
| 30 | + ToSchema, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | + |
| 35 | +#[derive(Debug, Deserialize, IntoParams)] |
| 36 | +#[into_params(parameter_in=Query)] |
| 37 | +pub struct LatestPublisherStakeCapsUpdateData { |
| 38 | + /// Get the most recent publisher stake caps update data. |
| 39 | +
|
| 40 | + /// Optional encoding type. If true, return the message in the encoding specified by the encoding parameter. Default is `hex`. |
| 41 | + #[serde(default)] |
| 42 | + encoding: EncodingType, |
| 43 | + |
| 44 | + /// If true, include the parsed update in the `parsed` field of each returned feed. Default is `true`. |
| 45 | + #[serde(default = "default_true")] |
| 46 | + parsed: bool, |
| 47 | +} |
| 48 | + |
| 49 | +fn default_true() -> bool { |
| 50 | + true |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] |
| 54 | +pub struct LatestPublisherStakeCapsUpdateDataResponse { |
| 55 | + pub binary: BinaryUpdate, |
| 56 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 57 | + pub parsed: Option<Vec<ParsedPublisherStakeCapsUpdate>>, |
| 58 | +} |
| 59 | + |
| 60 | +/// Get the most recent publisher stake caps update data. |
| 61 | +#[utoipa::path( |
| 62 | + get, |
| 63 | + path = "/v2/updates/publisher_stake_caps/latest", |
| 64 | + responses( |
| 65 | + (status = 200, description = "Publisher stake caps update data retrieved succesfully", body = Vec<PriceFeedMetadata>) |
| 66 | + ), |
| 67 | + params( |
| 68 | + LatestPublisherStakeCapsUpdateData |
| 69 | + ) |
| 70 | +)] |
| 71 | +pub async fn latest_publisher_stake_caps<S>( |
| 72 | + State(state): State<ApiState<S>>, |
| 73 | + QsQuery(params): QsQuery<LatestPublisherStakeCapsUpdateData>, |
| 74 | +) -> Result<Json<LatestPublisherStakeCapsUpdateDataResponse>, RestError> |
| 75 | +where |
| 76 | + S: Aggregates, |
| 77 | +{ |
| 78 | + let state = &*state.state; |
| 79 | + let publisher_stake_caps_with_update_data = |
| 80 | + Aggregates::get_latest_publisher_stake_caps_with_update_data(state) |
| 81 | + .await |
| 82 | + .map_err(|e| { |
| 83 | + tracing::warn!( |
| 84 | + "Error getting publisher stake caps with update data: {:?}", |
| 85 | + e |
| 86 | + ); |
| 87 | + RestError::UpdateDataNotFound |
| 88 | + })?; |
| 89 | + |
| 90 | + let encoded_data: Vec<String> = publisher_stake_caps_with_update_data |
| 91 | + .update_data |
| 92 | + .into_iter() |
| 93 | + .map(|data| match params.encoding { |
| 94 | + EncodingType::Base64 => base64_standard_engine.encode(data), |
| 95 | + EncodingType::Hex => hex::encode(data), |
| 96 | + }) |
| 97 | + .collect(); |
| 98 | + |
| 99 | + let binary = BinaryUpdate { |
| 100 | + encoding: params.encoding, |
| 101 | + data: encoded_data, |
| 102 | + }; |
| 103 | + |
| 104 | + let parsed: Option<Vec<ParsedPublisherStakeCapsUpdate>> = if params.parsed { |
| 105 | + Some(publisher_stake_caps_with_update_data.publisher_stake_caps) |
| 106 | + } else { |
| 107 | + None |
| 108 | + }; |
| 109 | + |
| 110 | + Ok(Json(LatestPublisherStakeCapsUpdateDataResponse { |
| 111 | + binary, |
| 112 | + parsed, |
| 113 | + })) |
| 114 | +} |
0 commit comments