@@ -110,9 +110,8 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
110110
111111 let client = new_blockchain_client ( & wallet_opts, & wallet, database_path) ?;
112112
113- let mut ctx = AppContext :: new ( network, home_dir)
114- . with_wallet ( & mut wallet)
115- . with_client ( & client) ;
113+ let mut ctx =
114+ AppContext :: new_online_wallet ( network, home_dir, & mut wallet, & client) ;
116115
117116 online_cmd. execute ( & mut ctx) . await ?;
118117 }
@@ -142,7 +141,7 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
142141
143142 let mut wallet = new_persisted_wallet ( network, & mut persister, & wallet_opts) ?;
144143
145- let mut ctx = AppContext :: new ( network, home_dir) . with_wallet ( & mut wallet) ;
144+ let mut ctx = AppContext :: new_offline_wallet ( network, home_dir, & mut wallet) ;
146145
147146 offline_cmd. execute ( & mut ctx) ?;
148147 }
@@ -169,7 +168,86 @@ async fn run(cli_opts: CliOpts) -> Result<(), Error> {
169168 let mut ctx = AppContext :: new ( cli_opts. network , home_dir) ;
170169 cmd. execute ( & mut ctx) ?. write_out ( std:: io:: stdout ( ) ) ?;
171170 }
172- CliSubCommand :: Repl { wallet : _ } => todo ! ( ) ,
171+ CliSubCommand :: Repl {
172+ wallet : wallet_name,
173+ } => {
174+ #[ cfg( feature = "repl" ) ]
175+ {
176+ let ( wallet_opts, network) = load_wallet_config ( & home_dir, & wallet_name) ?;
177+ let database_path = prepare_wallet_db_dir ( & home_dir, & wallet_name) ?;
178+
179+ #[ cfg( any( feature = "sqlite" , feature = "redb" ) ) ]
180+ let mut persister: Persister = match & wallet_opts. database_type {
181+ #[ cfg( feature = "sqlite" ) ]
182+ crate :: persister:: DatabaseType :: Sqlite => {
183+ let db_file = database_path. join ( "wallet.sqlite" ) ;
184+ let connection = bdk_wallet:: rusqlite:: Connection :: open ( db_file) ?;
185+ Persister :: Connection ( connection)
186+ }
187+ #[ cfg( feature = "redb" ) ]
188+ crate :: persister:: DatabaseType :: Redb => {
189+ use crate :: persister:: Persister ;
190+ let db = std:: sync:: Arc :: new ( bdk_redb:: redb:: Database :: create (
191+ home_dir. join ( "wallet.redb" ) ,
192+ ) ?) ;
193+ let store = RedbStore :: new ( db, wallet_name. clone ( ) ) ?;
194+ Persister :: RedbStore ( store)
195+ }
196+ } ;
197+
198+ let mut wallet = new_persisted_wallet ( network, & mut persister, & wallet_opts) ?;
199+
200+ #[ cfg( any(
201+ feature = "electrum" ,
202+ feature = "esplora" ,
203+ feature = "rpc" ,
204+ feature = "cbf"
205+ ) ) ]
206+ let client = Some ( new_blockchain_client ( & wallet_opts, & wallet, database_path) ?) ;
207+
208+ println ! (
209+ "Entering REPL mode for wallet '{}'. Type 'exit' to quit." ,
210+ wallet_name
211+ ) ;
212+
213+ loop {
214+ let line = crate :: handlers:: repl:: readline ( ) ?;
215+ if line. trim ( ) . is_empty ( ) {
216+ continue ;
217+ }
218+
219+ // Pass it to our newly refactored respond function
220+ let should_exit = crate :: handlers:: repl:: respond (
221+ network,
222+ & mut wallet,
223+ #[ cfg( any(
224+ feature = "electrum" ,
225+ feature = "esplora" ,
226+ feature = "rpc" ,
227+ feature = "cbf"
228+ ) ) ]
229+ client. as_ref ( ) ,
230+ & line,
231+ home_dir. clone ( ) ,
232+ & cli_opts,
233+ )
234+ . await
235+ . map_err ( Error :: Generic ) ?;
236+
237+ // Break the loop if the user typed `exit`
238+ if should_exit {
239+ break ;
240+ }
241+ }
242+ }
243+
244+ #[ cfg( not( feature = "repl" ) ) ]
245+ {
246+ return Err ( Error :: Generic (
247+ "The 'repl' feature is not enabled in this build." . into ( ) ,
248+ ) ) ;
249+ }
250+ }
173251 CliSubCommand :: Completions { shell } => {
174252 shell;
175253 }
0 commit comments