Skip to content

Commit

Permalink
I'm a genius
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Mar 2, 2025
1 parent 0430926 commit 2175a8a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/subcommand/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ impl Receive {
);
}

wallet.persist()?;

Ok(Some(Box::new(Output { addresses })))
}
}
7 changes: 7 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub(crate) struct Wallet {
has_rune_index: bool,
has_sat_index: bool,
ord_client: reqwest::blocking::Client,
persister: DatabasePersister,
rpc_url: Url,
settings: Settings,
pub(crate) wallet: PersistedWallet<DatabasePersister>,
Expand Down Expand Up @@ -126,6 +127,12 @@ impl Wallet {
Ok(descriptor)
}

pub(crate) fn persist(&mut self) -> Result {
self.wallet.persist(&mut self.persister)?;

Ok(())
}

pub(crate) fn get_wallet_sat_ranges(&self) -> Result<Vec<(OutPoint, Vec<(u64, u64)>)>> {
ensure!(
self.has_sat_index,
Expand Down
8 changes: 7 additions & 1 deletion src/wallet/database.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bdk_wallet::chain::Merge;

use super::*;

pub(crate) struct TransactionPersister<'a>(pub(crate) &'a mut WriteTransaction);
Expand All @@ -21,11 +23,15 @@ impl WalletPersister for TransactionPersister<'_> {
}

fn persist(persister: &mut Self, changeset: &ChangeSet) -> std::result::Result<(), Self::Error> {
let mut current = Self::initialize(persister)?;

current.merge(changeset.clone());

let wtx = &persister.0;

wtx
.open_table(CHANGESET)?
.insert((), serde_json::to_string(changeset)?.as_str())?;
.insert((), serde_json::to_string(&current)?.as_str())?;

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl WalletConstructor {
has_rune_index: status.rune_index,
has_sat_index: status.sat_index,
ord_client: self.ord_client,
persister,
rpc_url: self.rpc_url,
settings: self.settings,
wallet,
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn create_wallet(core: &mockcore::Handle, ord: &TestServer) -> Arc<TempDir> {
.core(core)
.ord(ord)
.stdout_regex(".*")
.stderr_regex(".*")
.run_and_extract_stdout();

tempdir
Expand Down
18 changes: 13 additions & 5 deletions tests/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ fn receive() {

let tempdir = create_wallet(&core, &ord);

let output = CommandBuilder::new("wallet receive")
.temp_dir(tempdir.clone())
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<receive::Output>();

let first_address = output.addresses.first().unwrap();

assert!(first_address.is_valid_for_network(Network::Bitcoin));

let output = CommandBuilder::new("wallet receive")
.temp_dir(tempdir)
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<receive::Output>();

assert!(output
.addresses
.first()
.unwrap()
.is_valid_for_network(Network::Bitcoin));
let second_address = output.addresses.first().unwrap();

assert!(second_address != first_address);
}

0 comments on commit 2175a8a

Please sign in to comment.