Skip to content

Commit

Permalink
Add documentation and minor struct attribute changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-pease committed May 15, 2024
1 parent 09f16d8 commit 609a02c
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 38 deletions.
50 changes: 49 additions & 1 deletion stellar_rust_sdk/src/horizon_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,55 @@ impl HorizonClient {
self.get::<LiquidityPool>(request).await
}

// TODO: Documentation
/// Retrieves detailed information for a specific transaction from the Horizon server.
///
/// This asynchronous method fetches details of a single transaction from the Horizon server.
/// It requires a [`SingleTransactionRequest`] parameterized with `TransactionHash`, which includes the hash
/// of the transaction to be retrieved.
///
/// Adheres to the <a href="https://developers.stellar.org/network/horizon/api-reference/resources/retrieve-a-transaction">Retrieve a Transaction endpoint</a>
/// endpoint.
///
/// # Arguments
///
/// * `request` - A reference to a [`SingleTransactionRequest<TransactionHash>`] instance, containing the
/// hash of the transaction for which details are to be fetched.
///
/// # Returns
///
/// Returns a `Result` containing an [`TransactionResponse`], which includes detailed
/// information about the requested transaction. If the request fails, it returns an error
/// encapsulated within `Result`.
///
/// # Usage
/// To use this method, create an instance of [`SingleTransactionRequest`] and set the
/// hash of the transaction to be queried.
///
/// ```
/// # use stellar_rs::transactions::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 = SingleTransactionRequest::new()
/// .set_transaction_hash("be0d59c8706e8fd525d2ab10910a55ec57323663858c65b330a3f93afb13ab0f".to_string()) // example transaction hash
/// .unwrap();
///
/// let response = horizon_client.get_single_transaction(&request).await;
///
/// // Access the details of the claimable balance
/// if let Ok(transaction_response) = response {
/// println!("Created at: {}", transaction_response.created_at());
/// // Further processing...
/// }
///
/// # Ok({})
/// # }
/// ```
///
pub async fn get_single_transaction(
&self,
request: &SingleTransactionRequest<TransactionHash>,
Expand Down
41 changes: 40 additions & 1 deletion stellar_rust_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,46 @@ pub mod operations;
///
pub mod order_book;

// TODO: Documentation
/// Provides `Request` and `Response` structs for retrieving transactions.
///
/// This module provides a set of specialized request and response structures designed for
/// interacting with the transaction-related endpoints of the Horizon server. These structures
/// facilitate the construction of requests to query transaction data and the interpretation of
/// the corresponding responses.
///
/// # Usage
///
/// This module is intended to be used in conjunction with the [`HorizonClient`](crate::horizon_client::HorizonClient)
/// for making specific transaction-related API calls to the Horizon server. The request
/// structures are designed to be passed to the client's methods, which handle the
/// communication with the server and return the corresponding response structures.
///
/// # Example
///
/// /// To use this module, you can create an instance of a request struct, such as
/// `SingleTransactionRequest`, set any desired query parameters, and pass the request to the
/// `HorizonClient`. The client will then execute the request and return the corresponding
/// response struct, like `TransactionResponse`.
///
/// ```rust
/// use stellar_rs::horizon_client::HorizonClient;
/// use stellar_rs::transactions::prelude::*;
/// use stellar_rs::models::Request;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let horizon_client = HorizonClient::new("https://horizon-testnet.stellar.org".to_string())?;
///
/// // Example: Fetching a transaction
/// let single_transaction_request = SingleTransactionRequest::new()
/// .set_transaction_hash("be0d59c8706e8fd525d2ab10910a55ec57323663858c65b330a3f93afb13ab0f".to_string())
/// .unwrap();
/// let single_transaction_response = horizon_client.get_single_transaction(&single_transaction_request).await?;
///
/// // Process the responses...
/// # Ok(())
/// # }
/// ```
///
pub mod transactions;

/// Contains core data structures and traits.
Expand Down
56 changes: 51 additions & 5 deletions stellar_rust_sdk/src/transactions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
// TODO: Documentation
/// Provides the `SingleTransactionRequest`.
///
/// # Usage
/// This module provides the `SingleTransactionRequest` struct, specifically designed for
/// constructing requests to query information about a single transaction from the Horizon
/// server. It is tailored for use with the [`HorizonClient::get_single_transaction`](crate::horizon_client::HorizonClient::get_single_transaction)
/// method.
///
pub mod single_transaction_request;

// TODO: Documentation
/// Provides the responses.
///
/// This module defines structures representing the response from the Horizon API when querying
/// for transactions. The structures are designed to deserialize the JSON response into Rust
/// objects, enabling straightforward access to various details of a single transaction.
///
/// # Usage
/// These structures are equipped with serialization capabilities to handle the JSON data from the
/// Horizon server and with getter methods for easy field access.
pub mod response;

// TODO: Documentation
/// The base path for transaction-related endpoints in the Horizon API.
///
/// # Usage
/// 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 TRANSACTIONS_PATH: &str = "transactions";

// TODO: Documentation
/// The `prelude` module of the `transactions` module.
///
/// # Usage
/// This module serves as a convenience for users of the Horizon Rust SDK, allowing for easy and
/// ergonomic import of the most commonly used items across various modules. It re-exports
/// key structs and traits from the sibling modules, simplifying access to these components
/// when using the library.
///
/// By importing the contents of `prelude`, users can conveniently access the primary
/// functionalities of the transaction-related modules without needing to import each item
/// individually.
///
/// # Contents
///
/// The `prelude` includes the following re-exports:
///
/// * From `single_transaction_request`: All items (e.g. `SingleTransactionRequest`).
/// * From `response`: All items (e.g. `SingleTransactionResponse`, `Preconditions`, etc.).
///
/// # Example
/// ```
/// # use crate::stellar_rs::models::*;
/// // Import the contents of the transactions prelude
/// use stellar_rs::transactions::prelude::*;
///
/// // Now you can directly use SingleTransactionRequest, SingleTransactionResponse, etc.
/// let single_transactions_request = SingleTransactionRequest::new();
/// ```
pub mod prelude {
pub use super::single_transaction_request::*;
pub use super::response::*;
Expand All @@ -16,7 +62,7 @@ pub mod prelude {
#[cfg(test)]
pub mod test {
use super::prelude::*;
use crate::{horizon_client::HorizonClient};
use crate::horizon_client::HorizonClient;

#[tokio::test]
async fn test_get_single_transaction() {
Expand Down
135 changes: 105 additions & 30 deletions stellar_rust_sdk/src/transactions/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use crate::models::prelude::*;
use derive_getters::Getters;
use serde::{Deserialize, Serialize};

// TODO: Documentation
/// Represents the navigational links in a response from the Horizon API.
///
/// # Usage
/// This struct includes various hyperlinks such as links to the transaction itself and
/// the ledger that the transaction was included in.
///
#[derive(Debug, Deserialize, Serialize, Clone, Getters)]
pub struct Links {
#[serde(rename = "self")]
Expand All @@ -16,45 +21,115 @@ pub struct Links {
transaction: Link,
}

// TODO: Documentation
/// Represents the set of transaction preconditions affecting its validity.
///
/// # Usage
/// This struct details information about the preconditions, including the time bounds, ledger bounds (optional),
/// minimum account sequence and its age(optional), mimimum account sequence leder gap (optional,
/// and an array of up to 2 additional signers (optional).
///
#[derive(Default, Debug, Clone, Serialize, Deserialize, Getters)]
pub struct Preconditions {
pub timebounds: Timebounds,
/// The time range for which this transaction is valid, with bounds as unsigned 64-bit UNIX timestamps.
timebounds: TimeBounds,
// The ledger range for which this transaction is valid.
ledger_bounds: Option<LedgerBounds>,
/// Containing a positive, signed 64-bit integer representing the lowest source account sequence number for which the transaction is valid.
min_account_sequence: Option<String>,
/// The minimum duration of time (in seconds as an unsigned 64-bit integer) that must have passed since the source account's sequence number changed for the transaction to be valid.
min_account_sequence_age: Option<i64>,
/// An unsigned 32-bit integer representing the minimum number of ledgers that must have closed since the source account's sequence number changed for the transaction to be valid.
min_account_sequence_ledger_gap: Option<i64>,
/// The list of up to two additional signers that must have corresponding signatures for this transaction to be valid.
extra_signers: Option<Vec<String>>,
}

// TODO: Documentation
/// Represents the time range for which this transaction is valid, with bounds as unsigned 64-bit UNIX timestamps.
///
/// # Usage
/// This struct details information about the time range, including the lower time bound
/// and the upper time bound (optional).
///
#[derive(Default, Debug, Clone, Serialize, Deserialize, Getters)]
pub struct Timebounds {
pub min_time: String,
pub max_time: Option<String>,
pub struct TimeBounds {
/// The lower bound.
min_time: String,
/// The upper bound.
max_time: Option<String>,
}

// TODO: Documentation
/// Represents the the ledger range for which this transaction is valid.
///
/// # Usage
/// This struct details information about the ledger range, including the lower ledger bound
/// and the upper ledger bound (optional).
///
#[derive(Default, Debug, Clone, Serialize, Deserialize, Getters)]
pub struct LedgerBounds {
/// The lower bound.
min_ledger: String,
/// The upper bound.
max_ledger: Option<String>,
}

/// Represents a single transaction record in the Horizon API response.
///
/// # Usage
/// This struct encapsulates detailed information about a single transaction, including its ID,
/// hash, creation time, source account, and other relevant data.
///
#[derive(Debug, Clone, Serialize, Deserialize, Getters)]
pub struct TransactionResponse {
#[serde(rename = "_links")]
pub links: Links,
pub id: String,
pub paging_token: String,
pub successful: bool,
pub hash: String,
pub ledger: i64,
pub created_at: String,
pub source_account: String,
pub source_account_sequence: String,
pub fee_account: String,
pub fee_charged: String,
pub max_fee: String,
pub operation_count: i64,
pub envelope_xdr: String,
pub result_xdr: String,
pub result_meta_xdr: String,
pub fee_meta_xdr: String,
pub memo_type: String,
pub signatures: Vec<String>,
pub valid_after: String,
pub valid_before: String,
pub preconditions: Preconditions,
links: Links,
/// A unique identifier for this transaction.
id: String,
/// A cursor value for use in pagination.
paging_token: String,
/// Indicates if this transaction was successful or not.
successful: bool,
/// A hex-encoded SHA-256 hash of this transaction’s XDR-encoded form.
hash: String,
/// The sequence number of the ledger that this transaction was included in.
ledger: i64,
/// The date this transaction was created.
created_at: String,
/// The account that originates the transaction.
source_account: String,
// TODO: Missing description in Stellar documentation.
account_muxed: Option<String>,
// TODO: Missing description in Stellar documentation.
account_muxed_id: Option<String>,
/// The source account's sequence number that this transaction consumed.
source_account_sequence: String,
/// The ID of the fee account.
fee_account: String,
/// The fee (in stroops) paid by the source account to apply this transaction to the ledger.
fee_charged: String,
/// The maximum fee (in stroops) that the source account was willing to pay.
max_fee: String,
/// The number of operations contained within this transaction.
operation_count: i64,
/// A base64 encoded string of the raw `TransactionEnvelope` XDR struct for this transaction.
envelope_xdr: String,
/// A base64 encoded string of the raw `TransactionResult` XDR struct for this transaction.
result_xdr: String,
/// A base64 encoded string of the raw `TransactionMeta` XDR struct for this transaction
result_meta_xdr: String,
/// A base64 encoded string of the raw `L`edgerEntryChanges` XDR struct produced by taking fees for this transaction.
fee_meta_xdr: String,
/// The optional memo attached to a transaction.
memo: Option<String>,
/// The type of memo. Potential values include `MEMO_TEXT`, `MEMO_ID`, `MEMO_HASH`, `MEMO_RETURN`.
memo_type: String,
/// An array of signatures used to sign this transaction.
signatures: Vec<String>,
/// The date after which a transaction is valid.
valid_after: String,
/// The date before which a transaction is valid.
valid_before: String,
/// A set of transaction preconditions affecting its validity.
preconditions: Preconditions,
}

impl Response for TransactionResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ impl SingleTransactionRequest<NoTransactionHash> {
SingleTransactionRequest::default()
}

// TODO: Documentation
/// Sets the transaction hash for the request.
///
/// # Arguments
/// * `transaction_hash` - A `String` specifying the operation hash.
///
pub fn set_transaction_hash(
self,
transaction_hash: String,
Expand Down

0 comments on commit 609a02c

Please sign in to comment.