Skip to content

Commit

Permalink
Refactor query parameter builder
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease authored and tluijken committed Jul 26, 2024
1 parent 33c56f8 commit 5bf8241
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 48 deletions.
8 changes: 4 additions & 4 deletions stellar_rust_sdk/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ pub mod test {
#[tokio::test]
async fn test_get_all_assets() {
static ASSET_TYPE: &str = "credit_alphanum4";
static ASSET_CODE: &str = "004";
static ASSET_ISSUER: &str = "GDJUV2K6YBYUTI6GAA6P6BKVBQQDIOBZYDV2YOWOZI35LLSVMO6J4K7B";
static ASSET_CODE: &str = "001";
static ASSET_ISSUER: &str = "GAIX6Y5CRIH7A67IWGM26J6KIVPNXBHDPUP6KNHYSKFJ6VWABJKZYMKA";
static NUM_ACCOUNTS: &u32 = &1;
static NUM_CLAIMABLE_BALANCES: &u32 = &0;
static NUM_LIQUIDITY_POOLS: &u32 = &0;

static AMOUNT: &str = "999.0000000";
static AMOUNT: &str = "10.0000000";
static AUTHORIZED: &u32 = &1;
static AUTHORIZED_TO_MAINTAIN_LIABILITIES: &u32 = &0;
static UNAUTHORIZED: &u32 = &0;
static CLAIMABLE_BALANCES_AMOUNT: &str = "0.0000000";
static LIQUIDITY_POOLS_AMOUNT: &str = "0.0000000";
static CONTRACTS_AMOUNT: &str = "0.0000000";
static BALANCES_AUTHORIZED: &str = "999.0000000";
static BALANCES_AUTHORIZED: &str = "10.0000000";
static BALANCES_UNAUTHORIZED: &str = "0.0000000";
static AUTH_REQUIRED: &bool = &false;
static AUTH_REVOCABLE: &bool = &false;
Expand Down
12 changes: 6 additions & 6 deletions stellar_rust_sdk/src/liquidity_pools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ async fn test_get_all_liquidity_pools() {
const RSP_2_LIQUIDITY_POOL_TOTAL_TRUSTLINES: &str = "1";
const RSP_2_LIQUIDITY_POOL_TOTAL_SHARES: &str = "249.0000000";
const RSP_2_LIQUIDITY_POOL_RESERVE_ASSET_0: &str = "native";
const RSP_2_LIQUIDITY_POOL_RESERVE_AMOUNT_0: &str = "249.0000000";
const RSP_2_LIQUIDITY_POOL_RESERVE_AMOUNT_0: &str = "1249.0000000";
const RSP_2_LIQUIDITY_POOL_RESERVE_ASSET_1: &str =
"FLUTTER:GCGTOQSNERFVVJ6Y7YZYDF3MTZIY63KIEFMKA26Q7YPV3AFYD2JSRNYN";
const RSP_2_LIQUIDITY_POOL_RESERVE_AMOUNT_1: &str = "249.0000000";
const RSP_2_LIQUIDITY_POOL_RESERVE_AMOUNT_1: &str = "49.7600322";

const RSP_3_LIQUIDITY_POOL_ID: &str =
"0b3c88caa5aeada296646c1810893e3b04cba0426cff8ff6a63cf6f35cc7f5b3";
Expand Down Expand Up @@ -294,12 +294,12 @@ async fn test_get_single_liquidity_pool() {
const LIQUIDITY_POOL_TOTAL_SHARES: &str = "249.0000000";
const LIQUIDITY_POOL_RESERVE_ASSET_0: &str =
"native";
const LIQUIDITY_POOL_RESERVE_AMOUNT_0: &str = "249.0000000";
const LIQUIDITY_POOL_RESERVE_AMOUNT_0: &str = "1249.0000000";
const LIQUIDITY_POOL_RESERVE_ASSET_1: &str =
"FLUTTER:GCGTOQSNERFVVJ6Y7YZYDF3MTZIY63KIEFMKA26Q7YPV3AFYD2JSRNYN";
const LIQUIDITY_POOL_RESERVE_AMOUNT_1: &str = "249.0000000";
const LIQUIDITY_POOL_LAST_MODIFIED_LEDGER: i64 = 258622;
const LIQUIDITY_POOL_LAST_MODIFIED_TIME: &str = "2024-06-27T14:27:46Z";
const LIQUIDITY_POOL_RESERVE_AMOUNT_1: &str = "49.7600322";
const LIQUIDITY_POOL_LAST_MODIFIED_LEDGER: i64 = 716080;
const LIQUIDITY_POOL_LAST_MODIFIED_TIME: &str = "2024-07-25T11:09:01Z";

let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions stellar_rust_sdk/src/order_book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub mod tests {
use crate::order_book::prelude::{Asset, AssetType, DetailsRequest};

const BIDS_N: &u32 = &3;
const BIDS_D: &u32 = &1;
const BIDS_PRICE: &str = "3.0000000";
const BIDS_D: &u32 = &2;
const BIDS_PRICE: &str = "1.5000000";
const ASKS_N: &u32 = &5;
const ASKS_D: &u32 = &1;
const ASKS_PRICE: &str = "5.0000000";
Expand Down
66 changes: 65 additions & 1 deletion stellar_rust_sdk/src/trade_aggregations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub mod test {
.get_trade_aggregations(&trade_aggregations_request)
.await;

assert!(trade_aggregations_response.clone().is_ok());
// assert!(trade_aggregations_response.clone().is_ok());
let binding = trade_aggregations_response.unwrap();

let response = &binding.embedded().records()[0];
Expand All @@ -183,4 +183,68 @@ pub mod test {
assert_eq!(response.close_ratio().numenator(), CLOSE_N);
assert_eq!(response.close_ratio().denominator(), CLOSE_D);
}

#[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.

// Test 2 different, non-native, asset types.
let request = TradeAggregationsRequest::new()
.set_base_asset(AssetType::Alphanumeric4(AssetData{
asset_issuer: "baseissuer".to_string(),
asset_code: "basecode".to_string()}))
.unwrap()
.set_counter_asset(AssetType::Alphanumeric12(AssetData{
asset_issuer: "counterissuer".to_string(),
asset_code: "countercode".to_string()}))
.unwrap()
.set_resolution(Resolution(ResolutionData::Duration604800000))
.unwrap();
assert_eq!(request.get_query_parameters(),
"?base_asset_type=credit_alphanum4&base_asset_code=basecode&base_asset_issuer=baseissuer&counter_asset_type=credit_alphanum12&counter_asset_code=countercode&counter_asset_issuer=counterissuer&resolution=604800000"
);

// Test 1 native, 1 non-native asset type.
let request = TradeAggregationsRequest::new()
.set_counter_asset(AssetType::Native)
.unwrap()
.set_base_asset(AssetType::Alphanumeric12(AssetData{
asset_issuer: "counterissuer".to_string(),
asset_code: "countercode".to_string()}))
.unwrap()
.set_resolution(Resolution(ResolutionData::Duration604800000))
.unwrap();
assert_eq!(request.get_query_parameters(),
"?base_asset_type=credit_alphanum12&base_asset_code=countercode&base_asset_issuer=counterissuer&counter_asset_type=native&resolution=604800000"
);

// Test 1 non-native, 1 native asset type.
let request = TradeAggregationsRequest::new()
.set_base_asset(AssetType::Alphanumeric4(AssetData{
asset_issuer: "counterissuer".to_string(),
asset_code: "countercode".to_string()}))
.unwrap()
.set_resolution(Resolution(ResolutionData::Duration604800000))
.unwrap()
.set_counter_asset(AssetType::Native)
.unwrap();
assert_eq!(request.get_query_parameters(),
"?base_asset_type=credit_alphanum4&base_asset_code=countercode&base_asset_issuer=counterissuer&counter_asset_type=native&resolution=604800000"
);

// Test 2 non-native asset types.
let request = TradeAggregationsRequest::new()
.set_base_asset(AssetType::Native)
.unwrap()
.set_resolution(Resolution(ResolutionData::Duration604800000))
.unwrap()
.set_counter_asset(AssetType::Native)
.unwrap();
assert_eq!(request.get_query_parameters(),
"?base_asset_type=native&counter_asset_type=native&resolution=604800000"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,44 +317,39 @@ impl <B, C> TradeAggregationsRequest<B, C, Resolution> {
}
}


impl Request for TradeAggregationsRequest<BaseAsset, CounterAsset, Resolution> {
fn get_query_parameters(&self) -> String {
let mut asset_parameters: Vec<String> = Vec::new();
match &self.base_asset.0 {
AssetType::Native => {
asset_parameters.push(format!("base_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
asset_parameters.push(format!("base_asset_type=credit_alphanum4"));
asset_parameters.push(format!("&base_asset_code={}", asset.asset_code));
asset_parameters.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
asset_parameters.push(format!("base_asset_type=credit_alphanum12"));
asset_parameters.push(format!("&base_asset_code={}", asset.asset_code));
asset_parameters.push(format!("&base_asset_issuer={}", asset.asset_issuer));
}
}

match &self.counter_asset.0 {
AssetType::Native => {
asset_parameters.push(format!("&counter_asset_type=native"));
}
AssetType::Alphanumeric4(asset) => {
asset_parameters.push(format!("&counter_asset_type=credit_alphanum4"));
asset_parameters.push(format!("&counter_asset_code={}", asset.asset_code));
asset_parameters.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
AssetType::Alphanumeric12(asset) => {
asset_parameters.push(format!("&counter_asset_type=credit_alphanum12"));
asset_parameters.push(format!("&counter_asset_code={}", asset.asset_code));
asset_parameters.push(format!("&counter_asset_issuer={}", asset.asset_issuer));
}
}
let asset_parameters =
vec![&self.base_asset.0, &self.counter_asset.0]
.iter()
.enumerate()
.fold(Vec::new(), |mut parameters, (i, asset)| {
let asset_type_prefix = if i == 0 { "base_asset_type=" } // no `&` for `base_asset_type`, as the query begins with `?`
else { "&counter_asset_type=" };
match asset {
AssetType::Native => parameters.push(format!("{}native", asset_type_prefix)),
AssetType::Alphanumeric4(asset_data) | AssetType::Alphanumeric12(asset_data) => {
let asset_type = match asset {
AssetType::Alphanumeric4(_) => "credit_alphanum4",
AssetType::Alphanumeric12(_) => "credit_alphanum12",
_ => "", // should not be reached
};
let asset_issuer_prefix = if i == 0 { "&base_asset_issuer=" } else { "&counter_asset_issuer=" };
let asset_code_prefix = if i == 0 { "&base_asset_code=" } else { "&counter_asset_code=" };
parameters.push(format!(
"{}{}{}{}{}{}",
asset_type_prefix, asset_type,
asset_code_prefix, asset_data.asset_code,
asset_issuer_prefix, asset_data.asset_issuer
));
}
}
parameters
})
.join("");

vec![
Some(asset_parameters.join("")),
Some(asset_parameters),
Some(format!("resolution={}", self.resolution.0)),
self.start_time.as_ref().map(|s| format!("start_time={}", s)),
self.end_time.as_ref().map(|e| format!("end_time={}", e)),
Expand All @@ -371,4 +366,7 @@ impl Request for TradeAggregationsRequest<BaseAsset, CounterAsset, Resolution> {
self.get_query_parameters()
)
}
}
}



0 comments on commit 5bf8241

Please sign in to comment.