From 092a0ca4f6a16692ed286bdc5a005f39fc6f07b1 Mon Sep 17 00:00:00 2001 From: Thomas Luijken Date: Tue, 10 Sep 2024 17:40:58 +0200 Subject: [PATCH] Refactored liquitity pools assettypes --- stellar_rust_sdk/src/horizon_client.rs | 2 +- .../all_liquidity_pools_request.rs | 73 ++++++++----------- stellar_rust_sdk/src/liquidity_pools/mod.rs | 16 ++-- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/stellar_rust_sdk/src/horizon_client.rs b/stellar_rust_sdk/src/horizon_client.rs index 5bf031d..d45654c 100644 --- a/stellar_rust_sdk/src/horizon_client.rs +++ b/stellar_rust_sdk/src/horizon_client.rs @@ -1659,7 +1659,7 @@ impl HorizonClient { /// # let base_url = "https://horizon-testnet.stellar.org"; /// # let horizon_client = HorizonClient::new(base_url)?; /// let request = AllLiquidityPoolsRequest::new() - /// .add_alphanumeric4_reserve("USDC".to_string(), "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5".to_string()); + /// .add_alphanumeric4_reserve("USDC", "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5"); /// /// let response = horizon_client.get_all_liquidity_pools(&request).await; /// diff --git a/stellar_rust_sdk/src/liquidity_pools/all_liquidity_pools_request.rs b/stellar_rust_sdk/src/liquidity_pools/all_liquidity_pools_request.rs index 66c6b3c..3853529 100644 --- a/stellar_rust_sdk/src/liquidity_pools/all_liquidity_pools_request.rs +++ b/stellar_rust_sdk/src/liquidity_pools/all_liquidity_pools_request.rs @@ -1,28 +1,9 @@ use crate::{ - models::{Order, Request}, + models::{prelude::{AssetData, AssetType}, Order, Request}, BuildQueryParametersExt, }; use stellar_rust_sdk_derive::pagination; -/// Represents a reserve for a liquidity pool. This struct is used to specify the asset code and -#[derive(PartialEq, Debug)] -pub struct Reserve { - pub asset_code: String, - pub asset_issuer: String, -} - -/// Represents a reserve type for a liquidity pool. This enum is used to specify the type of reserve -/// to filter by when querying the Horizon server for liquidity pool records. -#[derive(PartialEq, Debug)] -pub enum ReserveType { - /// A native reserve type. It holds no value. - Native, - /// An alphanumeric 4 reserve type. It holds a Reserve struct with asset code and asset issuer. - Alphanumeric4(Reserve), - /// An alphanumeric 12 reserve type. It holds a Reserve struct with asset code and asset issuer. - Alphanumeric12(Reserve), -} - /// Represents a request for listing all liquidity pools on the Stellar Horizon API. /// /// `AllLiquidityPoolsRequest` is a struct used to construct queries for retrieving information about liquidity pools from the Horizon server. It includes parameters that allow for pagination control and sorting of the liquidity pool records. @@ -42,7 +23,7 @@ pub enum ReserveType { /// .set_limit(20).unwrap() /// .set_order(Order::Desc).unwrap() /// .add_native_reserve() -/// .add_alphanumeric4_reserve("USD".to_string(), "GAXLYH...".to_string()); +/// .add_alphanumeric4_reserve("USD", "GAXLYH..."); /// /// // The request can now be used with a Horizon client to fetch liquidity pools. /// ``` @@ -51,7 +32,7 @@ pub enum ReserveType { #[derive(Default)] pub struct AllLiquidityPoolsRequest { /// A list of reserves to filter by. - reserves: Option>, + reserves: Option>, } impl AllLiquidityPoolsRequest { @@ -68,8 +49,8 @@ impl AllLiquidityPoolsRequest { /// Adds a native reserve to the request. pub fn add_native_reserve(mut self) -> AllLiquidityPoolsRequest { match self.reserves { - Some(ref mut reserves) => reserves.push(ReserveType::Native), - None => self.reserves = Some(vec![ReserveType::Native]), + Some(ref mut reserves) => reserves.push(AssetType::Native), + None => self.reserves = Some(vec![AssetType::Native]), } self } @@ -82,16 +63,18 @@ impl AllLiquidityPoolsRequest { /// pub fn add_alphanumeric4_reserve( mut self, - asset_code: String, - asset_issuer: String, + asset_code: impl Into, + asset_issuer: impl Into, ) -> AllLiquidityPoolsRequest { + let asset_code = asset_code.into(); + let asset_issuer = asset_issuer.into(); match self.reserves { - Some(ref mut reserves) => reserves.push(ReserveType::Alphanumeric4(Reserve { + Some(ref mut reserves) => reserves.push(AssetType::Alphanumeric4(AssetData { asset_code, asset_issuer, })), None => { - self.reserves = Some(vec![ReserveType::Alphanumeric4(Reserve { + self.reserves = Some(vec![AssetType::Alphanumeric4(AssetData{ asset_code, asset_issuer, })]) @@ -108,16 +91,18 @@ impl AllLiquidityPoolsRequest { /// pub fn add_alphanumeric12_reserve( mut self, - asset_code: String, - asset_issuer: String, + asset_code: impl Into, + asset_issuer: impl Into, ) -> AllLiquidityPoolsRequest { + let asset_code = asset_code.into(); + let asset_issuer = asset_issuer.into(); match self.reserves { - Some(ref mut reserves) => reserves.push(ReserveType::Alphanumeric12(Reserve { + Some(ref mut reserves) => reserves.push(AssetType::Alphanumeric12(AssetData { asset_code, asset_issuer, })), None => { - self.reserves = Some(vec![ReserveType::Alphanumeric12(Reserve { + self.reserves = Some(vec![AssetType::Alphanumeric12(AssetData { asset_code, asset_issuer, })]) @@ -139,9 +124,9 @@ impl Request for AllLiquidityPoolsRequest { .fold(Vec::new(), |mut acc, (i, reserve)| { let separator = if i == 0 { "reserves=" } else { "%2C" }; match reserve { - ReserveType::Native => acc.push(format!("{}native", separator)), - ReserveType::Alphanumeric4(reserve) - | ReserveType::Alphanumeric12(reserve) => { + AssetType::Native => acc.push(format!("{}native", separator)), + AssetType::Alphanumeric4(reserve) + | AssetType::Alphanumeric12(reserve) => { acc.push(format!( "{}{}%3A{}", separator, reserve.asset_code, reserve.asset_issuer @@ -207,7 +192,7 @@ mod tests { #[test] fn test_add_native_reserve() { let request = AllLiquidityPoolsRequest::new().add_native_reserve(); - assert_eq!(request.reserves, Some(vec![ReserveType::Native])); + assert_eq!(request.reserves, Some(vec![AssetType::Native])); } #[test] @@ -217,23 +202,23 @@ mod tests { .add_native_reserve(); assert_eq!( request.reserves, - Some(vec![ReserveType::Native, ReserveType::Native]) + Some(vec![AssetType::Native, AssetType::Native]) ); } #[test] fn test_add_alphanumeric4_reserve() { let mut request = AllLiquidityPoolsRequest::new(); - request = request.add_alphanumeric4_reserve("USD".to_string(), "issuer".to_string()); + request = request.add_alphanumeric4_reserve("USD", "issuer"); if let Some(reserves) = request.reserves { assert_eq!(reserves.len(), 1); match &reserves[0] { - ReserveType::Alphanumeric4(reserve) => { + AssetType::Alphanumeric4(reserve) => { assert_eq!(reserve.asset_code, "USD"); assert_eq!(reserve.asset_issuer, "issuer"); } - _ => panic!("Reserve type is not Alphanumeric4"), + _ => panic!("AssetData type is not Alphanumeric4"), } } else { panic!("Reserves is None"); @@ -243,16 +228,16 @@ mod tests { #[test] fn test_add_alphanumeric12_reserve() { let mut request = AllLiquidityPoolsRequest::new(); - request = request.add_alphanumeric12_reserve("LONGASSET".to_string(), "issuer".to_string()); + request = request.add_alphanumeric12_reserve("LONGASSET", "issuer"); if let Some(reserves) = request.reserves { assert_eq!(reserves.len(), 1); match &reserves[0] { - ReserveType::Alphanumeric12(reserve) => { + AssetType::Alphanumeric12(reserve) => { assert_eq!(reserve.asset_code, "LONGASSET"); assert_eq!(reserve.asset_issuer, "issuer"); } - _ => panic!("Reserve type is not Alphanumeric12"), + _ => panic!("AssetData type is not Alphanumeric12"), } } else { panic!("Reserves is None"); @@ -262,7 +247,7 @@ mod tests { #[test] fn test_get_query_parameters() { let mut request = AllLiquidityPoolsRequest::new(); - request = request.add_alphanumeric4_reserve("USD".to_string(), "issuer".to_string()); + request = request.add_alphanumeric4_reserve("USD", "issuer"); let query_parameters = request.get_query_parameters(); assert_eq!(query_parameters, "?reserves=USD%3Aissuer"); diff --git a/stellar_rust_sdk/src/liquidity_pools/mod.rs b/stellar_rust_sdk/src/liquidity_pools/mod.rs index 35387b0..7f89a3f 100644 --- a/stellar_rust_sdk/src/liquidity_pools/mod.rs +++ b/stellar_rust_sdk/src/liquidity_pools/mod.rs @@ -118,8 +118,8 @@ async fn test_get_all_liquidity_pools() { let all_liquidity_pools_request_1 = AllLiquidityPoolsRequest::new() .add_native_reserve() .add_alphanumeric4_reserve( - "USDC".to_string(), - "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5".to_string(), + "USDC", + "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5", ) .set_limit(2) .unwrap(); @@ -168,8 +168,8 @@ async fn test_get_all_liquidity_pools() { let all_liquidity_pools_request_2 = AllLiquidityPoolsRequest::new() .add_native_reserve() .add_alphanumeric12_reserve( - "FLUTTER".to_string(), - "GCGTOQSNERFVVJ6Y7YZYDF3MTZIY63KIEFMKA26Q7YPV3AFYD2JSRNYN".to_string(), + "FLUTTER", + "GCGTOQSNERFVVJ6Y7YZYDF3MTZIY63KIEFMKA26Q7YPV3AFYD2JSRNYN", ) .set_limit(2) .unwrap(); @@ -223,8 +223,8 @@ async fn test_get_all_liquidity_pools() { let all_liquidity_pools_request_3 = AllLiquidityPoolsRequest::new() .add_alphanumeric4_reserve( - "SDK".to_string(), - "GAGTRBIF75N7NUA37JGGJZKXIS4JJKTQERRFWTP5DN4SM4OC2T6QPMQB".to_string(), + "SDK", + "GAGTRBIF75N7NUA37JGGJZKXIS4JJKTQERRFWTP5DN4SM4OC2T6QPMQB", ) .set_limit(2) .unwrap(); @@ -301,9 +301,7 @@ async fn test_get_single_liquidity_pool() { let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org").unwrap(); let single_liquidity_pool_request = SingleLiquidityPoolRequest::new() - .set_liquidity_pool_id( - "03eac63a834b31201652ed575c816b4e7beb0c4eece47caf956ff83648f486d0" - ) + .set_liquidity_pool_id("03eac63a834b31201652ed575c816b4e7beb0c4eece47caf956ff83648f486d0") .unwrap(); let single_liquidity_pool_response = horizon_client