From ef20a9f1acab9c36e70248c8c32ba820df8340f1 Mon Sep 17 00:00:00 2001 From: rot13maxi Date: Wed, 21 Dec 2022 21:48:53 -0500 Subject: [PATCH 1/3] add balance command and test --- src/subcommand/wallet.rs | 4 ++++ src/subcommand/wallet/balance.rs | 20 ++++++++++++++++++++ tests/wallet.rs | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/subcommand/wallet/balance.rs diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index 93be1f67e5..73634314f9 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -1,5 +1,6 @@ use {super::*, transaction_builder::TransactionBuilder}; +mod balance; mod create; mod inscribe; mod inscriptions; @@ -12,6 +13,8 @@ mod utxos; #[derive(Debug, Parser)] pub(crate) enum Wallet { + #[clap(about = "Get wallet balance")] + Balance(balance::Balance), #[clap(about = "Create a new wallet")] Create(create::Create), #[clap(about = "Create an inscription")] @@ -33,6 +36,7 @@ pub(crate) enum Wallet { impl Wallet { pub(crate) fn run(self, options: Options) -> Result { match self { + Self::Balance(balance) => balance.run(options), Self::Create(create) => create.run(options), Self::Inscribe(inscribe) => inscribe.run(options), Self::Inscriptions(inscriptions) => inscriptions.run(options), diff --git a/src/subcommand/wallet/balance.rs b/src/subcommand/wallet/balance.rs new file mode 100644 index 0000000000..5bf01801d3 --- /dev/null +++ b/src/subcommand/wallet/balance.rs @@ -0,0 +1,20 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Balance {} + +impl Balance { + pub(crate) fn run(self, options: Options) -> Result { + println!( + "{}", + options + .bitcoin_rpc_client_for_wallet_command("ord wallet balance")? + .get_balances()? + .mine + .trusted + .to_sat() + ); + + Ok(()) + } +} diff --git a/tests/wallet.rs b/tests/wallet.rs index 4e2faa9a11..ed7b12c8f6 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -688,3 +688,21 @@ fn inscribe_gif() { ), ) } + +#[test] +fn wallet_balance() { + let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Regtest, "ord"); + + CommandBuilder::new("--regtest wallet balance") + .rpc_server(&rpc_server) + .expected_stdout("0\n") + .run(); + + let coinbase_tx = &rpc_server.mine_blocks_with_subsidy(1, 1_000_000)[0].txdata[0]; + let amount = coinbase_tx.output[0].value; + + CommandBuilder::new("--regtest wallet balance") + .rpc_server(&rpc_server) + .expected_stdout(format!("{amount}\n")) + .run(); +} From 9c14d8a88da9bc48795fe87d5356ebc610c2ceb2 Mon Sep 17 00:00:00 2001 From: rot13maxi Date: Fri, 23 Dec 2022 16:34:10 -0500 Subject: [PATCH 2/3] address PR feedback --- src/subcommand/wallet.rs | 4 ++-- src/subcommand/wallet/balance.rs | 27 +++++++++++---------------- tests/wallet.rs | 5 ++--- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index 73634314f9..e09fead4cd 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -14,7 +14,7 @@ mod utxos; #[derive(Debug, Parser)] pub(crate) enum Wallet { #[clap(about = "Get wallet balance")] - Balance(balance::Balance), + Balance, #[clap(about = "Create a new wallet")] Create(create::Create), #[clap(about = "Create an inscription")] @@ -36,7 +36,7 @@ pub(crate) enum Wallet { impl Wallet { pub(crate) fn run(self, options: Options) -> Result { match self { - Self::Balance(balance) => balance.run(options), + Self::Balance => balance::run(options), Self::Create(create) => create.run(options), Self::Inscribe(inscribe) => inscribe.run(options), Self::Inscriptions(inscriptions) => inscriptions.run(options), diff --git a/src/subcommand/wallet/balance.rs b/src/subcommand/wallet/balance.rs index 5bf01801d3..f375fc8981 100644 --- a/src/subcommand/wallet/balance.rs +++ b/src/subcommand/wallet/balance.rs @@ -1,20 +1,15 @@ use super::*; -#[derive(Debug, Parser)] -pub(crate) struct Balance {} +pub(crate) fn run(options: Options) -> Result { + println!( + "{}", + options + .bitcoin_rpc_client()? + .get_balances()? + .mine + .trusted + .to_sat() + ); -impl Balance { - pub(crate) fn run(self, options: Options) -> Result { - println!( - "{}", - options - .bitcoin_rpc_client_for_wallet_command("ord wallet balance")? - .get_balances()? - .mine - .trusted - .to_sat() - ); - - Ok(()) - } + Ok(()) } diff --git a/tests/wallet.rs b/tests/wallet.rs index 0b11ee29f6..fdbf3bce84 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -698,11 +698,10 @@ fn wallet_balance() { .expected_stdout("0\n") .run(); - let coinbase_tx = &rpc_server.mine_blocks_with_subsidy(1, 1_000_000)[0].txdata[0]; - let amount = coinbase_tx.output[0].value; + let _ = &rpc_server.mine_blocks(1); CommandBuilder::new("--regtest wallet balance") .rpc_server(&rpc_server) - .expected_stdout(format!("{amount}\n")) + .expected_stdout(format!("5000000000\n")) .run(); } From c02dc40989488f9c72ca31f8130abe414bb68edf Mon Sep 17 00:00:00 2001 From: rot13maxi Date: Fri, 23 Dec 2022 17:18:07 -0500 Subject: [PATCH 3/3] style cleanup --- tests/wallet.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/wallet.rs b/tests/wallet.rs index fdbf3bce84..f871c6692f 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -698,10 +698,10 @@ fn wallet_balance() { .expected_stdout("0\n") .run(); - let _ = &rpc_server.mine_blocks(1); + rpc_server.mine_blocks(1); CommandBuilder::new("--regtest wallet balance") .rpc_server(&rpc_server) - .expected_stdout(format!("5000000000\n")) + .expected_stdout("5000000000\n") .run(); }