Skip to content

Commit

Permalink
Fix the fedimint sweep fee for max balance
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio committed Feb 5, 2024
1 parent 51bb7b9 commit 3a04ebc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl<S: MutinyStorage> FederationClient<S> {
}
}
};
inv.fees_paid = Some(outgoing_payment.fee.sats_round_down());
inv.fees_paid = Some(sats_round_up(&outgoing_payment.fee));

self.maybe_update_after_checking_fedimint(inv.clone())
.await?;
Expand Down Expand Up @@ -617,6 +617,11 @@ impl<S: MutinyStorage> FederationClient<S> {
}
}

fn sats_round_up(amount: &Amount) -> u64 {
let rounded = Amount::from_msats(amount.msats + 999).sats_round_down();
rounded
}

// Get a preferred gateway from a federation
fn get_gateway_preference(
gateways: Vec<fedimint_ln_common::LightningGatewayAnnouncement>,
Expand Down
6 changes: 4 additions & 2 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ impl<S: MutinyStorage> MutinyWallet<S> {
);

let fees = fedimint_client.gateway_fee().await?;
// FIXME: this is still producing off by one. check round down
let amt = max_spendable_amount(current_balance, &fees)
.map_or(Err(MutinyError::InsufficientBalance), Ok)?;
log_debug!(self.logger, "max spendable: {}", amt);
Expand Down Expand Up @@ -1276,9 +1277,10 @@ impl<S: MutinyStorage> MutinyWallet<S> {
);
}

let total_fees = first_invoice_res.fees_paid.unwrap_or(0) + fee;
Ok(FedimintSweepResult {
amount: first_invoice_amount,
fees: Some(first_invoice_res.fees_paid.unwrap_or(0) + fee),
amount: current_balance - total_fees,
fees: Some(total_fees),
})
}

Expand Down

0 comments on commit 3a04ebc

Please sign in to comment.