From 4c8fa74dfef875aad8f102240ea216aac160d72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Fri, 24 Nov 2023 09:31:46 +0000 Subject: [PATCH] remove the panic --- .../contracts-common/src/types.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/common/cosmwasm-smart-contracts/contracts-common/src/types.rs b/common/cosmwasm-smart-contracts/contracts-common/src/types.rs index 46a6b174c6..95ba1fa6f3 100644 --- a/common/cosmwasm-smart-contracts/contracts-common/src/types.rs +++ b/common/cosmwasm-smart-contracts/contracts-common/src/types.rs @@ -75,9 +75,14 @@ impl Percent { pub fn pow(&self, exp: u32) -> Self { match self.0.checked_pow(exp) { Ok(res) => Percent(res), - Err(overflow) => { - // since the percent is meant to always be strictly less than 1, this should NEVER hapen - panic!("the percent invariant has been broken. the exponentiation result has overflown: {overflow}"); + Err(_overflow) => { + // since the percent is meant to always be less than 1, this should NEVER hapen, however, + // when the inevitable happens because of some misuse, just saturate the result + if self.0 < Decimal::one() { + Percent::zero() + } else { + Percent(Decimal::MAX) + } } } }