Skip to content

Commit

Permalink
Add wallet balance subcommand (ordinals#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror authored Aug 9, 2022
1 parent c39721a commit 54d29e0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;

mod balance;
mod fund;
mod init;
mod utxos;
Expand Down Expand Up @@ -54,16 +55,18 @@ fn get_wallet(options: Options) -> Result<bdk::wallet::Wallet<SqliteDatabase>> {

#[derive(Debug, Parser)]
pub(crate) enum Wallet {
Init,
Balance,
Fund,
Init,
Utxos,
}

impl Wallet {
pub(crate) fn run(self, options: Options) -> Result {
match self {
Self::Init => init::run(options),
Self::Balance => balance::run(options),
Self::Fund => fund::run(options),
Self::Init => init::run(options),
Self::Utxos => utxos::run(options),
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use super::*;

pub(crate) fn run(options: Options) -> Result {
println!("{}", get_wallet(options)?.get_balance()?);
Ok(())
}
44 changes: 44 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,47 @@ fn utxos() {
.stdout_regex("^[[:xdigit:]]{64}:0 5000000000\n")
.run()
}

#[test]
fn balance() {
let state = Test::new()
.command("--network regtest wallet init")
.expected_status(0)
.expected_stderr("Wallet initialized.\n")
.output()
.state;

let state = Test::with_state(state)
.command("--network regtest wallet balance")
.expected_status(0)
.expected_stdout("0\n")
.output()
.state;

let output = Test::with_state(state)
.command("--network regtest wallet fund")
.stdout_regex("^bcrt1.*\n")
.output();

output
.state
.client
.generate_to_address(
101,
&Address::from_str(
output
.stdout
.strip_suffix('\n')
.ok_or("Failed to strip suffix")
.unwrap(),
)
.unwrap(),
)
.unwrap();

Test::with_state(output.state)
.command("--network regtest wallet balance")
.expected_status(0)
.expected_stdout("5000000000\n")
.run()
}

0 comments on commit 54d29e0

Please sign in to comment.