spark_sdk/spark_test_utils/
faucet.rs

1use crate::error::SparkSdkError;
2
3#[derive(Debug)]
4pub struct Faucet {
5    base_url: String,
6}
7
8impl Faucet {
9    pub fn new() -> Self {
10        let base_url = "https://regtest-mempool.dev.dev.sparkinfra.net/api/v1/faucet/".to_string();
11
12        Self { base_url }
13    }
14
15    pub async fn make_faucet_request(
16        &self,
17        address: &str,
18        amount: u64,
19    ) -> Result<(), SparkSdkError> {
20        // amount must be less than 100,000 sats
21        if amount > 100_000 {
22            return Err(SparkSdkError::InvalidInput(
23                "Amount must be less than 100,000".into(),
24            ));
25        }
26
27        // TODO: get these from the environment
28        let regtest_username = "polarity";
29        let regtest_password = "hN7kD3wY6cF9sA2e";
30
31        let url = format!("{}{}/{}", self.base_url, address, amount);
32
33        let client = reqwest::Client::new();
34        let response = client
35            .get(url)
36            .basic_auth(regtest_username, Some(regtest_password))
37            .timeout(std::time::Duration::from_secs(30))
38            .send()
39            .await
40            .map_err(|e| {
41                SparkSdkError::InvalidInput(format!("Failed to make faucet request: {e}"))
42            })?;
43
44        let status_code = response.status();
45
46        if status_code.is_success() {
47            Ok(())
48        } else {
49            Err(SparkSdkError::FaucetRequestFailed(format!(
50                "Got status code: {}",
51                status_code
52            )))
53        }
54    }
55}