Skip to content

Commit

Permalink
Consolidated AssetType and AssetData models
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Luijken committed Sep 10, 2024
1 parent 3be9aef commit 4486f36
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 117 deletions.
6 changes: 3 additions & 3 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
Expand All @@ -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(),
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
Expand Down
10 changes: 5 additions & 5 deletions stellar_rust_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,15 @@ 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<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // 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(),
/// }))?;
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
Expand Down Expand Up @@ -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<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
Expand Down
2 changes: 2 additions & 0 deletions stellar_rust_sdk/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions stellar_rust_sdk/src/models/request_models.rs
Original file line number Diff line number Diff line change
@@ -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),
}
23 changes: 4 additions & 19 deletions stellar_rust_sdk/src/order_book/details_request.rs
Original file line number Diff line number Diff line change
@@ -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<S, B> {
Expand Down Expand Up @@ -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)
Expand All @@ -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(),
Expand Down
8 changes: 6 additions & 2 deletions stellar_rust_sdk/src/order_book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
12 changes: 6 additions & 6 deletions stellar_rust_sdk/src/paths/find_payment_paths_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -160,11 +160,11 @@ impl Request for FindPaymentsPathRequest<DestinationAsset, DestinationAmount, So
// Construct parameters for destination asset.
let parameters = match &self.destination_asset {
DestinationAsset(AssetType::Native) => 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
};

Expand All @@ -175,7 +175,7 @@ impl Request for FindPaymentsPathRequest<DestinationAsset, DestinationAmount, So
asset_code_prefix,
asset_data.asset_code,
asset_issuer_prefix,
asset_data.issuer_account_id
asset_data.asset_issuer
)
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl Default for Source {
/// # Example
/// ```
/// use stellar_rs::paths::prelude::*;
/// use stellar_rs::paths::{AssetType, IssuedOrNative};
/// use stellar_rs::models::prelude::*;
///
/// let request = ListStrictReceivePaymentPathsRequest::new()
/// .set_destination_asset(AssetType::Native).unwrap() // Sets the destination asset to native XLM.
Expand Down Expand Up @@ -188,11 +188,11 @@ impl Request for ListStrictReceivePaymentPathsRequest<DestinationAsset, Destinat
// Construct parameters for destination asset.
let destination_asset_parameters = match &self.destination_asset {
DestinationAsset(AssetType::Native) => 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
};

Expand All @@ -201,7 +201,7 @@ impl Request for ListStrictReceivePaymentPathsRequest<DestinationAsset, Destinat
asset_type_prefix,
asset_type,
asset_issuer_prefix,
asset_data.issuer_account_id,
asset_data.asset_issuer,
asset_code_prefix,
asset_data.asset_code,
)
Expand All @@ -222,7 +222,7 @@ impl Request for ListStrictReceivePaymentPathsRequest<DestinationAsset, Destinat
IssuedOrNative::Issued(asset_data) => {
format!(
"{}{}%3A{}",
prefix, asset_data.asset_code, asset_data.issuer_account_id
prefix, asset_data.asset_code, asset_data.asset_issuer
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -166,11 +166,11 @@ impl Request for ListStrictSendPaymentPathsRequest<SourceAsset, SourceAmount, De
// Construct parameters for source asset.
let source_asset_parameters = match &self.source_asset {
SourceAsset(AssetType::Native) => 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
};

Expand All @@ -179,7 +179,7 @@ impl Request for ListStrictSendPaymentPathsRequest<SourceAsset, SourceAmount, De
asset_type_prefix,
asset_type,
asset_issuer_prefix,
asset_data.issuer_account_id,
asset_data.asset_issuer,
asset_code_prefix,
asset_data.asset_code,
)
Expand All @@ -200,7 +200,7 @@ impl Request for ListStrictSendPaymentPathsRequest<SourceAsset, SourceAmount, De
IssuedOrNative::Issued(asset_data) => {
format!(
"{}{}%3A{}",
prefix, asset_data.asset_code, asset_data.issuer_account_id
prefix, asset_data.asset_code, asset_data.asset_issuer
)
}
}
Expand Down
Loading

0 comments on commit 4486f36

Please sign in to comment.