#[cfg(not(target_arch = "wasm32"))]
use crate::rpc::transaction_utils::wait_for_confirmation;
use crate::{
error::Result,
rpc::{
traits::RpcClient,
types::{Pubkey, Signature},
},
};
#[cfg(not(target_arch = "wasm32"))]
pub async fn request_airdrop_with_confirmation(
client: &impl RpcClient,
pubkey: &Pubkey,
amount: u64,
timeout_ms: Option<u32>,
) -> Result<Signature> {
let signature = client.request_airdrop(pubkey, amount).await?;
wait_for_confirmation(client, &signature, timeout_ms).await?;
Ok(signature)
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use super::*;
use crate::rpc::{mock_client::MockRpcClient, types::SignatureStatus};
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn test_airdrop_with_confirmation_success() {
let expected_sig_str = "5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW";
let expected_sig = Signature::from_str(expected_sig_str).unwrap();
let mock_rpc = MockRpcClient::new()
.with_signature(expected_sig)
.with_signature_statuses(vec![Some(SignatureStatus {
slot: 100,
err: None,
executed: true,
})]);
let pubkey = Pubkey::new_unique();
let result =
request_airdrop_with_confirmation(&mock_rpc, &pubkey, 1_000_000_000, None).await;
assert!(result.is_ok(), "Airdrop should succeed");
let signature = result.unwrap();
assert_eq!(signature.to_string(), expected_sig_str);
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn test_airdrop_with_confirmation_timeout() {
let expected_sig = Signature::from_str(
"5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW",
)
.unwrap();
let mock_rpc = MockRpcClient::new()
.with_signature(expected_sig)
.with_signature_statuses(vec![Some(SignatureStatus {
slot: 100,
err: None,
executed: false, })]);
let pubkey = Pubkey::new_unique();
let result =
request_airdrop_with_confirmation(&mock_rpc, &pubkey, 1_000_000_000, Some(100)).await;
assert!(result.is_err(), "Should timeout");
assert!(
matches!(
result.unwrap_err(),
crate::error::RialoError::TransactionTimeout
),
"Should be TransactionTimeout error"
);
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn test_airdrop_with_custom_timeout() {
let expected_sig = Signature::from_str(
"5VERv8NMvzbJMEkV8xnrLkEaWRtSz9CosKDYjCJjBRnbJLgp8uirBgmQpjKhoR4tjF3ZpRzrFmBV6UjKdiSZkQUW",
)
.unwrap();
let mock_rpc = MockRpcClient::new()
.with_signature(expected_sig)
.with_signature_statuses(vec![Some(SignatureStatus {
slot: 100,
err: None,
executed: true,
})]);
let pubkey = Pubkey::new_unique();
let result =
request_airdrop_with_confirmation(&mock_rpc, &pubkey, 1_000_000_000, Some(10000)).await;
assert!(result.is_ok(), "Should succeed with custom timeout");
}
}