Skip to content

Commit

Permalink
Run cargo fmt to follow Rust conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkathoh committed May 21, 2024
1 parent a5a744b commit 51c85af
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 296 deletions.
145 changes: 84 additions & 61 deletions tests/test_enhanced_transactions.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use std::sync::Arc;
use helius::config::Config;
use helius::rpc_client::RpcClient;
use helius::types::{
AccountData, Cluster, EnhancedTransaction, HeliusEndpoints, InnerInstruction, Instruction,
ParseTransactionsRequest, ParsedTransactionHistoryRequest, Source, TokenStandard, TokenTransfer, TransactionEvent,
TransactionType, TransferUserAccounts,
};
use helius::Helius;
use mockito::Server;
use reqwest::Client;
use serde_json::Number;
use helius::config::Config;
use helius::Helius;
use helius::rpc_client::RpcClient;
use helius::types::{AccountData, Cluster, EnhancedTransaction, HeliusEndpoints, InnerInstruction, Instruction, ParsedTransactionHistoryRequest, ParseTransactionsRequest, Source, TokenStandard, TokenTransfer, TransactionEvent, TransactionType, TransferUserAccounts};
use std::sync::Arc;

#[tokio::test]
async fn test_parse_transactions_success(){
let mut server:Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url:String = format!("{}/",server.url());
async fn test_parse_transactions_success() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = format!("{}/", server.url());

let mock_response = vec![EnhancedTransaction{
account_data: vec![AccountData{
let mock_response = vec![EnhancedTransaction {
account_data: vec![AccountData {
account: "".to_string(),
native_token_balance: Some(Number::from(10)),
token_balance_changes: None,
Expand All @@ -23,10 +27,11 @@ async fn test_parse_transactions_success(){
source: Source::Jupiter,
fee: 5000,
fee_payer: "8cRrU1NzNpjL3k2BwjW3VixAcX6VFc29KHr4KZg8cs2Y".to_string(),
signature: "yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string(),
signature: "yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK"
.to_string(),
slot: 148277128,
native_transfers: None,
token_transfers: Some(vec![TokenTransfer{
token_transfers: Some(vec![TokenTransfer {
user_accounts: TransferUserAccounts {
from_user_account: Some("2iK5FbRZcJHAfhUNYmYdKTzSLnZE9NGAECurPoxDA3o7".to_string()),
to_user_account: Some("DCAKxn5PFNN1mBREPWGdk1RXg5aVH9rPErLfBFEi2Emb".to_string()),
Expand All @@ -38,11 +43,11 @@ async fn test_parse_transactions_success(){
mint: "6oCioNHNTh4Xoz33mQSoTW4mxxmnyWVBNgv7zHjUuBkK".to_string(),
}]),
transaction_error: None,
instructions: vec![Instruction{
instructions: vec![Instruction {
accounts: vec![],
data: "kdL8HQJrbbvQRGXmoadaja1Qvs".to_string(),
program_id: "MEisE1HzehtrDpAAT8PnLHjpSSkRYakotTuJRPjTpo8".to_string(),
inner_instructions: vec![InnerInstruction{
inner_instructions: vec![InnerInstruction {
accounts: vec![],
data: "Dd1k91cWt84qJoQr3F".to_string(),
program_id: "Dd1k91cWt84qJoQr3FT7EXQpSaMtZtwPwdho7RbMWtEV".to_string(),
Expand All @@ -52,7 +57,8 @@ async fn test_parse_transactions_success(){
timestamp: 1656442333,
}];

server.mock("POST","/v0/transactions?api-key=fake_api_key")
server
.mock("POST", "/v0/transactions?api-key=fake_api_key")
.with_status(200)
.with_header("Content-Type", "application/json")
.with_body(serde_json::to_string(&mock_response).unwrap())
Expand All @@ -68,30 +74,34 @@ async fn test_parse_transactions_success(){
});

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{
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius = Helius {
config,
client,
rpc_client,
};

let request = ParseTransactionsRequest{
transactions: vec!["DiG7v24AXRQqGagDx8pcVxRgVrgFoXUpJgp7xb62ycG9".to_string(),
"46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string()],
let request = ParseTransactionsRequest {
transactions: vec![
"DiG7v24AXRQqGagDx8pcVxRgVrgFoXUpJgp7xb62ycG9".to_string(),
"46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string(),
],
};

let response = helius.parse_transactions(request).await;
assert!(response.is_ok(),"The API call failed: {:?}",response.err());
assert!(response.is_ok(), "The API call failed: {:?}", response.err());
let tx_response = response.unwrap();
assert_eq!(tx_response[0].signature,"yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string());
assert_eq!(tx_response[0].transaction_type,TransactionType::Any);

assert_eq!(
tx_response[0].signature,
"yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string()
);
assert_eq!(tx_response[0].transaction_type, TransactionType::Any);
}

#[tokio::test]
async fn test_parse_transactions_failure(){
let mut server:Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url:String = format!("{}/",server.url());
async fn test_parse_transactions_failure() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = format!("{}/", server.url());

let config: Arc<Config> = Arc::new(Config {
api_key: "fake_api_key".to_string(),
Expand All @@ -102,32 +112,35 @@ async fn test_parse_transactions_failure(){
},
});
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{
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius = Helius {
config,
client,
rpc_client,
};
let request = ParseTransactionsRequest{
transactions: vec!["DiG7v24AXRQqGagDx8pcVxRgVrgFoXUpJgp7xb62ycG9".to_string(),
"46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string()],
let request = ParseTransactionsRequest {
transactions: vec![
"DiG7v24AXRQqGagDx8pcVxRgVrgFoXUpJgp7xb62ycG9".to_string(),
"46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string(),
],
};
server.mock("POST","/v0/transactions?api-key=fake_api_key")
server
.mock("POST", "/v0/transactions?api-key=fake_api_key")
.with_status(500)
.with_header("Content-Type", "application/json")
.with_body(r#"{"error":"Internal Server Error"}"#)
.create();

let response = helius.parse_transactions(request).await;
assert!(response.is_err(),"Expected an error due to server failure");
assert!(response.is_err(), "Expected an error due to server failure");
}
#[tokio::test]
async fn test_parse_transaction_history_success(){
let mut server:Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url:String = format!("{}/",server.url());
async fn test_parse_transaction_history_success() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = format!("{}/", server.url());

let mock_response = vec![EnhancedTransaction{
account_data: vec![AccountData{
let mock_response = vec![EnhancedTransaction {
account_data: vec![AccountData {
account: "".to_string(),
native_token_balance: Some(Number::from(10)),
token_balance_changes: None,
Expand All @@ -137,10 +150,11 @@ async fn test_parse_transaction_history_success(){
source: Source::Jupiter,
fee: 5000,
fee_payer: "8cRrU1NzNpjL3k2BwjW3VixAcX6VFc29KHr4KZg8cs2Y".to_string(),
signature: "yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string(),
signature: "yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK"
.to_string(),
slot: 148277128,
native_transfers: None,
token_transfers: Some(vec![TokenTransfer{
token_transfers: Some(vec![TokenTransfer {
user_accounts: TransferUserAccounts {
from_user_account: Some("2iK5FbRZcJHAfhUNYmYdKTzSLnZE9NGAECurPoxDA3o7".to_string()),
to_user_account: Some("DCAKxn5PFNN1mBREPWGdk1RXg5aVH9rPErLfBFEi2Emb".to_string()),
Expand All @@ -152,11 +166,11 @@ async fn test_parse_transaction_history_success(){
mint: "6oCioNHNTh4Xoz33mQSoTW4mxxmnyWVBNgv7zHjUuBkK".to_string(),
}]),
transaction_error: None,
instructions: vec![Instruction{
instructions: vec![Instruction {
accounts: vec![],
data: "kdL8HQJrbbvQRGXmoadaja1Qvs".to_string(),
program_id: "MEisE1HzehtrDpAAT8PnLHjpSSkRYakotTuJRPjTpo8".to_string(),
inner_instructions: vec![InnerInstruction{
inner_instructions: vec![InnerInstruction {
accounts: vec![],
data: "Dd1k91cWt84qJoQr3F".to_string(),
program_id: "Dd1k91cWt84qJoQr3FT7EXQpSaMtZtwPwdho7RbMWtEV".to_string(),
Expand All @@ -166,7 +180,11 @@ async fn test_parse_transaction_history_success(){
timestamp: 1656442333,
}];

let mock = server.mock("GET","/v0/addresses/46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi/transactions?api-key=fake_api_key")
let mock = server
.mock(
"GET",
"/v0/addresses/46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi/transactions?api-key=fake_api_key",
)
.with_status(200)
.with_header("Content-Type", "application/json")
.with_body(serde_json::to_string(&mock_response).unwrap())
Expand All @@ -182,32 +200,33 @@ async fn test_parse_transaction_history_success(){
});

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{
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius = Helius {
config,
client,
rpc_client,
};

let request = ParsedTransactionHistoryRequest{
let request = ParsedTransactionHistoryRequest {
address: "46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string(),
before: None,
};

let response = helius.parsed_transaction_history(request).await;


assert!(response.is_ok(),"The API call failed: {:?}",response.err());
assert!(response.is_ok(), "The API call failed: {:?}", response.err());
let tx_response = response.unwrap();
assert_eq!(tx_response[0].signature,"yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string());
assert_eq!(tx_response[0].transaction_type,TransactionType::Any);

assert_eq!(
tx_response[0].signature,
"yy5BT9benHhx8fGCvhcAfTtLEHAtRJ3hRTzVL16bdrTCWm63t2vapfrZQZLJC3RcuagekaXjSs2zUGQvbcto8DK".to_string()
);
assert_eq!(tx_response[0].transaction_type, TransactionType::Any);
}

#[tokio::test]
async fn test_parse_transaction_history_failure(){
let mut server:Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url:String = format!("{}/",server.url());
async fn test_parse_transaction_history_failure() {
let mut server: Server = Server::new_with_opts_async(mockito::ServerOpts::default()).await;
let url: String = format!("{}/", server.url());

let config: Arc<Config> = Arc::new(Config {
api_key: "fake_api_key".to_string(),
Expand All @@ -218,22 +237,26 @@ async fn test_parse_transaction_history_failure(){
},
});
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{
let rpc_client: Arc<RpcClient> = Arc::new(RpcClient::new(Arc::new(client.clone()), Arc::clone(&config)).unwrap());
let helius = Helius {
config,
client,
rpc_client,
};
let request = ParsedTransactionHistoryRequest{
let request = ParsedTransactionHistoryRequest {
address: "46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi".to_string(),
before: None,
};
server.mock("GET","/v0/addresses/46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi/transactions?api-key=fake_api_key")
server
.mock(
"GET",
"/v0/addresses/46tC8n6GyWvUjFxpTE9juG5WZ72RXADpPhY4S1d6wvTi/transactions?api-key=fake_api_key",
)
.with_status(500)
.with_header("Content-Type", "application/json")
.with_body(r#"{"error":"Internal Server Error"}"#)
.create();

let response = helius.parsed_transaction_history(request).await;
assert!(response.is_err(),"Expected an error due to server failure");
}
assert!(response.is_err(), "Expected an error due to server failure");
}
7 changes: 3 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ mod rpc {
mod test_get_token_accounts;
mod test_search_assets;
}
mod webhook{
mod webhook {
mod test_create_webhook;
mod test_edit_webhook;
mod test_get_webhook_by_id;

mod test_get_all_webhooks;
mod test_delete_webhook;
mod test_append_addresses_to_webhook;
mod test_delete_webhook;
mod test_get_all_webhooks;
mod test_remove_addresses_from_webhook;

}
Loading

0 comments on commit 51c85af

Please sign in to comment.