|
| 1 | +//! An implementation of the GET `v2/cardano/assets` endpoint. |
| 2 | +
|
| 3 | +use std::collections::HashSet; |
| 4 | + |
| 5 | +use anyhow::Context; |
| 6 | +use futures::future::try_join_all; |
| 7 | +use poem_openapi::payload::Json; |
| 8 | +use poem_openapi_derive::ApiResponse; |
| 9 | + |
| 10 | +use crate::service::{ |
| 11 | + api::cardano::staking::assets_get::build_full_stake_info_response, |
| 12 | + common::{ |
| 13 | + auth::{none_or_rbac::NoneOrRBAC, rbac::token::CatalystRBACTokenV1}, |
| 14 | + objects::cardano::{network::Network, stake_info::FullStakeInfo}, |
| 15 | + responses::WithErrorResponses, |
| 16 | + types::{ |
| 17 | + cardano::{cip19_stake_address::Cip19StakeAddress, slot_no::SlotNo}, |
| 18 | + generic::error_msg::ErrorMessage, |
| 19 | + }, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +/// An endpoint responses. |
| 24 | +#[derive(ApiResponse)] |
| 25 | +pub(crate) enum ResponsesV2 { |
| 26 | + /// ## Ok |
| 27 | + /// |
| 28 | + /// The amount of ADA staked by the queried stake address, as at the indicated slot. |
| 29 | + #[oai(status = 200)] |
| 30 | + Ok(Json<FullStakeInfo>), |
| 31 | + |
| 32 | + /// ## Not Found |
| 33 | + /// |
| 34 | + /// The queried stake address was not found at the requested slot number. |
| 35 | + #[oai(status = 404)] |
| 36 | + NotFound, |
| 37 | + |
| 38 | + /// Response for unprocessable content. |
| 39 | + #[oai(status = 422)] |
| 40 | + UnprocessableContent(Json<ErrorMessage>), |
| 41 | +} |
| 42 | + |
| 43 | +/// All responses. |
| 44 | +pub type AllResponsesV2 = WithErrorResponses<ResponsesV2>; |
| 45 | + |
| 46 | +/// Get Cardano assets V2 endpoint. |
| 47 | +pub(crate) async fn endpoint( |
| 48 | + address: Option<Cip19StakeAddress>, |
| 49 | + network: Option<Network>, |
| 50 | + slot: Option<SlotNo>, |
| 51 | + auth: NoneOrRBAC, |
| 52 | +) -> AllResponsesV2 { |
| 53 | + let token: Option<_> = auth.into(); |
| 54 | + if address.is_none() && token.is_none() { |
| 55 | + return ResponsesV2::UnprocessableContent(Json( |
| 56 | + "Either stake address parameter or token must be provided" |
| 57 | + .to_string() |
| 58 | + .into(), |
| 59 | + )) |
| 60 | + .into(); |
| 61 | + } |
| 62 | + |
| 63 | + let stake_addresses = match stake_addresses(address, token).await { |
| 64 | + Err(e) => return AllResponsesV2::handle_error(&e), |
| 65 | + Ok(addresses) => addresses, |
| 66 | + }; |
| 67 | + let infos: Vec<_> = match try_join_all( |
| 68 | + stake_addresses |
| 69 | + .into_iter() |
| 70 | + .map(|a| build_full_stake_info_response(a, network.clone(), slot)), |
| 71 | + ) |
| 72 | + .await |
| 73 | + { |
| 74 | + Err(e) => return AllResponsesV2::handle_error(&e), |
| 75 | + Ok(r) => r.into_iter().flatten().collect(), |
| 76 | + }; |
| 77 | + |
| 78 | + let info = infos.into_iter().reduce(|mut acc, mut info| { |
| 79 | + acc.persistent.0.ada_amount = acc |
| 80 | + .persistent |
| 81 | + .0 |
| 82 | + .ada_amount |
| 83 | + .saturating_add(info.persistent.0.ada_amount); |
| 84 | + acc.persistent.0.slot_number = |
| 85 | + std::cmp::max(acc.persistent.0.slot_number, info.persistent.0.slot_number); |
| 86 | + acc.persistent |
| 87 | + .0 |
| 88 | + .assets |
| 89 | + .append(&mut info.persistent.0.assets); |
| 90 | + |
| 91 | + acc.volatile.0.ada_amount = acc |
| 92 | + .volatile |
| 93 | + .0 |
| 94 | + .ada_amount |
| 95 | + .saturating_add(info.volatile.0.ada_amount); |
| 96 | + acc.volatile.0.slot_number = |
| 97 | + std::cmp::max(acc.volatile.0.slot_number, info.volatile.0.slot_number); |
| 98 | + acc.volatile.0.assets.append(&mut info.volatile.0.assets); |
| 99 | + |
| 100 | + acc |
| 101 | + }); |
| 102 | + |
| 103 | + match info { |
| 104 | + Some(i) => ResponsesV2::Ok(Json(i)).into(), |
| 105 | + None => ResponsesV2::NotFound.into(), |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Returns a stake address from provided parameters. |
| 110 | +async fn stake_addresses( |
| 111 | + address: Option<Cip19StakeAddress>, |
| 112 | + token: Option<CatalystRBACTokenV1>, |
| 113 | +) -> anyhow::Result<HashSet<Cip19StakeAddress>> { |
| 114 | + if let Some(address) = address { |
| 115 | + return Ok([address].into()); |
| 116 | + } |
| 117 | + |
| 118 | + if let Some(mut token) = token { |
| 119 | + // This should never fail as a valid RBAC token can only be constructed after building a |
| 120 | + // corresponding registration chain. |
| 121 | + let chain = token |
| 122 | + .reg_chain() |
| 123 | + .await? |
| 124 | + .context("Missing registration chain for token")?; |
| 125 | + Ok(chain |
| 126 | + .stake_addresses() |
| 127 | + .into_iter() |
| 128 | + .map(Into::into) |
| 129 | + .collect()) |
| 130 | + } else { |
| 131 | + Ok(HashSet::new()) |
| 132 | + } |
| 133 | +} |
0 commit comments