Skip to content

Commit

Permalink
Fix Get Assets By Owner Call and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed Apr 29, 2024
1 parent d058e3e commit 9a8e09b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
17 changes: 3 additions & 14 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use std::sync::Arc;
use crate::config::Config;
use crate::error::Result;
use crate::request_handler::RequestHandler;
use crate::types::types::{ApiResponse, RpcRequest, RpcResponse};
use crate::types::types::{RpcRequest, RpcResponse};
use crate::types::{
AssetsByAuthorityRequest, AssetsByOwnerRequest, GetAssetRequest, GetAssetResponseForAsset, GetAssetResponseList,
};

use reqwest::{Client, Method, Url};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::{json, Value};

pub struct RpcClient {
pub handler: RequestHandler,
Expand Down Expand Up @@ -52,18 +51,8 @@ impl RpcClient {
}

/// Gets a list of assets owned by a given address
pub async fn get_assets_by_owner(&self, request: AssetsByOwnerRequest) -> Result<ApiResponse> {
let base_url: String = format!("{}?api-key={}", self.config.endpoints.rpc, self.config.api_key);
let url: Url = Url::parse(&base_url).expect("Failed to parse URL");

let request_body: Value = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "getAssetsByOwner",
"params": request
});

self.handler.send(Method::POST, url, Some(&request_body)).await
pub async fn get_assets_by_owner(&self, request: AssetsByOwnerRequest) -> Result<GetAssetResponseList> {
self.post_rpc_request("getAssetsByOwner".to_string(), request).await
}

/// Gets a list of assets of a given authority
Expand Down
2 changes: 1 addition & 1 deletion src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct RpcResponse<T> {
pub result: T,
}

#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct AssetsByOwnerRequest {
#[serde(rename = "ownerAddress")]
pub owner_address: String,
Expand Down
16 changes: 6 additions & 10 deletions tests/rpc/test_get_assets_by_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,13 @@ async fn test_get_assets_by_owner_success() {
sort_by: None,
};

let response: Result<ApiResponse, HeliusError> = helius.rpc().get_assets_by_owner(request).await;
let response: Result<GetAssetResponseList, HeliusError> = helius.rpc().get_assets_by_owner(request).await;
assert!(response.is_ok(), "The API call failed: {:?}", response.err());

let api_response: ApiResponse = response.unwrap();
if let ResponseType::GetAssetResponseList(list) = api_response.result {
assert_eq!(list.total, Some(1), "Total does not match");
assert!(list.items.is_some(), "Items are missing");
assert_eq!(list.items.unwrap().len(), 1, "Items count does not match");
} else {
panic!("Expected GetAssetResponseList");
}
let api_response: GetAssetResponseList = response.unwrap();
assert_eq!(api_response.total, Some(1), "Total does not match");
assert!(api_response.items.is_some(), "Items are missing");
assert_eq!(api_response.items.unwrap().len(), 1, "Items count does not match");
}

#[tokio::test]
Expand Down Expand Up @@ -154,6 +150,6 @@ async fn test_get_assets_by_owner_failure() {
sort_by: None,
};

let response: Result<ApiResponse, HeliusError> = helius.rpc().get_assets_by_owner(request).await;
let response: Result<GetAssetResponseList, HeliusError> = helius.rpc().get_assets_by_owner(request).await;
assert!(response.is_err(), "Expected an error due to server failure");
}

0 comments on commit 9a8e09b

Please sign in to comment.