-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Baseflow/claimable_balances
Claimable balances
- Loading branch information
Showing
9 changed files
with
958 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
src/claimable_balances/all_claimable_balances_request.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.