Skip to content

Commit

Permalink
feat: added OngoingSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
yse committed Mar 27, 2024
1 parent 076badc commit 3fb13c7
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 83 deletions.
3 changes: 2 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ mod tests {
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(())
}
Expand Down
59 changes: 43 additions & 16 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,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 +133,31 @@ 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,
},
}
}
}

#[derive(Debug)]
pub struct PreparePaymentResponse {
pub id: String,
pub funding_amount: u64,
pub funding_address: String,
}
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;",
]
}
157 changes: 117 additions & 40 deletions lib/src/persist/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod migrations;

use anyhow::Result;
use rusqlite::{params, Connection, Row};
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};

use crate::OngoingReceiveSwap;
use crate::OngoingSwap;

use migrations::current_migrations;

Expand All @@ -13,7 +13,7 @@ pub(crate) struct Persister {
}

impl Persister {
pub fn new(working_dir: String) -> Self {
pub fn new(working_dir: &str) -> Self {
let main_db_file = format!("{}/storage.sql", working_dir);
Persister { main_db_file }
}
Expand All @@ -35,67 +35,144 @@ impl Persister {
Ok(())
}

pub fn insert_ongoing_swaps(&self, swaps: &[OngoingReceiveSwap]) -> Result<()> {
pub fn insert_ongoing_swap(&self, swaps: &[OngoingSwap]) -> Result<()> {
let con = self.get_connection()?;

let mut stmt = con.prepare(
"
INSERT INTO ongoing_swaps (
for swap in swaps {
match swap {
OngoingSwap::Send {
id,
funding_address,
amount_sat,
} => {
let mut stmt = con.prepare(
"
INSERT INTO ongoing_send_swaps (
id,
amount_sat,
funding_address
)
VALUES (?, ?, ?)
",
)?;

_ = stmt.execute((&id, &amount_sat, &funding_address))?
}
OngoingSwap::Receive {
id,
preimage,
redeem_script,
blinding_key,
invoice_amount_sat,
onchain_amount_sat
)
VALUES (?, ?, ?, ?, ?, ?)
",
)?;

for swap in swaps {
_ = stmt.execute((
&swap.id,
&swap.preimage,
&swap.redeem_script,
&swap.blinding_key,
&swap.invoice_amount_sat,
&swap.onchain_amount_sat,
))?
onchain_amount_sat,
} => {
let mut stmt = con.prepare(
"
INSERT INTO ongoing_receive_swaps (
id,
preimage,
redeem_script,
blinding_key,
invoice_amount_sat,
onchain_amount_sat
)
VALUES (?, ?, ?, ?, ?, ?)
",
)?;

_ = stmt.execute((
&id,
&preimage,
&redeem_script,
&blinding_key,
&invoice_amount_sat,
&onchain_amount_sat,
))?
}
}
}

Ok(())
}

pub fn resolve_ongoing_swap(&self, id: String) -> Result<()> {
let con = self.get_connection()?;
pub fn resolve_ongoing_swap(&self, id: &str) -> Result<()> {
let mut con = self.get_connection()?;

con.prepare("DELETE FROM ongoing_swaps WHERE id = ?")?
.execute(params![id])?;
let tx = con.transaction()?;
tx.execute("DELETE FROM ongoing_send_swaps WHERE id = ?", params![id])?;
tx.execute(
"DELETE FROM ongoing_receive_swaps WHERE id = ?",
params![id],
)?;
tx.commit()?;

Ok(())
}

pub fn list_ongoing_swaps(&self) -> Result<Vec<OngoingReceiveSwap>> {
pub fn list_ongoing_swaps(&self) -> Result<Vec<OngoingSwap>> {
let con = self.get_connection()?;
let mut ongoing_swaps = self.list_ongoing_send(&con)?;
ongoing_swaps.append(&mut self.list_ongoing_receive(&con)?);
Ok(ongoing_swaps)
}

let mut stmt = con.prepare("SELECT * FROM ongoing_swaps")?;
fn list_ongoing_send(&self, con: &Connection) -> Result<Vec<OngoingSwap>, rusqlite::Error> {
let mut stmt = con.prepare(
"
SELECT
id,
amount_sat,
funding_address,
created_at
FROM ongoing_send_swaps
ORDER BY created_at
",
)?;

let swaps: Vec<OngoingReceiveSwap> = stmt
.query_map(params![], |row| self.sql_row_to_swap(row))?
let ongoing_send = stmt
.query_map(params![], |row| {
Ok(OngoingSwap::Send {
id: row.get(0)?,
amount_sat: row.get(1)?,
funding_address: row.get(2)?,
})
})?
.map(|i| i.unwrap())
.collect();

Ok(swaps)
Ok(ongoing_send)
}

fn sql_row_to_swap(&self, row: &Row) -> Result<OngoingReceiveSwap, rusqlite::Error> {
Ok(OngoingReceiveSwap {
id: row.get(0)?,
preimage: row.get(1)?,
redeem_script: row.get(2)?,
blinding_key: row.get(3)?,
invoice_amount_sat: row.get(4)?,
onchain_amount_sat: row.get(5)?,
})
fn list_ongoing_receive(&self, con: &Connection) -> Result<Vec<OngoingSwap>, rusqlite::Error> {
let mut stmt = con.prepare(
"
SELECT
id,
preimage,
redeem_script,
blinding_key,
invoice_amount_sat,
onchain_amount_sat,
created_at
FROM ongoing_receive_swaps
ORDER BY created_at
",
)?;

let ongoing_receive = stmt
.query_map(params![], |row| {
Ok(OngoingSwap::Receive {
id: row.get(0)?,
preimage: row.get(1)?,
redeem_script: row.get(2)?,
blinding_key: row.get(3)?,
invoice_amount_sat: row.get(4)?,
onchain_amount_sat: row.get(5)?,
})
})?
.map(|i| i.unwrap())
.collect();

Ok(ongoing_receive)
}
}
Loading

0 comments on commit 3fb13c7

Please sign in to comment.