SDK events framework (#193)

* Add events framework

* Adapt RN codegen and add synced event

* Only use get_connection internally
This commit is contained in:
Ross Savage
2024-05-25 06:20:14 +02:00
committed by GitHub
parent 5143aeb1dd
commit 06b848a8f3
58 changed files with 2527 additions and 376 deletions

View File

@@ -38,6 +38,8 @@ pub(crate) enum Command {
ListPayments,
/// Get the balance and general info of the current instance
GetInfo,
/// Sync local data with mempool and onchain data
Sync,
/// Empties the encrypted transaction cache
EmptyCache,
/// Backs up the current pending swaps
@@ -89,7 +91,7 @@ macro_rules! wait_confirmation {
};
}
pub(crate) fn handle_command(
pub(crate) async fn handle_command(
_rl: &mut Editor<CliHelper, DefaultHistory>,
sdk: &Arc<LiquidSdk>,
command: Command,
@@ -116,8 +118,9 @@ pub(crate) fn handle_command(
result
}
Command::SendPayment { bolt11, delay } => {
let prepare_response =
sdk.prepare_send_payment(&PrepareSendRequest { invoice: bolt11 })?;
let prepare_response = sdk
.prepare_send_payment(&PrepareSendRequest { invoice: bolt11 })
.await?;
wait_confirmation!(
format!(
@@ -131,23 +134,27 @@ pub(crate) fn handle_command(
let sdk_cloned = sdk.clone();
let prepare_cloned = prepare_response.clone();
thread::spawn(move || {
tokio::spawn(async move {
thread::sleep(Duration::from_secs(delay));
sdk_cloned.send_payment(&prepare_cloned).unwrap();
sdk_cloned.send_payment(&prepare_cloned).await.unwrap();
});
command_result!(prepare_response)
} else {
let response = sdk.send_payment(&prepare_response)?;
let response = sdk.send_payment(&prepare_response).await?;
command_result!(response)
}
}
Command::GetInfo => {
command_result!(sdk.get_info(GetInfoRequest { with_scan: true })?)
command_result!(sdk.get_info(GetInfoRequest { with_scan: true }).await?)
}
Command::ListPayments => {
let payments = sdk.list_payments()?;
command_result!(payments)
}
Command::Sync => {
sdk.sync().await?;
command_result!("Synced successfully")
}
Command::EmptyCache => {
sdk.empty_wallet_cache()?;
command_result!("Cache emptied successfully")

View File

@@ -45,7 +45,16 @@ fn show_results(result: Result<String>) -> Result<()> {
Ok(println!("{result_str}"))
}
fn main() -> Result<()> {
struct CliEventListener {}
impl EventListener for CliEventListener {
fn on_event(&self, e: LiquidSdkEvent) {
info!("Received event: {:?}", e);
}
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
let data_dir_str = args.data_dir.unwrap_or(DEFAULT_DATA_DIR.to_string());
@@ -66,6 +75,8 @@ fn main() -> Result<()> {
env_logger::builder()
.target(env_logger::Target::Pipe(Box::new(log_file)))
.filter(None, log::LevelFilter::Debug)
.filter(Some("rustyline"), log::LevelFilter::Warn)
.init();
let persistence = CliPersistence { data_dir };
@@ -86,7 +97,11 @@ fn main() -> Result<()> {
mnemonic: mnemonic.to_string(),
data_dir: Some(data_dir_str),
network,
})?;
})
.await?;
let listener_id = sdk
.add_event_listener(Box::new(CliEventListener {}))
.await?;
let cli_prompt = match network {
Network::Liquid => "breez-liquid-cli [mainnet]> ",
@@ -105,7 +120,7 @@ fn main() -> Result<()> {
println!("{}", cli_res.unwrap_err());
continue;
}
let res = handle_command(rl, &sdk, cli_res.unwrap());
let res = handle_command(rl, &sdk, cli_res.unwrap()).await;
show_results(res)?;
}
Err(ReadlineError::Interrupted) => {
@@ -123,5 +138,6 @@ fn main() -> Result<()> {
}
}
sdk.remove_event_listener(listener_id).await?;
rl.save_history(history_file).map_err(|e| anyhow!(e))
}