refactor: Edit ElectrumExt not to use WalletUpdate

This commit is contained in:
Vladimir Fomene
2023-08-21 11:20:38 +03:00
committed by 志宇
parent 2867e88b64
commit 6bcbb93233
3 changed files with 40 additions and 35 deletions

View File

@@ -8,7 +8,7 @@ use bdk_chain::{
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
indexed_tx_graph::{self, IndexedTxGraph},
keychain::WalletChangeSet,
local_chain::LocalChain,
local_chain::{self, LocalChain},
Append, ConfirmationHeightAnchor,
};
use bdk_electrum::{
@@ -269,25 +269,27 @@ fn main() -> anyhow::Result<()> {
.expect("must get time")
.as_secs();
let final_update = response.finalize(&client, Some(now), missing_txids)?;
let (graph_update, keychain_update, update_tip) =
response.finalize(&client, Some(now), missing_txids)?;
let db_changeset = {
let mut chain = chain.lock().unwrap();
let mut graph = graph.lock().unwrap();
let chain = chain.apply_update(final_update.chain)?;
let chain = chain.apply_update(local_chain::Update {
tip: update_tip,
introduce_older_blocks: true,
})?;
let indexed_tx_graph = {
let mut changeset =
indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
let (_, indexer) = graph
.index
.reveal_to_target_multi(&final_update.last_active_indices);
let (_, indexer) = graph.index.reveal_to_target_multi(&keychain_update);
changeset.append(indexed_tx_graph::ChangeSet {
indexer,
..Default::default()
});
changeset.append(graph.apply_update(final_update.graph));
changeset.append(graph.apply_update(graph_update));
changeset
};

View File

@@ -9,6 +9,7 @@ use std::str::FromStr;
use bdk::bitcoin::Address;
use bdk::SignOptions;
use bdk::{bitcoin::Network, Wallet};
use bdk_electrum::bdk_chain::{keychain::WalletUpdate, local_chain};
use bdk_electrum::electrum_client::{self, ElectrumApi};
use bdk_electrum::ElectrumExt;
use bdk_file_store::Store;
@@ -57,9 +58,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!();
let missing = electrum_update.missing_full_txs(wallet.as_ref());
let update = electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
let (graph_update, keychain_update, update_tip) =
electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
wallet.apply_update(update)?;
let wallet_update = WalletUpdate {
last_active_indices: keychain_update,
graph: graph_update,
chain: local_chain::Update {
tip: update_tip,
introduce_older_blocks: true,
},
};
wallet.apply_update(wallet_update)?;
wallet.commit()?;
let balance = wallet.get_balance();