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

JSON for everyone #1367

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ use {
inscription::Inscription,
inscription_id::InscriptionId,
media::Media,
object::Object,
options::Options,
outgoing::Outgoing,
rarity::Rarity,
representation::Representation,
sat::Sat,
subcommand::Subcommand,
tally::Tally,
},
Expand Down Expand Up @@ -74,7 +71,7 @@ use {
};

pub use crate::{
fee_rate::FeeRate, sat_point::SatPoint,
fee_rate::FeeRate, object::Object, rarity::Rarity, sat::Sat, sat_point::SatPoint,
subcommand::wallet::transaction_builder::TransactionBuilder,
};

Expand Down
20 changes: 19 additions & 1 deletion src/object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[derive(Debug, PartialEq)]
pub(crate) enum Object {
pub enum Object {
Address(Address),
Hash([u8; 32]),
InscriptionId(InscriptionId),
Expand Down Expand Up @@ -50,6 +50,24 @@ impl Display for Object {
}
}

impl Serialize for Object {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl<'de> Deserialize<'de> for Object {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(DeserializeFromStr::deserialize(deserializer)?.0)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Ord, PartialOrd, Deserialize, Serialize)]
#[serde(transparent)]
pub struct Sat(pub(crate) u64);
pub struct Sat(pub u64);

impl Sat {
pub(crate) const LAST: Self = Self(Self::SUPPLY - 1);
Expand Down
16 changes: 8 additions & 8 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::*;

mod epochs;
mod find;
pub mod epochs;
pub mod find;
mod index;
mod info;
mod list;
mod parse;
pub mod info;
pub mod list;
pub mod parse;
mod preview;
mod server;
mod subsidy;
mod supply;
mod traits;
pub mod subsidy;
pub mod supply;
pub mod traits;
pub mod wallet;

fn print_json(output: impl Serialize) -> Result {
Expand Down
11 changes: 10 additions & 1 deletion src/subcommand/epochs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub starting_sats: Vec<Sat>,
}

pub(crate) fn run() -> Result {
let mut starting_sats = Vec::new();
for sat in Epoch::STARTING_SATS {
println!("{}", sat);
starting_sats.push(sat);
}

print_json(Output { starting_sats })?;

Ok(())
}
7 changes: 6 additions & 1 deletion src/subcommand/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ pub(crate) struct Find {
sat: Sat,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub satpoint: SatPoint,
}

impl Find {
pub(crate) fn run(self, options: Options) -> Result {
let index = Index::open(&options)?;
Expand All @@ -14,7 +19,7 @@ impl Find {

match index.find(self.sat.0)? {
Some(satpoint) => {
println!("{satpoint}");
print_json(Output { satpoint })?;
Ok(())
}
None => Err(anyhow!("sat has not been mined as of index height")),
Expand Down
25 changes: 16 additions & 9 deletions src/subcommand/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ pub(crate) struct Info {
transactions: bool,
}

#[derive(Serialize, Deserialize)]
pub struct TransactionsOutput {
pub start: u64,
pub end: u64,
pub count: u64,
pub elapsed: f64,
}

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

if self.transactions {
println!("start\tend\tcount\telapsed");

let mut output = Vec::new();
for window in info.transactions.windows(2) {
let start = &window[0];
let end = &window[1];
println!(
"{}\t{}\t{}\t{:.2}",
start.starting_block_count,
end.starting_block_count,
end.starting_block_count - start.starting_block_count,
(end.starting_timestamp - start.starting_timestamp) as f64 / 1000.0 / 60.0
);
output.push(TransactionsOutput {
start: start.starting_block_count,
end: end.starting_block_count,
count: end.starting_block_count - start.starting_block_count,
elapsed: (end.starting_timestamp - start.starting_timestamp) as f64 / 1000.0 / 60.0,
});
}
print_json(output)?;
} else {
print_json(info)?;
}
Expand Down
20 changes: 19 additions & 1 deletion src/subcommand/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ pub(crate) struct List {
outpoint: OutPoint,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub output: OutPoint,
pub start: u64,
pub size: u64,
pub rarity: Rarity,
pub name: String,
}

impl List {
pub(crate) fn run(self, options: Options) -> Result {
let index = Index::open(&options)?;
Expand All @@ -14,10 +23,19 @@ 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) {
println!("{output}\t{start}\t{size}\t{rarity}\t{name}");
outputs.push(Output {
output,
start,
size,
rarity,
name,
});
}

print_json(outputs)?;

Ok(())
}
Some(crate::index::List::Spent) => Err(anyhow!("output spent.")),
Expand Down
9 changes: 8 additions & 1 deletion src/subcommand/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ pub(crate) struct Parse {
object: Object,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub object: Object,
}

impl Parse {
pub(crate) fn run(self) -> Result {
println!("{}", self.object);
print_json(Output {
object: self.object,
})?;
Ok(())
}
}
13 changes: 12 additions & 1 deletion src/subcommand/subsidy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ pub(crate) struct Subsidy {
height: Height,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub first: u64,
pub subsidy: u64,
pub name: String,
}

impl Subsidy {
pub(crate) fn run(self) -> Result {
let first = self.height.starting_sat();
Expand All @@ -16,7 +23,11 @@ impl Subsidy {
bail!("block {} has no subsidy", self.height);
}

println!("{}\t{}\t{}", first, self.height.subsidy(), first.name());
print_json(Output {
first: first.0,
subsidy,
name: first.name(),
})?;

Ok(())
}
Expand Down
18 changes: 14 additions & 4 deletions src/subcommand/supply.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub supply: u64,
pub first: u64,
pub last: u64,
pub last_mined_in_block: u64,
}

pub(crate) fn run() -> Result {
let mut last = 0;

Expand All @@ -10,10 +18,12 @@ pub(crate) fn run() -> Result {
last += 1;
}

println!("supply: {}", Sat::SUPPLY);
println!("first: {}", 0);
println!("last: {}", Sat::SUPPLY - 1);
println!("last mined in block: {}", last);
print_json(Output {
supply: Sat::SUPPLY,
first: 0,
last: Sat::SUPPLY - 1,
last_mined_in_block: last,
})?;

Ok(())
}
88 changes: 26 additions & 62 deletions src/subcommand/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,35 @@ pub(crate) struct Traits {
sat: Sat,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub number: u64,
pub decimal: String,
pub degree: String,
pub name: String,
pub height: u64,
pub cycle: u64,
pub epoch: u64,
pub period: u64,
pub offset: u64,
pub rarity: Rarity,
}

impl Traits {
pub(crate) fn run(self) -> Result {
print!("{}", self);
Ok(())
}
}
print_json(Output {
number: self.sat.n(),
decimal: self.sat.decimal().to_string(),
degree: self.sat.degree().to_string(),
name: self.sat.name(),
height: self.sat.height().0,
cycle: self.sat.cycle(),
epoch: self.sat.epoch().0,
period: self.sat.period(),
offset: self.sat.third(),
rarity: self.sat.rarity(),
})?;

impl Display for Traits {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
writeln!(f, "number: {}", self.sat.n())?;
writeln!(f, "decimal: {}", self.sat.decimal())?;
writeln!(f, "degree: {}", self.sat.degree())?;
writeln!(f, "name: {}", self.sat.name())?;
writeln!(f, "height: {}", self.sat.height())?;
writeln!(f, "cycle: {}", self.sat.cycle())?;
writeln!(f, "epoch: {}", self.sat.epoch())?;
writeln!(f, "period: {}", self.sat.period())?;
writeln!(f, "offset: {}", self.sat.third())?;
writeln!(f, "rarity: {}", self.sat.rarity())?;
Ok(())
}
}

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

#[test]
fn first() {
assert_eq!(
Traits { sat: Sat(0) }.to_string(),
"\
number: 0
decimal: 0.0
degree: 0°0′0″0‴
name: nvtdijuwxlp
height: 0
cycle: 0
epoch: 0
period: 0
offset: 0
rarity: mythic
",
);
}

#[test]
fn last() {
assert_eq!(
Traits {
sat: Sat(2099999997689999)
}
.to_string(),
"\
number: 2099999997689999
decimal: 6929999.0
degree: 5°209999′1007″0‴
name: a
height: 6929999
cycle: 5
epoch: 32
period: 3437
offset: 0
rarity: uncommon
",
);
}
}
7 changes: 0 additions & 7 deletions tests/command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ impl CommandBuilder {
}
}

pub(crate) fn expected_stdout(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::String(expected_stdout.as_ref().to_owned()),
..self
}
}

pub(crate) fn stdout_regex(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::regex(expected_stdout.as_ref()),
Expand Down
Loading