From 2b85ad059fc3a847af0610e8eebf68255869dc60 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Wed, 7 Dec 2022 23:49:52 +0100 Subject: [PATCH] list utxos in wallet --- src/subcommand/wallet.rs | 3 +++ src/subcommand/wallet/utxos.rs | 21 +++++++++++++++++++++ tests/wallet.rs | 14 ++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/subcommand/wallet/utxos.rs diff --git a/src/subcommand/wallet.rs b/src/subcommand/wallet.rs index 81a94f2a1a..dd44510c13 100644 --- a/src/subcommand/wallet.rs +++ b/src/subcommand/wallet.rs @@ -5,6 +5,7 @@ mod inscribe; mod receive; mod send; mod transaction_builder; +mod utxos; fn list_unspent(options: &Options, index: &Index) -> Result)>> { let client = options.bitcoin_rpc_client()?; @@ -61,6 +62,7 @@ pub(crate) enum Wallet { Inscribe(inscribe::Inscribe), Receive(receive::Receive), Send(send::Send), + Utxos(utxos::Utxos), } impl Wallet { @@ -70,6 +72,7 @@ impl Wallet { Self::Inscribe(inscribe) => inscribe.run(options), Self::Receive(receive) => receive.run(options), Self::Send(send) => send.run(options), + Self::Utxos(utxos) => utxos.run(options), } } } diff --git a/src/subcommand/wallet/utxos.rs b/src/subcommand/wallet/utxos.rs new file mode 100644 index 0000000000..eb3e98dc0a --- /dev/null +++ b/src/subcommand/wallet/utxos.rs @@ -0,0 +1,21 @@ +use super::*; + +#[derive(Debug, Parser)] +pub(crate) struct Utxos {} + +impl Utxos { + pub(crate) fn run(self, options: Options) -> Result { + let utxos = options + .bitcoin_rpc_client_for_wallet_command("ord wallet utxos")? + .list_unspent(None, None, None, None, None)? + .iter() + .map(|utxo| (OutPoint::new(utxo.txid, utxo.vout), utxo.amount)) + .collect::>(); + + for (outpoint, amount) in utxos { + println!("{outpoint}\t{}", amount.to_sat()); + } + + Ok(()) + } +} diff --git a/tests/wallet.rs b/tests/wallet.rs index 48a8c299f1..b3f3b06e68 100644 --- a/tests/wallet.rs +++ b/tests/wallet.rs @@ -534,3 +534,17 @@ fn receive() { assert!(Address::from_str(stdout.trim()).is_ok()); } + +#[test] +fn utxos() { + let rpc_server = test_bitcoincore_rpc::spawn(); + + let coinbase_tx = &rpc_server.mine_blocks_with_subsidy(1, 1_000_000)[0].txdata[0]; + let outpoint = OutPoint::new(coinbase_tx.txid(), 0); + let amount = coinbase_tx.output[0].value; + + CommandBuilder::new("wallet utxos") + .rpc_server(&rpc_server) + .expected_stdout(format!("{outpoint}\t{amount}\n")) + .run(); +}