Skip to content

Commit

Permalink
Add ord wallet cardinals command to list the cardinal outputs (#1904)
Browse files Browse the repository at this point in the history
Co-authored-by: Greg Martin <[email protected]>
Co-authored-by: raphjaph <[email protected]>
  • Loading branch information
3 people authored Mar 27, 2023
1 parent 92157e5 commit ab2f178
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
};

pub mod balance;
pub mod cardinals;
pub mod create;
pub(crate) mod inscribe;
pub mod inscriptions;
Expand Down Expand Up @@ -46,8 +47,10 @@ pub(crate) enum Wallet {
Send(send::Send),
#[clap(about = "See wallet transactions")]
Transactions(transactions::Transactions),
#[clap(about = "List wallet outputs")]
#[clap(about = "List all unspent outputs in wallet")]
Outputs,
#[clap(about = "List unspent cardinal outputs in wallet")]
Cardinals,
}

impl Wallet {
Expand All @@ -63,6 +66,7 @@ impl Wallet {
Self::Send(send) => send.run(options),
Self::Transactions(transactions) => transactions.run(options),
Self::Outputs => outputs::run(options),
Self::Cardinals => cardinals::run(options),
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/subcommand/wallet/cardinals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use {super::*, crate::wallet::Wallet, std::collections::BTreeSet};

#[derive(Serialize, Deserialize)]
pub struct Cardinal {
pub output: OutPoint,
pub amount: u64,
}

pub(crate) fn run(options: Options) -> Result {
let index = Index::open(&options)?;
index.update()?;

let inscribed_utxos = index
.get_inscriptions(None)?
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();

let cardinal_utxos = index
.get_unspent_outputs(Wallet::load(&options)?)?
.iter()
.filter_map(|(output, amount)| {
if inscribed_utxos.contains(output) {
None
} else {
Some(Cardinal {
output: *output,
amount: amount.to_sat(),
})
}
})
.collect::<Vec<Cardinal>>();

print_json(cardinal_utxos)?;

Ok(())
}
1 change: 1 addition & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;

mod balance;
mod cardinals;
mod create;
mod inscribe;
mod inscriptions;
Expand Down
23 changes: 23 additions & 0 deletions tests/wallet/cardinals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use {
super::*,
ord::subcommand::wallet::{cardinals::Cardinal, outputs::Output},
};

#[test]
fn cardinals() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);

// this creates 2 more cardinal outputs and one inscribed output
inscribe(&rpc_server);

let all_outputs = CommandBuilder::new("wallet outputs")
.rpc_server(&rpc_server)
.output::<Vec<Output>>();

let cardinal_outputs = CommandBuilder::new("wallet cardinals")
.rpc_server(&rpc_server)
.output::<Vec<Cardinal>>();

assert_eq!(all_outputs.len() - cardinal_outputs.len(), 1);
}

0 comments on commit ab2f178

Please sign in to comment.