Skip to content

Commit 6b0f330

Browse files
committed
feat(client-lib): get cardano stake distribution for latest epoch with/without offset
1 parent c5e00ae commit 6b0f330

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

mithril-client/src/aggregator_client.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ pub enum AggregatorRequest {
145145
epoch: Epoch,
146146
},
147147

148+
/// Get a specific [Cardano stake distribution][crate::CardanoStakeDistribution] from the aggregator for the latest epoch
149+
GetCardanoStakeDistributionForLatestEpoch {
150+
/// Optional offset to subtract to the latest epoch
151+
offset: Option<u64>,
152+
},
153+
148154
/// Lists the aggregator [Cardano stake distribution][crate::CardanoStakeDistribution]
149155
ListCardanoStakeDistributions,
150156

@@ -218,6 +224,12 @@ impl AggregatorRequest {
218224
AggregatorRequest::GetCardanoStakeDistributionByEpoch { epoch } => {
219225
format!("artifact/cardano-stake-distribution/epoch/{epoch}")
220226
}
227+
AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset } => {
228+
format!(
229+
"artifact/cardano-stake-distribution/epoch/latest{}",
230+
offset.map(|o| format!("-{o}")).unwrap_or_default()
231+
)
232+
}
221233
AggregatorRequest::ListCardanoStakeDistributions => {
222234
"artifact/cardano-stake-distributions".to_string()
223235
}
@@ -705,6 +717,17 @@ mod tests {
705717
AggregatorRequest::GetCardanoStakeDistributionByEpoch { epoch: Epoch(123) }.route()
706718
);
707719

720+
assert_eq!(
721+
"artifact/cardano-stake-distribution/epoch/latest".to_string(),
722+
AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset: None }.route()
723+
);
724+
725+
assert_eq!(
726+
"artifact/cardano-stake-distribution/epoch/latest-8".to_string(),
727+
AggregatorRequest::GetCardanoStakeDistributionForLatestEpoch { offset: Some(8) }
728+
.route()
729+
);
730+
708731
assert_eq!(
709732
"artifact/cardano-stake-distributions".to_string(),
710733
AggregatorRequest::ListCardanoStakeDistributions.route()

mithril-client/src/cardano_stake_distribution_client.rs

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,20 @@
4747
//! # Get a Cardano stake distribution by epoch
4848
//!
4949
//! 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
5152
//!
5253
//! ```no_run
5354
//! # async fn run() -> mithril_client::MithrilResult<()> {
54-
//! use mithril_client::ClientBuilder;
55-
//! use mithril_client::common::Epoch;
55+
//! use mithril_client::{ClientBuilder, common::Epoch};
5656
//!
5757
//! let client = ClientBuilder::aggregator("YOUR_AGGREGATOR_ENDPOINT", "YOUR_GENESIS_VERIFICATION_KEY").build()?;
58+
//! // For a specific epoch
5859
//! 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();
5964
//!
6065
//! println!(
6166
//! "Cardano stake distribution hash={}, epoch={}, stake_distribution={:?}",
@@ -117,6 +122,27 @@ impl CardanoStakeDistributionClient {
117122
.await
118123
}
119124

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+
120146
/// Fetch the given Cardano stake distribution data with an aggregator request.
121147
/// If it cannot be found, a None is returned.
122148
async fn fetch_with_aggregator_request(
@@ -280,4 +306,50 @@ mod tests {
280306

281307
assert_eq!(expected_message, cardano_stake_distribution);
282308
}
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+
}
283355
}

0 commit comments

Comments
 (0)