Skip to content

Commit

Permalink
all assets
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardTibben committed Sep 20, 2023
1 parent 9a0fb3c commit 5aa447b
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ Cargo.lock
# Added by cargo

/target

keys.txt
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'stellar-rust-sdk'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=stellar-rust-sdk"
],
"filter": {
"name": "stellar-rust-sdk",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
111 changes: 111 additions & 0 deletions src/assets/all_assets_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::models::Request;

use super::super::Order;

// AllAssetsRequest is the request for the /assets endpoint
// [More Details] https://www.stellar.org/developers/horizon/reference/endpoints/assets-all.html "Assets"
pub struct AllAssetsRequest {
asset_code: Option<String>,
asset_issuer: Option<String>,
cursor: Option<u32>,
limit: Option<u32>,
order: Option<Order>,
}

impl Request for AllAssetsRequest {
fn new() -> Self {
AllAssetsRequest {
asset_code: None,
asset_issuer: None,
cursor: None,
limit: None,
order: None,
}
}

fn get_path(&self) -> &str {
"/assets"
}

fn get_query_parameters(&self) -> String {
let mut query = String::new();
if let Some(asset_code) = &self.asset_code {
query.push_str(&format!("asset_code={}&", asset_code));
}
if let Some(asset_issuer) = &self.asset_issuer {
query.push_str(&format!("asset_issuer={}&", asset_issuer));
}
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()
}

fn validate(&self) -> Result<(), String> {
if let Some(asset_code) = &self.asset_code {
// TODO: implement full asset code regex
if asset_code.len() > 12 {
return Err("asset_code must be 12 characters or less".to_string());
}
}

if let Some(asset_issuer) = &self.asset_issuer {
// TODO: implement full asset issuer regex
if asset_issuer.len() != 56 {
return Err("asset_issuer must be 56 characters".to_string());
}
}

if let Some(limit) = &self.limit {
if *limit < 1 || *limit > 200 {
return Err("limit must be between 1 and 200".to_string());
}
}

if let Some(cursor) = &self.cursor {
if *cursor < 1 {
return Err("cursor must be greater than or equal to 1".to_string());
}
}

Ok(())
}

fn build_url(&self, base_url: &str) -> String {
format!(
"{}{}?{}",
base_url,
self.get_path(),
self.get_query_parameters()
)
}
}

impl AllAssetsRequest {
pub fn set_asset_code(&mut self, asset_code: String) {
self.asset_code = Some(asset_code);
}

pub fn set_asset_issuer(&mut self, asset_issuer: String) {
self.asset_issuer = Some(asset_issuer);
}

pub fn set_cursor(&mut self, cursor: u32) {
self.cursor = Some(cursor);
}

pub fn set_limit(&mut self, limit: u32) {
self.limit = Some(limit);
}

pub fn set_order(&mut self, order: Order) {
self.order = Some(order);
}
}
7 changes: 7 additions & 0 deletions src/assets/all_assets_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub struct AllAssetsResponse {}

impl Default for AllAssetsResponse {
fn default() -> Self {
Self {}
}
}
7 changes: 7 additions & 0 deletions src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod all_assets_request;
mod all_assets_response;

pub mod prelude {
pub use super::all_assets_request::*;
pub use super::all_assets_response::*;
}
28 changes: 27 additions & 1 deletion src/horizon_client/horizon_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::models::Request;
use crate::{models::Request, assets::prelude::{AllAssetsRequest, AllAssetsResponse}};
use reqwest;
use url::Url;

Expand Down Expand Up @@ -52,6 +52,13 @@ impl HorizonClient {
self.get::<SingleAccountsResponse>(request).await
}

pub async fn get_all_assets(
&self,
request: &AllAssetsRequest,
) -> Result<AllAssetsResponse, String> {
self.get::<AllAssetsResponse>(request).await
}

/// Sends a GET request to the server
/// # Arguments
/// * `TResponse` - The type of the response
Expand Down Expand Up @@ -112,6 +119,8 @@ fn url_validate(url: &str) -> Result<(), String> {

#[cfg(test)]
mod tests {
use crate::assets::prelude::AllAssetsRequest;

use super::*;

#[test]
Expand Down Expand Up @@ -167,4 +176,21 @@ mod tests {

assert!(_single_account_response.is_ok());
}

#[tokio::test]
async fn test_get_all_assests() {
// Initialize horizon client
let horizon_client =
HorizonClient::new("https://horizon-testnet.stellar.org".to_string()).unwrap();

// construct request
let mut all_assets_request = AllAssetsRequest::new();
all_assets_request.set_asset_issuer("GDQJUTQYK2MQX2VGDR2FYWLIYAQIEGXTQVTFEMGH2BEWFG4BRUY4CKI7".to_string());

let _all_assets_response = horizon_client
.get_all_assets(&all_assets_request)
.await;

assert!(_all_assets_response.is_ok());
}
}
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
mod assets;
mod accounts;
mod horizon_client;
mod models;
mod xdr;

/// The asset type
/// Native - The native asset
/// Issued - An issued asset
/// [AccountsRequest](struct.AccountsRequest.html)
pub enum AssetType {
Native,
Issued,
}

impl std::fmt::Display for AssetType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
AssetType::Native => write!(f, "native"),
AssetType::Issued => write!(f, "issued"),
}
}
}

/// The order of the records
/// Asc - Ascending order
/// Desc - Descending order
/// [AccountsRequest](struct.AccountsRequest.html)
pub enum Order {
Asc,
Desc,
}

impl std::fmt::Display for Order {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Order::Asc => write!(f, "asc"),
Order::Desc => write!(f, "desc"),
}
}
}
2 changes: 1 addition & 1 deletion src/xdr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {

use ::stellar_xdr::ReadXdr;
use ::stellar_xdr::{LedgerHeader, ReadXdr};
use stellar_xdr::curr as stellar_xdr;

// TODO, add vice versa.
Expand Down

0 comments on commit 5aa447b

Please sign in to comment.