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

Add ord wallet runics command #3734

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
pub mod balance;
mod batch_command;
pub mod cardinals;
pub mod runics;
pub mod create;
pub mod dump;
pub mod inscribe;
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
ldiego08 marked this conversation as resolved.
Show resolved Hide resolved
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_for_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 @@ -2,6 +2,7 @@ use super::*;

mod authentication;
mod balance;
mod runics;
mod batch_command;
mod cardinals;
mod create;
Expand Down
53 changes: 53 additions & 0 deletions tests/wallet/runics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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()
);


}
Loading