Skip to content

Commit

Permalink
Allow get_address_txns to get more than the first 25 txns
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenperera committed Oct 14, 2024
1 parent 47677f3 commit 4e09184
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::str::FromStr;
use bitcoin::consensus::{deserialize, serialize, Decodable, Encodable};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::hex::{DisplayHex, FromHex};
use bitcoin::Address;
use bitcoin::{
block::Header as BlockHeader, Block, BlockHash, MerkleBlock, Script, Transaction, Txid,
};
Expand Down Expand Up @@ -387,8 +388,17 @@ impl AsyncClient {

/// Get transaction history for the specified address/scripthash, sorted with newest first.
/// Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
pub async fn get_address_txns(&self, address: &Address) -> Result<Vec<Tx>, Error> {
let path = format!("/address/{address}/txs");
/// You can request more confirmed transactions using an after_txid parameter.
pub async fn get_address_txns(
&self,
address: &Address,
after_txid: Option<Txid>,
) -> Result<Vec<Tx>, Error> {
let path = match after_txid {
Some(after_txid) => format!("/address/{address}/txs/chain/{after_txid}"),
None => format!("/address/{address}/txs"),
};

self.get_response_json(&path).await
}

Expand Down
13 changes: 11 additions & 2 deletions src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,17 @@ impl BlockingClient {

/// Get transaction history for the specified address/scripthash, sorted with newest first.
/// Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
pub fn get_address_txns(&self, address: &Address) -> Result<Vec<Tx>, Error> {
let path = format!("/address/{address}/txs");
/// You can request more confirmed transactions using an after_txid parameter.
pub fn get_address_txns(
&self,
address: &Address,
after_txid: Option<Txid>,
) -> Result<Vec<Tx>, Error> {
let path = match after_txid {
Some(after_txid) => format!("/address/{address}/txs/chain/{after_txid}"),
None => format!("/address/{address}/txs"),
};

self.get_response_json(&path)
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,8 @@ mod test {
let _miner = MINER.lock().await;
generate_blocks_and_wait(1);

let address_txns_blocking = blocking_client.get_address_txns(&address).unwrap();
let address_txns_async = async_client.get_address_txns(&address).await.unwrap();
let address_txns_blocking = blocking_client.get_address_txns(&address, None).unwrap();
let address_txns_async = async_client.get_address_txns(&address, None).await.unwrap();

assert_eq!(address_txns_blocking, address_txns_async);
assert_eq!(address_txns_async[0].txid, txid);
Expand Down

0 comments on commit 4e09184

Please sign in to comment.