Skip to content

Commit

Permalink
Merge branch 'develop' into single-offer-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed Apr 22, 2024
2 parents 06fe152 + b9daa81 commit 24cc772
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 92 deletions.
115 changes: 85 additions & 30 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ use crate::{
single_ledger_request::Sequence,
},
offers::prelude::*,
liquidity_pools::{all_liquidity_pools_request::AllLiquidityPoolsRequest, prelude::{
AllLiquidityPoolsResponse, LiquidityPool, LiquidityPoolId,
SingleLiquidityPoolRequest,
}},
liquidity_pools::{
all_liquidity_pools_request::AllLiquidityPoolsRequest,
prelude::{
AllLiquidityPoolsResponse, LiquidityPool, LiquidityPoolId, SingleLiquidityPoolRequest,
},
},
models::{Request, Response},
operations::{
operations_for_account_request::OperationsForAccountRequest,
prelude::{AllOperationsRequest, OperationResponse, OperationsForLedgerRequest},
prelude::{
AllOperationsRequest, OperationResponse, OperationsForLedgerRequest,
OperationsForLiquidityPoolRequest,
},
response::Operation,
single_operation_request::{OperationId, SingleOperationRequest},
},
Expand Down Expand Up @@ -794,23 +799,23 @@ impl HorizonClient {
}

/// Retrieves a list of all fee stats from the Horizon server.
///
///
/// This asynchronous method fetches a list of all 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 all fee stats obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
///
/// # Usage
/// To use this method, create an instance of [`FeeStatsRequest`] and set any desired
/// filters or parameters.
///
///
/// ```
/// # use stellar_rs::fee_stats::fee_stats_request::FeeStatsRequest;
/// # use stellar_rs::horizon_client::HorizonClient;
Expand All @@ -820,9 +825,9 @@ impl HorizonClient {
/// # 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;
///
///
/// // Access the fee stats
/// if let Ok(fee_stats_response) = response {
/// println!("Max Fee: {:?}", fee_stats_response.max_fee());
Expand All @@ -831,7 +836,7 @@ impl HorizonClient {
/// # Ok({})
/// # }
/// ```
///
///
pub async fn get_fee_stats(
&self,
request: &FeeStatsRequest,
Expand Down Expand Up @@ -890,22 +895,22 @@ impl HorizonClient {
}

/// Retrieves detailed information for a specific operation from the Horizon server.
///
///
/// This asynchronous method fetches details of a single operation from the Horizon server.
/// It requires a [`SingleOperationRequest`] parameterized with `OperationId`, which includes the unique identifier
/// of the operation to be retrieved.
///
///
/// # Arguments
/// * `request` - A reference to a [`SingleOperationRequest`] instance containing the unique ID of the operation to be fetched.
///
///
/// # Returns
///
///
/// Returns a `Result` containing an [`Operation`], which includes detailed information about the requested operation.
/// If the request fails, it returns an error encapsulated within `Result`.
///
///
/// # Usage
/// To use this method, create an instance of [`SingleOperationRequest`] and set the unique ID of the operation to be queried.
///
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
Expand All @@ -917,9 +922,9 @@ impl HorizonClient {
/// # .expect("Failed to create Horizon Client");
/// let request = SingleOperationRequest::new()
/// .set_operation_id("459561504769".to_string());
///
///
/// let response = horizon_client.get_single_operation(&request).await;
///
///
/// if let Ok(operation) = response {
/// println!("Operation ID: {}", operation.id());
/// // Additional processing...
Expand All @@ -936,23 +941,23 @@ impl HorizonClient {
}

/// Retrieves a list of all operations for an account from the Horizon server.
///
///
/// This asynchronous method fetches a list of all operations for an account from the Horizon server.
/// It requires an [`OperationsForAccountRequest`] to specify the optional query parameters.
///
///
/// # Arguments
/// * `request` - A reference to an [`OperationsForAccountRequest`] instance, containing the
/// parameters for the operations for account request.
///
/// # Returns
///
///
/// On successful execution, returns a `Result` containing a [`OperationsForAccountRequest`], which includes
/// the list of all operations obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
///
/// # Usage
/// To use this method, create an instance of [`OperationsForAccountRequest`] and set any desired
/// filters or parameters.
///
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
Expand All @@ -965,9 +970,9 @@ impl HorizonClient {
/// # .expect("Failed to create Horizon Client");
/// let request = OperationsForAccountRequest::new()
/// .set_limit(2).unwrap();
///
///
/// let response = horizon_client.get_operation_for_account(&request).await;
///
///
/// // Access the payments
/// if let Ok(operations_for_account_response) = response {
/// for operation in operations_for_account_response.embedded().records() {
Expand All @@ -978,7 +983,7 @@ impl HorizonClient {
/// # Ok({})
/// # }
/// ```
///
///
pub async fn get_operation_for_account(
&self,
request: &OperationsForAccountRequest,
Expand All @@ -993,6 +998,56 @@ impl HorizonClient {
self.get::<OperationResponse>(request).await
}

/// Retrieves a list of all operations for a specific liquidity pool from the Horizon server.
///
/// This asynchronous method fetches a list of all operations for a specific liquidity pool from the Horizon server.
/// It requires an [`OperationsForLiquidityPoolRequest`] to specify the liquidity pool ID and optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`OperationsForLiquidityPoolRequest`] instance, containing the liquidity pool ID
/// and optional query parameters for the operations for liquidity pool request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing a [`OperationResponse`], which includes
/// the list of all operations obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`OperationsForLiquidityPoolRequest`] and set the liquidity pool ID and any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::operations::prelude::*;
/// # use stellar_rs::models::Request;
/// # use stellar_rs::horizon_client::HorizonClient;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # 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 = OperationsForLiquidityPoolRequest::new()
/// .set_liquidity_pool_id("000000006520216af66d20d63a58534d6cbdf28ba9f2a9c1e03f8d9a756bb7d988b29bca".to_string());
///
/// let response = horizon_client.get_operations_for_liquidity_pool(&request).await;
///
/// // Access the operations
/// if let Ok(operations_for_liquidity_pool_response) = response {
/// for operation in operations_for_liquidity_pool_response.embedded().records() {
///
/// println!("Operation ID: {}", operation.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_operations_for_liquidity_pool(
&self,
request: &OperationsForLiquidityPoolRequest,
) -> Result<OperationResponse, String> {
self.get::<OperationResponse>(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
Expand Down
87 changes: 78 additions & 9 deletions stellar_rust_sdk/src/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

use self::operations_for_account_request::OperationsForAccountRequest;

pub mod all_operations_request;
pub mod operations_for_account_request;
pub mod operations_for_ledger_request;
Expand All @@ -16,7 +13,7 @@ pub mod prelude {
pub use super::operations_for_account_request::*;
pub use super::operations_for_ledger_request::*;
pub use super::operations_for_liquidity_pool_request::*;
pub use super::operations_for_transaction_request::*;
//pub use super::operations_for_transaction_request::*;
pub use super::response::*;
pub use super::single_operation_request::*;
}
Expand All @@ -25,10 +22,11 @@ pub mod prelude {
pub mod tests {
use crate::{
horizon_client,
models::{IncludeFailed, Order},
operations::{
operations_for_account_request::OperationsForAccountRequest,
prelude::{AllOperationsRequest, OperationsForLedgerRequest},
prelude::{
AllOperationsRequest, OperationsForLedgerRequest, OperationsForLiquidityPoolRequest,
},
response::{Operation, OperationResponse},
single_operation_request::SingleOperationRequest,
}, Paginatable,
Expand Down Expand Up @@ -221,14 +219,85 @@ pub mod tests {
operation_for_ledger_response.transaction_successful(),
&TRANSACTION_SUCCESFULL
);
assert_eq!(operation_for_ledger_response.source_account(), SOURCE_ACCOUNT);
assert_eq!(
operation_for_ledger_response.source_account(),
SOURCE_ACCOUNT
);
assert_eq!(operation_for_ledger_response.type_field(), TYPE);
assert_eq!(operation_for_ledger_response.type_i(), &TYPE_I);
assert_eq!(operation_for_ledger_response.created_at(), CREATED_AT);
assert_eq!(operation_for_ledger_response.transaction_hash(), TRANSACTION_HASH);
assert_eq!(operation_for_ledger_response.starting_balance(), STARTING_BALANCE);
assert_eq!(
operation_for_ledger_response.transaction_hash(),
TRANSACTION_HASH
);
assert_eq!(
operation_for_ledger_response.starting_balance(),
STARTING_BALANCE
);
assert_eq!(operation_for_ledger_response.funder(), FUNDER);
assert_eq!(operation_for_ledger_response.account(), ACCOUNT);
}

#[tokio::test]
async fn test_get_operations_for_liquidity_pool() {
const ID: &str = "459561504769";
const PAGING_TOKEN: &str = "459561504769";
const TRANSACTION_SUCCESFULL: bool = true;
const SOURCE_ACCOUNT: &str = "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H";
const TYPE: &str = "create_account";
const TYPE_I: i64 = 0;
const CREATED_AT: &str = "2024-02-06T17:42:48Z";
const TRANSACTION_HASH: &str =
"b9d0b2292c4e09e8eb22d036171491e87b8d2086bf8b265874c8d182cb9c9020";
const STARTING_BALANCE: &str = "10000000000.0000000";
const FUNDER: &str = "GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H";
const ACCOUNT: &str = "GAIH3ULLFQ4DGSECF2AR555KZ4KNDGEKN4AFI4SU2M7B43MGK3QJZNSR";

let horizon_client =
horizon_client::HorizonClient::new("https://horizon-testnet.stellar.org".to_string())
.unwrap();

let operations_for_liquidity_pool_request = OperationsForLiquidityPoolRequest::new()
.set_limit(2)
.unwrap();

let operation_for_liquidity_pool_response = horizon_client
.get_operations_for_liquidity_pool(&operations_for_liquidity_pool_request)
.await;

assert!(operation_for_liquidity_pool_response.is_ok());

let binding = operation_for_liquidity_pool_response.unwrap();
let operation_for_liquidity_pool_response = &binding.embedded().records()[0];

assert_eq!(operation_for_liquidity_pool_response.id(), ID);
assert_eq!(
operation_for_liquidity_pool_response.paging_token(),
PAGING_TOKEN
);
assert_eq!(
operation_for_liquidity_pool_response.transaction_successful(),
&TRANSACTION_SUCCESFULL
);
assert_eq!(
operation_for_liquidity_pool_response.source_account(),
SOURCE_ACCOUNT
);
assert_eq!(operation_for_liquidity_pool_response.type_field(), TYPE);
assert_eq!(operation_for_liquidity_pool_response.type_i(), &TYPE_I);
assert_eq!(
operation_for_liquidity_pool_response.created_at(),
CREATED_AT
);
assert_eq!(
operation_for_liquidity_pool_response.transaction_hash(),
TRANSACTION_HASH
);
assert_eq!(
operation_for_liquidity_pool_response.starting_balance(),
STARTING_BALANCE
);
assert_eq!(operation_for_liquidity_pool_response.funder(), FUNDER);
assert_eq!(operation_for_liquidity_pool_response.account(), ACCOUNT);
}
}
Loading

0 comments on commit 24cc772

Please sign in to comment.