Skip to content

Commit

Permalink
Implement Get Asset Proof Batch Call
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed Apr 30, 2024
1 parent 65c8014 commit 09781ce
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;

Expand All @@ -6,8 +7,8 @@ use crate::error::Result;
use crate::request_handler::RequestHandler;
use crate::types::types::{RpcRequest, RpcResponse};
use crate::types::{
Asset, AssetList, AssetProof, GetAsset, GetAssetBatch, GetAssetProof, GetAssetsByAuthority, GetAssetsByCreator,
GetAssetsByOwner,
Asset, AssetList, AssetProof, GetAsset, GetAssetBatch, GetAssetProof, GetAssetProofBatch, GetAssetsByAuthority,
GetAssetsByCreator, GetAssetsByOwner,
};

use reqwest::{Client, Method, Url};
Expand Down Expand Up @@ -61,6 +62,14 @@ impl RpcClient {
self.post_rpc_request("getAssetProof", request).await
}

/// Gets multiple asset proofs by their IDs
pub async fn get_asset_proof_batch(
&self,
request: GetAssetProofBatch,
) -> Result<HashMap<String, Option<AssetProof>>> {
self.post_rpc_request("getAssetProofBatch", request).await
}

/// Gets a list of assets owned by a given address
pub async fn get_assets_by_owner(&self, request: GetAssetsByOwner) -> Result<AssetList> {
self.post_rpc_request("getAssetsByOwner", request).await
Expand Down
5 changes: 5 additions & 0 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ pub struct GetAssetProof {
pub id: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetAssetProofBatch {
pub ids: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AssetProof {
pub root: String,
Expand Down
133 changes: 133 additions & 0 deletions tests/rpc/test_get_asset_proof_batch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::collections::HashMap;
use std::sync::Arc;

use helius_sdk::client::Helius;
use helius_sdk::config::Config;
use helius_sdk::error::HeliusError;
use helius_sdk::rpc_client::RpcClient;
use helius_sdk::types::*;

use mockito::{self, Server};
use reqwest::Client;

#[tokio::test]
async fn test_get_asset_proof_batch_success() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = server.url();

let mock_response: ApiResponse<HashMap<String, Option<AssetProof>>> = ApiResponse {
jsonrpc: "2.0".to_string(),
result: {
let mut proofs: HashMap<String, Option<AssetProof>> = HashMap::new();
proofs.insert(
"81bxPqYCE8j34nQm7Rooqi8Vt3iMHLzgZJ71rUVbQQuz".to_string(),
Some(AssetProof {
root: "root_hash_1".to_string(),
proof: vec!["proof1".to_string(), "proof2".to_string()],
node_index: 123,
leaf: "leaf_hash_1".to_string(),
tree_id: "tree_id_1".to_string(),
}),
);
proofs.insert(
"CWHuz6GPjWYdwt7rTfRHKaorMwZP58Spyd7aqGK7xFbn".to_string(),
Some(AssetProof {
root: "root_hash_2".to_string(),
proof: vec!["proof3".to_string(), "proof4".to_string()],
node_index: 456,
leaf: "leaf_hash_2".to_string(),
tree_id: "tree_id_2".to_string(),
}),
);
proofs
},
id: "1".to_string(),
};

server
.mock("POST", "/?api-key=fake_api_key")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(serde_json::to_string(&mock_response).unwrap())
.create();

let config: Arc<Config> = Arc::new(Config {
api_key: "fake_api_key".to_string(),
cluster: Cluster::Devnet,
endpoints: HeliusEndpoints {
api: url.to_string(),
rpc: url.to_string(),
},
});

let client: Client = Client::new();
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius: Helius = Helius {
config,
client,
rpc_client,
};

let request: GetAssetProofBatch = GetAssetProofBatch {
ids: vec![
"81bxPqYCE8j34nQm7Rooqi8Vt3iMHLzgZJ71rUVbQQuz".to_string(),
"CWHuz6GPjWYdwt7rTfRHKaorMwZP58Spyd7aqGK7xFbn".to_string(),
],
};

let response: Result<HashMap<String, Option<AssetProof>>, HeliusError> =
helius.rpc().get_asset_proof_batch(request).await;
assert!(response.is_ok(), "API call failed with error: {:?}", response.err());

let proofs: HashMap<String, Option<AssetProof>> = response.unwrap();
assert_eq!(proofs.len(), 2);
assert!(proofs.contains_key("81bxPqYCE8j34nQm7Rooqi8Vt3iMHLzgZJ71rUVbQQuz"));
assert!(proofs.contains_key("CWHuz6GPjWYdwt7rTfRHKaorMwZP58Spyd7aqGK7xFbn"));

let proof_81: &AssetProof = proofs
.get("81bxPqYCE8j34nQm7Rooqi8Vt3iMHLzgZJ71rUVbQQuz")
.and_then(Option::as_ref)
.unwrap();
assert_eq!(
proof_81.leaf, "leaf_hash_1",
"Asset Proof Leaf does not match expected value"
);
}

#[tokio::test]
async fn test_get_asset_proof_failure() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = server.url();

server
.mock("POST", "/?api-key=fake_api_key")
.with_status(500)
.with_header("content-type", "application/json")
.with_body(r#"{"error": "Internal Server Error"}"#)
.create();

let config: Arc<Config> = Arc::new(Config {
api_key: "fake_api_key".to_string(),
cluster: Cluster::Devnet,
endpoints: HeliusEndpoints {
api: url.to_string(),
rpc: url.to_string(),
},
});

let client: Client = Client::new();
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius: Helius = Helius {
config,
client,
rpc_client,
};

let request: GetAssetProofBatch = GetAssetProofBatch {
ids: vec!["Hello there".to_string(), "General Kenobi".to_string()],
};

let response: Result<HashMap<String, Option<AssetProof>>, HeliusError> =
helius.rpc().get_asset_proof_batch(request).await;
assert!(response.is_err(), "Expected an error but got success");
}
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod rpc {
mod test_get_asset;
mod test_get_asset_batch;
mod test_get_asset_proof;
mod test_get_asset_proof_batch;
mod test_get_assets_by_authority;
mod test_get_assets_by_creator;
mod test_get_assets_by_owner;
Expand Down

0 comments on commit 09781ce

Please sign in to comment.