Skip to content

Commit

Permalink
Merge pull request #5 from Baseflow/claimable_balances
Browse files Browse the repository at this point in the history
Claimable balances
  • Loading branch information
LeonardTibben authored Nov 14, 2023
2 parents 693c8c2 + 664245a commit ea56cb1
Show file tree
Hide file tree
Showing 9 changed files with 958 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ serde = { version = "1.0.188", features = ["derive"] }
derive-getters = "0.3.0"
hex = "0.4.3"
base64 = "0.21.4"
chrono = "0.4.31"
183 changes: 183 additions & 0 deletions src/claimable_balances/all_claimable_balances_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use crate::models::*;

use super::super::Order;
use super::super::AssetType;

/// AllClaimableBalancesRequest is the request type for the /claimable_balances/all endpoint
/// [More Details] (https://www.stellar.org/developers/horizon/reference/endpoints/claimable_balances-all.html) "All Claimable Balances")
pub struct AllClaimableBalancesRequest {
/// Account ID of the sponsor. Every account in the response will either be sponsored by the
/// given account ID or have a subentry (trustline, offer, or data entry) which is sponsored by
/// the given account ID.
sponsor: Option<String>,
/// Account ID of the signer. Every account in the response will have the given account ID as a
/// signer.
asset: Option<AssetType>,
/// An object that holds both the destination account that can claim the ClaimableBalanceEntry
/// and a ClaimPredicate that must evaluate to true for the claim to succeed.
claimant: Option<String>,
/// Account ID of the signer. Every account in the response will have the given account ID as a
/// signer.
cursor: Option<u32>,
/// The maximum number of records returned. The limit can range from 1 to 200 - an upper limit
/// that is hardcoded in Horizon for performance reasons. If this argument isn’t designated, it
/// defaults to 10.
limit: Option<u32>,
/// A designation of the order in which records should appear. Options include asc (ascending)
/// or desc (descending). If this argument isn’t set, it defaults to asc.
order: Option<Order>,
}

impl Request for AllClaimableBalancesRequest {
/// Creates a new request object
/// # Returns
/// A new request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
fn new() -> Self {
AllClaimableBalancesRequest {
sponsor: None,
asset: None,
claimant: None,
cursor: None,
limit: None,
order: None,
}
}

/// Gets the relative URL for the request
fn get_path(&self) -> &str {
"/claimable_balances/"
}

// Gets the query parameters for the request
fn get_query_parameters(&self) -> String {
let mut query = String::new();
if let Some(sponsor) = &self.sponsor {
query.push_str(&format!("sponsor={}&", sponsor));
}
if let Some(asset) = &self.asset {
query.push_str(&format!("asset={}&", asset));
}
if let Some(claimant) = &self.claimant {
query.push_str(&format!("claimant={}&", claimant));
}
if let Some(cursor) = &self.cursor {
query.push_str(&format!("cursor={}&", cursor));
}
if let Some(limit) = &self.limit {
query.push_str(&format!("limit={}&", limit));
}
if let Some(order) = &self.order {
query.push_str(&format!("order={}&", order));
}

query.trim_end_matches('&').to_string()
}

/// Builds the URL for the request
/// # Arguments
/// * `self` - The request object
/// * `base_url` - The base URL for the Horizon server
/// # Returns
/// The URL for the request
fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}?{}",
base_url,
self.get_path(),
self.get_query_parameters()
)
}

fn validate(&self) -> Result<(), String> {
if let Some(sponsor) = &self.sponsor {
let is_valid = is_public_key(sponsor);
if is_valid.is_err() {
return Err(is_valid.unwrap_err());
}
}

if let Some(claimant) = &self.claimant {
let is_valid = is_public_key(claimant);
if is_valid.is_err() {
return Err(is_valid.unwrap_err());
}
}

Ok(())
}
}

impl AllClaimableBalancesRequest {
/// Sets the sponsor for the request
/// # Arguments
/// * `self` - The request object
/// * `sponsor` - The sponsor for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_sponsor(&mut self, sponsor: String) -> &mut Self {
self.sponsor = Some(sponsor);
self
}

/// Sets the asset for the request
/// # Arguments
/// * `self` - The request object
/// * `asset` - The asset for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_asset(&mut self, asset: AssetType) -> &mut Self {
self.asset = Some(asset);
self
}

/// Sets the claimant for the request
/// # Arguments
/// * `self` - The request object
/// * `claimant` - The claimant for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_claimant(&mut self, claimant: String) -> &mut Self {
self.claimant = Some(claimant);
self
}

/// Sets the cursor for the request
/// # Arguments
/// * `self` - The request object
/// * `cursor` - The cursor for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_cursor(&mut self, cursor: u32) -> &mut Self {
self.cursor = Some(cursor);
self
}

/// Sets the limit for the request
/// # Arguments
/// * `self` - The request object
/// * `limit` - The limit for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_limit(&mut self, limit: u32) -> &mut Self {
self.limit = Some(limit);
self
}

/// Sets the order for the request
/// # Arguments
/// * `self` - The request object
/// * `order` - The order for the request
/// # Returns
/// The request object
/// [AllClaimableBalancesRequest](struct.AllClaimableBalancesRequest.html)
pub fn set_order(&mut self, order: Order) -> &mut Self {
self.order = Some(order);
self
}
}
Loading

0 comments on commit ea56cb1

Please sign in to comment.