|
47 | 47 | //! # Get a Cardano stake distribution by epoch |
48 | 48 | //! |
49 | 49 | //! To get a Cardano stake distribution by epoch using the [ClientBuilder][crate::client::ClientBuilder]. |
50 | | -//! The epoch represents the epoch at the end of which the Cardano stake distribution is computed by the Cardano node |
| 50 | +//! |
| 51 | +//! **Note:** The epoch represents the epoch at the end of which the Cardano stake distribution is computed by the Cardano node |
51 | 52 | //! |
52 | 53 | //! ```no_run |
53 | 54 | //! # async fn run() -> mithril_client::MithrilResult<()> { |
54 | | -//! use mithril_client::ClientBuilder; |
55 | | -//! use mithril_client::common::Epoch; |
| 55 | +//! use mithril_client::{ClientBuilder, common::Epoch}; |
56 | 56 | //! |
57 | 57 | //! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?; |
| 58 | +//! // For a specific epoch |
58 | 59 | //! let cardano_stake_distribution = client.cardano_stake_distribution().get_by_epoch(Epoch(500)).await?.unwrap(); |
| 60 | +//! // For the latest epoch known by the Mithril aggregator |
| 61 | +//! let cardano_stake_distribution = client.cardano_stake_distribution().get_for_latest_epoch().await?.unwrap(); |
| 62 | +//! // For the latest epoch known by the Mithril aggregator with an offset |
| 63 | +//! let cardano_stake_distribution = client.cardano_stake_distribution().get_for_latest_epoch_with_offset(4).await?.unwrap(); |
59 | 64 | //! |
60 | 65 | //! println!( |
61 | 66 | //! "Cardano stake distribution hash={}, epoch={}, stake_distribution={:?}", |
@@ -117,6 +122,27 @@ impl CardanoStakeDistributionClient { |
117 | 122 | .await |
118 | 123 | } |
119 | 124 |
|
| 125 | + /// Get the given Cardano stake distribution data by epoch. |
| 126 | + pub async fn get_for_latest_epoch(&self) -> MithrilResult<Option<CardanoStakeDistribution>> { |
| 127 | + self.fetch_with_aggregator_request( |
| 128 | + AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset: None }, |
| 129 | + ) |
| 130 | + .await |
| 131 | + } |
| 132 | + |
| 133 | + /// Get the given Cardano stake distribution data by epoch. |
| 134 | + pub async fn get_for_latest_epoch_with_offset( |
| 135 | + &self, |
| 136 | + offset: u64, |
| 137 | + ) -> MithrilResult<Option<CardanoStakeDistribution>> { |
| 138 | + self.fetch_with_aggregator_request( |
| 139 | + AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { |
| 140 | + offset: Some(offset), |
| 141 | + }, |
| 142 | + ) |
| 143 | + .await |
| 144 | + } |
| 145 | + |
120 | 146 | /// Fetch the given Cardano stake distribution data with an aggregator request. |
121 | 147 | /// If it cannot be found, a None is returned. |
122 | 148 | async fn fetch_with_aggregator_request( |
@@ -280,4 +306,50 @@ mod tests { |
280 | 306 |
|
281 | 307 | assert_eq!(expected_message, cardano_stake_distribution); |
282 | 308 | } |
| 309 | + |
| 310 | + #[tokio::test] |
| 311 | + async fn get_cardano_stake_distribution_for_latest_epoch_returns_message() { |
| 312 | + let expected_message = CardanoStakeDistribution::dummy(); |
| 313 | + let mut http_client = MockAggregatorClient::new(); |
| 314 | + http_client |
| 315 | + .expect_get_content() |
| 316 | + .with(eq( |
| 317 | + AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset: None }, |
| 318 | + )) |
| 319 | + .return_once(move |_| { |
| 320 | + Ok(serde_json::to_string(&CardanoStakeDistribution::dummy()).unwrap()) |
| 321 | + }); |
| 322 | + let client = CardanoStakeDistributionClient::new(Arc::new(http_client)); |
| 323 | + |
| 324 | + let cardano_stake_distribution = client |
| 325 | + .get_for_latest_epoch() |
| 326 | + .await |
| 327 | + .unwrap() |
| 328 | + .expect("This test returns a Cardano stake distribution"); |
| 329 | + |
| 330 | + assert_eq!(expected_message, cardano_stake_distribution); |
| 331 | + } |
| 332 | + |
| 333 | + #[tokio::test] |
| 334 | + async fn get_cardano_stake_distribution_for_latest_with_offset_epoch_returns_message() { |
| 335 | + let expected_message = CardanoStakeDistribution::dummy(); |
| 336 | + let mut http_client = MockAggregatorClient::new(); |
| 337 | + http_client |
| 338 | + .expect_get_content() |
| 339 | + .with(eq( |
| 340 | + AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset: Some(4) }, |
| 341 | + )) |
| 342 | + .return_once(move |_| { |
| 343 | + Ok(serde_json::to_string(&CardanoStakeDistribution::dummy()).unwrap()) |
| 344 | + }); |
| 345 | + let client = CardanoStakeDistributionClient::new(Arc::new(http_client)); |
| 346 | + |
| 347 | + let cardano_stake_distribution = client |
| 348 | + .get_for_latest_epoch_with_offset(4) |
| 349 | + .await |
| 350 | + .unwrap() |
| 351 | + .expect("This test returns a Cardano stake distribution"); |
| 352 | + |
| 353 | + assert_eq!(expected_message, cardano_stake_distribution); |
| 354 | + } |
283 | 355 | } |
0 commit comments