Skip to content

Commit f0358b1

Browse files
committed
Add MempoolClient::get_address
1 parent 45e5f9f commit f0358b1

6 files changed

Lines changed: 162 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2024"
66
# TODO: add socks feature
77

88
[dependencies]
9+
bitcoin = { version = "0.32", default-features = false, features = ["std", "serde"] }
910
reqwest = { version = "0.12", features = ["json"] }
1011
serde = { version = "1.0", features = ["derive"] }
1112
url = "2.5"

examples/mempool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::str::FromStr;
2+
13
use mempoolspace::prelude::*;
24

35
#[tokio::main]
@@ -8,4 +10,10 @@ async fn main() {
810
// Get prices
911
let prices = client.get_prices().await.unwrap();
1012
println!("{:?}", prices);
13+
14+
// Get prices
15+
let address = Address::from_str("1wiz18xYmhRX6xStj2b9t1rwWX4GKUgpv").unwrap();
16+
let address = address.assume_checked();
17+
let stats = client.get_address(&address).await.unwrap();
18+
println!("{:?}", stats);
1119
}

src/client.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Client
22
3+
use bitcoin::Address;
34
use reqwest::{Client, Response};
45
use url::Url;
56

67
use crate::error::Error;
7-
use crate::response::{DifficultyAdjustment, Prices};
8+
use crate::response::{AddressStats, DifficultyAdjustment, Prices};
89

910
/// Mempool Space client
1011
pub struct MempoolClient {
@@ -44,4 +45,14 @@ impl MempoolClient {
4445
let response: Response = self.client.get(url).send().await?;
4546
Ok(response.json().await?)
4647
}
48+
49+
/// Get details about an address.
50+
pub async fn get_address(&self, address: &Address) -> Result<AddressStats, Error> {
51+
let url: Url = self
52+
.url
53+
.join("/api/address/")?
54+
.join(address.to_string().as_str())?;
55+
let response: Response = self.client.get(url).send().await?;
56+
Ok(response.json().await?)
57+
}
4758
}

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(ambiguous_glob_reexports)]
55
#![doc(hidden)]
66

7+
pub use bitcoin::*;
78
pub use url::*;
89

910
pub use crate::client::*;

src/response.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Responses
22
3+
use bitcoin::Amount;
4+
use bitcoin::address::{Address, NetworkUnchecked};
35
use serde::{Deserialize, Serialize};
46

57
/// Prices
@@ -64,3 +66,29 @@ pub struct DifficultyAdjustment {
6466
#[serde(rename = "timeOffset")]
6567
pub time_offset: i64,
6668
}
69+
70+
/// Bitcoin address statistics
71+
#[derive(Debug, Serialize, Deserialize)]
72+
pub struct AddressStats {
73+
/// Bitcoin address
74+
pub address: Address<NetworkUnchecked>,
75+
/// On-chain statistics
76+
pub chain_stats: TransactionStats,
77+
/// Mempool statistics
78+
pub mempool_stats: TransactionStats,
79+
}
80+
81+
/// Transaction statistics for an address
82+
#[derive(Debug, Serialize, Deserialize)]
83+
pub struct TransactionStats {
84+
/// Number of funded transaction outputs
85+
pub funded_txo_count: u32,
86+
/// Total amount of funded transaction outputs
87+
pub funded_txo_sum: Amount,
88+
/// Number of spent transaction outputs
89+
pub spent_txo_count: u32,
90+
/// Total amount of spent transaction outputs
91+
pub spent_txo_sum: Amount,
92+
/// Total number of transactions
93+
pub tx_count: u32,
94+
}

0 commit comments

Comments
 (0)