rust_mempool/api/
addresses.rs

1use anyhow::Result;
2use serde::Deserialize;
3
4#[derive(Debug, Deserialize)]
5pub struct UtxoStatus {
6    pub confirmed: bool,
7    pub block_height: Option<u32>,
8    pub block_hash: Option<String>,
9    pub block_time: Option<u64>,
10}
11
12#[derive(Debug, Deserialize)]
13pub struct Utxo {
14    pub txid: String,
15    pub vout: u32,
16    pub status: UtxoStatus,
17    pub value: u64,
18}
19
20impl crate::MempoolClient {
21    pub async fn get_address_utxo(&self, address: &str) -> Result<Vec<Utxo>> {
22        let url = format!("{}/address/{}/utxo", self.base_url, address);
23        let response = self.client.get(&url).send().await?.error_for_status()?;
24
25        Ok(response.json().await?)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use bitcoin::Network;
32
33    use crate::MempoolClient;
34
35    #[tokio::test]
36    async fn test_get_address_utxo() {
37        let client = MempoolClient::new(Network::Bitcoin);
38
39        let utxos = client
40            .get_address_utxo("1KFHE7w8BhaENAswwryaoccDb6qcT6DbYY")
41            .await
42            .unwrap();
43        println!("{:?}", utxos);
44    }
45}