Skip to content

Commit

Permalink
Exclude inscribed utxos when calculating wallet balance (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Jan 23, 2023
1 parent 7d13d1f commit fa25d58
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/subcommand/wallet/balance.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
use super::*;
use std::collections::BTreeSet;

pub(crate) fn run(options: Options) -> Result {
println!(
"{}",
options
.bitcoin_rpc_client_for_wallet_command(false)?
.get_balances()?
.mine
.trusted
.to_sat()
);
let index = Index::open(&options)?;
index.update()?;

let inscription_outputs = index
.get_inscriptions(None)?
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<BTreeSet<OutPoint>>();

let mut balance = 0;
for (outpoint, amount) in get_unspent_outputs(&options)? {
if !inscription_outputs.contains(&outpoint) {
balance += amount.to_sat()
}
}

println!("{}", balance);

Ok(())
}
18 changes: 18 additions & 0 deletions tests/wallet/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,21 @@ fn wallet_balance() {
.expected_stdout("5000000000\n")
.run();
}

#[test]
fn wallet_balance_only_counts_cardinal_utxos() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);

CommandBuilder::new("wallet balance")
.rpc_server(&rpc_server)
.expected_stdout("0\n")
.run();

inscribe(&rpc_server);

CommandBuilder::new("wallet balance")
.rpc_server(&rpc_server)
.expected_stdout(format!("{}\n", 100 * COIN_VALUE - 10_000))
.run();
}

0 comments on commit fa25d58

Please sign in to comment.