use serde::Deserialize;
pub const TEMPO_TESTNET_FAUCET_URL: &str = "https://docs.tempo.xyz/api/faucet";
#[derive(Debug, thiserror::Error)]
pub enum FaucetError {
#[error("faucet http request failed: {0}")]
Http(#[from] reqwest::Error),
#[error("faucet returned status {status}: {body}")]
Status { status: u16, body: String },
#[error("faucet response missing data array")]
MalformedResponse,
}
#[derive(Debug, Deserialize)]
struct FaucetResponse {
#[serde(default)]
data: Option<Vec<FaucetEntry>>,
}
#[derive(Debug, Deserialize)]
struct FaucetEntry {
#[serde(default, alias = "tx_hash")]
hash: Option<String>,
#[serde(default)]
token: Option<String>,
}
#[derive(Debug, Clone)]
pub struct FaucetDrop {
pub token: Option<String>,
pub tx_hash: Option<String>,
}
pub async fn request_testnet_funds(
http: &reqwest::Client,
address: &str,
) -> Result<Vec<FaucetDrop>, FaucetError> {
let resp = http
.post(TEMPO_TESTNET_FAUCET_URL)
.json(&serde_json::json!({ "address": address }))
.send()
.await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
return Err(FaucetError::Status {
status: status.as_u16(),
body,
});
}
let parsed: FaucetResponse = resp.json().await?;
let entries = parsed.data.ok_or(FaucetError::MalformedResponse)?;
Ok(entries
.into_iter()
.map(|e| FaucetDrop {
token: e.token,
tx_hash: e.hash,
})
.collect())
}