Skip to content

Commit

Permalink
Add ord list-ranges <OUTPOINT> (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Nov 25, 2022
1 parent 3ef2bd8 commit a8dcf46
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) enum Subcommand {
Find(find::Find),
Index,
Info,
List(list::List),
ListRanges(list::List),
Parse(parse::Parse),
Range(range::Range),
Server(server::Server),
Expand All @@ -35,7 +35,7 @@ impl Subcommand {
Self::Find(find) => find.run(options),
Self::Index => index::run(options),
Self::Info => info::run(options),
Self::List(list) => list.run(options),
Self::ListRanges(list) => list.run(options),
Self::Parse(parse) => parse.run(),
Self::Range(range) => range.run(),
Self::Server(server) => {
Expand Down
64 changes: 62 additions & 2 deletions src/subcommand/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,73 @@ impl List {

match index.list(self.outpoint)? {
Some(crate::index::List::Unspent(ranges)) => {
for (start, end) in ranges {
println!("[{start},{end})");
for (output, start, size, rarity, name) in list(self.outpoint, ranges) {
println!("{output}\t{start}\t{size}\t{rarity}\t{name}");
}

Ok(())
}
Some(crate::index::List::Spent) => Err(anyhow!("output spent.")),
None => Err(anyhow!("output not found")),
}
}
}

fn list(outpoint: OutPoint, ranges: Vec<(u64, u64)>) -> Vec<(OutPoint, u64, u64, Rarity, String)> {
ranges
.into_iter()
.map(|(start, end)| {
let size = end - start;
let rarity = Ordinal(start).rarity();
let name = Ordinal(start).name();

(outpoint, start, size, rarity, name)
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn list_ranges() {
let outpoint =
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap();
let ranges = vec![
(50 * COIN_VALUE, 55 * COIN_VALUE),
(10, 100),
(1050000000000000, 1150000000000000),
];
assert_eq!(
list(outpoint, ranges),
vec![
(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
50 * COIN_VALUE,
5 * COIN_VALUE,
Rarity::Uncommon,
"nvtcsezkbth".to_string()
),
(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
10,
90,
Rarity::Common,
"nvtdijuwxlf".to_string()
),
(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
1050000000000000,
100000000000000,
Rarity::Epic,
"gkjbdrhkfqf".to_string()
)
]
)
}
}
3 changes: 0 additions & 3 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use {super::*, transaction_builder::TransactionBuilder};

mod identify;
mod inscribe;
mod list;
mod send;
mod transaction_builder;

Expand Down Expand Up @@ -42,7 +41,6 @@ fn get_change_addresses(options: &Options, n: usize) -> Result<Vec<Address>> {
pub(crate) enum Wallet {
Identify(identify::Identify),
Inscribe(inscribe::Inscribe),
List,
Send(send::Send),
}

Expand All @@ -51,7 +49,6 @@ impl Wallet {
match self {
Self::Identify(identify) => identify.run(options),
Self::Inscribe(inscribe) => inscribe.run(options),
Self::List => list::run(options),
Self::Send(send) => send.run(options),
}
}
Expand Down
76 changes: 0 additions & 76 deletions src/subcommand/wallet/list.rs

This file was deleted.

6 changes: 3 additions & 3 deletions tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use super::*;
fn output_found() {
let rpc_server = test_bitcoincore_rpc::spawn();
CommandBuilder::new(
"--index-ordinals list 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0",
"--index-ordinals list-ranges 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0",
)
.rpc_server(&rpc_server)
.expected_stdout("[0,5000000000)\n")
.expected_stdout("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0\t0\t5000000000\tmythic\tnvtdijuwxlp\n")
.run();
}

#[test]
fn output_not_found() {
let rpc_server = test_bitcoincore_rpc::spawn();
CommandBuilder::new(
"--index-ordinals list 0000000000000000000000000000000000000000000000000000000000000000:0",
"--index-ordinals list-ranges 0000000000000000000000000000000000000000000000000000000000000000:0",
)
.rpc_server(&rpc_server)
.expected_exit_code(1)
Expand Down
16 changes: 0 additions & 16 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@ fn identify_from_tsv_file_not_found() {
.run();
}

#[test]
fn list() {
let rpc_server = test_bitcoincore_rpc::spawn();
let second_coinbase = rpc_server.mine_blocks(1)[0].txdata[0].txid();

CommandBuilder::new("--index-ordinals wallet list")
.rpc_server(&rpc_server)
.expected_stdout(format!(
"{}\t{}\t{}\tuncommon\tnvtcsezkbth\n",
OutPoint::new(second_coinbase, 0),
50 * COIN_VALUE,
50 * COIN_VALUE,
))
.run();
}

#[test]
fn send_works_on_signet() {
let rpc_server = test_bitcoincore_rpc::spawn_with(Network::Signet, "ord");
Expand Down

0 comments on commit a8dcf46

Please sign in to comment.