From e989a5e72e5f703664b568c594463f1bf3785dac Mon Sep 17 00:00:00 2001 From: LeonardTibben Date: Tue, 27 Feb 2024 13:18:42 +0100 Subject: [PATCH] fee stats --- src/fee_stats/fee_stats_request.rs | 41 ++++++++++++++++ src/fee_stats/fee_stats_response.rs | 73 +++++++++++++++++++++++++++++ src/fee_stats/mod.rs | 9 ++++ src/horizon_client.rs | 55 ++++++++++++++++++++++ src/lib.rs | 2 + 5 files changed, 180 insertions(+) create mode 100644 src/fee_stats/fee_stats_request.rs create mode 100644 src/fee_stats/fee_stats_response.rs create mode 100644 src/fee_stats/mod.rs diff --git a/src/fee_stats/fee_stats_request.rs b/src/fee_stats/fee_stats_request.rs new file mode 100644 index 0000000..09ac34c --- /dev/null +++ b/src/fee_stats/fee_stats_request.rs @@ -0,0 +1,41 @@ +use crate::models::Request; + +/// Represents a request to fetch fee stats from the Stellar Horizon API. +/// +/// `FeeStatsRequest` is a struct used to construct queries for retrieving information about fee stats +/// from the Horizon server. This request does not include any parameters. +/// +/// # Usage +/// Instances of `FeeStatsRequest` are created and optionally configured using the builder pattern. +/// Once the desired parameters are set, the request can be passed to the Horizon client to fetch +/// fee stats. +/// +/// # Example +/// ```rust +/// use stellar_rs::fee_stats::fee_stats_request::FeeStatsRequest; +/// use stellar_rs::models::*; +/// +/// let request = FeeStatsRequest::new(); +/// +/// // The request can now be used with a Horizon client to fetch fee stats. +/// ``` +/// +#[derive(Default)] +pub struct FeeStatsRequest {} + +impl FeeStatsRequest { + /// Creates a new `FeeStatsRequest` with default parameters. + pub fn new() -> FeeStatsRequest { + FeeStatsRequest::default() + } +} + +impl Request for FeeStatsRequest { + fn get_query_parameters(&self) -> String { + "".to_string() + } + + fn build_url(&self, base_url: &str) -> String { + format!("{}/{}", base_url, super::FEE_STATS_PATH) + } +} diff --git a/src/fee_stats/fee_stats_response.rs b/src/fee_stats/fee_stats_response.rs new file mode 100644 index 0000000..43e2bc7 --- /dev/null +++ b/src/fee_stats/fee_stats_response.rs @@ -0,0 +1,73 @@ +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; + +use crate::models::Response; + +/// Represents the response from the Stellar Horizon API when requesting fee stats. +/// +/// This struct encapsulates detailed information about the fee stats, including the last ledger, +/// last ledger base fee, ledger capacity usage, and the fee charged and max fee for transactions. +/// +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)] +#[serde(rename_all = "camelCase")] +pub struct FeeStatsResponse { + /// The last ledger number. + #[serde(rename = "last_ledger")] + pub last_ledger: String, + /// The last ledger base fee. + #[serde(rename = "last_ledger_base_fee")] + pub last_ledger_base_fee: String, + /// The ledger capacity usage. + #[serde(rename = "ledger_capacity_usage")] + pub ledger_capacity_usage: String, + /// The fee charged for transactions. + #[serde(rename = "fee_charged")] + pub fee_charged: Fee, + /// The maximum fee for transactions. + #[serde(rename = "max_fee")] + pub max_fee: Fee, +} + +/// Represents the fee charged and max fee for transactions. +/// +/// This struct encapsulates detailed information about the fee charged and max fee for transactions, +/// including the max, min, mode, and percentile values. +/// +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)] +#[serde(rename_all = "camelCase")] +pub struct Fee { + /// The maximum fee for transactions. + pub max: String, + /// The minimum fee for transactions. + pub min: String, + /// The mode fee for transactions. + pub mode: String, + /// The 10th percentile fee for transactions. + pub p10: String, + /// The 20th percentile fee for transactions. + pub p20: String, + /// The 30th percentile fee for transactions. + pub p30: String, + /// The 40th percentile fee for transactions. + pub p40: String, + /// The 50th percentile fee for transactions. + pub p50: String, + /// The 60th percentile fee for transactions. + pub p60: String, + /// The 70th percentile fee for transactions. + pub p70: String, + /// The 80th percentile fee for transactions. + pub p80: String, + /// The 90th percentile fee for transactions. + pub p90: String, + /// The 95th percentile fee for transactions. + pub p95: String, + /// The 99th percentile fee for transactions. + pub p99: String, +} + +impl Response for FeeStatsResponse { + fn from_json(json: String) -> Result { + serde_json::from_str(&json).map_err(|e| e.to_string()) + } +} diff --git a/src/fee_stats/mod.rs b/src/fee_stats/mod.rs new file mode 100644 index 0000000..0c8c65b --- /dev/null +++ b/src/fee_stats/mod.rs @@ -0,0 +1,9 @@ +pub mod fee_stats_request; +pub mod fee_stats_response; + +static FEE_STATS_PATH: &str = "fee_stats"; + +pub mod prelude { + pub use super::fee_stats_request::*; + pub use super::fee_stats_response::*; +} \ No newline at end of file diff --git a/src/horizon_client.rs b/src/horizon_client.rs index c1983d1..88f07a9 100644 --- a/src/horizon_client.rs +++ b/src/horizon_client.rs @@ -3,6 +3,7 @@ use crate::{ assets::prelude::{AllAssetsRequest, AllAssetsResponse}, claimable_balances::prelude::*, effects::prelude::*, + fee_stats::prelude::*, ledgers::{ prelude::{LedgersRequest, LedgersResponse, SingleLedgerRequest, SingleLedgerResponse}, single_ledger_request::Sequence, @@ -621,6 +622,48 @@ impl HorizonClient { self.get::(request).await } + /// Retrieves a list of fee stats from the Horizon server. + /// + /// This asynchronous method fetches a list of fee stats from the Horizon server. + /// It requires a [`FeeStatsRequest`] to specify the optional query parameters. + /// + /// # Arguments + /// * `request` - A reference to a [`FeeStatsRequest`] instance, containing the + /// parameters for the fee stats request. + /// + /// # Returns + /// + /// On successful execution, returns a `Result` containing a [`FeeStatsResponse`], which includes + /// the list of fee stats obtained from the Horizon server. If the request fails, it returns an error within `Result`. + /// + /// # Example + /// To use this method, create an instance of [`FeeStatsRequest`] and set any desired + /// filters or parameters. + /// + /// ``` + /// # use stellar_rs::fee_stats::prelude::*; + /// # use stellar_rs::models::Request; + /// # use stellar_rs::horizon_client::HorizonClient; + /// # + /// # async fn example() -> Result<(), Box> { + /// # let base_url = "https://horizon-testnet.stellar.org".to_string(); + /// # let horizon_client = HorizonClient::new(base_url) + /// # .expect("Failed to create Horizon Client"); + /// let request = FeeStatsRequest::new(); + /// + /// let response = horizon_client.get_fee_stats(&request).await; + /// + /// # Ok({}) + /// # } + /// ``` + /// + pub async fn get_fee_stats( + &self, + request: &FeeStatsRequest, + ) -> Result { + self.get::(request).await + } + /// Sends a GET request to the Horizon server and retrieves a specified response type. /// /// This internal asynchronous method is designed to handle various GET requests to the @@ -1948,4 +1991,16 @@ mod tests { "account_debited" ); } + + #[tokio::test] + async fn test_get_fee_stats() { + let horizon_client = + HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap(); + + let fee_stats_request = FeeStatsRequest::new(); + let _fee_stats_response = horizon_client.get_fee_stats(&fee_stats_request).await; + + assert!(_fee_stats_response.clone().is_ok()); + // there is not much use in testing the values of the response, as they are subject to constant change + } } diff --git a/src/lib.rs b/src/lib.rs index 91b7f43..f079b97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -271,6 +271,8 @@ pub mod ledgers; pub mod effects; +pub mod fee_stats; + /// Contains core data structures and traits. /// /// This module is used by the Stellar Rust SDK to interact with the Horizon API.