Skip to content

Commit

Permalink
Merge pull request #355 from breez/ok300-dynamic-fees-swap-fees-table
Browse files Browse the repository at this point in the history
Store swap dynamic fees in separate table
  • Loading branch information
ok300 authored Aug 1, 2023
2 parents 9c7ef23 + aa35a5f commit 22169eb
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 86 deletions.
3 changes: 2 additions & 1 deletion libs/sdk-core/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ impl BackupWorker {

#[cfg(test)]
mod tests {
use crate::test_utils::get_test_ofp_48h;
use crate::{
backup::BackupRequest,
persist::db::SqliteStorage,
Expand Down Expand Up @@ -760,7 +761,7 @@ mod tests {
min_allowed_deposit: 0,
max_allowed_deposit: 100,
last_redeem_error: None,
channel_opening_fees: None,
channel_opening_fees: Some(get_test_ofp_48h(1, 1).into()),
};
persister.insert_swap(tested_swap_info).unwrap();
}
Expand Down
10 changes: 7 additions & 3 deletions libs/sdk-core/src/persist/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,14 @@ pub(crate) fn current_migrations() -> Vec<&'static str> {
created_at TEXT DEFAULT CURRENT_TIMESTAMP
) STRICT;
",
// Add dynamic fee params used when opening the swap channel, stored as JSON
// Swaps synchronization: Add sync table that stores the fees used in swaps
"
ALTER TABLE swaps_info ADD COLUMN channel_opening_fees TEXT;
",
CREATE TABLE IF NOT EXISTS sync.swaps_fees (
bitcoin_address TEXT PRIMARY KEY NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
channel_opening_fees TEXT NOT NULL
) STRICT;
",
]
}

Expand Down
52 changes: 38 additions & 14 deletions libs/sdk-core/src/persist/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::models::{SwapInfo, SwapStatus};
use super::db::{SqliteStorage, StringArray};
use crate::OpeningFeeParams;
use anyhow::{anyhow, Result};
use rusqlite::{named_params, OptionalExtension, Params, Row};
use rusqlite::{named_params, OptionalExtension, Params, Row, Transaction};

impl SqliteStorage {
pub(crate) fn insert_swap(&self, swap_info: SwapInfo) -> Result<()> {
Expand Down Expand Up @@ -50,9 +50,8 @@ impl SqliteStorage {
unconfirmed_sats,
unconfirmed_tx_ids,
confirmed_sats,
confirmed_tx_ids,
channel_opening_fees
) VALUES (:bitcoin_address, :status, :bolt11, :paid_sats, :unconfirmed_sats, :unconfirmed_tx_ids, :confirmed_sats, :confirmed_tx_ids, :channel_opening_fees)",
confirmed_tx_ids
) VALUES (:bitcoin_address, :status, :bolt11, :paid_sats, :unconfirmed_sats, :unconfirmed_tx_ids, :confirmed_sats, :confirmed_tx_ids)",
named_params! {
":bitcoin_address": swap_info.bitcoin_address,
":status": swap_info.status as i32,
Expand All @@ -62,9 +61,17 @@ impl SqliteStorage {
":unconfirmed_tx_ids": StringArray(swap_info.unconfirmed_tx_ids),
":confirmed_sats": swap_info.confirmed_sats,
":confirmed_tx_ids": StringArray(swap_info.confirmed_tx_ids),
":channel_opening_fees": swap_info.channel_opening_fees
},
)?;

Self::insert_swaps_fees(
&tx,
swap_info.bitcoin_address,
swap_info
.channel_opening_fees
.ok_or_else(|| anyhow!("Dynamic fees must be set when creating a new swap"))?,
)?;

tx.commit()?;
Ok(())
}
Expand Down Expand Up @@ -113,22 +120,37 @@ impl SqliteStorage {
Ok(())
}

pub(crate) fn update_swap_fees(
&self,
fn insert_swaps_fees(
tx: &Transaction,
bitcoin_address: String,
channel_opening_fees: Option<OpeningFeeParams>,
channel_opening_fees: OpeningFeeParams,
) -> Result<()> {
self.get_connection()?.execute(
"UPDATE swaps_info SET channel_opening_fees=:channel_opening_fees where bitcoin_address=:bitcoin_address",
tx.execute(
"INSERT OR REPLACE INTO sync.swaps_fees (bitcoin_address, created_at, channel_opening_fees) VALUES(:bitcoin_address, CURRENT_TIMESTAMP, :channel_opening_fees)",
named_params! {
":channel_opening_fees": channel_opening_fees,
":bitcoin_address": bitcoin_address,
":channel_opening_fees": channel_opening_fees,
},
)?;

Ok(())
}

/// Update the dynamic fees associated with a swap
pub(crate) fn update_swap_fees(
&self,
bitcoin_address: String,
channel_opening_fees: OpeningFeeParams,
) -> Result<()> {
let mut con = self.get_connection()?;
let tx = con.transaction()?;

Self::insert_swaps_fees(&tx, bitcoin_address, channel_opening_fees)?;

tx.commit()?;
Ok(())
}

pub(crate) fn insert_swap_refund_tx_ids(
&self,
bitcoin_address: String,
Expand Down Expand Up @@ -173,7 +195,7 @@ impl SqliteStorage {
"
SELECT
swaps.bitcoin_address as bitcoin_address,
created_at as created_at,
swaps.created_at as created_at,
lock_height as lock_height,
payment_hash as payment_hash,
preimage as preimage,
Expand All @@ -192,9 +214,10 @@ impl SqliteStorage {
unconfirmed_tx_ids as unconfirmed_tx_ids,
confirmed_tx_ids as confirmed_tx_ids,
last_redeem_error as last_redeem_error,
channel_opening_fees as channel_opening_fees
swaps_fees.channel_opening_fees as channel_opening_fees
FROM sync.swaps as swaps
LEFT JOIN swaps_info ON swaps.bitcoin_address = swaps_info.bitcoin_address
LEFT JOIN sync.swaps_fees as swaps_fees ON swaps.bitcoin_address = swaps_fees.bitcoin_address
LEFT JOIN sync.swap_refunds as swap_refunds ON swaps.bitcoin_address = swap_refunds.bitcoin_address
WHERE {}
",
Expand Down Expand Up @@ -299,6 +322,7 @@ impl SqliteStorage {
#[cfg(test)]
mod tests {
use crate::persist::db::SqliteStorage;
use crate::test_utils::get_test_ofp_48h;
use crate::{OpeningFeeParams, SwapInfo, SwapStatus};
use anyhow::Result;
use rusqlite::{named_params, Connection};
Expand Down Expand Up @@ -338,7 +362,7 @@ mod tests {
min_allowed_deposit: 0,
max_allowed_deposit: 100,
last_redeem_error: None,
channel_opening_fees: None,
channel_opening_fees: Some(get_test_ofp_48h(1, 1).into()),
};
storage.insert_swap(tested_swap_info.clone())?;
let item_value = storage.get_swap_info_by_address("1".to_string())?.unwrap();
Expand Down
Loading

0 comments on commit 22169eb

Please sign in to comment.