Skip to content

Commit

Permalink
Fix build (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea212 authored Oct 23, 2023
1 parent 6c466cd commit 4e25695
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
42 changes: 26 additions & 16 deletions primitives/src/math/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ use crate::{
},
},
};
use alloc::{borrow::ToOwned, format, string::ToString};
use core::convert::TryFrom;
use alloc::{
borrow::ToOwned,
format,
string::{String, ToString},
};
use core::{cmp::Ordering, convert::TryFrom};
use fixed::{traits::Fixed, ParseFixedError};
use frame_support::dispatch::DispatchError;

Expand Down Expand Up @@ -252,21 +256,27 @@ impl<F: Fixed + ToString, N: TryFrom<u128>> FromFixedToDecimal<F> for N {
}

let mut increment_int_part = false;
let new_frac_part = if frac_part.len() < decimals_usize {
format!("{}{}", frac_part, "0".repeat(decimals_usize.saturating_sub(frac_part.len())))
} else if frac_part.len() > decimals_usize {
let round_digit =
frac_part.chars().nth(decimals_usize).and_then(|c| c.to_digit(10)).ok_or(
"The char at decimals_usize was not found, although the frac_part length is \
higher than decimals_usize.",
)?;
if round_digit >= 5 {
increment_int_part = true;

let new_frac_part = match frac_part.len().cmp(&decimals_usize) {
Ordering::Less => {
format!(
"{}{}",
frac_part,
"0".repeat(decimals_usize.saturating_sub(frac_part.len()))
)
}
frac_part.chars().take(decimals_usize).collect::<String>()
} else {
// There's no need to round here, the fractional part already fits perfectly.
frac_part.to_string()
Ordering::Greater => {
let round_digit =
frac_part.chars().nth(decimals_usize).and_then(|c| c.to_digit(10)).ok_or(
"The char at decimals_usize was not found, although the frac_part length \
is higher than decimals_usize.",
)?;
if round_digit >= 5 {
increment_int_part = true;
}
frac_part.chars().take(decimals_usize).collect::<String>()
}
Ordering::Equal => frac_part.to_string(),
};

let mut fixed_decimal: u128 = format!("{}{}", int_part, new_frac_part)
Expand Down
1 change: 1 addition & 0 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@ mod pallet {
}

// Infallible, should fee transfer fail, the informant will keep the fees and an event is emitted.
#[allow(clippy::too_many_arguments)]
fn handle_creator_fees(
amount: BalanceOf<T>,
fee_asset: Asset<MarketIdOf<T>>,
Expand Down

0 comments on commit 4e25695

Please sign in to comment.