From 4486f364d05611217f0b3f4531125843a1b15e4d Mon Sep 17 00:00:00 2001 From: Thomas Luijken Date: Tue, 10 Sep 2024 14:11:13 +0200 Subject: [PATCH] Consolidated AssetType and AssetData models --- stellar_rust_sdk/src/horizon_client.rs | 6 +-- stellar_rust_sdk/src/lib.rs | 10 ++-- stellar_rust_sdk/src/models/mod.rs | 2 + stellar_rust_sdk/src/models/request_models.rs | 18 ++++++++ .../src/order_book/details_request.rs | 23 ++-------- stellar_rust_sdk/src/order_book/mod.rs | 8 +++- .../src/paths/find_payment_paths_request.rs | 12 ++--- ...st_strict_receive_payment_paths_request.rs | 14 +++--- .../list_strict_send_payment_paths_request.rs | 14 +++--- stellar_rust_sdk/src/paths/mod.rs | 46 +++++++------------ .../src/trade_aggregations/mod.rs | 2 +- .../trade_aggregations_request.rs | 21 +-------- .../src/trades/all_trades_request.rs | 20 +------- 13 files changed, 79 insertions(+), 117 deletions(-) create mode 100644 stellar_rust_sdk/src/models/request_models.rs diff --git a/stellar_rust_sdk/src/horizon_client.rs b/stellar_rust_sdk/src/horizon_client.rs index cbadfc4..eeddd7f 100644 --- a/stellar_rust_sdk/src/horizon_client.rs +++ b/stellar_rust_sdk/src/horizon_client.rs @@ -1351,7 +1351,7 @@ impl HorizonClient { /// /// ``` /// # use stellar_rs::order_book::prelude::*; - /// # use stellar_rs::models::Request; + /// # use stellar_rs::models::prelude::*; /// # use stellar_rs::horizon_client::HorizonClient; /// /// # async fn example() -> Result<(), Box> { @@ -1361,7 +1361,7 @@ impl HorizonClient { /// # let details_request = DetailsRequest::new() /// # .set_buying_asset(AssetType::Native) /// # .unwrap() - /// # .set_selling_asset(AssetType::Alphanumeric4(Asset { + /// # .set_selling_asset(AssetType::Alphanumeric4(AssetData { /// # asset_code: "USDC".to_string(), /// # asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5" /// # .to_string(), @@ -1401,7 +1401,7 @@ impl HorizonClient { /// ```rust /// use stellar_rs::horizon_client::HorizonClient; /// use stellar_rs::trade_aggregations::prelude::*; - /// use stellar_rs::models::Request; + /// use stellar_rs::models::prelude::*; /// /// # async fn example() -> Result<(), Box> { /// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?; diff --git a/stellar_rust_sdk/src/lib.rs b/stellar_rust_sdk/src/lib.rs index 4d808cf..71f7050 100644 --- a/stellar_rust_sdk/src/lib.rs +++ b/stellar_rust_sdk/src/lib.rs @@ -480,7 +480,7 @@ pub mod operations; /// ```rust /// use stellar_rs::horizon_client::HorizonClient; /// use stellar_rs::order_book::prelude::*; -/// use stellar_rs::models::Request; +/// use stellar_rs::models::prelude::*; /// /// # async fn example() -> Result<(), Box> { /// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?; @@ -488,7 +488,7 @@ pub mod operations; /// // Example: Fetching order book details /// let details_request = DetailsRequest::new() /// .set_buying_asset(AssetType::Native)? -/// .set_selling_asset(AssetType::Alphanumeric4(Asset { +/// .set_selling_asset(AssetType::Alphanumeric4(AssetData { /// asset_code: "USDC".to_string(), /// asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5".to_string(), /// }))?; @@ -521,7 +521,7 @@ pub mod order_book; /// ```rust /// use stellar_rs::horizon_client::HorizonClient; /// use stellar_rs::trade_aggregations::prelude::*; -/// use stellar_rs::models::Request; +/// use stellar_rs::models::prelude::*; /// /// # async fn example() -> Result<(), Box> { /// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?; @@ -648,8 +648,8 @@ pub mod trades; /// /// ```rust /// use stellar_rs::horizon_client::HorizonClient; -/// use stellar_rs::paths::{prelude::*, AssetType}; -/// use stellar_rs::models::Request; +/// use stellar_rs::paths::prelude::*; +/// use stellar_rs::models::prelude::*; /// /// # async fn example() -> Result<(), Box> { /// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?; diff --git a/stellar_rust_sdk/src/models/mod.rs b/stellar_rust_sdk/src/models/mod.rs index 75bb7ee..5acfbf8 100644 --- a/stellar_rust_sdk/src/models/mod.rs +++ b/stellar_rust_sdk/src/models/mod.rs @@ -1,7 +1,9 @@ mod response_models; +mod request_models; pub mod prelude { pub use super::response_models::*; + pub use super::request_models::*; pub use super::Request; pub use super::Response; } diff --git a/stellar_rust_sdk/src/models/request_models.rs b/stellar_rust_sdk/src/models/request_models.rs new file mode 100644 index 0000000..b8e96e0 --- /dev/null +++ b/stellar_rust_sdk/src/models/request_models.rs @@ -0,0 +1,18 @@ +/// Contains the details of a non-native asset. +#[derive(Clone, PartialEq, Debug, Default)] +pub struct AssetData { + pub asset_code: String, + pub asset_issuer: String, +} + +/// Represents the asset type of an asset. +#[derive(Default, Clone, PartialEq, Debug)] +pub enum AssetType { + /// A native asset_type type. It holds no value. + #[default] + Native, + /// An alphanumeric 4 asset_type type. It holds an Asset struct with asset code and asset issuer. + Alphanumeric4(AssetData), + /// An alphanumeric 12 asset_type type. It holds an Asset struct with asset code and asset issuer. + Alphanumeric12(AssetData), +} diff --git a/stellar_rust_sdk/src/order_book/details_request.rs b/stellar_rust_sdk/src/order_book/details_request.rs index 9e04594..b2a1b03 100644 --- a/stellar_rust_sdk/src/order_book/details_request.rs +++ b/stellar_rust_sdk/src/order_book/details_request.rs @@ -1,26 +1,10 @@ use crate::models::Request; +use crate::models::prelude::AssetType; pub struct SellingAsset(AssetType); pub struct NoSellingAsset; pub struct BuyingAsset(AssetType); pub struct NoBuyingAsset; -#[derive(PartialEq, Debug)] -pub struct Asset { - pub asset_code: String, - pub asset_issuer: String, -} - -/// Represents the asset type of an asset. -#[derive(PartialEq, Debug)] -pub enum AssetType { - /// A native asset_type type. It holds no Value - Native, - /// An alphanumeric 4 asset_type type. It holds a Asset struct with asset code and asset issuer. - Alphanumeric4(Asset), - /// An alphanumeric 12 asset_type type. It holds a Asset struct with asset code and asset issuer. - Alphanumeric12(Asset), -} - /// Represents the request for the details of an order book. #[derive(PartialEq, Debug)] pub struct DetailsRequest { @@ -156,7 +140,8 @@ mod tests { #[test] fn test_details_request() { - use super::{Asset, AssetType, DetailsRequest}; + use crate::models::prelude::{AssetType, AssetData}; + use super::DetailsRequest; use crate::models::Request; let details_request = DetailsRequest::new() .set_buying_asset(AssetType::Native) @@ -172,7 +157,7 @@ mod tests { let details_request = DetailsRequest::new() .set_buying_asset(AssetType::Native) .unwrap() - .set_selling_asset(AssetType::Alphanumeric4(Asset { + .set_selling_asset(AssetType::Alphanumeric4(AssetData { asset_code: "USDC".to_string(), asset_issuer: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5" .to_string(), diff --git a/stellar_rust_sdk/src/order_book/mod.rs b/stellar_rust_sdk/src/order_book/mod.rs index 03a68a2..820c232 100644 --- a/stellar_rust_sdk/src/order_book/mod.rs +++ b/stellar_rust_sdk/src/order_book/mod.rs @@ -9,10 +9,14 @@ pub mod prelude { } pub mod tests { + + #[tokio::test] async fn get_order_bookdetails() { + + use crate::models::prelude::*; use crate::horizon_client; - use crate::order_book::prelude::{Asset, AssetType, DetailsRequest}; + use crate::order_book::prelude::DetailsRequest; const BIDS_N: &u32 = &1; const BIDS_D: &u32 = &5; @@ -32,7 +36,7 @@ pub mod tests { let details_request = DetailsRequest::new() .set_selling_asset(AssetType::Native) .unwrap() - .set_buying_asset(AssetType::Alphanumeric4(Asset { + .set_buying_asset(AssetType::Alphanumeric4(AssetData { asset_code: "IOM".to_string(), asset_issuer: "GCDE6MVFIOYF7YZCSVA6V7MDCFTNWMIOF5PQU3DWPH27AHNX4ERY6AKS" .to_string(), diff --git a/stellar_rust_sdk/src/paths/find_payment_paths_request.rs b/stellar_rust_sdk/src/paths/find_payment_paths_request.rs index f2f54c6..c8bb6fe 100644 --- a/stellar_rust_sdk/src/paths/find_payment_paths_request.rs +++ b/stellar_rust_sdk/src/paths/find_payment_paths_request.rs @@ -19,7 +19,7 @@ use crate::BuildQueryParametersExt; /// # Example /// ``` /// use stellar_rs::paths::prelude::*; -/// use stellar_rs::paths::{AssetType}; +/// use stellar_rs::models::prelude::AssetType; /// /// let request = FindPaymentsPathRequest::new() /// .set_destination_asset(AssetType::Native).unwrap() // Sets the destination asset to native XLM. @@ -160,11 +160,11 @@ impl Request for FindPaymentsPathRequest format!("{}native", asset_type_prefix), - DestinationAsset(AssetType::CreditAlphanum4(asset_data)) - | DestinationAsset(AssetType::CreditAlphanum12(asset_data)) => { + DestinationAsset(AssetType::Alphanumeric4(asset_data)) + | DestinationAsset(AssetType::Alphanumeric12(asset_data)) => { let asset_type = match self.destination_asset { - DestinationAsset(AssetType::CreditAlphanum4(_)) => "credit_alphanum4", - DestinationAsset(AssetType::CreditAlphanum12(_)) => "credit_alphanum12", + DestinationAsset(AssetType::Alphanumeric4(_)) => "credit_alphanum4", + DestinationAsset(AssetType::Alphanumeric12(_)) => "credit_alphanum12", _ => "", // should not be reached }; @@ -175,7 +175,7 @@ impl Request for FindPaymentsPathRequest format!("{}native", asset_type_prefix), - DestinationAsset(AssetType::CreditAlphanum4(asset_data)) - | DestinationAsset(AssetType::CreditAlphanum12(asset_data)) => { + DestinationAsset(AssetType::Alphanumeric4(asset_data)) + | DestinationAsset(AssetType::Alphanumeric12(asset_data)) => { let asset_type = match self.destination_asset { - DestinationAsset(AssetType::CreditAlphanum4(_)) => "credit_alphanum4", - DestinationAsset(AssetType::CreditAlphanum12(_)) => "credit_alphanum12", + DestinationAsset(AssetType::Alphanumeric4(_)) => "credit_alphanum4", + DestinationAsset(AssetType::Alphanumeric12(_)) => "credit_alphanum12", _ => "", // should not be reached }; @@ -201,7 +201,7 @@ impl Request for ListStrictReceivePaymentPathsRequest { format!( "{}{}%3A{}", - prefix, asset_data.asset_code, asset_data.issuer_account_id + prefix, asset_data.asset_code, asset_data.asset_issuer ) } } diff --git a/stellar_rust_sdk/src/paths/list_strict_send_payment_paths_request.rs b/stellar_rust_sdk/src/paths/list_strict_send_payment_paths_request.rs index 9c690ef..49d8938 100644 --- a/stellar_rust_sdk/src/paths/list_strict_send_payment_paths_request.rs +++ b/stellar_rust_sdk/src/paths/list_strict_send_payment_paths_request.rs @@ -52,7 +52,7 @@ impl Default for Destination { /// # Example /// ``` /// use stellar_rs::paths::prelude::*; -/// use stellar_rs::paths::{AssetType, IssuedOrNative}; +/// use stellar_rs::models::prelude::*; /// /// let request = ListStrictSendPaymentPathsRequest::new() /// .set_source_asset(AssetType::Native).unwrap() // Sets the source asset to native XLM. @@ -166,11 +166,11 @@ impl Request for ListStrictSendPaymentPathsRequest format!("{}native", asset_type_prefix), - SourceAsset(AssetType::CreditAlphanum4(asset_data)) - | SourceAsset(AssetType::CreditAlphanum12(asset_data)) => { + SourceAsset(AssetType::Alphanumeric4(asset_data)) + | SourceAsset(AssetType::Alphanumeric12(asset_data)) => { let asset_type = match self.source_asset { - SourceAsset(AssetType::CreditAlphanum4(_)) => "credit_alphanum4", - SourceAsset(AssetType::CreditAlphanum12(_)) => "credit_alphanum12", + SourceAsset(AssetType::Alphanumeric4(_)) => "credit_alphanum4", + SourceAsset(AssetType::Alphanumeric12(_)) => "credit_alphanum12", _ => "", // should not be reached }; @@ -179,7 +179,7 @@ impl Request for ListStrictSendPaymentPathsRequest { format!( "{}{}%3A{}", - prefix, asset_data.asset_code, asset_data.issuer_account_id + prefix, asset_data.asset_code, asset_data.asset_issuer ) } } diff --git a/stellar_rust_sdk/src/paths/mod.rs b/stellar_rust_sdk/src/paths/mod.rs index 93e5939..5142b9b 100644 --- a/stellar_rust_sdk/src/paths/mod.rs +++ b/stellar_rust_sdk/src/paths/mod.rs @@ -1,3 +1,4 @@ +use crate::models::prelude::*; /// Provides the `FindPaymentPathsRequest`. /// /// # Usage @@ -74,28 +75,12 @@ pub struct NoSourceAccount; #[derive(Default, Clone, Debug)] pub struct SourceAccount(String); -/// Represents structure of the required asset. -#[derive(Default, Clone, Debug)] -pub enum AssetType { - #[default] - Native, - CreditAlphanum4(Asset), - CreditAlphanum12(Asset), -} - -/// Represents an asset containing an asset code and issuer account ID. -#[derive(Clone, Debug)] -pub struct Asset { - pub asset_code: String, - pub issuer_account_id: String, -} - /// Represents structure of an asset used in the vector of optional assets. #[derive(Default, Clone, Debug)] pub enum IssuedOrNative { #[default] Native, - Issued(Asset), + Issued(AssetData), } /// The `prelude` module of the `paths` module. @@ -133,6 +118,7 @@ pub mod prelude { #[cfg(test)] mod tests { use super::prelude::*; + use crate::models::prelude::*; use super::{AssetType, IssuedOrNative}; use crate::{horizon_client::HorizonClient, models::*}; @@ -143,13 +129,13 @@ mod tests { #[tokio::test] async fn test_find_payment_paths_request() { - use crate::paths::{Asset, PATHS_PATH}; + use crate::paths::PATHS_PATH; // Test creating and sending a request with source assets. Only the response status will be checked, as the request will not yield comparable data. let request = FindPaymentsPathRequest::new() - .set_destination_asset(AssetType::CreditAlphanum4(Asset { + .set_destination_asset(AssetType::Alphanumeric4(AssetData { asset_code: "USDC".to_string(), - issuer_account_id: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" + asset_issuer: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" .to_string(), })) .unwrap() @@ -229,13 +215,13 @@ mod tests { #[tokio::test] async fn test_list_strict_receive_payment_paths_request() { - use crate::paths::{Asset, PATHS_PATH, PATHS_STRICT_RECEIVE_PATH}; + use crate::paths::{PATHS_PATH, PATHS_STRICT_RECEIVE_PATH}; // Test creating and sending a request with source assets. Only the response status will be checked, as the request will not yield comparable data. let request = ListStrictReceivePaymentPathsRequest::new() - .set_destination_asset(AssetType::CreditAlphanum4(Asset { + .set_destination_asset(AssetType::Alphanumeric4(AssetData { asset_code: "USDC".to_string(), - issuer_account_id: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" + asset_issuer: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" .to_string(), })) .unwrap() @@ -244,9 +230,9 @@ mod tests { .set_source(Source::SourceAssets(vec![ IssuedOrNative::Native, IssuedOrNative::Native, - IssuedOrNative::Issued(Asset { + IssuedOrNative::Issued(AssetData { asset_code: "USDC".to_string(), - issuer_account_id: "GBAKINTNEGR7PO6Z6XW2S5ITT5VARNW6DZ5K4OYSLFNEA2CSMUM2UEF4" + asset_issuer: "GBAKINTNEGR7PO6Z6XW2S5ITT5VARNW6DZ5K4OYSLFNEA2CSMUM2UEF4" .to_string(), }), ])) @@ -355,13 +341,13 @@ mod tests { #[tokio::test] async fn test_list_strict_send_payment_paths_request() { - use crate::paths::{Asset, PATHS_PATH, PATHS_STRICT_SEND_PATH}; + use crate::paths::{PATHS_PATH, PATHS_STRICT_SEND_PATH}; // Test creating and sending a request with destination assets. Only the response status will be checked, as the request will not yield comparable data. let request = ListStrictSendPaymentPathsRequest::new() - .set_source_asset(AssetType::CreditAlphanum4(Asset { + .set_source_asset(AssetType::Alphanumeric4(AssetData { asset_code: "USDC".to_string(), - issuer_account_id: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" + asset_issuer: "GBJJ5OCBXNZWHSJJ4YQ6ECK24MBJSZMLEMINHKGGEWUA5RU2EDMPN6MS" .to_string(), })) .unwrap() @@ -370,9 +356,9 @@ mod tests { .set_destination(Destination::DestinationAssets(vec![ IssuedOrNative::Native, IssuedOrNative::Native, - IssuedOrNative::Issued(Asset { + IssuedOrNative::Issued(AssetData { asset_code: "USDC".to_string(), - issuer_account_id: "GBAKINTNEGR7PO6Z6XW2S5ITT5VARNW6DZ5K4OYSLFNEA2CSMUM2UEF4" + asset_issuer: "GBAKINTNEGR7PO6Z6XW2S5ITT5VARNW6DZ5K4OYSLFNEA2CSMUM2UEF4" .to_string(), }), ])) diff --git a/stellar_rust_sdk/src/trade_aggregations/mod.rs b/stellar_rust_sdk/src/trade_aggregations/mod.rs index b09da6f..6afdcc3 100644 --- a/stellar_rust_sdk/src/trade_aggregations/mod.rs +++ b/stellar_rust_sdk/src/trade_aggregations/mod.rs @@ -61,6 +61,7 @@ pub mod prelude { #[cfg(test)] pub mod test { + use crate::models::prelude::*; use crate::{horizon_client::HorizonClient, trade_aggregations::prelude::*}; // Request constants. @@ -194,7 +195,6 @@ pub mod test { #[tokio::test] async fn test_asset_query_parameters() { - use crate::models::*; // Test if different combinations of asset types result in a valid RESTful query. The `Native` asset, for example, // has a different amount of parameters than the alphanumeric types. The separators should always be correct, whatever // the combination. diff --git a/stellar_rust_sdk/src/trade_aggregations/trade_aggregations_request.rs b/stellar_rust_sdk/src/trade_aggregations/trade_aggregations_request.rs index da57641..a263590 100644 --- a/stellar_rust_sdk/src/trade_aggregations/trade_aggregations_request.rs +++ b/stellar_rust_sdk/src/trade_aggregations/trade_aggregations_request.rs @@ -1,3 +1,4 @@ +use crate::models::prelude::AssetType; use crate::{models::*, BuildQueryParametersExt}; /// Represents the base asset. Contains an enum of one of the possible asset types. @@ -16,25 +17,6 @@ pub struct CounterAsset(AssetType); #[derive(PartialEq, Debug)] pub struct NoCounterAsset; -/// Contains the details of a non-native asset. -#[derive(Clone, PartialEq, Debug, Default)] -pub struct AssetData { - pub asset_code: String, - pub asset_issuer: String, -} - -/// Represents the asset type of an asset. -#[derive(Clone, PartialEq, Debug)] -pub enum AssetType { - /// A native asset_type type. It holds no value. - // #[default] - Native, - /// An alphanumeric 4 asset_type type. It holds an Asset struct with asset code and asset issuer. - Alphanumeric4(AssetData), - /// An alphanumeric 12 asset_type type. It holds an Asset struct with asset code and asset issuer. - Alphanumeric12(AssetData), -} - /// Represents the absense of a resolution value. #[derive(Default, Clone)] pub struct NoResolution; @@ -82,6 +64,7 @@ impl std::fmt::Display for ResolutionData { /// # Example /// ``` /// use stellar_rs::{trade_aggregations::prelude::*, models::*}; +/// use stellar_rs::models::prelude::*; /// /// let request = TradeAggregationsRequest::new() /// .set_base_asset(AssetType::Native).unwrap() diff --git a/stellar_rust_sdk/src/trades/all_trades_request.rs b/stellar_rust_sdk/src/trades/all_trades_request.rs index 07acb9e..11d7a9b 100644 --- a/stellar_rust_sdk/src/trades/all_trades_request.rs +++ b/stellar_rust_sdk/src/trades/all_trades_request.rs @@ -1,28 +1,11 @@ use crate::models::*; use stellar_rust_sdk_derive::pagination; +use crate::models::prelude::AssetType; /// Represents the base and counter assets. Contains an enum of one of the possible asset types. #[derive(PartialEq, Debug)] pub struct TradeAsset(AssetType); -// Contains the details of a non-native asset. -#[derive(PartialEq, Debug)] -pub struct AssetData { - pub asset_code: String, - pub asset_issuer: String, -} - -/// Represents the asset type of an asset. -#[derive(PartialEq, Debug)] -pub enum AssetType { - /// A native asset_type type. It holds no value. - Native, - /// An alphanumeric 4 asset_type type. It holds an Asset struct with asset code and asset issuer. - Alphanumeric4(AssetData), - /// An alphanumeric 12 asset_type type. It holds an Asset struct with asset code and asset issuer. - Alphanumeric12(AssetData), -} - /// Represents a request to list all trades from the Stellar Horizon API. /// /// This structure is used to construct a query to retrieve a comprehensive list of trades, which @@ -39,6 +22,7 @@ pub enum AssetType { /// # Example /// ``` /// use stellar_rs::{trades::prelude::*, models::*}; +/// use stellar_rs::models::prelude::AssetType; /// /// let request = AllTradesRequest::new() /// .set_base_asset(AssetType::Native).unwrap() // Optional selling asset filter