Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 8f50ea2

Browse files
Return fedimint sweep result
1 parent d96b827 commit 8f50ea2

File tree

3 files changed

+82
-17
lines changed

3 files changed

+82
-17
lines changed

mutiny-core/src/lib.rs

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,17 @@ impl MutinyInvoice {
411411
}
412412
}
413413

414+
/// FedimintSweepResult is the result of how much was swept and the fees paid.
415+
#[derive(Serialize, Deserialize, Clone, Debug)]
416+
pub struct FedimintSweepResult {
417+
/// The final amount that was swept.
418+
/// This should be the amount specified if it was not max.
419+
pub amount: u64,
420+
421+
/// The total fees paid for the sweep.
422+
pub fees: Option<u64>,
423+
}
424+
414425
pub struct MutinyWalletConfigBuilder {
415426
xprivkey: ExtendedPrivKey,
416427
#[cfg(target_arch = "wasm32")]
@@ -1185,7 +1196,10 @@ impl<S: MutinyStorage> MutinyWallet<S> {
11851196
})
11861197
}
11871198

1188-
pub async fn sweep_federation_balance(&self, amount: Option<u64>) -> Result<(), MutinyError> {
1199+
pub async fn sweep_federation_balance(
1200+
&self,
1201+
amount: Option<u64>,
1202+
) -> Result<FedimintSweepResult, MutinyError> {
11891203
// Attempt to create federation invoice if available and below max amount
11901204
let federation_ids = self.list_federation_ids().await?;
11911205
if federation_ids.is_empty() {
@@ -1202,10 +1216,13 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12021216
// if the user provided amount, this is easy
12031217
if let Some(amt) = amount {
12041218
let inv = self.node_manager.create_invoice(amt).await?;
1205-
let _ = fedimint_client
1219+
let pay_res = fedimint_client
12061220
.pay_invoice(inv.bolt11.expect("create inv had one job"), vec![])
12071221
.await?;
1208-
return Ok(());
1222+
return Ok(FedimintSweepResult {
1223+
amount: amt,
1224+
fees: pay_res.fees_paid,
1225+
});
12091226
}
12101227

12111228
// If no amount, figure out the amount to send over
@@ -1219,29 +1236,43 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12191236
let fees = fedimint_client.gateway_fee().await?;
12201237
let amt = max_spendable_amount(current_balance, fees.clone())
12211238
.map_or(Err(MutinyError::InsufficientBalance), Ok)?;
1222-
log_info!(self.logger, "max spendable: {}", amt);
1239+
log_debug!(self.logger, "max spendable: {}", amt);
12231240

12241241
// try to get an invoice for this exact amount
12251242
let inv = self.node_manager.create_invoice(amt).await?;
12261243

1244+
// check if we can afford that invoice
12271245
let inv_amt = inv.amount_sats.ok_or(MutinyError::BadAmountError)?;
1228-
let inv_to_pay = if inv_amt > amt {
1229-
let new_amt = inv_amt - (inv_amt - amt);
1230-
log_info!(self.logger, "adjusting amount to swap to: {}", amt);
1231-
self.node_manager.create_invoice(new_amt).await?
1246+
let first_invoice_amount = if inv_amt > amt {
1247+
log_debug!(self.logger, "adjusting amount to swap to: {}", amt);
1248+
inv_amt - (inv_amt - amt)
1249+
} else {
1250+
inv_amt
1251+
};
1252+
1253+
// if invoice amount changed, create a new invoice
1254+
let inv_to_pay = if first_invoice_amount != inv_amt {
1255+
self.node_manager
1256+
.create_invoice(first_invoice_amount)
1257+
.await?
12321258
} else {
12331259
inv.clone()
12341260
};
12351261

1236-
log_info!(self.logger, "attempting payment from fedimint client");
1237-
let _ = fedimint_client
1262+
log_debug!(self.logger, "attempting payment from fedimint client");
1263+
let mut final_result = FedimintSweepResult {
1264+
amount: first_invoice_amount,
1265+
fees: None,
1266+
};
1267+
let first_invoice_res = fedimint_client
12381268
.pay_invoice(inv_to_pay.bolt11.expect("create inv had one job"), vec![])
12391269
.await?;
1270+
final_result.fees = first_invoice_res.fees_paid;
12401271

12411272
// pay_invoice returns invoice if Succeeded or Err if something else
12421273
// it's safe to assume that it went through and we can check remaining balance
12431274
let remaining_balance = fedimint_client.get_balance().await?;
1244-
log_info!(
1275+
log_debug!(
12451276
self.logger,
12461277
"remaining fedimint balance: {}",
12471278
remaining_balance
@@ -1250,7 +1281,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12501281
// the fee for existing channel is voltage 1 sat + base fee + ppm
12511282
let remaining_balance_minus_fee = max_spendable_amount(remaining_balance - 1, fees);
12521283
if remaining_balance_minus_fee.is_none() {
1253-
return Ok(());
1284+
return Ok(final_result);
12541285
}
12551286
let remaining_balance_minus_fee = remaining_balance_minus_fee.unwrap();
12561287

@@ -1263,8 +1294,14 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12631294
.pay_invoice(inv.bolt11.expect("create inv had one job"), vec![])
12641295
.await
12651296
{
1266-
Ok(_) => {
1267-
log_info!(self.logger, "paid remaining balance")
1297+
Ok(r) => {
1298+
log_debug!(self.logger, "paid remaining balance");
1299+
final_result.amount += remaining_balance_minus_fee;
1300+
final_result.fees = final_result.fees.map_or(r.fees_paid, |val| {
1301+
r.fees_paid
1302+
.map(|add_val| val + add_val)
1303+
.map_or(Some(val), |_| None)
1304+
});
12681305
}
12691306
Err(e) => {
12701307
// Don't want to return this error since it's just "incomplete",
@@ -1274,7 +1311,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
12741311
}
12751312
}
12761313

1277-
Ok(())
1314+
Ok(final_result)
12781315
}
12791316

12801317
async fn create_lightning_invoice(

mutiny-wasm/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,8 +997,11 @@ impl MutinyWallet {
997997
}
998998

999999
/// Sweep the federation balance into a lightning channel
1000-
pub async fn sweep_federation_balance(&self, amount: Option<u64>) -> Result<(), MutinyJsError> {
1001-
Ok(self.inner.sweep_federation_balance(amount).await?)
1000+
pub async fn sweep_federation_balance(
1001+
&self,
1002+
amount: Option<u64>,
1003+
) -> Result<FedimintSweepResult, MutinyJsError> {
1004+
Ok(self.inner.sweep_federation_balance(amount).await?.into())
10021005
}
10031006

10041007
/// Closes a channel with the given outpoint.

mutiny-wasm/src/models.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,31 @@ impl From<mutiny_core::Plan> for Plan {
10741074
}
10751075
}
10761076

1077+
/// FedimintSweepResult is the result of how much was swept and the fees paid.
1078+
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
1079+
#[wasm_bindgen]
1080+
pub struct FedimintSweepResult {
1081+
pub amount: u64,
1082+
pub fees: Option<u64>,
1083+
}
1084+
1085+
#[wasm_bindgen]
1086+
impl FedimintSweepResult {
1087+
#[wasm_bindgen(getter)]
1088+
pub fn value(&self) -> JsValue {
1089+
JsValue::from_serde(&serde_json::to_value(self).unwrap()).unwrap()
1090+
}
1091+
}
1092+
1093+
impl From<mutiny_core::FedimintSweepResult> for FedimintSweepResult {
1094+
fn from(m: mutiny_core::FedimintSweepResult) -> Self {
1095+
FedimintSweepResult {
1096+
amount: m.amount,
1097+
fees: m.fees,
1098+
}
1099+
}
1100+
}
1101+
10771102
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10781103
#[wasm_bindgen]
10791104
pub enum BudgetPeriod {

0 commit comments

Comments
 (0)