Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add several wallet APIs #10

Merged
merged 5 commits into from Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/bitcoind_rpc.rs
Expand Up @@ -37,6 +37,19 @@ impl Client {
Ok(blockchain_info.mediantime)
}

pub async fn block_height(&self) -> Result<u32> {
let block_height = self
.rpc_client
.send::<Vec<()>, _>(json_rpc::Request::new(
"getblockcount",
vec![],
JSONRPC_VERSION.into(),
))
.await?;

Ok(block_height)
}

async fn blockchain_info(&self) -> Result<BlockchainInfo> {
let blockchain_info = self
.rpc_client
Expand Down
25 changes: 25 additions & 0 deletions src/wallet.rs
Expand Up @@ -47,6 +47,10 @@ impl Wallet {
Ok(self.bitcoind_client.median_time().await?)
}

pub async fn block_height(&self) -> Result<u32> {
Ok(self.bitcoind_client.block_height().await?)
}

pub async fn new_address(&self) -> Result<Address> {
self.bitcoind_client
.get_new_address(&self.name, None, Some("bech32".into()))
Expand Down Expand Up @@ -114,9 +118,12 @@ impl Wallet {

#[cfg(test)]
mod test {
use std::time::Duration;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline.

use crate::{Bitcoind, Wallet};
use bitcoin::util::psbt::PartiallySignedTransaction;
use bitcoin::{Amount, Transaction, TxOut};
use tokio::time::delay_for;

#[tokio::test]
async fn get_wallet_transaction() {
Expand Down Expand Up @@ -230,4 +237,22 @@ mod test {
.unwrap();
println!("Final tx_id: {:?}", txid);
}

#[tokio::test]
async fn block_height() {
let tc_client = testcontainers::clients::Cli::default();
let bitcoind = Bitcoind::new(&tc_client, "0.19.1").unwrap();
bitcoind.init(5).await.unwrap();

let wallet = Wallet::new("wallet", bitcoind.node_url.clone())
.await
.unwrap();

let height_0 = wallet.block_height().await.unwrap();
delay_for(Duration::from_secs(2)).await;

let height_1 = wallet.block_height().await.unwrap();

assert!(height_1 > height_0)
}
}