Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trades endpoint #92

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 155 additions & 2 deletions stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,10 @@ impl HorizonClient {
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTradesResponse`], which includes
/// the list of all offers obtained from the Horizon server. If the request fails, it returns an error within `Result`.
/// the list of all trades obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`AllOffersRequest`] and set any desired
/// To use this method, create an instance of [`AllTradesRequest`] and set any desired
/// filters or parameters.
///
/// ```
Expand Down Expand Up @@ -1189,6 +1189,159 @@ impl HorizonClient {
self.get::<AllTradesResponse>(request).await
}

/// Retrieves a list of all trades for a given account from the Horizon server.
///
/// This asynchronous method fetches a list of all trades for a given account from the Horizon server.
/// It requires an [`TradesForAccountRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TradesForAccountRequest`] instance, containing the
/// parameters for the trades request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTradesResponse`], which includes
/// the list of all trades obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TradesForAccountRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::trades::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 = TradesForAccountRequest::new()
/// .set_account_id("GCUOMNFW7YG55YHY5S5W7FE247PWODUDUZ4SOVZFEON47KZ7AXFG6D6A".to_string())
/// .unwrap();
///
/// let response = horizon_client.get_trades_for_account(&request).await;
///
/// // Access the trades
/// if let Ok(trades_response) = response {
/// for trade in trades_response.embedded().records() {
/// println!("Trade ID: {}", trade.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_trades_for_account(
&self,
request: &TradesForAccountRequest<TradeAccountId>,
) -> Result<AllTradesResponse, String> {
self.get::<AllTradesResponse>(request).await
}

/// Retrieves a list of all successful trades fulfilled by the given liquidity pool from the Horizon server.
///
/// This asynchronous method fetches a list of all successful trades fulfilled by the given liquidity pool
/// from the Horizon server. It requires an [`TradesForLiquidityPoolRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TradesForLiquidityPoolRequest`] instance, containing the
/// parameters for the trades request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTradesResponse`], which includes
/// the list of all trades obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TradesForLiquidityPoolRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::trades::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 = TradesForLiquidityPoolRequest::new()
/// .set_liquidity_pool_id("0b3c88caa5aeada296646c1810893e3b04cba0426cff8ff6a63cf6f35cc7f5b3".to_string())
/// .unwrap();
///
/// let response = horizon_client.get_trades_for_liquidity_pool(&request).await;
///
/// // Access the trades
/// if let Ok(trades_response) = response {
/// for trade in trades_response.embedded().records() {
/// println!("Trade ID: {}", trade.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_trades_for_liquidity_pool(
&self,
request: &TradesForLiquidityPoolRequest<TradeLiquidityPoolId>,
) -> Result<AllTradesResponse, String> {
self.get::<AllTradesResponse>(request).await
}

/// Retrieves a list of all trades for a given offer from the Horizon server.
///
/// This asynchronous method fetches a list of all trades for a given offer from the Horizon server.
/// It requires an [`TradesForOfferRequest`] to specify the optional query parameters.
///
/// # Arguments
/// * `request` - A reference to an [`TradesForOfferRequest`] instance, containing the
/// parameters for the trades request.
///
/// # Returns
///
/// On successful execution, returns a `Result` containing an [`AllTradesResponse`], which includes
/// the list of all trades obtained from the Horizon server. If the request fails, it returns an error within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`TradesForOfferRequest`] and set any desired
/// filters or parameters.
///
/// ```
/// # use stellar_rs::trades::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 = TradesForOfferRequest::new()
/// .set_offer_id("42".to_string())
/// .unwrap();
///
/// let response = horizon_client.get_trades_for_offer(&request).await;
///
/// // Access the trades
/// if let Ok(trades_response) = response {
/// for trade in trades_response.embedded().records() {
/// println!("Trade ID: {}", trade.id());
/// // Further processing...
/// }
/// }
/// # Ok({})
/// # }
/// ```
///
pub async fn get_trades_for_offer(
&self,
request: &TradesForOfferRequest<TradeOfferId>,
) -> Result<AllTradesResponse, String> {
self.get::<AllTradesResponse>(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.
Expand Down
8 changes: 4 additions & 4 deletions stellar_rust_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
//! stabilization.
//!
//! #### Supported endpoints:
//! ![67%](https://progress-bar.dev/67/?width=200)
//! ![80%](https://progress-bar.dev/80/?width=200)
//! * Accounts
//! * Assets
//! * Effects
//! * Claimable balance
//! * Ledgers
//! * Effects
//! * Fee stats
//! * Ledgers
//! * Liquidity pools
//! * Operations
//! * Offers
//! * Orderbook
//! * Trades
//!
//! #### Endpoints on the roadmap:
//! * Paths
//! * Payments
//! * Trade aggregations
//! * Trades
//! * Transactions

//!
Expand Down
2 changes: 1 addition & 1 deletion stellar_rust_sdk/src/liquidity_pools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod response;
/// This variable is intended to be used internally by the request-building logic
/// to ensure consistent and accurate path construction for liquidity pool related API calls.
///
static LIQUIDITY_POOLS_PATH: &str = "liquidity_pools";
pub(crate) static LIQUIDITY_POOLS_PATH: &str = "liquidity_pools";

/// The `prelude` module of the `liquidity_pools` module.
///
Expand Down
2 changes: 1 addition & 1 deletion stellar_rust_sdk/src/offers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mod response;
/// This variable is intended to be used internally by the request-building logic
/// to ensure consistent and accurate path construction for offer-related API calls.
///
static OFFERS_PATH: &str = "offers";
pub(crate) static OFFERS_PATH: &str = "offers";

/// The `prelude` module of the `offers` module.
///
Expand Down
Loading