Skip to content

Commit

Permalink
Modify ord list output to include the end of each range (ordinals#1998
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gmart7t2 authored and Raylin51 committed Aug 11, 2023
1 parent e1bf4f5 commit 5a0acf2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
65 changes: 57 additions & 8 deletions src/subcommand/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub(crate) struct List {
pub struct Output {
pub output: OutPoint,
pub start: u64,
pub end: u64,
pub size: u64,
pub offset: u64,
pub rarity: Rarity,
pub name: String,
}
Expand All @@ -24,11 +26,22 @@ impl List {
match index.list(self.outpoint)? {
Some(crate::index::List::Unspent(ranges)) => {
let mut outputs = Vec::new();
for (output, start, size, rarity, name) in list(self.outpoint, ranges) {
for Output {
output,
start,
end,
size,
offset,
rarity,
name,
} in list(self.outpoint, ranges)
{
outputs.push(Output {
output,
start,
end,
size,
offset,
rarity,
name,
});
Expand All @@ -44,15 +57,25 @@ impl List {
}
}

fn list(outpoint: OutPoint, ranges: Vec<(u64, u64)>) -> Vec<(OutPoint, u64, u64, Rarity, String)> {
fn list(outpoint: OutPoint, ranges: Vec<(u64, u64)>) -> Vec<Output> {
let mut offset = 0;
ranges
.into_iter()
.map(|(start, end)| {
let size = end - start;
let rarity = Sat(start).rarity();
let name = Sat(start).name();
let output = Output {
output: outpoint,
start,
end,
size,
offset,
name: Sat(start).name(),
rarity: Sat(start).rarity(),
};

(outpoint, start, size, rarity, name)
offset += size;

output
})
.collect()
}
Expand All @@ -61,6 +84,26 @@ fn list(outpoint: OutPoint, ranges: Vec<(u64, u64)>) -> Vec<(OutPoint, u64, u64,
mod tests {
use super::*;

fn output(
output: OutPoint,
start: u64,
end: u64,
size: u64,
offset: u64,
rarity: Rarity,
name: String,
) -> Output {
Output {
output,
start,
end,
size,
offset,
name,
rarity,
}
}

#[test]
fn list_ranges() {
let outpoint =
Expand All @@ -74,27 +117,33 @@ mod tests {
assert_eq!(
list(outpoint, ranges),
vec![
(
output(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
50 * COIN_VALUE,
55 * COIN_VALUE,
5 * COIN_VALUE,
0,
Rarity::Uncommon,
"nvtcsezkbth".to_string()
),
(
output(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
10,
100,
90,
5 * COIN_VALUE,
Rarity::Common,
"nvtdijuwxlf".to_string()
),
(
output(
OutPoint::from_str("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:5")
.unwrap(),
1050000000000000,
1150000000000000,
100000000000000,
5 * COIN_VALUE + 90,
Rarity::Epic,
"gkjbdrhkfqf".to_string()
)
Expand Down
2 changes: 2 additions & 0 deletions tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ fn output_found() {
.parse()
.unwrap(),
start: 0,
end: 50 * COIN_VALUE,
size: 50 * COIN_VALUE,
offset: 0,
rarity: "mythic".parse().unwrap(),
name: "nvtdijuwxlp".into(),
}]
Expand Down

0 comments on commit 5a0acf2

Please sign in to comment.