breez_sdk_core/swap_out/
mod.rs1pub(crate) mod boltzswap;
2pub(crate) mod error;
3pub(crate) mod reverseswap;
4
5pub(crate) fn get_service_fee_sat(invoice_amount_sat: u64, fees_percentage: f64) -> u64 {
9 ((invoice_amount_sat as f64) * fees_percentage / 100.0).ceil() as u64
10}
11
12pub(crate) fn get_invoice_amount_sat(
16 invoice_amount_minus_service_fee: u64,
17 fees_percentage: f64,
18) -> u64 {
19 (invoice_amount_minus_service_fee as f64 * 100.0 / (100.0 - fees_percentage)).ceil() as u64
22}
23
24#[cfg(test)]
25mod tests {
26 use crate::swap_out::{get_invoice_amount_sat, get_service_fee_sat};
27
28 #[test]
29 fn test_get_service_fee_sat() {
30 assert_eq!(250, get_service_fee_sat(50_000, 0.5));
32 assert_eq!(300, get_service_fee_sat(50_000, 0.6));
33 assert_eq!(750, get_service_fee_sat(100_000, 0.75));
34
35 assert_eq!(251, get_service_fee_sat(50_001, 0.5));
37 assert_eq!(301, get_service_fee_sat(50_001, 0.6));
38 assert_eq!(751, get_service_fee_sat(100_001, 0.75));
39 }
40
41 #[test]
42 fn test_get_invoice_amount_sat() {
43 test_invoice_amount_calculation_in_reverse(50_000, 0.5);
45 test_invoice_amount_calculation_in_reverse(51_000, 0.5);
46 test_invoice_amount_calculation_in_reverse(52_000, 0.5);
47 test_invoice_amount_calculation_in_reverse(53_000, 0.5);
48 test_invoice_amount_calculation_in_reverse(54_000, 0.5);
49 test_invoice_amount_calculation_in_reverse(60_000, 0.6);
50 test_invoice_amount_calculation_in_reverse(100_000, 0.75);
51
52 test_invoice_amount_calculation_in_reverse(50_001, 0.5);
54 test_invoice_amount_calculation_in_reverse(50_999, 0.5);
55 test_invoice_amount_calculation_in_reverse(51_001, 0.5);
56 test_invoice_amount_calculation_in_reverse(52_001, 0.5);
57 test_invoice_amount_calculation_in_reverse(53_001, 0.5);
58 test_invoice_amount_calculation_in_reverse(54_001, 0.5);
59 test_invoice_amount_calculation_in_reverse(60_001, 0.6);
60 test_invoice_amount_calculation_in_reverse(75_001, 0.75);
61 }
62
63 fn test_invoice_amount_calculation_in_reverse(invoice_amount_sat: u64, fees_percentage: f64) {
64 let service_fee_sat = get_service_fee_sat(invoice_amount_sat, fees_percentage);
65 let calculated_invoice_amount_sat =
66 get_invoice_amount_sat(invoice_amount_sat - service_fee_sat, fees_percentage);
67
68 assert!(
80 (calculated_invoice_amount_sat == invoice_amount_sat)
81 || (calculated_invoice_amount_sat == invoice_amount_sat - 1)
82 );
83 }
84}