pub mod test_cases;
pub mod test_data;
extern crate alloc;
pub use bp_test_utils::test_header;
pub use parachains_runtimes_test_utils::*;
use sp_runtime::Perbill;
pub use test_cases::helpers::{
ensure_opened_bridge, open_bridge_with_extrinsic, open_bridge_with_storage,
};
pub fn check_sane_fees_values(
const_name: &str,
actual: u128,
calculate_estimated_fee: fn() -> u128,
overestimate_in_percent: Perbill,
margin_overestimate_diff_in_percent_for_lowering: Option<i16>,
label: &str,
) {
let estimated = calculate_estimated_fee();
let estimated_plus_overestimate = estimated + (overestimate_in_percent * estimated);
let diff_to_estimated = diff_as_percent(actual, estimated);
let diff_to_estimated_plus_overestimate = diff_as_percent(actual, estimated_plus_overestimate);
sp_tracing::try_init_simple();
log::error!(
target: "bridges::estimate",
"{label}:\nconstant: {const_name}\n[+] actual: {actual}\n[+] estimated: {estimated} ({diff_to_estimated:.2?})\n[+] estimated(+33%): {estimated_plus_overestimate} ({diff_to_estimated_plus_overestimate:.2?})",
);
assert!(
estimated <= actual,
"estimated: {estimated}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
);
assert!(
estimated_plus_overestimate <= actual,
"estimated_plus_overestimate: {estimated_plus_overestimate}, actual: {actual}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
);
if let Some(margin_overestimate_diff_in_percent_for_lowering) =
margin_overestimate_diff_in_percent_for_lowering
{
assert!(
diff_to_estimated_plus_overestimate > margin_overestimate_diff_in_percent_for_lowering as f64,
"diff_to_estimated_plus_overestimate: {diff_to_estimated_plus_overestimate:.2}, overestimate_diff_in_percent_for_lowering: {margin_overestimate_diff_in_percent_for_lowering}, please adjust `{const_name}` to the value: {estimated_plus_overestimate}",
);
}
}
pub fn diff_as_percent(left: u128, right: u128) -> f64 {
let left = left as f64;
let right = right as f64;
((left - right).abs() / left) * 100f64 * (if left >= right { -1 } else { 1 }) as f64
}
#[test]
fn diff_as_percent_works() {
assert_eq!(-20_f64, diff_as_percent(100, 80));
assert_eq!(25_f64, diff_as_percent(80, 100));
assert_eq!(33_f64, diff_as_percent(13351000000, 17756830000));
}