Skip to content

Commit

Permalink
Add ord wallet runics command (ordinals#3734)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldiego08 authored May 17, 2024
1 parent 60b2fa1 commit 917b65c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod outputs;
pub mod receive;
pub mod restore;
pub mod resume;
pub mod runics;
pub mod sats;
pub mod send;
mod shared_args;
Expand Down Expand Up @@ -47,6 +48,8 @@ pub(crate) enum Subcommand {
Batch(batch_command::Batch),
#[command(about = "List unspent cardinal outputs in wallet")]
Cardinals,
#[command(about = "List unspent runic outputs in wallet")]
Runics,
#[command(about = "Create new wallet")]
Create(create::Create),
#[command(about = "Dump wallet descriptors")]
Expand Down Expand Up @@ -101,6 +104,7 @@ impl WalletCommand {
Subcommand::Balance => balance::run(wallet),
Subcommand::Batch(batch) => batch.run(wallet),
Subcommand::Cardinals => cardinals::run(wallet),
Subcommand::Runics => runics::run(wallet),
Subcommand::Create(_) | Subcommand::Restore(_) => unreachable!(),
Subcommand::Dump => dump::run(wallet),
Subcommand::Inscribe(inscribe) => inscribe.run(wallet),
Expand Down
44 changes: 44 additions & 0 deletions src/subcommand/wallet/runics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use super::*;

#[derive(Serialize, Deserialize)]
pub struct RunicUtxo {
pub output: OutPoint,
pub runes: BTreeMap<SpacedRune, Decimal>,
}

pub(crate) fn run(wallet: Wallet) -> SubcommandResult {
let unspent_outputs = wallet.utxos();
let runic_utxos = wallet.get_runic_outputs()?;

let runic_utxos = unspent_outputs
.iter()
.filter_map(|(output, _)| {
if runic_utxos.contains(output) {
let rune_balances = wallet.get_runes_balances_in_output(output).ok()?;
let mut runes = BTreeMap::new();

for (spaced_rune, pile) in rune_balances {
runes
.entry(spaced_rune)
.and_modify(|decimal: &mut Decimal| {
assert_eq!(decimal.scale, pile.divisibility);
decimal.value += pile.amount;
})
.or_insert(Decimal {
value: pile.amount,
scale: pile.divisibility,
});
}

Some(RunicUtxo {
output: *output,
runes,
})
} else {
None
}
})
.collect::<Vec<RunicUtxo>>();

Ok(Some(Box::new(runic_utxos)))
}
1 change: 1 addition & 0 deletions tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod receive;
mod restore;
#[cfg(unix)]
mod resume;
mod runics;
mod sats;
mod selection;
mod send;
Expand Down
54 changes: 54 additions & 0 deletions tests/wallet/runics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use {
super::*,
ord::{decimal::Decimal, subcommand::wallet::runics::RunicUtxo},
};

#[test]
fn wallet_runics() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord = TestServer::spawn_with_server_args(&core, &["--regtest", "--index-runes"], &[]);

create_wallet(&core, &ord);

let rune = Rune(RUNE);

batch(
&core,
&ord,
batch::File {
etching: Some(batch::Etching {
divisibility: 0,
premine: "1000".parse().unwrap(),
rune: SpacedRune { rune, spacers: 1 },
supply: "1000".parse().unwrap(),
symbol: '¢',
terms: None,
turbo: false,
}),
inscriptions: vec![batch::Entry {
file: Some("inscription.jpeg".into()),
..default()
}],
..default()
},
);

pretty_assert_eq!(
CommandBuilder::new("--regtest --index-runes wallet runics")
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<Vec<RunicUtxo>>()
.first()
.unwrap()
.runes,
vec![(
SpacedRune { rune, spacers: 1 },
Decimal {
value: 1000,
scale: 0
}
)]
.into_iter()
.collect()
);
}

0 comments on commit 917b65c

Please sign in to comment.