diff --git a/pallets/vsbond-auction/src/lib.rs b/pallets/vsbond-auction/src/lib.rs index 4b594234d5..0dcc406727 100644 --- a/pallets/vsbond-auction/src/lib.rs +++ b/pallets/vsbond-auction/src/lib.rs @@ -167,6 +167,7 @@ pub mod pallet { InvalidVsbond, Unexpected, InvalidRateInput, + Overflow, } #[pallet::event] @@ -455,7 +456,7 @@ pub mod pallet { // Calculate the real quantity to clinch let quantity_clinchd = min(order_info.remain, quantity); // Calculate the total price that buyer need to pay - let price_to_pay = Self::price_to_pay(quantity_clinchd, order_info.unit_price()); + let price_to_pay = Self::price_to_pay(quantity_clinchd, order_info.unit_price())?; let (token_to_get, amount_to_get, token_to_pay, amount_to_pay) = match order_info .order_type @@ -647,13 +648,12 @@ pub mod pallet { pub(crate) fn price_to_pay( quantity: BalanceOf, unit_price: FixedU128, - ) -> BalanceOf { + ) -> Result, Error> { let quantity: u128 = quantity.saturated_into(); + let total_price = + unit_price.checked_mul_int(quantity).ok_or(Error::::Overflow)?; - let total_price = (unit_price.saturating_mul(quantity.into())).floor().into_inner() / - FixedU128::accuracy(); - - BalanceOf::::saturated_from(total_price) + Ok(BalanceOf::::saturated_from(total_price)) } pub(crate) fn do_order_revoke(order_id: OrderId) -> DispatchResultWithPostInfo { diff --git a/pallets/vsbond-auction/src/tests.rs b/pallets/vsbond-auction/src/tests.rs index a6b86511e3..1b2a11cae7 100644 --- a/pallets/vsbond-auction/src/tests.rs +++ b/pallets/vsbond-auction/src/tests.rs @@ -948,6 +948,6 @@ fn check_price_to_pay() { let price_to_pays: [BalanceOf; 4] = [0, 10, 109, 1099]; for (quantity, price_to_pay) in quantities.iter().zip(price_to_pays.iter()) { - assert_eq!(Auction::price_to_pay(*quantity, unit_price), *price_to_pay); + assert_eq!(Auction::price_to_pay(*quantity, unit_price).unwrap(), *price_to_pay); } }