Skip to content

Commit

Permalink
Implement Get RWA Asset Call and Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed May 3, 2024
1 parent a95c8f6 commit 8b7eb83
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use crate::types::types::{RpcRequest, RpcResponse};
use crate::types::{
Asset, AssetList, AssetProof, EditionsList, GetAsset, GetAssetBatch, GetAssetProof, GetAssetProofBatch,
GetAssetSignatures, GetAssetsByAuthority, GetAssetsByCreator, GetAssetsByGroup, GetAssetsByOwner, GetNftEditions,
GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, GetTokenAccounts, SearchAssets, TokenAccountsList,
TransactionSignatureList,
GetPriorityFeeEstimateRequest, GetPriorityFeeEstimateResponse, GetRwaAssetRequest, GetRwaAssetResponse,
GetTokenAccounts, SearchAssets, TokenAccountsList, TransactionSignatureList,
};

use reqwest::{Client, Method, Url};
Expand Down Expand Up @@ -253,4 +253,16 @@ impl RpcClient {
) -> Result<GetPriorityFeeEstimateResponse> {
self.post_rpc_request("getPriorityFeeEstimate", vec![request]).await
}

/// Gets a Real-World Asset (RWA) based on its mint address
///
/// # Arguments
/// * `request` - A struct containing the mint ID of the RWA
///
/// # Returns
/// A `Result` that, if successful, wraps a `GetRwaAssetResponse` struct containing:
/// - `items`: Details of the RWA including controllers, registries, and policy engine data
pub async fn get_rwa_asset(&self, request: GetRwaAssetRequest) -> Result<GetRwaAssetResponse> {
self.post_rpc_request("getRwaAccountsByMint", request).await
}
}
58 changes: 58 additions & 0 deletions src/types/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,61 @@ pub struct GetPriorityFeeEstimateResponse {
pub priority_fee_estimate: Option<f64>,
pub priority_fee_levels: Option<MicroLamportPriorityFeeLevels>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct GetRwaAssetRequest {
pub id: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GetRwaAssetResponse {
pub items: FullRwaAccount,
}

#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct FullRwaAccount {
pub asset_controller: Option<AssetControllerAccount>,
pub data_registry: Option<DataRegistryAccount>,
pub identity_registry: Option<IdentityRegistryAccount>,
pub policy_engine: Option<PolicyEngine>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AssetControllerAccount {
pub address: String,
pub mint: String,
pub authority: String,
pub delegate: String,
pub version: u32,
pub closed: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct DataRegistryAccount {
pub address: String,
pub mint: String,
pub version: u32,
pub closed: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct IdentityRegistryAccount {
pub address: String,
pub mint: String,
pub authority: String,
pub delegate: String,
pub version: u32,
pub closed: bool,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct PolicyEngine {
pub address: String,
pub mint: String,
pub authority: String,
pub delegate: String,
pub policies: Vec<String>,
pub version: u32,
pub closed: bool,
}
113 changes: 113 additions & 0 deletions tests/rpc/test_get_rwa_asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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_rwa_asset_success() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = server.url();

let mock_response: ApiResponse<GetRwaAssetResponse> = ApiResponse {
jsonrpc: "2.0".to_string(),
result: GetRwaAssetResponse {
items: FullRwaAccount {
asset_controller: Some(AssetControllerAccount {
address: "JeffAlbertson".to_string(),
mint: "RadioactiveMan#1".to_string(),
authority: "TheAndroidsDungeonandBaseballCardShop".to_string(),
delegate: "JeffAlbertson".to_string(),
version: 1,
closed: false,
}),
data_registry: Some(DataRegistryAccount {
address: "CGC".to_string(),
mint: "CertifiedGuarantyCompany".to_string(),
version: 10,
closed: false,
}),
identity_registry: None,
policy_engine: None,
},
},
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: GetRwaAssetRequest = GetRwaAssetRequest {
id: "RadioactiveMan#1".to_string(),
};

let response: Result<GetRwaAssetResponse, HeliusError> = helius.rpc().get_rwa_asset(request).await;
assert!(response.is_ok());
assert_eq!(
response.unwrap().items.asset_controller.unwrap().address,
"JeffAlbertson"
);
}

#[tokio::test]
async fn test_get_rwa_asset_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(404)
.with_header("content-type", "application/json")
.with_body(r#"{"error": "Asset not found"}"#)
.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: GetRwaAssetRequest = GetRwaAssetRequest {
id: "Flanders'BookofFaith".to_string(),
};

let response: Result<GetRwaAssetResponse, HeliusError> = helius.rpc().get_rwa_asset(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 @@ -15,6 +15,7 @@ mod rpc {
mod test_get_assets_by_owner;
mod test_get_nft_editions;
mod test_get_priority_fee_estimate;
mod test_get_rwa_asset;
mod test_get_token_accounts;
mod test_search_assets;
}

0 comments on commit 8b7eb83

Please sign in to comment.