Skip to content

Commit

Permalink
feat: add support for pending send (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Mar 27, 2024
1 parent 076badc commit 21f6d70
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 124 deletions.
7 changes: 4 additions & 3 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustyline::history::DefaultHistory;
use rustyline::Editor;
use rustyline::{hint::HistoryHinter, Completer, Helper, Hinter, Validator};

use breez_sdk_liquid::{ReceivePaymentRequest, Wallet};
use breez_sdk_liquid::{ReceivePaymentRequest, SendPaymentResponse, Wallet};

#[derive(Parser, Debug, Clone, PartialEq)]
pub(crate) enum Command {
Expand Down Expand Up @@ -61,13 +61,14 @@ pub(crate) fn handle_command(
))
}
Command::SendPayment { bolt11 } => {
let response = wallet.send_payment(&bolt11)?;
let prepare_response = wallet.prepare_payment(&bolt11)?;
let SendPaymentResponse { txid } = wallet.send_payment(&prepare_response)?;

Ok(format!(
r#"
Successfully paid the invoice!
You can view the onchain transaction at https://blockstream.info/liquidtestnet/tx/{}"#,
response.txid
txid
))
}
Command::GetInfo => {
Expand Down
11 changes: 7 additions & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod tests {
use anyhow::Result;
use bip39::{Language, Mnemonic};

use crate::{ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR};
use crate::{Network, ReceivePaymentRequest, Wallet, DEFAULT_DATA_DIR};

const PHRASE_FILE_NAME: &str = "phrase";

Expand Down Expand Up @@ -56,20 +56,23 @@ mod tests {

#[test]
fn normal_submarine_swap() -> Result<()> {
let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?;
let breez_wallet =
Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?;

let mut invoice = String::new();
println!("Please paste the invoice to be paid: ");
io::stdin().read_line(&mut invoice)?;

breez_wallet.send_payment(&invoice)?;
let prepare_response = breez_wallet.prepare_payment(&invoice)?;
breez_wallet.send_payment(&prepare_response)?;

Ok(())
}

#[test]
fn reverse_submarine_swap_success() -> Result<()> {
let breez_wallet = Wallet::init(get_mnemonic()?.to_string())?;
let breez_wallet =
Wallet::init(&get_mnemonic()?.to_string(), None, Network::LiquidTestnet)?;

let swap_response = breez_wallet.receive_payment(ReceivePaymentRequest {
onchain_amount_sat: Some(1000),
Expand Down
84 changes: 54 additions & 30 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use boltz_client::network::Chain;
use lwk_signer::SwSigner;
use lwk_wollet::{ElectrumUrl, ElementsNetwork, WolletDescriptor};

#[derive(Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Network {
Liquid,
LiquidTestnet,
Expand All @@ -27,6 +27,7 @@ impl From<Network> for Chain {
}
}

#[derive(Debug)]
pub struct WalletOptions {
pub signer: SwSigner,
pub network: Network,
Expand All @@ -42,28 +43,31 @@ pub struct WalletOptions {
}

#[derive(Debug)]
pub struct SwapLbtcResponse {
pub id: String,
pub invoice: String,
pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
}

pub enum SwapStatus {
Created,
Mempool,
Completed,
#[derive(Debug)]
pub struct ReceivePaymentResponse {
pub id: String,
pub invoice: String,
}

pub struct ReceivePaymentRequest {
pub invoice_amount_sat: Option<u64>,
pub onchain_amount_sat: Option<u64>,
#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}

#[derive(Debug)]
pub struct SendPaymentResponse {
pub txid: String,
}

#[derive(thiserror::Error, Debug)]
pub enum SwapError {
pub enum PaymentError {
#[error("Invoice amount is out of range")]
AmountOutOfRange,

Expand All @@ -86,9 +90,9 @@ pub enum SwapError {
BoltzGeneric { err: String },
}

impl From<Error> for SwapError {
impl From<Error> for PaymentError {
fn from(err: Error) -> Self {
SwapError::BoltzGeneric {
PaymentError::BoltzGeneric {
err: format!("{err:?}"),
}
}
Expand All @@ -102,29 +106,28 @@ pub struct WalletInfo {
}

#[derive(Debug)]
pub struct OngoingReceiveSwap {
pub id: String,
pub preimage: String,
pub redeem_script: String,
pub blinding_key: String,
pub invoice_amount_sat: u64,
pub onchain_amount_sat: u64,
}

pub struct OngoingSendSwap {
pub id: String,
// pub preimage: String,
// pub redeem_script: String,
// pub blinding_key: String,
// pub invoice_amount_sat: Option<u64>,
// pub onchain_amount_sat: Option<u64>,
pub(crate) enum OngoingSwap {
Send {
id: String,
amount_sat: u64,
funding_address: String,
},
Receive {
id: String,
preimage: String,
redeem_script: String,
blinding_key: String,
invoice_amount_sat: u64,
onchain_amount_sat: u64,
},
}

#[derive(Debug)]
pub enum PaymentType {
Sent,
Received,
PendingReceive,
PendingSend,
}

#[derive(Debug)]
Expand All @@ -134,3 +137,24 @@ pub struct Payment {
pub amount_sat: u64,
pub payment_type: PaymentType,
}

impl From<OngoingSwap> for Payment {
fn from(swap: OngoingSwap) -> Self {
match swap {
OngoingSwap::Send { amount_sat, .. } => Payment {
id: None,
timestamp: None,
payment_type: PaymentType::PendingSend,
amount_sat,
},
OngoingSwap::Receive {
onchain_amount_sat, ..
} => Payment {
id: None,
timestamp: None,
payment_type: PaymentType::PendingReceive,
amount_sat: onchain_amount_sat,
},
}
}
}
11 changes: 9 additions & 2 deletions lib/src/persist/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
pub(crate) fn current_migrations() -> Vec<&'static str> {
vec![
"CREATE TABLE IF NOT EXISTS ongoing_swaps (
"CREATE TABLE IF NOT EXISTS ongoing_receive_swaps (
id TEXT NOT NULL PRIMARY KEY,
preimage TEXT NOT NULL,
redeem_script TEXT NOT NULL,
blinding_key TEXT NOT NULL,
invoice_amount_sat INTEGER NOT NULL,
onchain_amount_sat INTEGER NOT NULL
onchain_amount_sat INTEGER NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
) STRICT;",
"CREATE TABLE IF NOT EXISTS ongoing_send_swaps (
id TEXT NOT NULL PRIMARY KEY,
amount_sat INTEGER NOT NULL,
funding_address TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
) STRICT;",
]
}
Loading

0 comments on commit 21f6d70

Please sign in to comment.