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

added the --all flag on ord wallet sats #3824

Merged
29 changes: 28 additions & 1 deletion src/subcommand/wallet/sats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ use super::*;
pub(crate) struct Sats {
#[arg(
long,
conflicts_with = "all",
help = "Find satoshis listed in first column of tab-separated value file <TSV>."
)]
tsv: Option<PathBuf>,
#[arg(
long,
conflicts_with = "tsv",
help = "Display list of all sat ranges in wallet."
)]
all: bool,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -23,6 +30,12 @@ pub struct OutputRare {
pub rarity: Rarity,
}

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct OutputAll {
pub output: OutPoint,
pub ranges: Vec<String>,
}

impl Sats {
pub(crate) fn run(&self, wallet: Wallet) -> SubcommandResult {
ensure!(
Expand All @@ -32,7 +45,20 @@ impl Sats {

let haystacks = wallet.get_wallet_sat_ranges()?;

if let Some(path) = &self.tsv {
if self.all {
Ok(Some(Box::new(
haystacks
.into_iter()
.map(|(outpoint, ranges)| OutputAll {
output: outpoint,
ranges: ranges
.into_iter()
.map(|range| format!("{}-{}", range.0, range.1))
.collect::<Vec<String>>(),
})
.collect::<Vec<OutputAll>>(),
)))
} else if let Some(path) = &self.tsv {
let tsv = fs::read_to_string(path)
.with_context(|| format!("I/O error reading `{}`", path.display()))?;

Expand All @@ -57,6 +83,7 @@ impl Sats {
rarity,
});
}

Ok(Some(Box::new(output)))
}
}
Expand Down
28 changes: 27 additions & 1 deletion tests/wallet/sats.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::*,
ord::subcommand::wallet::sats::{OutputRare, OutputTsv},
ord::subcommand::wallet::sats::{OutputAll, OutputRare, OutputTsv},
};

#[test]
Expand Down Expand Up @@ -94,3 +94,29 @@ fn sats_from_tsv_file_not_found() {
.stderr_regex("error: I/O error reading `.*`\n\nbecause:.*")
.run_and_extract_stdout();
}

#[test]
fn sats_all() {
let core = mockcore::spawn();

let ord = TestServer::spawn_with_server_args(&core, &["--index-sats"], &[]);

create_wallet(&core, &ord);

let second_coinbase = core.mine_blocks(1)[0].txdata[0].txid();

let output = CommandBuilder::new("--index-sats wallet sats --all")
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<Vec<OutputAll>>();

assert_eq!(
output,
vec![OutputAll {
output: format!("{second_coinbase}:0").parse::<OutPoint>().unwrap(),
ranges: vec![format!("{}-{}", 50 * COIN_VALUE, 100 * COIN_VALUE)],
}]
.into_iter()
.collect::<Vec<OutputAll>>()
);
}
Loading