Skip to content

Commit aa8876d

Browse files
committed
refactor(main): add runtime wallet module
- add wallet runtime module to serve as context manager - fix clippy errors
1 parent 63162be commit aa8876d

14 files changed

Lines changed: 282 additions & 214 deletions

File tree

src/client.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(feature = "rpc")]
2-
use bdk_bitcoind_rpc::{Emitter, bitcoincore_rpc::RpcApi};
31
#[cfg(feature = "esplora")]
42
use bdk_esplora::EsploraAsyncExt;
53
#[cfg(any(
@@ -14,11 +12,15 @@ use {
1412
bdk_wallet::{
1513
Wallet,
1614
bitcoin::{Transaction, Txid},
17-
chain::CanonicalizationParams,
1815
},
1916
clap::ValueEnum,
2017
std::path::PathBuf,
2118
};
19+
#[cfg(feature = "rpc")]
20+
use {
21+
bdk_bitcoind_rpc::{Emitter, bitcoincore_rpc::RpcApi},
22+
bdk_wallet::chain::CanonicalizationParams,
23+
};
2224

2325
#[cfg(feature = "cbf")]
2426
use {
@@ -79,7 +81,7 @@ pub(crate) enum BlockchainClient {
7981
impl BlockchainClient {
8082
pub async fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
8183
match self {
82-
// #[cfg(feature = "electrum")]
84+
#[cfg(feature = "electrum")]
8385
Self::Electrum { client, .. } => client
8486
.transaction_broadcast(&tx)
8587
.map_err(|e| Error::Generic(e.to_string())),

src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub enum CliSubCommand {
116116
#[cfg(feature = "compiler")]
117117
#[clap(long_about = "Miniscript policy compiler")]
118118
Compile(CompileCommand),
119-
// #[cfg(feature = "repl")]
119+
#[cfg(feature = "repl")]
120120
/// REPL command loop mode.
121121
///
122122
/// REPL command loop can be used to make recurring callbacks to an already loaded wallet.

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum BDKCliError {
6161
#[error("PsbtError: {0}")]
6262
PsbtError(#[from] bdk_wallet::bitcoin::psbt::Error),
6363

64-
// #[cfg(feature = "sqlite")]
64+
#[cfg(feature = "sqlite")]
6565
#[error("Rusqlite error: {0}")]
6666
RusqliteError(Box<bdk_wallet::rusqlite::Error>),
6767

src/handlers/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ pub trait AppCommand<C> {
102102
))]
103103
pub trait AsyncAppCommand<C> {
104104
type Output: FormatOutput;
105-
105+
106106
async fn execute(&self, ctx: &mut C) -> Result<Self::Output, Error>;
107107
}
108-
109-
// context for online and online
110-
// => cli.rs
111-
// handlers/{mod for commands}
112-
// wallet subdir /
113-
// wallet-offline and wallet-online (client mod)

src/handlers/offline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for CombinePsbtCommand {
522522
}
523523
}
524524

525-
// #[cfg(feature = "bip322")]
525+
#[cfg(feature = "bip322")]
526526
#[derive(Debug, Parser, Clone, PartialEq)]
527527
pub struct SignMessageCommand {
528528
/// The message to sign
@@ -572,7 +572,7 @@ impl AppCommand<AppContext<OfflineOperations<'_>>> for SignMessageCommand {
572572
}
573573
}
574574

575-
// #[cfg(feature = "bip322")]
575+
#[cfg(feature = "bip322")]
576576
#[derive(Debug, Parser, Clone, PartialEq)]
577577
pub struct VerifyMessageCommand {
578578
/// The signature proof to verify

src/handlers/online.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for FullScanCommand {
170170
sync_kyoto_client(wallet, client).await?;
171171
}
172172
}
173-
Ok(StatusResult::new("Full scan completed successfully."))
173+
Ok(StatusResult {
174+
message: "Full scan completed successfully.".to_string(),
175+
})
174176
}
175177
}
176178

@@ -190,7 +192,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for SyncCommand {
190192
&self,
191193
ctx: &mut AppContext<OnlineOperations<'_>>,
192194
) -> Result<Self::Output, Error> {
193-
let mut wallet = &mut ctx.state.wallet;
195+
let wallet = &mut ctx.state.wallet;
194196
let client = ctx.state.client;
195197
#[cfg(any(feature = "electrum", feature = "esplora"))]
196198
let request = wallet
@@ -267,12 +269,14 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for SyncCommand {
267269
let mempool_txs = emitter.mempool()?;
268270
wallet.apply_unconfirmed_txs(mempool_txs.update);
269271
}
270-
// #[cfg(feature = "cbf")]
271-
KyotoClient { client } => sync_kyoto_client(&mut wallet, client)
272+
#[cfg(feature = "cbf")]
273+
KyotoClient { client } => sync_kyoto_client(wallet, client)
272274
.await
273275
.map_err(|e| Error::Generic(e.to_string()))?,
274276
}
275-
Ok(StatusResult::new("Wallet synced successfully."))
277+
Ok(StatusResult {
278+
message: "Wallet synced successfully.".to_string(),
279+
})
276280
}
277281
}
278282

@@ -321,7 +325,7 @@ impl AsyncAppCommand<AppContext<OnlineOperations<'_>>> for BroadcastCommand {
321325
psbt.extract_tx()?
322326
}
323327
(None, Some(tx)) => {
324-
let tx_bytes = Vec::<u8>::from_hex(&tx)?;
328+
let tx_bytes = Vec::<u8>::from_hex(tx)?;
325329
Transaction::consensus_decode(&mut tx_bytes.as_slice())?
326330
}
327331
(Some(_), Some(_)) => {

src/handlers/repl.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use bdk_wallet::{Wallet, bitcoin::Network};
2-
3-
#[cfg(feature = "repl")]
4-
use crate::handlers::AppCommand;
5-
#[cfg(feature = "repl")]
6-
use crate::{handlers::AppContext, utils::output::FormatOutput};
7-
81
#[cfg(feature = "repl")]
9-
use crate::commands::ReplSubCommand;
10-
use clap::Parser;
2+
use {
3+
crate::commands::ReplSubCommand,
4+
crate::handlers::{AppCommand, AppContext},
5+
crate::utils::output::FormatOutput,
6+
bdk_wallet::{Wallet, bitcoin::Network},
7+
clap::Parser,
8+
};
119

1210
#[cfg(any(
1311
feature = "electrum",
@@ -17,11 +15,7 @@ use clap::Parser;
1715
))]
1816
use crate::client::BlockchainClient;
1917
#[cfg(feature = "repl")]
20-
use std::io::Write;
21-
use {
22-
crate::commands::{CliOpts, WalletSubCommand},
23-
crate::error::BDKCliError as Error,
24-
};
18+
use {crate::commands::WalletSubCommand, crate::error::BDKCliError as Error, std::io::Write};
2519

2620
#[cfg(feature = "repl")]
2721
pub(crate) async fn respond(
@@ -36,7 +30,6 @@ pub(crate) async fn respond(
3630
client: Option<&BlockchainClient>,
3731
line: &str,
3832
datadir: std::path::PathBuf,
39-
_cli_opts: &CliOpts,
4033
) -> Result<bool, String> {
4134
let args = shlex::split(line).ok_or("error: Invalid quoting".to_string())?;
4235

0 commit comments

Comments
 (0)