Skip to content

Commit

Permalink
effects for liquidity pools
Browse files Browse the repository at this point in the history
comment test
  • Loading branch information
leonard committed Feb 13, 2024
1 parent c46fb18 commit 67f0fe4
Show file tree
Hide file tree
Showing 5 changed files with 494 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/effects/effects_for_account_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct Record {
/// The type of the effect.
#[serde(rename = "type")]
pub type_field: String,
/// The asset code of the effect.
/// The type_i code of the effect.
#[serde(rename = "type_i")]
pub type_i: i64,
/// the epoch timestamp when the effect was created.
Expand Down
109 changes: 109 additions & 0 deletions src/effects/effects_for_liquidity_pools_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use crate::models::{Order, Request};
use crate::BuildQueryParametersExt;

#[derive(Default)]
pub struct EffectsForLiquidityPoolsRequest {
/// The liquidity pool id
liquidity_pool_id: Option<String>,

/// A pointer to a specific location in a collection of responses, derived from the
/// `paging_token` value of a record. Used for pagination control in the API response.
cursor: Option<u32>,

/// Specifies the maximum number of records to be returned in a single response.
/// The range for this parameter is from 1 to 200. The default value is set to 10.
limit: Option<u8>,

/// Determines the [`Order`] of the records in the response. Valid options are [`Order::Asc`] (ascending)
/// and [`Order::Desc`] (descending). If not specified, it defaults to ascending.
order: Option<Order>,
}

impl EffectsForLiquidityPoolsRequest {
/// Creates a new `EffectsForLiquidityPoolsRequest` with default parameters.
pub fn new() -> Self {
EffectsForLiquidityPoolsRequest::default()
}

/// Sets the liquidity pool id for the request.
///
/// # Arguments
/// * `liquidity_pool_id` - A `String` value representing the liquidity pool id.
///
pub fn set_liquidity_pool_id(
self,
liquidity_pool_id: String,
) -> EffectsForLiquidityPoolsRequest {
EffectsForLiquidityPoolsRequest {
liquidity_pool_id: Some(liquidity_pool_id),
..self
}
}

/// Sets the cursor for pagination.
///
/// # Arguments
/// * `cursor` - A `u32` value pointing to a specific location in a collection of responses.
///
pub fn set_cursor(self, cursor: u32) -> Result<EffectsForLiquidityPoolsRequest, String> {
if cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}

Ok(EffectsForLiquidityPoolsRequest {
cursor: Some(cursor),
..self
})
}

/// Sets the maximum number of records to return.
///
/// # Arguments
/// * `limit` - A `u8` value specifying the maximum number of records. Range: 1 to 200. Defaults to 10.
///
pub fn set_limit(self, limit: u8) -> Result<EffectsForLiquidityPoolsRequest, String> {
if limit < 1 || limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}

Ok(EffectsForLiquidityPoolsRequest {
limit: Some(limit),
..self
})
}

/// Sets the order of the returned records.
///
/// # Arguments
/// * `order` - An [`Order`] enum value specifying the order (ascending or descending).
///
pub fn set_order(self, order: Order) -> EffectsForLiquidityPoolsRequest {
EffectsForLiquidityPoolsRequest {
order: Some(order),
..self
}
}
}

impl Request for EffectsForLiquidityPoolsRequest {
fn get_query_parameters(&self) -> String {
vec![
self.liquidity_pool_id
.as_ref()
.map(|l| format!("liquidity_pool_id={}", l)),
self.cursor.as_ref().map(|c| format!("cursor={}", c)),
self.limit.as_ref().map(|l| format!("limit={}", l)),
self.order.as_ref().map(|o| format!("order={}", o)),
]
.build_query_parameters()
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}/{}{}",
base_url,
super::EFFECTS_PATH,
self.get_query_parameters()
)
}
}
110 changes: 110 additions & 0 deletions src/effects/effects_for_liquidity_pools_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use derive_getters::Getters;
use serde::{Deserialize, Serialize};

use crate::models::Response;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct EffectsForLiquidityPoolResponse {
/// Navigational links for the current, next, and previous pages of the response.
#[serde(rename = "_links")]
pub links: Links,
/// Contains the actual list of effect records in the `records` field.
#[serde(rename = "_embedded")]
pub embedded: Embedded,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Links {
/// Navigational links for the current, next, and previous pages of the response.
#[serde(rename = "self")]
pub self_field: Link,
/// Navigational links for the next page of the response.
pub next: Link,
/// Navigational links for the previous page of the response.
pub prev: Link,
}

/// Represents different kinds of links a Response might have.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Link {
/// Navigational links for the current, next, and previous pages of the response.
#[serde(rename = "self")]
pub self_field: Option<String>,
/// Navigational links for the next page of the response.
pub next: Option<String>,
/// Navigational links for the previous page of the response.
pub prev: Option<String>,
/// Navigational links for the operation of the response.
pub operation: Option<String>,
/// Navigational links for the succeeds of the response.
pub succeeds: Option<String>,
/// Navigational links for the precedes of the response.
pub precedes: Option<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Embedded {
/// A list of effect records.
pub records: Vec<Record>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct Record {
/// Navigational links for the current, next, and previous pages of the response.
#[serde(rename = "_links")]
pub links: RecordLinks,
/// A unique identifier of the effect
pub id: String,
#[serde(rename = "paging_token")]
/// A token used for paging through results.
pub paging_token: String,
/// The account that generated the effect.
pub account: String,
/// The type of effect.
#[serde(rename = "type")]
pub type_field: String,
/// The type_i of the effect.
#[serde(rename = "type_i")]
pub type_i: i64,
/// the epoch timestamp when the effect was created.
#[serde(rename = "created_at")]
pub created_at: String,
/// The starting balance of the effect.
#[serde(rename = "starting_balance")]
pub starting_balance: Option<String>,
/// The asset type of the effect.
#[serde(rename = "asset_type")]
pub asset_type: Option<String>,
/// the amount of the effect
pub amount: Option<String>,
/// the wheight of the effect
pub weight: Option<i64>,
#[serde(rename = "public_key")]
/// The public key of the effect
pub public_key: Option<String>,
/// The trustor of the effect
pub key: Option<String>,
}

/// Represents different kinds of links a Record might have.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Getters)]
#[serde(rename_all = "camelCase")]
pub struct RecordLinks {
/// Navigational links for the current, next, and previous pages of the response.
pub operation: Link,
/// Navigational links for the next page of the response.
pub succeeds: Link,
/// Navigational links for the previous page of the response.
pub precedes: Link,
}

impl Response for EffectsForLiquidityPoolResponse {
fn from_json(json: String) -> Result<Self, String> {
serde_json::from_str(&json).map_err(|e| e.to_string())
}
}
4 changes: 4 additions & 0 deletions src/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod all_effects_request;
pub mod all_effects_response;
pub mod effects_for_account_request;
pub mod effects_for_account_response;
pub mod effects_for_liquidity_pools_request;
pub mod effects_for_liquidity_pools_response;

static EFFECTS_PATH: &str = "effects";

Expand All @@ -10,6 +12,8 @@ pub mod prelude {
pub use super::all_effects_response::*;

Check warning on line 12 in src/effects/mod.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

ambiguous glob re-exports

Check warning on line 12 in src/effects/mod.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

ambiguous glob re-exports
pub use super::effects_for_account_request::*;
pub use super::effects_for_account_response::*;

Check warning on line 14 in src/effects/mod.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

ambiguous glob re-exports

Check warning on line 14 in src/effects/mod.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

ambiguous glob re-exports

Check warning on line 14 in src/effects/mod.rs

View workflow job for this annotation

GitHub Actions / Build and test Stellar SDK

ambiguous glob re-exports
pub use super::effects_for_liquidity_pools_request::*;
pub use super::effects_for_liquidity_pools_response::*;
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 67f0fe4

Please sign in to comment.