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 balance command #1047

Merged
merged 4 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {super::*, transaction_builder::TransactionBuilder};

mod balance;
mod create;
mod inscribe;
mod inscriptions;
Expand All @@ -12,6 +13,8 @@ mod utxos;

#[derive(Debug, Parser)]
pub(crate) enum Wallet {
#[clap(about = "Get wallet balance")]
Balance,
#[clap(about = "Create a new wallet")]
Create(create::Create),
#[clap(about = "Create an inscription")]
Expand All @@ -33,6 +36,7 @@ pub(crate) enum Wallet {
impl Wallet {
pub(crate) fn run(self, options: Options) -> Result {
match self {
Self::Balance => balance::run(options),
Self::Create(create) => create.run(options),
Self::Inscribe(inscribe) => inscribe.run(options),
Self::Inscriptions(inscriptions) => inscriptions.run(options),
Expand Down
15 changes: 15 additions & 0 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::*;

pub(crate) fn run(options: Options) -> Result {
println!(
"{}",
options
.bitcoin_rpc_client()?
.get_balances()?
.mine
.trusted
.to_sat()
);

Ok(())
}
17 changes: 17 additions & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,20 @@ 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 _ = &rpc_server.mine_blocks(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can just do:

Suggested change
let _ = &rpc_server.mine_blocks(1);
rpc_server.mine_blocks(1);

let _ = is only needed when ignoring a type that has #[must_use] on it, like Result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right you are


CommandBuilder::new("--regtest wallet balance")
.rpc_server(&rpc_server)
.expected_stdout(format!("5000000000\n"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this doesn't have any interpolations, format is unnecessary:

Suggested change
.expected_stdout(format!("5000000000\n"))
.expected_stdout("5000000000\n")

There's a just recipe for running some of the checks that run on ci, try just ci, or look for the ci recipe in the justfile to find the commands to run. CI runs clippy (which would complain about this), checks formatting, and runs bin/forbid, which forbids certain strings from appearing in the codebase.

bin/forbid is actually quite useful. It forbids the string todo, so if you want to make sure you don't forget something before comitting, write todo: I need to do blah blah blah and CI will complain, so you don't need to worry about forgetting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah! I haven't used just before. I'll check that out so you don't have to keep commenting on things that the linter would check. thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries! just is kind of like make, but only for saving and running commands, and less weird.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, this is great. should be a lot easier to make sure im conforming to style, etc. for future PRs :)

.run();
}