Skip to content

Commit

Permalink
Merge branch 'add-sat-and-number-to-wallet-inscriptions'
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Martin committed Apr 16, 2023
2 parents ad3dd2f + ccc1f69 commit 825d4fe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
48 changes: 37 additions & 11 deletions src/subcommand/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {super::*, crate::wallet::Wallet};

#[derive(Serialize, Deserialize)]
pub struct Output {
pub struct OutputWithSat {
pub sat: Sat,
pub number: u64,
pub inscription: InscriptionId,
Expand All @@ -10,10 +10,21 @@ pub struct Output {
pub amount: u64,
}

#[derive(Serialize, Deserialize)]
pub struct OutputWithoutSat {
pub number: u64,
pub inscription: InscriptionId,
pub location: SatPoint,
pub explorer: String,
pub amount: u64,
}

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

let index_has_sats = index.has_sat_index()?;

let inscriptions = index.get_inscriptions(None)?;
let unspent_outputs = index.get_unspent_outputs(Wallet::load(&options)?)?;

Expand All @@ -24,25 +35,40 @@ pub(crate) fn run(options: Options) -> Result {
Chain::Testnet => "https://testnet.ordinals.com/inscription/",
};

let mut output = Vec::new();
let mut output_with_sat = Vec::new();
let mut output_without_sat = Vec::new();

for (location, inscription) in inscriptions {
if unspent_outputs.contains_key(&location.outpoint) {
let entry = index
.get_inscription_entry(inscription)?
.ok_or_else(|| anyhow!("Inscription {inscription} not found"))?;
output.push(Output {
sat: entry.sat.unwrap(),
number: entry.number,
location,
inscription,
explorer: format!("{explorer}{inscription}"),
amount: unspent_outputs.get(&location.outpoint).unwrap().to_sat(),
});
if index_has_sats {
output_with_sat.push(OutputWithSat {
sat: entry.sat.unwrap(),
number: entry.number,
location,
inscription,
explorer: format!("{explorer}{inscription}"),
amount: unspent_outputs.get(&location.outpoint).unwrap().to_sat(),
});
} else {
output_without_sat.push(OutputWithoutSat {
number: entry.number,
location,
inscription,
explorer: format!("{explorer}{inscription}"),
amount: unspent_outputs.get(&location.outpoint).unwrap().to_sat(),
});
}
}
}

print_json(&output)?;
if index_has_sats {
print_json(&output_with_sat)?;
} else {
print_json(&output_without_sat)?;
}

Ok(())
}
8 changes: 4 additions & 4 deletions tests/wallet/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::*,
ord::subcommand::wallet::{inscriptions::Output, receive},
ord::subcommand::wallet::{inscriptions::OutputWithoutSat, receive},
};

#[test]
Expand All @@ -19,7 +19,7 @@ fn inscriptions() {

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

assert_eq!(output.len(), 1);
assert_eq!(output[0].inscription, inscription.parse().unwrap());
Expand All @@ -46,7 +46,7 @@ fn inscriptions() {

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

assert_eq!(output.len(), 1);
assert_eq!(output[0].inscription, inscription.parse().unwrap());
Expand Down Expand Up @@ -77,7 +77,7 @@ fn inscriptions_includes_locked_utxos() {

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

assert_eq!(output.len(), 1);
assert_eq!(output[0].inscription, inscription.parse().unwrap());
Expand Down

0 comments on commit 825d4fe

Please sign in to comment.