Skip to content

Commit

Permalink
feat: add invoice_amount_sat field to swaps (#33)
Browse files Browse the repository at this point in the history
* fix: rebasing and updating cargo.lock

* feat: add `requested-amount-sat` field to pending swaps

* lint

* feat: added list-payments method

* linting

* feat: adding pending transactions to list-payments

* fix: rename received_amount to invoice_amount

* fix: renaming Pending to PendingReceive

* fix: remove expect from preimage unwrapping
  • Loading branch information
hydra-yse authored Mar 21, 2024
1 parent d254496 commit 03c2c39
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 82 deletions.
71 changes: 2 additions & 69 deletions cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub(crate) enum Command {
SendPayment { bolt11: String },
/// Receive lbtc and send btc through a swap
ReceivePayment { amount_sat: u64 },
/// List incoming and outgoing payments
ListPayments,
/// Get the balance of the currently loaded wallet
GetInfo,
}
Expand Down Expand Up @@ -56,13 +58,34 @@ pub(crate) async fn handle_command(
response.txid
))
}
Command::GetInfo {} => {
Command::GetInfo => {
let info = wallet.get_info(true)?;

Ok(format!(
"Current Balance: {} sat\nPublic Key: {}\nLiquid Address: {}",
info.balance_sat, info.pubkey, info.active_address
))
}
Command::ListPayments => {
let payments_str = wallet
.list_payments(true, true)?
.iter()
.map(|tx| {
format!(
"Id: {} | Type: {} | Amount: {} sat | Timestamp: {}",
tx.id.clone().unwrap_or("None".to_string()),
tx.payment_type.to_string(),
tx.amount_sat,
match tx.timestamp {
Some(t) => t.to_string(),
None => "None".to_string(),
},
)
})
.collect::<Vec<String>>()
.join("\n");

Ok(payments_str)
}
}
}
12 changes: 6 additions & 6 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub enum SwapError {
#[error("Could not store the swap details locally")]
PersistError,

#[error("The generated preimage is not valid")]
InvalidPreimage,

#[error("Generic boltz error: {err}")]
BoltzGeneric { err: String },
}
Expand Down Expand Up @@ -106,4 +109,29 @@ pub struct OngoingSwap {
pub preimage: String,
pub redeem_script: String,
pub blinding_key: String,
pub invoice_amount_sat: u64,
}

pub enum PaymentType {
Sent,
Received,
PendingReceive,
}

impl ToString for PaymentType {
fn to_string(&self) -> String {
match self {
PaymentType::Sent => "Sent",
PaymentType::Received => "Received",
PaymentType::PendingReceive => "Pending Receive",
}
.to_string()
}
}

pub struct Payment {
pub id: Option<String>,
pub timestamp: Option<u32>,
pub amount_sat: u64,
pub payment_type: PaymentType,
}
3 changes: 2 additions & 1 deletion lib/src/persist/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub(crate) fn current_migrations() -> Vec<&'static str> {
id TEXT NOT NULL PRIMARY KEY,
preimage TEXT NOT NULL,
redeem_script TEXT NOT NULL,
blinding_key TEXT NOT NULL
blinding_key TEXT NOT NULL,
invoice_amount_sat INTEGER
) STRICT;",
]
}
7 changes: 5 additions & 2 deletions lib/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ impl Persister {
id,
preimage,
redeem_script,
blinding_key
blinding_key,
invoice_amount_sat
)
VALUES (?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?)
",
)?;

Expand All @@ -56,6 +57,7 @@ impl Persister {
&swap.preimage,
&swap.redeem_script,
&swap.blinding_key,
&swap.invoice_amount_sat,
))?
}

Expand Down Expand Up @@ -90,6 +92,7 @@ impl Persister {
preimage: row.get(1)?,
redeem_script: row.get(2)?,
blinding_key: row.get(3)?,
invoice_amount_sat: row.get(4)?,
})
}
}
51 changes: 48 additions & 3 deletions lib/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use lwk_wollet::{
};

use crate::{
persist::Persister, Network, OngoingSwap, SendPaymentResponse, SwapError, SwapLbtcResponse,
WalletInfo, WalletOptions,
persist::Persister, Network, OngoingSwap, Payment, PaymentType, SendPaymentResponse, SwapError,
SwapLbtcResponse, WalletInfo, WalletOptions,
};

// To avoid sendrawtransaction error "min relay fee not met"
Expand Down Expand Up @@ -294,6 +294,7 @@ impl Wallet {
let lsk = LiquidSwapKey::from(swap_key);

let preimage = Preimage::new();
let preimage_str = preimage.to_string().ok_or(SwapError::InvalidPreimage)?;
let preimage_hash = preimage.sha256.to_string();

let swap_response = client.create_swap(CreateSwapRequest::new_lbtc_reverse_invoice_amt(
Expand Down Expand Up @@ -323,9 +324,10 @@ impl Wallet {
self.swap_persister
.insert_ongoing_swaps(&[OngoingSwap {
id: swap_id.clone(),
preimage: preimage.to_string().expect("Expecting valid preimage"),
preimage: preimage_str,
blinding_key: blinding_str,
redeem_script,
invoice_amount_sat: amount_sat,
}])
.map_err(|_| SwapError::PersistError)?;

Expand All @@ -335,6 +337,49 @@ impl Wallet {
})
}

pub fn list_payments(&self, with_scan: bool, include_pending: bool) -> Result<Vec<Payment>> {
if with_scan {
self.scan()?;
}

let transactions = self.wallet.lock().unwrap().transactions()?;

let mut payments: Vec<Payment> = transactions
.iter()
.map(|tx| {
let amount_sat = tx.balance.values().sum::<i64>();

Payment {
id: Some(tx.tx.txid().to_string()),
timestamp: tx.timestamp,
amount_sat: amount_sat.unsigned_abs(),
payment_type: match amount_sat >= 0 {
true => PaymentType::Received,
false => PaymentType::Sent,
},
}
})
.collect();

if include_pending {
let pending_swaps = self.swap_persister.list_ongoing_swaps()?;

for swap in pending_swaps {
payments.insert(
0,
Payment {
id: None,
timestamp: None,
payment_type: PaymentType::PendingReceive,
amount_sat: swap.invoice_amount_sat,
},
);
}
}

Ok(payments)
}

pub fn recover_funds(&self, recovery: &LBtcReverseRecovery) -> Result<String> {
let script: LBtcSwapScript = recovery.try_into().unwrap();
let network_config = self.get_network_config();
Expand Down

0 comments on commit 03c2c39

Please sign in to comment.